diff --git a/neutron/plugins/hyperv/agent/utilsv2.py b/neutron/plugins/hyperv/agent/utilsv2.py index 73eed9824f4..d2a9a7d3ef7 100644 --- a/neutron/plugins/hyperv/agent/utilsv2.py +++ b/neutron/plugins/hyperv/agent/utilsv2.py @@ -74,6 +74,12 @@ class HyperVUtilsV2(utils.HyperVUtils): ResourceSettings=[res_setting_data.path_()]) self._check_job_status(ret_val, job) + def _add_virt_feature(self, element, res_setting_data): + vs_man_svc = self._conn.Msvm_VirtualSystemManagementService()[0] + (job_path, out_set_data, ret_val) = vs_man_svc.AddFeatureSettings( + element.path_(), [res_setting_data.GetText_(1)]) + self._check_job_status(ret_val, job_path) + def disconnect_switch_port( self, vswitch_name, switch_port_name, delete_port): """Disconnects the switch port.""" diff --git a/neutron/tests/unit/hyperv/test_hyperv_neutron_agent.py b/neutron/tests/unit/hyperv/test_hyperv_neutron_agent.py index c622968ef3a..dc835647d7d 100644 --- a/neutron/tests/unit/hyperv/test_hyperv_neutron_agent.py +++ b/neutron/tests/unit/hyperv/test_hyperv_neutron_agent.py @@ -27,6 +27,10 @@ from neutron.plugins.hyperv.agent import hyperv_neutron_agent from neutron.plugins.hyperv.agent import utilsfactory from neutron.tests import base +cfg.CONF.import_opt('enable_metrics_collection', + 'neutron.plugins.hyperv.agent.hyperv_neutron_agent', + 'AGENT') + class TestHyperVNeutronAgent(base.BaseTestCase): @@ -65,14 +69,28 @@ class TestHyperVNeutronAgent(base.BaseTestCase): 'start_flag': True} self.agent_state = fake_agent_state - def test_port_bound(self): - port = mock.Mock() + def test_port_bound_enable_metrics(self): + cfg.CONF.set_override('enable_metrics_collection', True, 'AGENT') + self._test_port_bound(True) + + def test_port_bound_no_metrics(self): + cfg.CONF.set_override('enable_metrics_collection', False, 'AGENT') + self._test_port_bound(False) + + def _test_port_bound(self, enable_metrics): + port = mock.MagicMock() + mock_enable_metrics = mock.MagicMock() net_uuid = 'my-net-uuid' - with mock.patch.object( - self.agent._utils, 'connect_vnic_to_vswitch'): - with mock.patch.object( - self.agent._utils, 'set_vswitch_port_vlan_id'): - self.agent._port_bound(port, net_uuid, 'vlan', None, None) + + with mock.patch.multiple( + self.agent._utils, + connect_vnic_to_vswitch=mock.MagicMock(), + set_vswitch_port_vlan_id=mock.MagicMock(), + enable_port_metrics_collection=mock_enable_metrics): + + self.agent._port_bound(port, net_uuid, 'vlan', None, None) + + self.assertEqual(enable_metrics, mock_enable_metrics.called) def test_port_unbound(self): map = { diff --git a/neutron/tests/unit/hyperv/test_hyperv_utilsv2.py b/neutron/tests/unit/hyperv/test_hyperv_utilsv2.py index 76f88ead2b0..82786c91880 100644 --- a/neutron/tests/unit/hyperv/test_hyperv_utilsv2.py +++ b/neutron/tests/unit/hyperv/test_hyperv_utilsv2.py @@ -97,6 +97,24 @@ class TestHyperVUtilsV2(base.BaseTestCase): mock_svc.AddResourceSettings.assert_called_with(self._FAKE_VM_PATH, [self._FAKE_RES_DATA]) + def test_add_virt_feature(self): + mock_svc = self._utils._conn.Msvm_VirtualSystemManagementService()[0] + mock_svc.AddFeatureSettings.return_value = (self._FAKE_JOB_PATH, + mock.MagicMock(), + self._FAKE_RET_VAL) + mock_res_setting_data = mock.MagicMock() + mock_res_setting_data.GetText_.return_value = self._FAKE_RES_DATA + + mock_vm = mock.MagicMock() + mock_vm.path_.return_value = self._FAKE_VM_PATH + + self._utils._check_job_status = mock.MagicMock() + + self._utils._add_virt_feature(mock_vm, mock_res_setting_data) + + mock_svc.AddFeatureSettings.assert_called_once_with( + self._FAKE_VM_PATH, [self._FAKE_RES_DATA]) + def test_modify_virt_resource(self): mock_svc = self._utils._conn.Msvm_VirtualSystemManagementService()[0] mock_svc.ModifyResourceSettings.return_value = (self._FAKE_JOB_PATH, @@ -220,11 +238,14 @@ class TestHyperVUtilsV2(base.BaseTestCase): mock_port, True)) mock_acl = mock.MagicMock() - self._utils._get_default_setting_data = mock.MagicMock( - return_value=mock_acl) - self._utils._add_virt_feature = mock.MagicMock() - self._utils.enable_port_metrics_collection(self._FAKE_PORT_NAME) + with mock.patch.multiple( + self._utils, + _get_default_setting_data=mock.MagicMock(return_value=mock_acl), + _add_virt_feature=mock.MagicMock()): - self.assertEqual(4, len(self._utils._add_virt_feature.mock_calls)) - self._utils._add_virt_feature.assert_called_with(mock_port, mock_acl) + self._utils.enable_port_metrics_collection(self._FAKE_PORT_NAME) + + self.assertEqual(4, len(self._utils._add_virt_feature.mock_calls)) + self._utils._add_virt_feature.assert_called_with( + mock_port, mock_acl)