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_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_get_id_from_name_multiple_with_project_not_found(self): name = 'web_server' project = uuidutils.generate_uuid() resstr_notfound = self.client.serialize({'security_groups': []}) resp = (test_cli20.MyResp(200), resstr_notfound) path = getattr(self.client, "security_groups_path") with mock.patch.object(self.client.httpclient, "request", return_value=resp) as mock_request: exc = self.assertRaises(exceptions.NotFound, neutronV20.find_resourceid_by_name_or_id, self.client, 'security_group', name, project) mock_request.assert_called_once_with( test_cli20.MyUrlComparator( test_cli20.end_url( path, "fields=id&name=%s&tenant_id=%s" % (name, project)), self.client), 'GET', body=None, headers=test_cli20.ContainsKeyValue( {'X-Auth-Token': test_cli20.TOKEN})) self.assertIn('Unable to find', exc.message) self.assertEqual(404, exc.status_code)
def test_get_id_from_name_multiple(self): name = 'myname' reses = { 'networks': [{ 'id': uuidutils.generate_uuid() }, { 'id': uuidutils.generate_uuid() }] } resstr = self.client.serialize(reses) resp = (test_cli20.MyResp(200), resstr) path = getattr(self.client, "networks_path") with mock.patch.object(self.client.httpclient, "request", return_value=resp) as mock_request: exception = self.assertRaises( exceptions.NeutronClientNoUniqueMatch, neutronV20.find_resourceid_by_name_or_id, self.client, 'network', name) mock_request.assert_called_once_with( test_cli20.MyUrlComparator( test_cli20.end_url(path, "fields=id&name=" + name), self.client), 'GET', body=None, headers=test_cli20.ContainsKeyValue( {'X-Auth-Token': test_cli20.TOKEN})) self.assertIn('Multiple', exception.message)
def _build_test_data(self, data, excess=0): # Length of a query filter on security group rule id # in these testcases, id='secgroupid%02d' (with len(id)=12) sec_group_id_filter_len = 12 response = [] replace_rules = {'security_group_id': 'security_group', 'remote_group_id': 'remote_group'} search_opts = {'fields': ['id', 'name']} sec_group_ids = set() for rule in data: for key in replace_rules: sec_group_ids.add(rule[key]) response.append({'id': rule[key], 'name': 'default'}) sec_group_ids = list(sec_group_ids) result = [] sec_group_count = len(sec_group_ids) max_size = ((sec_group_id_filter_len * sec_group_count) - excess) chunk_size = max_size / sec_group_id_filter_len for i in range(0, sec_group_count, chunk_size): search_opts['id'] = sec_group_ids[i: i + chunk_size] params = utils.safe_encode_dict(search_opts) resp_str = self.client.serialize({'security_groups': response}) result.append({ 'filter': six.moves.urllib.parse.urlencode(params, doseq=1), 'response': (test_cli20.MyResp(200), resp_str), }) return result
def test_get_id_from_name_multiple_with_project_not_found(self): name = 'web_server' project = str(uuid.uuid4()) reses = { 'security_groups': [{ 'id': str(uuid.uuid4()), 'tenant_id': str(uuid.uuid4()) }] } resstr = self.client.serialize(reses) self.mox.StubOutWithMock(self.client.httpclient, "request") path = getattr(self.client, "security_groups_path") self.client.httpclient.request( test_cli20.MyUrlComparator( test_cli20.end_url( path, "fields=id&name=%s&tenant_id=%s" % (name, project)), self.client), '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, 'security_group', name, project) except exceptions.NotFound as ex: self.assertIn('Unable to find', ex.message) self.assertEqual(404, ex.status_code)
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_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_resource_by_id(self, id_only=False): _id = str(uuid.uuid4()) 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_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_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_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_tag_operation(self, cmd, path, method, args, prog_name, body=None): self.mox.StubOutWithMock(cmd, "get_client") self.mox.StubOutWithMock(self.client.httpclient, "request") cmd.get_client().MultipleTimes().AndReturn(self.client) if body: body = test_cli20.MyComparator(body, self.client) self.client.httpclient.request(test_cli20.MyUrlComparator( test_cli20.end_url(path, format=self.format), self.client), method, body=body, headers=mox.ContainsKeyValue( 'X-Auth-Token', test_cli20.TOKEN)).AndReturn( (test_cli20.MyResp(204), None)) self.mox.ReplayAll() cmd_parser = cmd.get_parser(prog_name) shell.run_command(cmd, cmd_parser, 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, 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] 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', } 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_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_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.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_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_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.MyUrlComparator( test_cli20.end_url(path, "fields=id&name=" + name), self.client), 'GET', body=None, headers=mox.ContainsKeyValue('X-Auth-Token', test_cli20.TOKEN)).AndReturn( (test_cli20.MyResp(200), resstr)) self.mox.ReplayAll() exception = self.assertRaises(exceptions.NeutronClientNoUniqueMatch, neutronV20.find_resourceid_by_name_or_id, self.client, 'network', name) self.assertIn('Multiple', exception.message)
def test_get_id_from_name(self): name = 'myname' _id = uuidutils.generate_uuid() reses = { 'networks': [ { 'id': _id, }, ], } resstr = self.client.serialize(reses) resp = (test_cli20.MyResp(200), resstr) path = getattr(self.client, "networks_path") with mock.patch.object(self.client.httpclient, "request", return_value=resp) as mock_request: returned_id = neutronV20.find_resourceid_by_name_or_id( self.client, 'network', name) mock_request.assert_called_once_with( test_cli20.MyUrlComparator( test_cli20.end_url(path, "fields=id&name=" + name), self.client), 'GET', body=None, headers=test_cli20.ContainsKeyValue( {'X-Auth-Token': test_cli20.TOKEN})) self.assertEqual(_id, returned_id)
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_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_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_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 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.MyUrlComparator(test_cli20.end_url(path, query), self.client), 'GET', body=None, headers=mox.ContainsKeyValue('X-Auth-Token', test_cli20.TOKEN)).AndReturn(resp)
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 _build_test_data(self, data): subnet_ids = [] response = [] filters = "" for n in data: if 'subnets' in n: subnet_ids.extend(n['subnets']) for subnet_id in n['subnets']: filters = "%s&id=%s" % (filters, subnet_id) response.append({'id': subnet_id, 'cidr': '192.168.0.0/16'}) resp_str = self.client.serialize({'subnets': response}) resp = (test_cli20.MyResp(200), resp_str) return filters, resp
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)