Browse Source

Merge "tests: Use mock autospec in unit tests"

tags/6.0.0
Zuul 1 year ago
parent
commit
3334fb5e5b

+ 1
- 0
networking_hyperv/neutron/agent/hnv_neutron_agent.py View File

@@ -72,6 +72,7 @@ class HNVAgent(hyperv_base.Layer2Agent):
72 72
         """Bind the port to the recived network."""
73 73
         super(HNVAgent, self)._port_bound(port_id, network_id, network_type,
74 74
                                           physical_network, segmentation_id,
75
+                                          port_security_enabled,
75 76
                                           set_port_sriov)
76 77
         LOG.debug("Getting the profile id for the current port.")
77 78
         profile_id = self._neutron_client.get_port_profile_id(port_id)

+ 2
- 2
networking_hyperv/neutron/agent/layer2.py View File

@@ -109,12 +109,12 @@ class Layer2Agent(base_agent.BaseAgent):
109 109
             [topics.PORT, topics.DELETE]
110 110
         ])
111 111
 
112
-        self.connection = agent_rpc.create_consumers(
112
+        self._connection = agent_rpc.create_consumers(
113 113
             self._endpoints, self._topic, self._consumers,
114 114
             start_listening=False
115 115
         )
116 116
         self._setup_qos_extension()
117
-        self.connection.consume_in_threads()
117
+        self._connection.consume_in_threads()
118 118
 
119 119
         report_interval = CONF.AGENT.report_interval
120 120
         if report_interval:

+ 15
- 0
networking_hyperv/tests/base.py View File

@@ -27,6 +27,7 @@ import fixtures
27 27
 import mock
28 28
 from os_win import utilsfactory
29 29
 from oslo_utils import strutils
30
+from oslotest import mock_fixture
30 31
 import testtools
31 32
 
32 33
 from networking_hyperv.neutron import config
@@ -35,6 +36,8 @@ CONF = config.CONF
35 36
 
36 37
 LOG_FORMAT = "%(asctime)s %(levelname)8s [%(name)s] %(message)s"
37 38
 
39
+mock_fixture.patch_mock_module()
40
+
38 41
 
39 42
 def bool_from_env(key, strict=False, default=False):
40 43
     value = os.environ.get(key)
@@ -43,8 +46,12 @@ def bool_from_env(key, strict=False, default=False):
43 46
 
44 47
 class BaseTestCase(testtools.TestCase):
45 48
 
49
+    _autospec_classes = []
50
+
46 51
     def setUp(self):
47 52
         super(BaseTestCase, self).setUp()
53
+        self.useFixture(mock_fixture.MockAutospecFixture())
54
+        self._patch_autospec_classes()
48 55
 
49 56
         self.addCleanup(CONF.reset)
50 57
         self.addCleanup(mock.patch.stopall)
@@ -78,6 +85,14 @@ class BaseTestCase(testtools.TestCase):
78 85
 
79 86
         self.addOnException(self.check_for_systemexit)
80 87
 
88
+    def _patch_autospec_classes(self):
89
+        for class_type in self._autospec_classes:
90
+            mocked_class = mock.Mock(autospec=class_type)
91
+            patcher = mock.patch(
92
+                '.'.join([class_type.__module__, class_type.__name__]),
93
+                mocked_class)
94
+            patcher.start()
95
+
81 96
     def check_for_systemexit(self, exc_info):
82 97
         if isinstance(exc_info[1], SystemExit):
83 98
             self.fail("A SystemExit was raised during the test. %s"

+ 3
- 7
networking_hyperv/tests/unit/neutron/agent/test_base.py View File

@@ -18,6 +18,7 @@ Unit tests for Neutron base agent.
18 18
 """
19 19
 
20 20
 import mock
21
+from neutron.agent import rpc as agent_rpc
21 22
 
22 23
 from networking_hyperv.neutron.agent import base as agent_base
23 24
 from networking_hyperv.tests import base as test_base
@@ -44,13 +45,8 @@ class TestBaseAgent(test_base.HyperVBaseTestCase):
44 45
 
45 46
         self._agent._agent_id = mock.sentinel.agent_id
46 47
         self._agent._context = mock.sentinel.admin_context
47
-        self._agent._utils = mock.MagicMock()
48
-
49
-        self._agent._client = mock.MagicMock()
50
-        self._agent._plugin_rpc = mock.Mock()
51
-        self._agent._connection = mock.MagicMock()
52
-
53
-        self._agent._state_rpc = mock.MagicMock()
48
+        self._agent._state_rpc = mock.MagicMock(
49
+            autospec=agent_rpc.PluginReportStateAPI)
54 50
 
55 51
     def test_set_agent_state(self):
56 52
         self._agent._agent_state = {}

+ 4
- 5
networking_hyperv/tests/unit/neutron/agent/test_hnv_metadata_agent.py View File

@@ -30,15 +30,14 @@ CONF = cfg.CONF
30 30
 
31 31
 class TestMetadataProxyHandler(test_base.BaseTestCase):
32 32
 
33
-    @mock.patch("networking_hyperv.neutron.neutron_client.NeutronAPIClient")
34
-    @mock.patch("neutron_lib.context.get_admin_context_without_session")
35
-    def _get_proxy(self, mock_get_context, mock_neutron_client):
36
-        return hnv_metadata_agent._MetadataProxyHandler()
33
+    _autospec_classes = [
34
+        hnv_metadata_agent.neutron_client.NeutronAPIClient,
35
+    ]
37 36
 
38 37
     def setUp(self):
39 38
         super(TestMetadataProxyHandler, self).setUp()
40 39
         hnv_metadata_agent.register_config_opts()
41
-        self._proxy = self._get_proxy()
40
+        self._proxy = hnv_metadata_agent._MetadataProxyHandler()
42 41
         self._neutron_client = self._proxy._neutron_client
43 42
 
44 43
     @mock.patch.object(hnv_metadata_agent._MetadataProxyHandler,

+ 7
- 2
networking_hyperv/tests/unit/neutron/agent/test_hnv_neutron_agent.py View File

@@ -28,6 +28,10 @@ from networking_hyperv.tests import base as test_base
28 28
 
29 29
 class TestHNVAgent(test_base.HyperVBaseTestCase):
30 30
 
31
+    _autospec_classes = [
32
+        hnv_agent.neutron_client.NeutronAPIClient,
33
+    ]
34
+
31 35
     @mock.patch.object(hnv_agent.HNVAgent, "_setup")
32 36
     @mock.patch.object(hnv_agent.HNVAgent, "_setup_rpc")
33 37
     @mock.patch.object(hnv_agent.HNVAgent, "_set_agent_state")
@@ -38,7 +42,7 @@ class TestHNVAgent(test_base.HyperVBaseTestCase):
38 42
         super(TestHNVAgent, self).setUp()
39 43
 
40 44
         self.agent = self._get_agent()
41
-        self.agent._neutron_client = mock.Mock()
45
+        self.agent._utils = mock.Mock(autospec=self.agent._utils)
42 46
 
43 47
     def test_get_agent_configurations(self):
44 48
         self.config(logical_network=mock.sentinel.logical_network,
@@ -88,7 +92,8 @@ class TestHNVAgent(test_base.HyperVBaseTestCase):
88 92
         mock_super_port_bound.assert_called_once_with(
89 93
             mock.sentinel.port_id, mock.sentinel.network_id,
90 94
             mock.sentinel.network_type, mock.sentinel.physical_network,
91
-            mock.sentinel.segmentation_id, mock.sentinel.set_port_sriov)
95
+            mock.sentinel.segmentation_id, mock.sentinel.port_security_enabled,
96
+            mock.sentinel.set_port_sriov)
92 97
         mock_neutron_client = self.agent._neutron_client
93 98
         mock_neutron_client.get_port_profile_id.assert_called_once_with(
94 99
             mock.sentinel.port_id)

+ 18
- 12
networking_hyperv/tests/unit/neutron/agent/test_hyperv_neutron_agent.py View File

@@ -22,6 +22,8 @@ import sys
22 22
 
23 23
 import ddt
24 24
 import mock
25
+from neutron.agent import rpc as agent_rpc
26
+from neutron.common import rpc as n_rpc
25 27
 from neutron.common import topics
26 28
 from os_win import exceptions
27 29
 
@@ -69,18 +71,22 @@ class TestHyperVNeutronAgent(base.HyperVBaseTestCase):
69 71
         super(TestHyperVNeutronAgent, self).setUp()
70 72
         self.agent = self._get_agent()
71 73
 
72
-        self.agent._qos_ext = mock.MagicMock()
73
-        self.agent._plugin_rpc = mock.Mock()
74
-        self.agent._metricsutils = mock.MagicMock()
75
-        self.agent._utils = mock.MagicMock()
76
-        self.agent._sec_groups_agent = mock.MagicMock()
77
-        self.agent._context = mock.Mock()
78
-        self.agent._client = mock.MagicMock()
79
-        self.agent._connection = mock.MagicMock()
80
-        self.agent._agent_id = mock.Mock()
81
-        self.agent._utils = mock.MagicMock()
82
-        self.agent._nvgre_ops = mock.MagicMock()
83
-        self.agent._vlan_driver = mock.MagicMock()
74
+        self.agent._utils = mock.MagicMock(autospec=self.agent._utils)
75
+        self.agent._metricsutils = mock.MagicMock(
76
+            autospec=self.agent._metricsutils)
77
+        self.agent._nvgre_ops = mock.MagicMock(
78
+            autospec=hyperv_agent.nvgre_ops.HyperVNvgreOps)
79
+
80
+        self.agent._sec_groups_agent = mock.MagicMock(
81
+            autospec=hyperv_agent.HyperVSecurityAgent)
82
+        self.agent._vlan_driver = mock.MagicMock(
83
+            autospec=hyperv_agent.trunk_driver.HyperVTrunkDriver)
84
+        self.agent._qos_ext = mock.MagicMock(
85
+            autospec=hyperv_agent.qos_extension.QosAgentExtension)
86
+
87
+        self.agent._plugin_rpc = mock.MagicMock(autospec=agent_rpc.PluginApi)
88
+        self.agent._client = mock.MagicMock(autospec=n_rpc.BackingOffClient)
89
+        self.agent._connection = mock.MagicMock(autospec=n_rpc.Connection)
84 90
         self.agent._refresh_cache = False
85 91
         self.agent._added_ports = set()
86 92
 

+ 9
- 14
networking_hyperv/tests/unit/neutron/agent/test_layer2.py View File

@@ -66,21 +66,16 @@ class TestLayer2Agent(test_base.HyperVBaseTestCase):
66 66
 
67 67
         self._agent = self._get_agent()
68 68
 
69
-        self._agent._qos_ext = mock.MagicMock()
70
-        self._agent._plugin_rpc = mock.Mock()
71
-        self._agent._metricsutils = mock.MagicMock()
72
-        self._agent._utils = mock.MagicMock()
73
-        self._agent._context = mock.Mock()
74
-        self._agent._client = mock.MagicMock()
75
-        self._agent._connection = mock.MagicMock()
76
-        self._agent._agent_id = mock.Mock()
77
-        self._agent._utils = mock.MagicMock()
78
-        self._agent._nvgre_ops = mock.MagicMock()
79
-        self._agent._vlan_driver = mock.MagicMock()
80
-        self._agent._physical_network_mappings = collections.OrderedDict()
81
-        self._agent._config = mock.MagicMock()
69
+        self._agent._utils = mock.MagicMock(
70
+            autospec=self._agent._utils)
71
+        self._agent._plugin_rpc = mock.Mock(
72
+            autospec=agent_base.agent_rpc.PluginApi)
82 73
         self._agent._endpoints = mock.MagicMock()
83
-        self._agent._event_callback_pairs = mock.MagicMock()
74
+        self._agent._client = mock.MagicMock(
75
+            autospec=agent_base.n_rpc.BackingOffClient)
76
+        self._agent._connection = mock.MagicMock(
77
+            autospec=agent_base.n_rpc.Connection)
78
+        self._agent._physical_network_mappings = collections.OrderedDict()
84 79
         self._agent._network_vswitch_map = {}
85 80
 
86 81
     def _get_fake_port_details(self):

+ 2
- 1
networking_hyperv/tests/unit/neutron/qos/test_qos_driver.py View File

@@ -30,7 +30,8 @@ class TestQosHyperVAgentDriver(base.BaseTestCase):
30 30
     def setUp(self):
31 31
         super(TestQosHyperVAgentDriver, self).setUp()
32 32
         self.driver = qos_driver.QosHyperVAgentDriver()
33
-        self.driver._utils = mock.Mock()
33
+        self.driver.initialize()
34
+        self.driver._utils = mock.Mock(autospec=self.driver._utils)
34 35
 
35 36
     @mock.patch.object(qos_driver, 'networkutils')
36 37
     def test_initialize(self, mock_networkutils):

+ 4
- 1
networking_hyperv/tests/unit/neutron/test_neutron_client.py View File

@@ -29,6 +29,10 @@ CONF = config.CONF
29 29
 
30 30
 class TestNeutronClient(base.BaseTestCase):
31 31
 
32
+    _autospec_classes = [
33
+        neutron_client.clientv20.Client,
34
+    ]
35
+
32 36
     _FAKE_CIDR = '10.0.0.0/24'
33 37
     _FAKE_GATEWAY = '10.0.0.1'
34 38
     _FAKE_HOST = 'fake_host'
@@ -36,7 +40,6 @@ class TestNeutronClient(base.BaseTestCase):
36 40
     def setUp(self):
37 41
         super(TestNeutronClient, self).setUp()
38 42
         self._neutron = neutron_client.NeutronAPIClient()
39
-        self._neutron._client = mock.MagicMock()
40 43
 
41 44
     @mock.patch.object(neutron_client.clientv20, "Client")
42 45
     @mock.patch.object(neutron_client, "ks_loading")

+ 11
- 5
networking_hyperv/tests/unit/neutron/test_nvgre_ops.py View File

@@ -29,6 +29,10 @@ CONF = config.CONF
29 29
 
30 30
 class TestHyperVNvgreOps(base.HyperVBaseTestCase):
31 31
 
32
+    _autospec_classes = [
33
+        nvgre_ops.neutron_client.NeutronAPIClient,
34
+    ]
35
+
32 36
     FAKE_MAC_ADDR = 'fa:ke:ma:ca:dd:re:ss'
33 37
     FAKE_CIDR = '10.0.0.0/24'
34 38
     FAKE_VSWITCH_NAME = 'fake_vswitch'
@@ -41,11 +45,11 @@ class TestHyperVNvgreOps(base.HyperVBaseTestCase):
41 45
         self.ops._vswitch_ips[mock.sentinel.network_name] = (
42 46
             mock.sentinel.ip_addr)
43 47
         self.ops.context = self.context
44
-        self.ops._notifier = mock.MagicMock()
45
-        self.ops._hyperv_utils = mock.MagicMock()
46
-        self.ops._nvgre_utils = mock.MagicMock()
47
-        self.ops._n_client = mock.MagicMock()
48
-        self.ops._db = mock.MagicMock()
48
+        self.ops._notifier = mock.MagicMock(
49
+            autospec=nvgre_ops.hyperv_agent_notifier.AgentNotifierApi)
50
+        self.ops._hyperv_utils = mock.MagicMock(
51
+            autospec=self.ops._hyperv_utils)
52
+        self.ops._nvgre_utils = mock.MagicMock(autospec=self.ops._nvgre_utils)
49 53
 
50 54
     @mock.patch.object(nvgre_ops.hyperv_agent_notifier, 'AgentNotifierApi')
51 55
     def test_init_notifier(self, mock_notifier):
@@ -227,6 +231,7 @@ class TestHyperVNvgreOps(base.HyperVBaseTestCase):
227 231
 
228 232
     @mock.patch.object(nvgre_ops.HyperVNvgreOps, '_register_lookup_record')
229 233
     def test_refresh_nvgre_records(self, mock_register_record):
234
+        self.ops._n_client.get_tunneling_agents.return_value = {}
230 235
         self.ops._nvgre_ports.append(mock.sentinel.processed_port_id)
231 236
         self.ops._tunneling_agents[mock.sentinel.host_id] = (
232 237
             mock.sentinel.agent_ip)
@@ -262,6 +267,7 @@ class TestHyperVNvgreOps(base.HyperVBaseTestCase):
262 267
 
263 268
     @mock.patch.object(nvgre_ops.HyperVNvgreOps, '_register_lookup_record')
264 269
     def test_refresh_nvgre_records_exception(self, mock_register_record):
270
+        self.ops._n_client.get_tunneling_agents.return_value = {}
265 271
         self.ops._tunneling_agents[mock.sentinel.host_id] = (
266 272
             mock.sentinel.agent_ip)
267 273
         self.ops._network_vsids[mock.sentinel.net_id] = (mock.sentinel.vsid)

+ 6
- 2
networking_hyperv/tests/unit/neutron/test_security_groups_driver.py View File

@@ -60,6 +60,10 @@ class SecurityGroupRuleTestHelper(base.HyperVBaseTestCase):
60 60
 
61 61
 class TestHyperVSecurityGroupsDriver(SecurityGroupRuleTestHelper):
62 62
 
63
+    _autospec_classes = [
64
+        sg_driver.SecurityGroupRuleGeneratorR2,
65
+    ]
66
+
63 67
     _FAKE_DEVICE = 'fake_device'
64 68
     _FAKE_ID = 'fake_id'
65 69
     _FAKE_PARAM_NAME = 'fake_param_name'
@@ -69,8 +73,7 @@ class TestHyperVSecurityGroupsDriver(SecurityGroupRuleTestHelper):
69 73
         super(TestHyperVSecurityGroupsDriver, self).setUp()
70 74
 
71 75
         self._driver = sg_driver.HyperVSecurityGroupsDriver()
72
-        self._driver._utils = mock.MagicMock()
73
-        self._driver._sg_gen = mock.MagicMock()
76
+        self._driver._utils = mock.MagicMock(autospec=self._driver._utils)
74 77
 
75 78
     def test__select_sg_rules_for_port(self):
76 79
         mock_port = self._get_port()
@@ -194,6 +197,7 @@ class TestHyperVSecurityGroupsDriver(SecurityGroupRuleTestHelper):
194 197
             return_value=self._FAKE_SOURCE_IP_PREFIX)
195 198
 
196 199
         mock_gen_rules.return_value = {new_mock_port['id']: [fake_rule_new]}
200
+        self._driver._sg_gen.expand_wildcard_rules.return_value = []
197 201
 
198 202
         self._driver._security_ports[mock_port['device']] = mock_port
199 203
         self._driver._sec_group_rules[new_mock_port['id']] = []

+ 6
- 4
networking_hyperv/tests/unit/neutron/test_trunk_driver.py View File

@@ -31,8 +31,10 @@ from networking_hyperv.tests import base
31 31
 
32 32
 class TestHyperVTrunkDriver(base.HyperVBaseTestCase):
33 33
 
34
-    @mock.patch.object(trunk_driver.trunk_rpc, 'TrunkStub',
35
-                       lambda *args, **kwargs: None)
34
+    _autospec_classes = [
35
+        trunk_driver.trunk_rpc.TrunkStub,
36
+    ]
37
+
36 38
     @mock.patch.object(trunk_driver.trunk_rpc.TrunkSkeleton, '__init__',
37 39
                        lambda *args, **kwargs: None)
38 40
     def setUp(self):
@@ -40,8 +42,8 @@ class TestHyperVTrunkDriver(base.HyperVBaseTestCase):
40 42
 
41 43
         self.trunk_driver = trunk_driver.HyperVTrunkDriver(
42 44
             mock.sentinel.context)
43
-        self.trunk_driver._utils = mock.MagicMock()
44
-        self.trunk_driver._trunk_rpc = mock.MagicMock()
45
+        self.trunk_driver._utils = mock.MagicMock(
46
+            autospec=self.trunk_driver._utils)
45 47
 
46 48
     def test_handle_trunks_deleted(self):
47 49
         mock_trunk = mock.MagicMock()

Loading…
Cancel
Save