From ce623030ee19619eb28276e6f9f0a395261560ba Mon Sep 17 00:00:00 2001 From: Hongbin Lu Date: Fri, 16 Mar 2018 19:26:18 +0000 Subject: [PATCH] Remove mox/mox3 usage from lb modules Change-Id: I667dd870b615d4a9ccefd35fcb1ebf15760b1d6d Partial-Bug: #1753504 --- .../tests/unit/lb/test_cli20_healthmonitor.py | 54 +++++++++---------- .../tests/unit/lb/test_cli20_pool.py | 27 +++++----- .../unit/lb/v2/test_cli20_loadbalancer.py | 50 +++++++++-------- 3 files changed, 65 insertions(+), 66 deletions(-) diff --git a/neutronclient/tests/unit/lb/test_cli20_healthmonitor.py b/neutronclient/tests/unit/lb/test_cli20_healthmonitor.py index 999116e2a..7c4d15c09 100644 --- a/neutronclient/tests/unit/lb/test_cli20_healthmonitor.py +++ b/neutronclient/tests/unit/lb/test_cli20_healthmonitor.py @@ -16,7 +16,7 @@ import sys -from mox3 import mox +import mock from neutronclient.neutron.v2_0.lb import healthmonitor from neutronclient.tests.unit import test_cli20 @@ -149,10 +149,6 @@ class CLITestV20LbHealthmonitorJSON(test_cli20.CLITestV20Base): pool_id = 'p_id' args = [health_monitor_id, pool_id] - self.mox.StubOutWithMock(cmd, "get_client") - self.mox.StubOutWithMock(self.client.httpclient, "request") - cmd.get_client().MultipleTimes().AndReturn(self.client) - body = {resource: {'id': health_monitor_id}} result = {resource: {'id': health_monitor_id}, } result_str = self.client.serialize(result) @@ -160,17 +156,21 @@ class CLITestV20LbHealthmonitorJSON(test_cli20.CLITestV20Base): path = getattr(self.client, "associate_pool_health_monitors_path") % pool_id return_tup = (test_cli20.MyResp(200), result_str) - self.client.httpclient.request( - test_cli20.end_url(path), 'POST', - body=test_cli20.MyComparator(body, self.client), - headers=mox.ContainsKeyValue( - 'X-Auth-Token', test_cli20.TOKEN)).AndReturn(return_tup) - self.mox.ReplayAll() cmd_parser = cmd.get_parser('test_' + resource) parsed_args = cmd_parser.parse_args(args) - cmd.run(parsed_args) - self.mox.VerifyAll() - self.mox.UnsetStubs() + + with mock.patch.object(cmd, "get_client", + return_value=self.client) as mock_get_client, \ + mock.patch.object(self.client.httpclient, "request", + return_value=return_tup) as mock_request: + cmd.run(parsed_args) + + mock_get_client.assert_called_once_with() + mock_request.assert_called_once_with( + test_cli20.end_url(path), 'POST', + body=test_cli20.MyComparator(body, self.client), + headers=test_cli20.ContainsKeyValue( + {'X-Auth-Token': test_cli20.TOKEN})) def test_disassociate_healthmonitor(self): cmd = healthmonitor.DisassociateHealthMonitor( @@ -181,22 +181,22 @@ class CLITestV20LbHealthmonitorJSON(test_cli20.CLITestV20Base): pool_id = 'p_id' args = [health_monitor_id, pool_id] - self.mox.StubOutWithMock(cmd, "get_client") - self.mox.StubOutWithMock(self.client.httpclient, "request") - cmd.get_client().MultipleTimes().AndReturn(self.client) - path = (getattr(self.client, "disassociate_pool_health_monitors_path") % {'pool': pool_id, 'health_monitor': health_monitor_id}) return_tup = (test_cli20.MyResp(204), None) - self.client.httpclient.request( - test_cli20.end_url(path), 'DELETE', - body=None, - headers=mox.ContainsKeyValue( - 'X-Auth-Token', test_cli20.TOKEN)).AndReturn(return_tup) - self.mox.ReplayAll() cmd_parser = cmd.get_parser('test_' + resource) parsed_args = cmd_parser.parse_args(args) - cmd.run(parsed_args) - self.mox.VerifyAll() - self.mox.UnsetStubs() + + with mock.patch.object(cmd, "get_client", + return_value=self.client) as mock_get_client, \ + mock.patch.object(self.client.httpclient, "request", + return_value=return_tup) as mock_request: + cmd.run(parsed_args) + + mock_get_client.assert_called_once_with() + mock_request.assert_called_once_with( + test_cli20.end_url(path), 'DELETE', + body=None, + headers=test_cli20.ContainsKeyValue( + {'X-Auth-Token': test_cli20.TOKEN})) diff --git a/neutronclient/tests/unit/lb/test_cli20_pool.py b/neutronclient/tests/unit/lb/test_cli20_pool.py index 9a08f66fe..1106aed92 100644 --- a/neutronclient/tests/unit/lb/test_cli20_pool.py +++ b/neutronclient/tests/unit/lb/test_cli20_pool.py @@ -16,7 +16,7 @@ import sys -from mox3 import mox +import mock from neutronclient.neutron.v2_0.lb import pool from neutronclient.tests.unit import test_cli20 @@ -139,27 +139,28 @@ class CLITestV20LbPoolJSON(test_cli20.CLITestV20Base): fields = ['bytes_in', 'bytes_out'] args = ['--fields', 'bytes_in', '--fields', 'bytes_out', my_id] - self.mox.StubOutWithMock(cmd, "get_client") - self.mox.StubOutWithMock(self.client.httpclient, "request") - cmd.get_client().MultipleTimes().AndReturn(self.client) query = "&".join(["fields=%s" % field for field in fields]) expected_res = {'stats': {'bytes_in': '1234', 'bytes_out': '4321'}} resstr = self.client.serialize(expected_res) path = getattr(self.client, "pool_path_stats") return_tup = (test_cli20.MyResp(200), resstr) - self.client.httpclient.request( - test_cli20.end_url(path % my_id, query), 'GET', - body=None, - headers=mox.ContainsKeyValue( - 'X-Auth-Token', test_cli20.TOKEN)).AndReturn(return_tup) - self.mox.ReplayAll() cmd_parser = cmd.get_parser("test_" + resource) parsed_args = cmd_parser.parse_args(args) - cmd.run(parsed_args) - self.mox.VerifyAll() - self.mox.UnsetStubs() + with mock.patch.object(cmd, "get_client", + return_value=self.client) as mock_get_client, \ + mock.patch.object(self.client.httpclient, "request", + return_value=return_tup) as mock_request: + cmd.run(parsed_args) + + self.assert_mock_multiple_calls_with_same_arguments( + mock_get_client, mock.call(), 2) + mock_request.assert_called_once_with( + test_cli20.end_url(path % my_id, query), 'GET', + body=None, + headers=test_cli20.ContainsKeyValue( + {'X-Auth-Token': test_cli20.TOKEN})) _str = self.fake_stdout.make_string() self.assertIn('bytes_in', _str) self.assertIn('bytes_out', _str) diff --git a/neutronclient/tests/unit/lb/v2/test_cli20_loadbalancer.py b/neutronclient/tests/unit/lb/v2/test_cli20_loadbalancer.py index b62ce3435..6db07cc1a 100644 --- a/neutronclient/tests/unit/lb/v2/test_cli20_loadbalancer.py +++ b/neutronclient/tests/unit/lb/v2/test_cli20_loadbalancer.py @@ -16,7 +16,7 @@ import sys -from mox3 import mox +import mock from neutronclient.neutron.v2_0.lb.v2 import loadbalancer as lb from neutronclient.tests.unit import test_cli20 @@ -150,27 +150,27 @@ class CLITestV20LbLoadBalancerJSON(test_cli20.CLITestV20Base): fields = ['bytes_in', 'bytes_out'] args = ['--fields', 'bytes_in', '--fields', 'bytes_out', my_id] - self.mox.StubOutWithMock(cmd, "get_client") - self.mox.StubOutWithMock(self.client.httpclient, "request") - cmd.get_client().MultipleTimes().AndReturn(self.client) query = "&".join(["fields=%s" % field for field in fields]) expected_res = {'stats': {'bytes_in': '1234', 'bytes_out': '4321'}} resstr = self.client.serialize(expected_res) path = getattr(self.client, "lbaas_loadbalancer_path_stats") return_tup = (test_cli20.MyResp(200), resstr) - self.client.httpclient.request( - test_cli20.end_url(path % my_id, query), 'GET', - body=None, - headers=mox.ContainsKeyValue( - 'X-Auth-Token', test_cli20.TOKEN)).AndReturn(return_tup) - self.mox.ReplayAll() cmd_parser = cmd.get_parser("test_" + resource) parsed_args = cmd_parser.parse_args(args) - cmd.run(parsed_args) + with mock.patch.object(cmd, "get_client", + return_value=self.client) as mock_get_client, \ + mock.patch.object(self.client.httpclient, "request", + return_value=return_tup) as mock_request: + cmd.run(parsed_args) - self.mox.VerifyAll() - self.mox.UnsetStubs() + self.assert_mock_multiple_calls_with_same_arguments( + mock_get_client, mock.call(), 2) + mock_request.assert_called_once_with( + test_cli20.end_url(path % my_id, query), 'GET', + body=None, + headers=test_cli20.ContainsKeyValue( + {'X-Auth-Token': test_cli20.TOKEN})) _str = self.fake_stdout.make_string() self.assertIn('bytes_in', _str) self.assertIn('1234', _str) @@ -184,10 +184,6 @@ class CLITestV20LbLoadBalancerJSON(test_cli20.CLITestV20Base): my_id = self.test_id args = [my_id] - self.mox.StubOutWithMock(cmd, "get_client") - self.mox.StubOutWithMock(self.client.httpclient, "request") - cmd.get_client().MultipleTimes().AndReturn(self.client) - expected_res = {'statuses': {'operating_status': 'ONLINE', 'provisioning_status': 'ACTIVE'}} @@ -195,19 +191,21 @@ class CLITestV20LbLoadBalancerJSON(test_cli20.CLITestV20Base): path = getattr(self.client, "lbaas_loadbalancer_path_status") return_tup = (test_cli20.MyResp(200), resstr) - self.client.httpclient.request( - test_cli20.end_url(path % my_id), 'GET', - body=None, - headers=mox.ContainsKeyValue( - 'X-Auth-Token', test_cli20.TOKEN)).AndReturn(return_tup) - self.mox.ReplayAll() cmd_parser = cmd.get_parser("test_" + resource) parsed_args = cmd_parser.parse_args(args) - cmd.run(parsed_args) + with mock.patch.object(cmd, "get_client", + return_value=self.client) as mock_get_client, \ + mock.patch.object(self.client.httpclient, "request", + return_value=return_tup) as mock_request: + cmd.run(parsed_args) - self.mox.VerifyAll() - self.mox.UnsetStubs() + mock_get_client.assert_called_once_with() + mock_request.assert_called_once_with( + test_cli20.end_url(path % my_id), 'GET', + body=None, + headers=test_cli20.ContainsKeyValue( + {'X-Auth-Token': test_cli20.TOKEN})) _str = self.fake_stdout.make_string() self.assertIn('operating_status', _str) self.assertIn('ONLINE', _str)