def test_list_nets_sort_with_dirs_more_than_keys(self): """list nets: --sort-key name --sort-dir desc --sort-dir asc """ cmd = network.ListNetwork(test_cli20.MyApp(sys.stdout), None) self._test_list_networks(cmd, sort_key=['name'], sort_dir=['desc', 'asc'])
def test_list_nets_sort(self): # list nets: # --sort-key name --sort-key id --sort-dir asc --sort-dir desc cmd = network.ListNetwork(test_cli20.MyApp(sys.stdout), None) self._test_list_networks(cmd, sort_key=['name', 'id'], sort_dir=['asc', 'desc'])
def test_extend_list(self): data = [{ 'id': 'netid%d' % i, 'name': 'net%d' % i, 'subnets': ['mysubid%d' % i] } for i in range(10)] filters, response = self._build_test_data(data) 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", return_value=response) as mock_request: known_args, _vs = cmd.get_parser('create_subnets')\ .parse_known_args() 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, 'fields=id&fields=cidr' + filters), self.client), 'GET', body=None, headers=test_cli20.ContainsKeyValue( {'X-Auth-Token': test_cli20.TOKEN}))
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_list_nets_empty_with_column(self): resources = "networks" cmd = network.ListNetwork(test_cli20.MyApp(sys.stdout), None) reses = {resources: []} resstr = self.client.serialize(reses) resp = (test_cli20.MyResp(200), resstr) # url method body query = "id=myfakeid" args = ['-c', 'id', '--', '--id', 'myfakeid'] path = getattr(self.client, resources + "_path") 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_list_nets_pagination(self): cmd = network.ListNetwork(test_cli20.MyApp(sys.stdout), None) with mock.patch.object(network.ListNetwork, "extend_list", return_value=None) as mock_extend_list: self._test_list_resources_with_pagination("networks", cmd) mock_extend_list.assert_called_once_with(test_cli20.IsA(list), mock.ANY)
def _test_list_resources_with_formatter(self, fmt): resources = 'networks' cmd = network.ListNetwork(MyApp(sys.stdout), None) # ListNetwork has its own extend_list, so we need to stub out it # to avoid an extra API call. self.mox.StubOutWithMock(network.ListNetwork, "extend_list") network.ListNetwork.extend_list(mox.IsA(list), mox.IgnoreArg()) self._test_list_resources(resources, cmd, output_format=fmt)
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_tags_query(self): # This test examines that '-' in the tag related filters # is not converted to '_'. resources = 'networks' cmd = network.ListNetwork(test_cli20.MyApp(sys.stdout), None) with mock.patch.object(network.ListNetwork, 'extend_list'): args = ['--not-tags', 'red,blue', '--tags-any', 'green', '--not-tags-any', 'black'] query = "not-tags=red,blue&tags-any=green¬-tags-any=black" self._test_tags_query(cmd, resources, args, query)
def test_list_nets_with_default_column(self): cmd = network.ListNetwork(test_cli20.MyApp(sys.stdout), None) returned_body = {"networks": [{"name": "buildname3", "id": "id3", "tenant_id": "tenant_3", "subnets": []}]} self._test_list_nets_columns(cmd, returned_body) _str = self.fake_stdout.make_string() returned_networks = jsonutils.loads(_str) self.assertEqual(1, len(returned_networks)) net = returned_networks[0] self.assertEqual(3, len(net)) self.assertEqual(0, len(set(net) ^ set(cmd.list_columns)))
def test_tags_query(self): # This test examines that '-' in the tag related filters # is not converted to '_'. resources = 'networks' cmd = network.ListNetwork(test_cli20.MyApp(sys.stdout), None) self.mox.StubOutWithMock(network.ListNetwork, "extend_list") network.ListNetwork.extend_list(mox.IsA(list), mox.IgnoreArg()) args = [ '--not-tags', 'red,blue', '--tags-any', 'green', '--not-tags-any', 'black' ] query = "not-tags=red,blue&tags-any=green¬-tags-any=black" self._test_tags_query(cmd, resources, args, query)
def _test_extend_list(self, mox_calls): data = [{'id': 'netid%d' % i, 'name': 'net%d' % i, 'subnets': ['mysubid%d' % i]} for i in range(10)] self.mox.StubOutWithMock(self.client.httpclient, "request") path = getattr(self.client, 'subnets_path') cmd = network.ListNetwork(test_cli20.MyApp(sys.stdout), None) self.mox.StubOutWithMock(cmd, "get_client") cmd.get_client().MultipleTimes().AndReturn(self.client) mox_calls(path, data) self.mox.ReplayAll() known_args, _vs = cmd.get_parser('create_subnets').parse_known_args() cmd.extend_list(data, known_args) self.mox.VerifyAll()
def test_list_nets_defined_column(self): cmd = network.ListNetwork(test_cli20.MyApp(sys.stdout), None) returned_body = {"networks": [{"name": "buildname3", "id": "id3", "tenant_id": "tenant_3", "subnets": []}]} self._test_list_nets_columns(cmd, returned_body, args=['-f', 'json', '-c', 'id']) _str = self.fake_stdout.make_string() returned_networks = jsonutils.loads(_str) self.assertEqual(1, len(returned_networks)) net = returned_networks[0] self.assertEqual(1, len(net)) self.assertIn("id", net.keys())
def _test_list_nets_extend_subnets(self, data, expected): 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) cmd = network.ListNetwork(test_cli20.MyApp(sys.stdout), None) self.mox.StubOutWithMock(cmd, 'get_client') self.mox.StubOutWithMock(self.client.httpclient, 'request') cmd.get_client().AndReturn(self.client) setup_list_stub('networks', data, '') cmd.get_client().AndReturn(self.client) filters = '' for n in data: for s in n['subnets']: filters = filters + "&id=%s" % s setup_list_stub('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' }], query='fields=id&fields=cidr' + filters) self.mox.ReplayAll() args = [] cmd_parser = cmd.get_parser('list_networks') parsed_args = cmd_parser.parse_args(args) result = cmd.get_data(parsed_args) self.mox.VerifyAll() self.mox.UnsetStubs() _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_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)] 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_list_nets_fields(self): # List nets: --fields a --fields b -- --fields c d. cmd = network.ListNetwork(test_cli20.MyApp(sys.stdout), None) self._test_list_networks(cmd, fields_1=['a', 'b'], fields_2=['c', 'd'])
def test_list_shared_networks(self): # list nets : --shared False cmd = network.ListNetwork(test_cli20.MyApp(sys.stdout), None) self._test_list_networks(cmd, base_args='--shared False'.split(), query='shared=False')
def test_list_nets_tags_with_unicode(self): # List nets: -- --tags u'\u7f51\u7edc'. cmd = network.ListNetwork(test_cli20.MyApp(sys.stdout), None) self._test_list_networks(cmd, tags=[u'\u7f51\u7edc'])
def test_list_nets_detail_tags(self): # List nets: -D -- --tags a b. cmd = network.ListNetwork(test_cli20.MyApp(sys.stdout), None) self._test_list_networks(cmd, detail=True, tags=['a', 'b'])
def test_list_nets_detail(self): # list nets: -D. cmd = network.ListNetwork(test_cli20.MyApp(sys.stdout), None) self._test_list_networks(cmd, True)
def test_list_nets_limit(self): # list nets: -P. cmd = network.ListNetwork(test_cli20.MyApp(sys.stdout), None) self._test_list_networks(cmd, page_size=1000)
def test_list_nets_sort_with_keys_more_than_dirs(self): # list nets: --sort-key name --sort-key id --sort-dir desc cmd = network.ListNetwork(test_cli20.MyApp(sys.stdout), None) self._test_list_networks(cmd, sort_key=['name', 'id'], sort_dir=['desc'])
def test_list_nets_tags(self): """List nets: -- --tags a b.""" cmd = network.ListNetwork(test_cli20.MyApp(sys.stdout), None) self._test_list_networks(cmd, tags=['a', 'b'])
def test_list_nets_pagination(self): cmd = network.ListNetwork(test_cli20.MyApp(sys.stdout), None) self.mox.StubOutWithMock(network.ListNetwork, "extend_list") network.ListNetwork.extend_list(mox.IsA(list), mox.IgnoreArg()) self._test_list_resources_with_pagination("networks", cmd)