def test_get_id_from_id_then_name_empty(self): _id = str(uuid.uuid4()) reses = {'networks': [{'id': _id, }, ], } resstr = self.client.serialize(reses) resstr1 = self.client.serialize({'networks': []}) self.mox.StubOutWithMock(self.client.httpclient, "request") path = getattr(self.client, "networks_path") self.client.httpclient.request( test_cli20.MyUrlComparator( test_cli20.end_url(path, "fields=id&id=" + _id), self.client), 'GET', body=None, headers=mox.ContainsKeyValue('X-Auth-Token', test_cli20.TOKEN) ).AndReturn((test_cli20.MyResp(200), resstr1)) self.client.httpclient.request( test_cli20.MyUrlComparator( test_cli20.end_url(path, "fields=id&name=" + _id), self.client), 'GET', body=None, headers=mox.ContainsKeyValue('X-Auth-Token', test_cli20.TOKEN) ).AndReturn((test_cli20.MyResp(200), resstr)) self.mox.ReplayAll() returned_id = neutronV20.find_resourceid_by_name_or_id( self.client, 'network', _id) self.assertEqual(_id, returned_id)
def test_get_id_from_id_then_name_empty(self): _id = uuidutils.generate_uuid() reses = {'networks': [{'id': _id, }, ], } resstr = self.client.serialize(reses) resstr1 = self.client.serialize({'networks': []}) path = getattr(self.client, "networks_path") with mock.patch.object(self.client.httpclient, "request") as mock_request: mock_request.side_effect = [(test_cli20.MyResp(200), resstr1), (test_cli20.MyResp(200), resstr)] returned_id = neutronV20.find_resourceid_by_name_or_id( self.client, 'network', _id) self.assertEqual(2, mock_request.call_count) mock_request.assert_has_calls([ mock.call( test_cli20.MyUrlComparator( test_cli20.end_url(path, "fields=id&id=" + _id), self.client), 'GET', body=None, headers=test_cli20.ContainsKeyValue( {'X-Auth-Token': test_cli20.TOKEN})), mock.call( test_cli20.MyUrlComparator( test_cli20.end_url(path, "fields=id&name=" + _id), self.client), 'GET', body=None, headers=test_cli20.ContainsKeyValue( {'X-Auth-Token': test_cli20.TOKEN}))]) self.assertEqual(_id, returned_id)
def _test_list_nets_extend_subnets(self, data, expected): cmd = network.ListNetwork(test_cli20.MyApp(sys.stdout), None) nets_path = getattr(self.client, 'networks_path') subnets_path = getattr(self.client, 'subnets_path') nets_query = '' filters = '' for n in data: for s in n['subnets']: filters = filters + "&id=%s" % s subnets_query = 'fields=id&fields=cidr' + filters with mock.patch.object(cmd, 'get_client', return_value=self.client) as mock_get_client, \ mock.patch.object(self.client.httpclient, "request") as mock_request: resp1 = (test_cli20.MyResp(200), self.client.serialize({'networks': data})) resp2 = (test_cli20.MyResp(200), self.client.serialize({'subnets': [ {'id': 'mysubid1', 'cidr': '192.168.1.0/24'}, {'id': 'mysubid2', 'cidr': '172.16.0.0/24'}, {'id': 'mysubid3', 'cidr': '10.1.1.0/24'}]})) mock_request.side_effect = [resp1, resp2] args = [] cmd_parser = cmd.get_parser('list_networks') parsed_args = cmd_parser.parse_args(args) result = cmd.take_action(parsed_args) mock_get_client.assert_called_with() self.assertEqual(2, mock_request.call_count) mock_request.assert_has_calls([ mock.call( test_cli20.MyUrlComparator( test_cli20.end_url(nets_path, nets_query), self.client), 'GET', body=None, headers=test_cli20.ContainsKeyValue( {'X-Auth-Token': test_cli20.TOKEN})), mock.call( test_cli20.MyUrlComparator( test_cli20.end_url(subnets_path, subnets_query), self.client), 'GET', body=None, headers=test_cli20.ContainsKeyValue( {'X-Auth-Token': test_cli20.TOKEN}))]) _result = [x for x in result[1]] self.assertEqual(len(expected), len(_result)) for res, exp in zip(_result, expected): self.assertEqual(len(exp), len(res)) for obsrvd, expctd in zip(res, exp): self.assertEqual(expctd, obsrvd)
def test_associate_healthmonitor(self): cmd = healthmonitor.AssociateHealthMonitor( test_cli20.MyApp(sys.stdout), None) resource = 'health_monitor' health_monitor_id = 'hm-id' pool_id = 'p_id' args = [health_monitor_id, pool_id] body = {resource: {'id': health_monitor_id}} result = {resource: {'id': health_monitor_id}, } result_str = self.client.serialize(result) path = getattr(self.client, "associate_pool_health_monitors_path") % pool_id return_tup = (test_cli20.MyResp(200), result_str) cmd_parser = cmd.get_parser('test_' + resource) parsed_args = cmd_parser.parse_args(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) 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_remove_firewall_rule(self): """firewall-policy-remove-rule myid ruleid """ resource = 'firewall_policy' cmd = firewallpolicy.FirewallPolicyRemoveRule( test_cli20.MyApp(sys.stdout), None) myid = 'myid' args = ['myid', 'removerule'] extrafields = {'firewall_rule_id': 'removerule', } self.mox.StubOutWithMock(cmd, "get_client") self.mox.StubOutWithMock(self.client.httpclient, "request") cmd.get_client().MultipleTimes().AndReturn(self.client) body = extrafields path = getattr(self.client, resource + "_remove_path") self.client.httpclient.request( test_cli20.MyUrlComparator( test_cli20.end_url(path % myid, format=self.format), self.client), 'PUT', body=test_cli20.MyComparator(body, self.client), headers=mox.ContainsKeyValue( 'X-Auth-Token', test_cli20.TOKEN)).AndReturn((test_cli20.MyResp(204), None)) args.extend(['--request-format', self.format]) self.mox.ReplayAll() cmd_parser = cmd.get_parser(resource + "_remove_rule") shell.run_command(cmd, cmd_parser, args) self.mox.VerifyAll() self.mox.UnsetStubs()
def test_associate_healthmonitor(self): cmd = healthmonitor.AssociateHealthMonitor( test_cli20.MyApp(sys.stdout), None) resource = 'health_monitor' health_monitor_id = 'hm-id' 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) 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()
def test_disassociate_healthmonitor(self): cmd = healthmonitor.DisassociateHealthMonitor( test_cli20.MyApp(sys.stdout), None) resource = 'health_monitor' health_monitor_id = 'hm-id' 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()
def test_remove_firewall_rule(self): # firewall-policy-remove-rule myid ruleid resource = 'firewall_policy' cmd = firewallpolicy.FirewallPolicyRemoveRule( test_cli20.MyApp(sys.stdout), None) myid = 'myid' args = ['myid', 'removerule'] extrafields = {'firewall_rule_id': 'removerule', } self.mox.StubOutWithMock(cmd, "get_client") self.mox.StubOutWithMock(self.client.httpclient, "request") cmd.get_client().MultipleTimes().AndReturn(self.client) body = extrafields path = getattr(self.client, resource + "_remove_path") self.client.httpclient.request( test_cli20.MyUrlComparator( test_cli20.end_url(path % myid), self.client), 'PUT', body=test_cli20.MyComparator(body, self.client), headers=mox.ContainsKeyValue( 'X-Auth-Token', test_cli20.TOKEN)).AndReturn((test_cli20.MyResp(204), None)) self.mox.ReplayAll() cmd_parser = cmd.get_parser(resource + "_remove_rule") shell.run_command(cmd, cmd_parser, args) self.mox.VerifyAll() self.mox.UnsetStubs()
def test_insert_firewall_rule(self): # firewall-policy-insert-rule myid newruleid --insert-before ruleAid # --insert-after ruleBid resource = 'firewall_policy' cmd = firewallpolicy.FirewallPolicyInsertRule( test_cli20.MyApp(sys.stdout), None) myid = 'myid' args = [ 'myid', 'newrule', '--insert-before', 'rule2', '--insert-after', 'rule1' ] extrafields = { 'firewall_rule_id': 'newrule', 'insert_before': 'rule2', 'insert_after': 'rule1' } self.mox.StubOutWithMock(cmd, "get_client") self.mox.StubOutWithMock(self.client.httpclient, "request") cmd.get_client().MultipleTimes().AndReturn(self.client) body = extrafields path = getattr(self.client, resource + "_insert_path") self.client.httpclient.request( test_cli20.MyUrlComparator(test_cli20.end_url(path % myid), self.client), 'PUT', body=test_cli20.MyComparator(body, self.client), headers=mox.ContainsKeyValue('X-Auth-Token', test_cli20.TOKEN)).AndReturn( (test_cli20.MyResp(204), None)) self.mox.ReplayAll() cmd_parser = cmd.get_parser(resource + "_insert_rule") shell.run_command(cmd, cmd_parser, args) self.mox.VerifyAll() self.mox.UnsetStubs()
def test_retrieve_pool_stats(self): # lb-pool-stats test_id. resource = 'pool' cmd = pool.RetrievePoolStats(test_cli20.MyApp(sys.stdout), None) my_id = self.test_id 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() _str = self.fake_stdout.make_string() self.assertIn('bytes_in', _str) self.assertIn('bytes_out', _str)
def test_get_loadbalancer_statuses(self): # lbaas-loadbalancer-status test_id. resource = 'loadbalancer' cmd = lb.RetrieveLoadBalancerStatus(test_cli20.MyApp(sys.stdout), None) my_id = self.test_id args = [my_id] expected_res = {'statuses': {'operating_status': 'ONLINE', 'provisioning_status': 'ACTIVE'}} resstr = self.client.serialize(expected_res) path = getattr(self.client, "lbaas_loadbalancer_path_status") return_tup = (test_cli20.MyResp(200), resstr) cmd_parser = cmd.get_parser("test_" + resource) parsed_args = cmd_parser.parse_args(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) 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) self.assertIn('provisioning_status', _str) self.assertIn('ACTIVE', _str)
def test_get_id_from_name_multiple(self): name = 'myname' reses = { 'networks': [{ 'id': str(uuid.uuid4()) }, { 'id': str(uuid.uuid4()) }] } resstr = self.client.serialize(reses) self.mox.StubOutWithMock(self.client.httpclient, "request") path = getattr(self.client, "networks_path") self.client.httpclient.request( test_cli20.end_url(path, "fields=id&name=" + name), 'GET', body=None, headers=mox.ContainsKeyValue('X-Auth-Token', test_cli20.TOKEN)).AndReturn( (test_cli20.MyResp(200), resstr)) self.mox.ReplayAll() try: neutronV20.find_resourceid_by_name_or_id(self.client, 'network', name) except exceptions.NeutronClientNoUniqueMatch as ex: self.assertTrue('Multiple' in ex.message)
def _test_show_ext_resource(self, resource, cmd, myid, args, fields=(), cmd_resource=None, parent_id=None): if not cmd_resource: cmd_resource = resource query = "&".join(["fields=%s" % field for field in fields]) expected_res = {resource: {self.id_field: myid, 'name': 'myname', }, } resstr = self.client.serialize(expected_res) path = getattr(self.client, cmd_resource + "_path") if parent_id: path = path % parent_id path = path % myid cmd_parser = cmd.get_parser("show_" + cmd_resource) resp = (MyResp(200), resstr) with mock.patch.object(cmd, 'get_client', return_value=self.client)as mock_get_client, \ mock.patch.object(self.client.httpclient, 'request', return_value=resp) as mock_request: shell.run_command(cmd, cmd_parser, args) self.assert_mock_multiple_calls_with_same_arguments( mock_get_client, mock.call(), None) mock_request.assert_called_once_with( test_cli20.end_url(path, query), 'GET', body=None, headers=test_cli20.ContainsKeyValue({'X-Auth-Token': TOKEN})) _str = self.fake_stdout.make_string() self.assertIn(myid, _str) self.assertIn('myname', _str)
def test_extend_list(self): data = [{'name': 'default', 'remote_group_id': 'remgroupid%02d' % i} for i in range(10)] data.append({'name': 'default', 'remote_group_id': None}) resources = "security_groups" cmd = securitygroup.ListSecurityGroupRule( test_cli20.MyApp(sys.stdout), None) path = getattr(self.client, resources + '_path') responses = self._build_test_data(data) known_args, _vs = cmd.get_parser( 'list' + resources).parse_known_args() resp = responses[0]['response'] with mock.patch.object(cmd, "get_client", return_value=self.client) as mock_get_client, \ mock.patch.object(self.client.httpclient, "request", return_value=resp) as mock_request: cmd.extend_list(data, known_args) mock_get_client.assert_called_once_with() mock_request.assert_called_once_with( test_cli20.MyUrlComparator(test_cli20.end_url( path, responses[0]['filter']), self.client), 'GET', body=None, headers=test_cli20.ContainsKeyValue( {'X-Auth-Token': test_cli20.TOKEN}))
def test_disassociate_healthmonitor(self): cmd = healthmonitor.DisassociateHealthMonitor( test_cli20.MyApp(sys.stdout), None) resource = 'health_monitor' health_monitor_id = 'hm-id' pool_id = 'p_id' args = [health_monitor_id, pool_id] path = (getattr(self.client, "disassociate_pool_health_monitors_path") % {'pool': pool_id, 'health_monitor': health_monitor_id}) return_tup = (test_cli20.MyResp(204), None) cmd_parser = cmd.get_parser('test_' + resource) parsed_args = cmd_parser.parse_args(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) 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}))
def _test_get_resource_by_id(self, id_only=False): _id = uuidutils.generate_uuid() net = {'id': _id, 'name': 'test'} reses = {'networks': [net], } resstr = self.client.serialize(reses) resp = (test_cli20.MyResp(200), resstr) path = getattr(self.client, "networks_path") if id_only: query_params = "fields=id&id=%s" % _id else: query_params = "id=%s" % _id with mock.patch.object(self.client.httpclient, "request", return_value=resp) as mock_request: if id_only: returned_id = neutronV20.find_resourceid_by_id( self.client, 'network', _id) self.assertEqual(_id, returned_id) else: result = neutronV20.find_resource_by_id( self.client, 'network', _id) self.assertEqual(net, result) mock_request.assert_called_once_with( test_cli20.MyUrlComparator( test_cli20.end_url(path, query_params), self.client), 'GET', body=None, headers=test_cli20.ContainsKeyValue( {'X-Auth-Token': test_cli20.TOKEN}))
def test_associate_healthmonitor(self): cmd = healthmonitor.AssociateHealthMonitor( test_cli20.MyApp(sys.stdout), None) resource = 'health_monitor' health_monitor_id = 'hm-id' pool_id = 'p_id' args = [health_monitor_id, pool_id] body = {resource: {'id': health_monitor_id}} result = { resource: { 'id': health_monitor_id }, } result_str = self.client.serialize(result) path = getattr(self.client, "associate_pool_health_monitors_path") % pool_id return_tup = (test_cli20.MyResp(200), result_str) cmd_parser = cmd.get_parser('test_' + resource) parsed_args = cmd_parser.parse_args(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) 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_list_external_nets_empty_with_column(self): resources = "networks" cmd = network.ListExternalNetwork(test_cli20.MyApp(sys.stdout), None) reses = {resources: []} resstr = self.client.serialize(reses) # url method body query = "router%3Aexternal=True&id=myfakeid" args = ['-c', 'id', '--', '--id', 'myfakeid'] path = getattr(self.client, resources + "_path") resp = (test_cli20.MyResp(200), resstr) with mock.patch.object(cmd, "get_client", return_value=self.client) as mock_get_client, \ mock.patch.object(self.client.httpclient, "request", return_value=resp) as mock_request, \ mock.patch.object(network.ListNetwork, "extend_list", return_value=None) as mock_extend_list: cmd_parser = cmd.get_parser("list_" + resources) shell.run_command(cmd, cmd_parser, args) mock_get_client.assert_called_once_with() mock_request.assert_called_once_with( test_cli20.MyUrlComparator( test_cli20.end_url(path, query), self.client), 'GET', body=None, headers=test_cli20.ContainsKeyValue( {'X-Auth-Token': test_cli20.TOKEN})) mock_extend_list.assert_called_once_with(test_cli20.IsA(list), mock.ANY) _str = self.fake_stdout.make_string() self.assertEqual('\n', _str)
def test_disassociate_healthmonitor(self): cmd = healthmonitor.DisassociateHealthMonitor( test_cli20.MyApp(sys.stdout), None) resource = 'health_monitor' health_monitor_id = 'hm-id' pool_id = 'p_id' args = [health_monitor_id, pool_id] path = ( getattr(self.client, "disassociate_pool_health_monitors_path") % { 'pool': pool_id, 'health_monitor': health_monitor_id }) return_tup = (test_cli20.MyResp(204), None) cmd_parser = cmd.get_parser('test_' + resource) parsed_args = cmd_parser.parse_args(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) 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}))
def _test_get_resource_by_id(self, id_only=False): _id = uuidutils.generate_uuid() net = {'id': _id, 'name': 'test'} reses = {'networks': [net], } resstr = self.client.serialize(reses) self.mox.StubOutWithMock(self.client.httpclient, "request") path = getattr(self.client, "networks_path") if id_only: query_params = "fields=id&id=%s" % _id else: query_params = "id=%s" % _id self.client.httpclient.request( test_cli20.MyUrlComparator( test_cli20.end_url(path, query_params), self.client), 'GET', body=None, headers=mox.ContainsKeyValue('X-Auth-Token', test_cli20.TOKEN) ).AndReturn((test_cli20.MyResp(200), resstr)) self.mox.ReplayAll() if id_only: returned_id = neutronV20.find_resourceid_by_id( self.client, 'network', _id) self.assertEqual(_id, returned_id) else: result = neutronV20.find_resource_by_id( self.client, 'network', _id) self.assertEqual(net, result)
def test_list_external_nets_empty_with_column(self): resources = "networks" cmd = network.ListExternalNetwork(test_cli20.MyApp(sys.stdout), None) self.mox.StubOutWithMock(cmd, "get_client") self.mox.StubOutWithMock(self.client.httpclient, "request") self.mox.StubOutWithMock(network.ListNetwork, "extend_list") network.ListNetwork.extend_list(mox.IsA(list), mox.IgnoreArg()) cmd.get_client().MultipleTimes().AndReturn(self.client) reses = {resources: []} resstr = self.client.serialize(reses) # url method body query = "router%3Aexternal=True&id=myfakeid" args = ['-c', 'id', '--', '--id', 'myfakeid'] path = getattr(self.client, resources + "_path") self.client.httpclient.request( test_cli20.end_url(path, query), 'GET', body=None, headers=mox.ContainsKeyValue( 'X-Auth-Token', test_cli20.TOKEN)).AndReturn( (test_cli20.MyResp(200), resstr)) self.mox.ReplayAll() cmd_parser = cmd.get_parser("list_" + resources) shell.run_command(cmd, cmd_parser, args) self.mox.VerifyAll() self.mox.UnsetStubs() _str = self.fake_stdout.make_string() self.assertEqual('\n', _str)
def _test_delete_ext_resource(self, resource, cmd, myid, args, cmd_resource=None, parent_id=None): if not cmd_resource: cmd_resource = resource path = getattr(self.client, cmd_resource + "_path") if parent_id: path = path % parent_id path = path % myid cmd_parser = cmd.get_parser("delete_" + cmd_resource) resp = (MyResp(204), None) with mock.patch.object(cmd, 'get_client', return_value=self.client)as mock_get_client, \ mock.patch.object(self.client.httpclient, 'request', return_value=resp) as mock_request: shell.run_command(cmd, cmd_parser, args) self.assert_mock_multiple_calls_with_same_arguments( mock_get_client, mock.call(), None) mock_request.assert_called_once_with( test_cli20.end_url(path), 'DELETE', body=None, headers=test_cli20.ContainsKeyValue({'X-Auth-Token': TOKEN})) _str = self.fake_stdout.make_string() self.assertIn(myid, _str)
def test_retrieve_pool_stats(self): # lb-pool-stats test_id. resource = 'pool' cmd = pool.RetrievePoolStats(test_cli20.MyApp(sys.stdout), None) my_id = self.test_id fields = ['bytes_in', 'bytes_out'] args = ['--fields', 'bytes_in', '--fields', 'bytes_out', my_id] 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) cmd_parser = cmd.get_parser("test_" + resource) parsed_args = cmd_parser.parse_args(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.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)
def _test_show_ext_resource(self, resource, cmd, myid, args, fields=(), cmd_resource=None, parent_id=None): self.mox.StubOutWithMock(cmd, "get_client") self.mox.StubOutWithMock(self.client.httpclient, "request") cmd.get_client().MultipleTimes().AndReturn(self.client) if not cmd_resource: cmd_resource = resource query = "&".join(["fields=%s" % field for field in fields]) expected_res = {resource: {self.id_field: myid, 'name': 'myname', }, } resstr = self.client.serialize(expected_res) path = getattr(self.client, cmd_resource + "_path") if parent_id: path = path % parent_id path = path % myid self.client.httpclient.request( test_cli20.end_url(path, query, format=self.format), 'GET', body=None, headers=mox.ContainsKeyValue( 'X-Auth-Token', TOKEN)).AndReturn((MyResp(200), resstr)) self.mox.ReplayAll() cmd_parser = cmd.get_parser("show_" + cmd_resource) shell.run_command(cmd, cmd_parser, args) self.mox.VerifyAll() self.mox.UnsetStubs() _str = self.fake_stdout.make_string() self.assertIn(myid, _str) self.assertIn('myname', _str)
def test_get_id_from_name_multiple(self): name = 'myname' reses = { 'networks': [{ 'id': str(uuid.uuid4()) }, { 'id': str(uuid.uuid4()) }] } resstr = self.client.serialize(reses) self.mox.StubOutWithMock(self.client.httpclient, "request") path = getattr(self.client, "networks_path") self.client.httpclient.request( test_cli20.end_url(path, "fields=id&name=" + name), 'GET', body=None, headers=mox.ContainsKeyValue('X-Auth-Token', test_cli20.TOKEN)).AndReturn( (test_cli20.MyResp(200), resstr)) self.mox.ReplayAll() try: neutronV20.find_resourceid_by_name_or_id(self.client, 'network', name) except exceptions.NeutronClientNoUniqueMatch as ex: self.assertIn('Multiple', ex.message)
def test_associate_healthmonitor(self): cmd = healthmonitor.AssociateHealthMonitor( test_cli20.MyApp(sys.stdout), None) resource = 'health_monitor' health_monitor_id = 'hm-id' 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) 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()
def _test_list_external_nets(self, resources, cmd, detail=False, tags=(), fields_1=(), fields_2=()): self.mox.StubOutWithMock(cmd, "get_client") self.mox.StubOutWithMock(self.client.httpclient, "request") self.mox.StubOutWithMock(network.ListNetwork, "extend_list") network.ListNetwork.extend_list(mox.IsA(list), mox.IgnoreArg()) cmd.get_client().MultipleTimes().AndReturn(self.client) reses = {resources: [{"id": "myid1"}, {"id": "myid2"}]} resstr = self.client.serialize(reses) # url method body query = "" args = detail and ["-D"] or [] if fields_1: for field in fields_1: args.append("--fields") args.append(field) if tags: args.append("--") args.append("--tag") for tag in tags: args.append(tag) if (not tags) and fields_2: args.append("--") if fields_2: args.append("--fields") for field in fields_2: args.append(field) for field in itertools.chain(fields_1, fields_2): if query: query += "&fields=" + field else: query = "fields=" + field if query: query += "&router%3Aexternal=True" else: query += "router%3Aexternal=True" for tag in tags: if query: query += "&tag=" + tag else: query = "tag=" + tag if detail: query = query and query + "&verbose=True" or "verbose=True" path = getattr(self.client, resources + "_path") self.client.httpclient.request( test_cli20.MyUrlComparator(test_cli20.end_url(path, query), self.client), "GET", body=None, headers=mox.ContainsKeyValue("X-Auth-Token", test_cli20.TOKEN), ).AndReturn((test_cli20.MyResp(200), resstr)) self.mox.ReplayAll() cmd_parser = cmd.get_parser("list_" + resources) shell.run_command(cmd, cmd_parser, args) self.mox.VerifyAll() self.mox.UnsetStubs() _str = self.fake_stdout.make_string() self.assertIn("myid1", _str)
def test_show_quota_default(self): resource = 'quota' cmd = test_quota.ShowQuotaDefault( test_cli20.MyApp(sys.stdout), None) args = ['--tenant-id', self.test_id] expected_res = {'quota': {'port': 50, 'network': 10, 'subnet': 10}} resstr = self.client.serialize(expected_res) path = getattr(self.client, "quota_default_path") return_tup = (test_cli20.MyResp(200), resstr) 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_parser = cmd.get_parser("test_" + resource) parsed_args = cmd_parser.parse_args(args) cmd.run(parsed_args) mock_get_client.assert_called_once_with() mock_request.assert_called_once_with( test_cli20.end_url(path % self.test_id), 'GET', body=None, headers=test_cli20.ContainsKeyValue( {'X-Auth-Token': test_cli20.TOKEN})) _str = self.fake_stdout.make_string() self.assertIn('network', _str) self.assertIn('subnet', _str) self.assertIn('port', _str) self.assertNotIn('subnetpool', _str)
def test_list_nets_empty_with_column(self): resources = "networks" cmd = network.ListNetwork(test_cli20.MyApp(sys.stdout), None) self.mox.StubOutWithMock(cmd, "get_client") self.mox.StubOutWithMock(self.client.httpclient, "request") self.mox.StubOutWithMock(network.ListNetwork, "extend_list") network.ListNetwork.extend_list(mox.IsA(list), mox.IgnoreArg()) cmd.get_client().MultipleTimes().AndReturn(self.client) reses = {resources: []} resstr = self.client.serialize(reses) # url method body query = "id=myfakeid" args = ["-c", "id", "--", "--id", "myfakeid"] path = getattr(self.client, resources + "_path") self.client.httpclient.request( test_cli20.MyUrlComparator(test_cli20.end_url(path, query), self.client), "GET", body=None, headers=mox.ContainsKeyValue("X-Auth-Token", test_cli20.TOKEN), ).AndReturn((test_cli20.MyResp(200), resstr)) self.mox.ReplayAll() cmd_parser = cmd.get_parser("list_" + resources) shell.run_command(cmd, cmd_parser, args) self.mox.VerifyAll() self.mox.UnsetStubs() _str = self.fake_stdout.make_string() self.assertEqual("\n", _str)
def test_retrieve_loadbalancer_stats(self): # lbaas-loadbalancer-stats test_id. resource = 'loadbalancer' cmd = lb.RetrieveLoadBalancerStats(test_cli20.MyApp(sys.stdout), None) my_id = self.test_id 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) self.mox.VerifyAll() self.mox.UnsetStubs() _str = self.fake_stdout.make_string() self.assertIn('bytes_in', _str) self.assertIn('1234', _str) self.assertIn('bytes_out', _str) self.assertIn('4321', _str)
def test_remove_firewall_rule(self): # firewall-policy-remove-rule myid ruleid resource = 'firewall_policy' cmd = firewallpolicy.FirewallPolicyRemoveRule( test_cli20.MyApp(sys.stdout), None) myid = 'myid' args = ['myid', 'removerule'] extrafields = {'firewall_rule_id': 'removerule', } body = extrafields path = getattr(self.client, resource + "_remove_path") cmd_parser = cmd.get_parser(resource + "_remove_rule") resp = (test_cli20.MyResp(204), None) with mock.patch.object(cmd, "get_client", return_value=self.client) as mock_get_client, \ mock.patch.object(self.client.httpclient, "request", return_value=resp) as mock_request: shell.run_command(cmd, cmd_parser, args) self.assert_mock_multiple_calls_with_same_arguments( mock_get_client, mock.call(), 2) mock_request.assert_called_once_with( test_cli20.MyUrlComparator( test_cli20.end_url(path % myid), self.client), 'PUT', 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( test_cli20.MyApp(sys.stdout), None) resource = 'health_monitor' health_monitor_id = 'hm-id' 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()
def _test_list_router_port(self, resources, cmd, myid, detail=False, tags=[], fields_1=[], fields_2=[]): self.mox.StubOutWithMock(cmd, "get_client") self.mox.StubOutWithMock(self.client.httpclient, "request") cmd.get_client().MultipleTimes().AndReturn(self.client) reses = {resources: [{'id': 'myid1', }, {'id': 'myid2', }, ], } resstr = self.client.serialize(reses) # url method body query = "" args = detail and ['-D', ] or [] if fields_1: for field in fields_1: args.append('--fields') args.append(field) args.append(myid) if tags: args.append('--') args.append("--tag") for tag in tags: args.append(tag) if (not tags) and fields_2: args.append('--') if fields_2: args.append("--fields") for field in fields_2: args.append(field) fields_1.extend(fields_2) for field in fields_1: if query: query += "&fields=" + field else: query = "fields=" + field for tag in tags: if query: query += "&tag=" + tag else: query = "tag=" + tag if detail: query = query and query + '&verbose=True' or 'verbose=True' query = query and query + '&device_id=%s' or 'device_id=%s' path = getattr(self.client, resources + "_path") self.client.httpclient.request( test_cli20.end_url(path, query % myid), 'GET', body=None, headers=mox.ContainsKeyValue('X-Auth-Token', test_cli20.TOKEN) ).AndReturn((test_cli20.MyResp(200), resstr)) self.mox.ReplayAll() cmd_parser = cmd.get_parser("list_" + resources) shell.run_command(cmd, cmd_parser, args) self.mox.VerifyAll() self.mox.UnsetStubs() _str = self.fake_stdout.make_string() self.assertTrue('myid1' in _str)
def test_list_external_nets_empty_with_column(self): resources = "networks" cmd = network.ListExternalNetwork(test_cli20.MyApp(sys.stdout), None) self.mox.StubOutWithMock(cmd, "get_client") self.mox.StubOutWithMock(self.client.httpclient, "request") self.mox.StubOutWithMock(network.ListNetwork, "extend_list") network.ListNetwork.extend_list(mox.IsA(list), mox.IgnoreArg()) cmd.get_client().MultipleTimes().AndReturn(self.client) reses = {resources: []} resstr = self.client.serialize(reses) # url method body query = "router%3Aexternal=True&id=myfakeid" args = ['-c', 'id', '--', '--id', 'myfakeid'] path = getattr(self.client, resources + "_path") self.client.httpclient.request( test_cli20.end_url(path, query), 'GET', body=None, headers=mox.ContainsKeyValue( 'X-Auth-Token', test_cli20.TOKEN)).AndReturn( (test_cli20.MyResp(200), resstr)) self.mox.ReplayAll() cmd_parser = cmd.get_parser("list_" + resources) shell.run_command(cmd, cmd_parser, args) self.mox.VerifyAll() self.mox.UnsetStubs() _str = self.fake_stdout.make_string() self.assertEquals('\n', _str)
def test_show_quota_default(self): resource = 'quota' cmd = test_quota.ShowQuotaDefault( test_cli20.MyApp(sys.stdout), None) args = ['--tenant-id', self.test_id] self.mox.StubOutWithMock(cmd, "get_client") self.mox.StubOutWithMock(self.client.httpclient, "request") cmd.get_client().MultipleTimes().AndReturn(self.client) expected_res = {'quota': {'port': 50, 'network': 10, 'subnet': 10}} resstr = self.client.serialize(expected_res) path = getattr(self.client, "quota_default_path") return_tup = (test_cli20.MyResp(200), resstr) self.client.httpclient.request( test_cli20.end_url(path % self.test_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) self.mox.VerifyAll() self.mox.UnsetStubs() _str = self.fake_stdout.make_string() self.assertIn('network', _str) self.assertIn('subnet', _str) self.assertIn('port', _str) self.assertNotIn('subnetpool', _str)
def test_extend_list_exceed_max_uri_len(self): data = [{'id': 'netid%d' % i, 'name': 'net%d' % i, 'subnets': ['mysubid%d' % i]} for i in range(10)] # Since in pagination we add &marker=<uuid> (44 symbols), total change # is 45 symbols. Single subnet takes 40 symbols (id=<uuid>&). # Because of it marker will take more space than single subnet filter, # and we expect neutron to send last 2 subnets in separate response. filters1, response1 = self._build_test_data(data[:len(data) - 2]) filters2, response2 = self._build_test_data(data[len(data) - 2:]) path = getattr(self.client, 'subnets_path') cmd = network.ListNetwork(test_cli20.MyApp(sys.stdout), None) with mock.patch.object(cmd, "get_client", return_value=self.client) as mock_get_client, \ mock.patch.object(self.client.httpclient, "request") as mock_request, \ mock.patch.object(self.client.httpclient, "_check_uri_length", return_value=None) as mock_check_uri_length: # 1 char of extra URI len will cause a split in 2 requests mock_check_uri_length.side_effect = [ exceptions.RequestURITooLong(excess=1), None, None] mock_request.side_effect = [response1, response2] known_args, _vs = cmd.get_parser('create_subnets')\ .parse_known_args() cmd.extend_list(data, known_args) mock_get_client.assert_called_once_with() self.assertEqual(2, mock_request.call_count) mock_request.assert_has_calls([ mock.call( test_cli20.MyUrlComparator( test_cli20.end_url( path, 'fields=id&fields=cidr%s' % filters1), self.client), 'GET', body=None, headers=test_cli20.ContainsKeyValue( {'X-Auth-Token': test_cli20.TOKEN})), mock.call( test_cli20.MyUrlComparator( test_cli20.end_url( path, 'fields=id&fields=cidr%s' % filters2), self.client), 'GET', body=None, headers=test_cli20.ContainsKeyValue( {'X-Auth-Token': test_cli20.TOKEN}))])
def test_extend_list_exceed_max_uri_len(self): data = [{ 'id': 'netid%d' % i, 'name': 'net%d' % i, 'subnets': ['mysubid%d' % i] } for i in range(10)] # Since in pagination we add &marker=<uuid> (44 symbols), total change # is 45 symbols. Single subnet takes 40 symbols (id=<uuid>&). # Because of it marker will take more space than single subnet filter, # and we expect neutron to send last 2 subnets in separate response. filters1, response1 = self._build_test_data(data[:len(data) - 2]) filters2, response2 = self._build_test_data(data[len(data) - 2:]) path = getattr(self.client, 'subnets_path') cmd = network.ListNetwork(test_cli20.MyApp(sys.stdout), None) with mock.patch.object(cmd, "get_client", return_value=self.client) as mock_get_client, \ mock.patch.object(self.client.httpclient, "request") as mock_request, \ mock.patch.object(self.client.httpclient, "_check_uri_length", return_value=None) as mock_check_uri_length: # 1 char of extra URI len will cause a split in 2 requests mock_check_uri_length.side_effect = [ exceptions.RequestURITooLong(excess=1), None, None ] mock_request.side_effect = [response1, response2] known_args, _vs = cmd.get_parser('create_subnets')\ .parse_known_args() cmd.extend_list(data, known_args) mock_get_client.assert_called_once_with() self.assertEqual(2, mock_request.call_count) mock_request.assert_has_calls([ mock.call(test_cli20.MyUrlComparator( test_cli20.end_url(path, 'fields=id&fields=cidr%s' % filters1), self.client), 'GET', body=None, headers=test_cli20.ContainsKeyValue( {'X-Auth-Token': test_cli20.TOKEN})), mock.call(test_cli20.MyUrlComparator( test_cli20.end_url(path, 'fields=id&fields=cidr%s' % filters2), self.client), 'GET', body=None, headers=test_cli20.ContainsKeyValue( {'X-Auth-Token': test_cli20.TOKEN})) ])
def mox_calls(path, data): filters, response = self._build_test_data(data) self.client.httpclient.request( test_cli20.end_url(path, 'fields=id&fields=cidr' + filters), 'GET', body=None, headers=mox.ContainsKeyValue( 'X-Auth-Token', test_cli20.TOKEN)).AndReturn(response)
def mox_calls(path, data): responses = self._build_test_data(data) self.client.httpclient.request( test_cli20.MyUrlComparator(test_cli20.end_url(path, responses[0]["filter"]), self.client), "GET", body=None, headers=mox.ContainsKeyValue("X-Auth-Token", test_cli20.TOKEN), ).AndReturn(responses[0]["response"])
def _test_assoc_with_cfg_agent(self, resource, cmd, cmd_args, destination, body, result): path = ((scheduler.ConfigAgentHandlingHostingDevice.resource_path + destination) % cmd_args[0]) result_str = self.client.serialize(result) return_tup = (test_cli20.MyResp(200), result_str) cmd_parser = cmd.get_parser('test_' + resource) parsed_args = cmd_parser.parse_args(cmd_args) if getattr(self, 'mox', None): self.mox.StubOutWithMock(cmd, "get_client") self.mox.StubOutWithMock(self.client.httpclient, "request") cmd.get_client().MultipleTimes().AndReturn(self.client) 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.run(parsed_args) self.mox.VerifyAll() self.mox.UnsetStubs() else: mock_request_calls = [ mock.call(test_cli20.end_url(path), 'POST', body=test_cli20.MyComparator(body, self.client), headers=test_cli20.ContainsKeyValue( {'X-Auth-Token': test_cli20.TOKEN})) ] with mock.patch.object( cmd, "get_client", return_value=self.client) as mock_get_client: with mock.patch.object( self.client.httpclient, "request", return_value=return_tup) as mock_request: cmd.run(parsed_args) mock_request.assert_has_calls(mock_request_calls) self.assert_mock_multiple_calls_with_same_arguments( mock_get_client, mock.call(), None)
def mox_calls(path, data): responses = self._build_test_data(data) self.client.httpclient.request(test_cli20.MyUrlComparator( test_cli20.end_url(path, responses[0]['filter']), self.client), 'GET', body=None, headers=mox.ContainsKeyValue( 'X-Auth-Token', test_cli20.TOKEN)).AndReturn( responses[0]['response'])
def setup_create_stub(resources, data): reses = {resources: data} resstr = self.client.serialize(reses) resp = (test_cli20.MyResp(200), resstr) path = getattr(self.client, resources + '_path') self.client.httpclient.request( test_cli20.end_url(path), 'POST', body=resstr, headers=mox.ContainsKeyValue('X-Auth-Token', test_cli20.TOKEN)).AndReturn(resp)
def setup_list_stub(resources, data, query): reses = {resources: data} resstr = self.client.serialize(reses) resp = (test_cli20.MyResp(200), resstr) path = getattr(self.client, resources + '_path') self.client.httpclient.request( test_cli20.end_url(path, query), 'GET', body=None, headers=mox.ContainsKeyValue( 'X-Auth-Token', test_cli20.TOKEN)).AndReturn(resp)
def test_extend_list_exceed_max_uri_len(self): data = [{ 'id': 'netid%d' % i, 'name': 'net%d' % i, 'subnets': ['mysubid%d' % i] } for i in range(10)] filters1, response1 = self._build_test_data(data[:len(data) - 1]) filters2, response2 = self._build_test_data(data[len(data) - 1:]) path = getattr(self.client, 'subnets_path') cmd = network.ListNetwork(test_cli20.MyApp(sys.stdout), None) with mock.patch.object(cmd, "get_client", return_value=self.client) as mock_get_client, \ mock.patch.object(self.client.httpclient, "request") as mock_request, \ mock.patch.object(self.client.httpclient, "_check_uri_length", return_value=None) as mock_check_uri_length: # 1 char of extra URI len will cause a split in 2 requests mock_check_uri_length.side_effect = [ exceptions.RequestURITooLong(excess=1), None, None ] mock_request.side_effect = [response1, response2] known_args, _vs = cmd.get_parser('create_subnets')\ .parse_known_args() cmd.extend_list(data, known_args) mock_get_client.assert_called_once_with() self.assertEqual(2, mock_request.call_count) mock_request.assert_has_calls([ mock.call(test_cli20.MyUrlComparator( test_cli20.end_url(path, 'fields=id&fields=cidr%s' % filters1), self.client), 'GET', body=None, headers=test_cli20.ContainsKeyValue( {'X-Auth-Token': test_cli20.TOKEN})), mock.call(test_cli20.MyUrlComparator( test_cli20.end_url(path, 'fields=id&fields=cidr%s' % filters2), self.client), 'GET', body=None, headers=test_cli20.ContainsKeyValue( {'X-Auth-Token': test_cli20.TOKEN})) ])
def test_extend_list_exceed_max_uri_len(self): data = [{'id': 'netid%d' % i, 'name': 'net%d' % i, 'subnets': ['mysubid%d' % i]} for i in range(10)] filters1, response1 = self._build_test_data(data[:len(data) - 1]) filters2, response2 = self._build_test_data(data[len(data) - 1:]) path = getattr(self.client, 'subnets_path') cmd = network.ListNetwork(test_cli20.MyApp(sys.stdout), None) with mock.patch.object(cmd, "get_client", return_value=self.client) as mock_get_client, \ mock.patch.object(self.client.httpclient, "request") as mock_request, \ mock.patch.object(self.client.httpclient, "_check_uri_length", return_value=None) as mock_check_uri_length: # 1 char of extra URI len will cause a split in 2 requests mock_check_uri_length.side_effect = [ exceptions.RequestURITooLong(excess=1), None, None] mock_request.side_effect = [response1, response2] known_args, _vs = cmd.get_parser('create_subnets')\ .parse_known_args() cmd.extend_list(data, known_args) mock_get_client.assert_called_once_with() self.assertEqual(2, mock_request.call_count) mock_request.assert_has_calls([ mock.call( test_cli20.MyUrlComparator( test_cli20.end_url( path, 'fields=id&fields=cidr%s' % filters1), self.client), 'GET', body=None, headers=test_cli20.ContainsKeyValue( {'X-Auth-Token': test_cli20.TOKEN})), mock.call( test_cli20.MyUrlComparator( test_cli20.end_url( path, 'fields=id&fields=cidr%s' % filters2), self.client), 'GET', body=None, headers=test_cli20.ContainsKeyValue( {'X-Auth-Token': test_cli20.TOKEN}))])
def mox_calls(path, data): responses = self._build_test_data(data) self.client.httpclient.request( test_cli20.MyUrlComparator(test_cli20.end_url( path, responses[0]['filter']), self.client), 'GET', body=None, headers=mox.ContainsKeyValue( 'X-Auth-Token', test_cli20.TOKEN)).AndReturn( responses[0]['response'])
def setup_list_stub(resources, data, query): reses = {resources: data} resstr = self.client.serialize(reses) resp = (test_cli20.MyResp(200), resstr) path = getattr(self.client, resources + '_path') self.client.httpclient.request( test_cli20.MyUrlComparator(test_cli20.end_url(path, query), self.client), 'GET', body=None, headers=mox.ContainsKeyValue('X-Auth-Token', test_cli20.TOKEN)).AndReturn(resp)
def test_get_id_from_id_then_name_empty(self): _id = str(uuid.uuid4()) reses = {'networks': [{'id': _id, }, ], } resstr = self.client.serialize(reses) resstr1 = self.client.serialize({'networks': []}) self.mox.StubOutWithMock(self.client.httpclient, "request") path = getattr(self.client, "networks_path") self.client.httpclient.request( test_cli20.end_url(path, "fields=id&id=" + _id), 'GET', body=None, headers=mox.ContainsKeyValue('X-Auth-Token', test_cli20.TOKEN) ).AndReturn((test_cli20.MyResp(200), resstr1)) self.client.httpclient.request( test_cli20.end_url(path, "fields=id&name=" + _id), 'GET', body=None, headers=mox.ContainsKeyValue('X-Auth-Token', test_cli20.TOKEN) ).AndReturn((test_cli20.MyResp(200), resstr)) self.mox.ReplayAll() returned_id = neutronV20.find_resourceid_by_name_or_id( self.client, 'network', _id) self.assertEqual(_id, returned_id)
def setup_list_stub(resources, data, query, mock_calls, mock_returns): reses = {resources: data} resstr = self.client.serialize(reses) resp = (test_cli20.MyResp(200), resstr) path = getattr(self.client, resources + '_path') mock_calls.append( mock.call(test_cli20.MyUrlComparator( test_cli20.end_url(path, query), self.client), 'GET', body=None, headers=test_cli20.ContainsKeyValue( {'X-Auth-Token': test_cli20.TOKEN}))) mock_returns.append(resp)
def _test_remove_from_hosting_device(self, resource, cmd, cmd_args, destination): path = ((hostingdevice.HostingDevice.resource_path + destination + '/%s') % cmd_args) return_tup = (test_cli20.MyResp(204), None) cmd_parser = cmd.get_parser('test_' + resource) parsed_args = cmd_parser.parse_args(cmd_args) if getattr(self, 'mox', None): self.mox.StubOutWithMock(cmd, "get_client") self.mox.StubOutWithMock(self.client.httpclient, "request") cmd.get_client().MultipleTimes().AndReturn(self.client) 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.run(parsed_args) self.mox.VerifyAll() self.mox.UnsetStubs() else: mock_request_calls = [ mock.call( test_cli20.end_url(path), 'DELETE', body=None, headers=test_cli20.ContainsKeyValue( {'X-Auth-Token': test_cli20.TOKEN})) ] with mock.patch.object(cmd, "get_client", return_value=self.client) as mock_get_client: with mock.patch.object(self.client.httpclient, "request", return_value=return_tup) as mock_request: cmd.run(parsed_args) mock_request.assert_has_calls(mock_request_calls) self.assert_mock_multiple_calls_with_same_arguments( mock_get_client, mock.call(), None)
def test_extend_list_exceed_max_uri_len(self): data = [{ 'name': 'default', 'security_group_id': 'secgroupid%02d' % i, 'remote_group_id': 'remgroupid%02d' % i } for i in range(10)] data.append({ 'name': 'default', 'security_group_id': 'secgroupid10', 'remote_group_id': None }) resources = "security_groups" cmd = securitygroup.ListSecurityGroupRule(test_cli20.MyApp(sys.stdout), None) path = getattr(self.client, resources + '_path') responses = self._build_test_data(data, excess=1) known_args, _vs = cmd.get_parser('list' + resources).parse_known_args() mock_request_side_effects = [] mock_request_calls = [] mock_check_uri_side_effects = [exceptions.RequestURITooLong(excess=1)] mock_check_uri_calls = [mock.call(mock.ANY)] for item in responses: mock_request_side_effects.append(item['response']) mock_request_calls.append( mock.call(test_cli20.MyUrlComparator( test_cli20.end_url(path, item['filter']), self.client), 'GET', body=None, headers=test_cli20.ContainsKeyValue( {'X-Auth-Token': test_cli20.TOKEN}))) mock_check_uri_side_effects.append(None) mock_check_uri_calls.append(mock.call(mock.ANY)) with mock.patch.object(cmd, "get_client", return_value=self.client) as mock_get_client, \ mock.patch.object(self.client.httpclient, "request") as mock_request, \ mock.patch.object(self.client.httpclient, "_check_uri_length") as mock_check_uri: mock_request.side_effect = mock_request_side_effects mock_check_uri.side_effect = mock_check_uri_side_effects cmd.extend_list(data, known_args) mock_get_client.assert_called_once_with() mock_request.assert_has_calls(mock_request_calls) mock_check_uri.assert_has_calls(mock_check_uri_calls) self.assertEqual(len(mock_request_calls), mock_request.call_count) self.assertEqual(len(mock_check_uri_calls), mock_check_uri.call_count)
def test_get_id_from_id_then_name_empty(self): _id = uuidutils.generate_uuid() reses = { 'networks': [ { 'id': _id, }, ], } resstr = self.client.serialize(reses) resstr1 = self.client.serialize({'networks': []}) path = getattr(self.client, "networks_path") with mock.patch.object(self.client.httpclient, "request") as mock_request: mock_request.side_effect = [(test_cli20.MyResp(200), resstr1), (test_cli20.MyResp(200), resstr)] returned_id = neutronV20.find_resourceid_by_name_or_id( self.client, 'network', _id) self.assertEqual(2, mock_request.call_count) mock_request.assert_has_calls([ mock.call(test_cli20.MyUrlComparator( test_cli20.end_url(path, "fields=id&id=" + _id), self.client), 'GET', body=None, headers=test_cli20.ContainsKeyValue( {'X-Auth-Token': test_cli20.TOKEN})), mock.call(test_cli20.MyUrlComparator( test_cli20.end_url(path, "fields=id&name=" + _id), self.client), 'GET', body=None, headers=test_cli20.ContainsKeyValue( {'X-Auth-Token': test_cli20.TOKEN})) ]) self.assertEqual(_id, returned_id)
def mox_calls(path, data): # 1 char of extra URI len will cause a split in 2 requests self.mox.StubOutWithMock(self.client, '_check_uri_length') self.client._check_uri_length(mox.IgnoreArg()).AndRaise( exceptions.RequestURITooLong(excess=1)) responses = self._build_test_data(data, excess=1) for item in responses: self.client._check_uri_length(mox.IgnoreArg()).AndReturn(None) self.client.httpclient.request( test_cli20.end_url(path, item['filter']), 'GET', body=None, headers=mox.ContainsKeyValue('X-Auth-Token', test_cli20.TOKEN)).AndReturn( item['response'])
def _test_tag_operation(self, cmd, path, method, args, prog_name, body=None): with mock.patch.object(cmd, 'get_client', return_value=self.client) as mock_get_client, \ mock.patch.object(self.client.httpclient, 'request', return_value=(test_cli20.MyResp(204), None) ) as mock_request: if body: body = test_cli20.MyComparator(body, self.client) cmd_parser = cmd.get_parser(prog_name) shell.run_command(cmd, cmd_parser, args) mock_get_client.assert_called_once_with() mock_request.assert_called_once_with( test_cli20.MyUrlComparator(test_cli20.end_url(path), self.client), method, body=body, headers=test_cli20.ContainsKeyValue( {'X-Auth-Token': test_cli20.TOKEN}))
def test_get_id_from_name_notfound(self): name = 'myname' reses = {'networks': []} resstr = self.client.serialize(reses) self.mox.StubOutWithMock(self.client.httpclient, "request") path = getattr(self.client, "networks_path") self.client.httpclient.request( test_cli20.end_url(path, "fields=id&name=" + name), 'GET', body=None, headers=mox.ContainsKeyValue('X-Auth-Token', test_cli20.TOKEN) ).AndReturn((test_cli20.MyResp(200), resstr)) self.mox.ReplayAll() try: neutronV20.find_resourceid_by_name_or_id( self.client, 'network', name) except exceptions.NeutronClientException as ex: self.assertIn('Unable to find', ex.message) self.assertEqual(404, ex.status_code)
def _test_remove_from_agent(self, resource, cmd, cmd_args, destination): path = ((self.client.agent_path + destination + '/%s') % cmd_args) self.mox.StubOutWithMock(cmd, "get_client") self.mox.StubOutWithMock(self.client.httpclient, "request") cmd.get_client().MultipleTimes().AndReturn(self.client) 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(cmd_args) cmd.run(parsed_args) self.mox.VerifyAll() self.mox.UnsetStubs()
def _test_remove_from_agent(self, resource, cmd, cmd_args, destination): path = ((self.client.agent_path + destination + '/%s') % cmd_args) return_tup = (test_cli20.MyResp(204), None) cmd_parser = cmd.get_parser('test_' + resource) parsed_args = cmd_parser.parse_args(cmd_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) 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}))
def mox_calls(path, data): sub_data_lists = [data[:len(data) - 1], data[len(data) - 1:]] filters, response = self._build_test_data(data) # 1 char of extra URI len will cause a split in 2 requests self.mox.StubOutWithMock(self.client, "_check_uri_length") self.client._check_uri_length(mox.IgnoreArg()).AndRaise( exceptions.RequestURITooLong(excess=1)) for data in sub_data_lists: filters, response = self._build_test_data(data) self.client._check_uri_length(mox.IgnoreArg()).AndReturn(None) self.client.httpclient.request( test_cli20.end_url(path, 'fields=id&fields=cidr%s' % filters), 'GET', body=None, headers=mox.ContainsKeyValue( 'X-Auth-Token', test_cli20.TOKEN)).AndReturn(response)