Browse Source

Replace mox with mock in the ovsvapp unit tests

Partial-Bug: #1753504

Change-Id: Ia0d5a4e754af5391716fb4663c162c6e6ae67b72
Stephen Ma 1 year ago
parent
commit
424629c833

+ 61
- 38
networking_vsphere/tests/unit/neutronclient/test_cli20.py View File

@@ -11,7 +11,8 @@
11 11
 #    under the License.
12 12
 #
13 13
 
14
-from mox3 import mox
14
+import mock
15
+
15 16
 from neutronclient import shell as neutronshell
16 17
 from neutronclient.tests.unit import test_cli20 as neutron_test_cli20
17 18
 from neutronclient.v2_0 import client as ovsvappclient
@@ -40,6 +41,10 @@ class MyUrlComparator(neutron_test_cli20.MyUrlComparator):
40 41
     pass
41 42
 
42 43
 
44
+class ContainsKeyValue(neutron_test_cli20.ContainsKeyValue):
45
+    pass
46
+
47
+
43 48
 class CLITestV20Base(neutron_test_cli20.CLITestV20Base):
44 49
 
45 50
     def setUp(self, plurals=None):
@@ -47,14 +52,20 @@ class CLITestV20Base(neutron_test_cli20.CLITestV20Base):
47 52
         self.client = ovsvappclient.Client(token=TOKEN,
48 53
                                            endpoint_url=self.endurl)
49 54
 
55
+    def assert_mock_multiple_calls_with_same_arguments(
56
+            self, mocked_method, expected_call, count):
57
+        if count is None:
58
+            self.assertLessEqual(1, mocked_method.call_count)
59
+        else:
60
+            self.assertEqual(count, mocked_method.call_count)
61
+        mocked_method.assert_has_calls(
62
+            [expected_call] * mocked_method.call_count)
63
+
50 64
     def _test_create_resource(self, resource, cmd, name, myid, args,
51 65
                               position_names, position_values,
52 66
                               tenant_id=None, tags=None, admin_state_up=True,
53 67
                               extra_body=None, cmd_resource=None,
54 68
                               parent_id=None, **kwargs):
55
-        self.mox.StubOutWithMock(cmd, "get_client")
56
-        self.mox.StubOutWithMock(self.client.httpclient, "request")
57
-        cmd.get_client().MultipleTimes().AndReturn(self.client)
58 69
         if not cmd_resource:
59 70
             cmd_resource = resource
60 71
         body = {resource: {}, }
@@ -70,27 +81,35 @@ class CLITestV20Base(neutron_test_cli20.CLITestV20Base):
70 81
         # url method body
71 82
         resource_plural = self.client.get_resource_plural(cmd_resource)
72 83
         path = getattr(self.client, resource_plural + "_path")
73
-        mox_body = MyComparator(body, self.client)
74
-        self.client.httpclient.request(
75
-            end_url(path), 'POST',
76
-            body=mox_body,
77
-            headers=mox.ContainsKeyValue(
78
-                'X-Auth-Token', TOKEN)).AndReturn((MyResp(200), resstr))
79
-        self.mox.ReplayAll()
84
+        mock_body = MyComparator(body, self.client)
85
+
80 86
         cmd_parser = cmd.get_parser('create_' + resource)
81
-        neutronshell.run_command(cmd, cmd_parser, args)
82
-        self.mox.VerifyAll()
83
-        self.mox.UnsetStubs()
84
-        _str = self.fake_stdout.make_string()
85
-        self.assertIn(myid, _str)
86
-        if name:
87
-            self.assertIn(name, _str)
87
+
88
+        resp = (MyResp(200), resstr)
89
+
90
+        with mock.patch.object(cmd, "get_client",
91
+                               return_value=self.client) as mock_get_client, \
92
+                mock.patch.object(self.client.httpclient, "request",
93
+                                  return_value=resp) as mock_request:
94
+
95
+            neutronshell.run_command(cmd, cmd_parser, args)
96
+
97
+            _str = self.fake_stdout.make_string()
98
+            self.assertIn(myid, _str)
99
+            if name:
100
+                self.assertIn(name, _str)
101
+
102
+            self.assert_mock_multiple_calls_with_same_arguments(
103
+                mock_get_client, mock.call(), None)
104
+
105
+            mock_request.asswert_called_once_with(
106
+                MyUrlComparator(end_url(path), self.client),
107
+                'POST',
108
+                body=mock_body,
109
+                headers=ContainsKeyValue({'X-Auth-Token': TOKEN}))
88 110
 
89 111
     def _test_update_resource(self, resource, cmd, myid, args, extrafields,
90 112
                               cmd_resource=None, parent_id=None):
91
-        self.mox.StubOutWithMock(cmd, "get_client")
92
-        self.mox.StubOutWithMock(self.client.httpclient, "request")
93
-        cmd.get_client().MultipleTimes().AndReturn(self.client)
94 113
         if not cmd_resource:
95 114
             cmd_resource = resource
96 115
 
@@ -101,21 +120,25 @@ class CLITestV20Base(neutron_test_cli20.CLITestV20Base):
101 120
             path = path % (parent_id, myid)
102 121
         else:
103 122
             path = path % extrafields['vcenter_id']
104
-        mox_body = MyComparator(body, self.client)
105
-
106
-        self.client.httpclient.request(
107
-            MyUrlComparator(end_url(path),
108
-                            self.client),
109
-            'PUT',
110
-            body=mox_body,
111
-            headers=mox.ContainsKeyValue(
112
-                'X-Auth-Token', TOKEN)).AndReturn((MyResp(204), None))
113
-        self.mox.ReplayAll()
123
+        mock_body = MyComparator(body, self.client)
124
+
114 125
         cmd_parser = cmd.get_parser("update_" + cmd_resource)
115
-        neutronshell.run_command(cmd, cmd_parser, args)
116
-        self.mox.VerifyAll()
117
-        self.mox.UnsetStubs()
118
-        _str = self.fake_stdout.make_string()
119
-        # Delete a given vcenter cluster with given details
120
-        # will return nothing
121
-        self.assertEqual(_str, '')
126
+
127
+        resp = (MyResp(204), None)
128
+        with mock.patch.object(cmd, "get_client",
129
+                               return_value=self.client) as mock_get_client, \
130
+                mock.patch.object(self.client.httpclient, "request",
131
+                                  return_value=resp) as mock_request:
132
+
133
+            neutronshell.run_command(cmd, cmd_parser, args)
134
+            _str = self.fake_stdout.make_string()
135
+            self.assertEqual(_str, '')
136
+
137
+            self.assert_mock_multiple_calls_with_same_arguments(
138
+                mock_get_client, mock.call(), None)
139
+
140
+            mock_request.asswert_called_once_with(
141
+                MyUrlComparator(end_url(path), self.client),
142
+                'PUT',
143
+                body=mock_body,
144
+                headers=ContainsKeyValue({'X-Auth-Token': TOKEN}))

+ 0
- 3
networking_vsphere/tests/unit/nova/virt/vmwareapi/test_ovsvapp_vc_driver.py View File

@@ -21,7 +21,6 @@ from oslo_concurrency import lockutils
21 21
 from oslo_config import fixture as config_fixture
22 22
 from oslo_utils import uuidutils
23 23
 from oslo_vmware import vim_util as vutil
24
-from oslotest import moxstubout
25 24
 
26 25
 from networking_vsphere.nova.virt.vmwareapi import ovsvapp_vc_driver
27 26
 
@@ -143,8 +142,6 @@ class OVSvAppVCDriverTestCase(test.TestCase):
143 142
         self.vnc_host = 'ha-host'
144 143
         self.conn = ovsvapp_vc_driver.OVSvAppVCDriver(None)
145 144
         self.node_name = self.conn._nodename
146
-        mox_fixture = self.useFixture(moxstubout.MoxStubout())
147
-        self.mox = mox_fixture.mox
148 145
 
149 146
     def _get_network_info(self, network_id=NETWORK_NAME):
150 147
         """Gets network_info from the test utils and then change the network id

Loading…
Cancel
Save