def test_resource_collection(self, mock_session): mock_session.get_json.return_value = { 'foos': [{ 'href': self.BASE + '/foo/ec1afeaa-8930-43b0-a60a-939f23a50724', 'uuid': 'ec1afeaa-8930-43b0-a60a-939f23a50724' }, { 'href': self.BASE + '/foo/c2588045-d6fb-4f37-9f46-9451f653fb6a', 'uuid': 'c2588045-d6fb-4f37-9f46-9451f653fb6a' }] } Context().shell.current_path = Path('/foo') result = self.mgr.get('ls')() self.assertEqual( '\n'.join([ 'ec1afeaa-8930-43b0-a60a-939f23a50724', 'c2588045-d6fb-4f37-9f46-9451f653fb6a' ]), result) Context().shell.current_path = Path('/') result = self.mgr.get('ls')(paths=['foo']) self.assertEqual( '\n'.join([ 'foo/ec1afeaa-8930-43b0-a60a-939f23a50724', 'foo/c2588045-d6fb-4f37-9f46-9451f653fb6a' ]), result)
def test_resource_long_ls(self, mock_session): mock_session.id_to_fqname.return_value = { 'type': 'foo', 'fq_name': FQName('default-project:foo:ec1afeaa-8930-43b0-a60a-939f23a50724') } mock_session.get_json.return_value = { 'foo': { 'href': self.BASE + '/foo/ec1afeaa-8930-43b0-a60a-939f23a50724', 'uuid': 'ec1afeaa-8930-43b0-a60a-939f23a50724', 'fq_name': ['default-project', 'foo', 'ec1afeaa-8930-43b0-a60a-939f23a50724'], 'prop': { 'foo': False, 'bar': [1, 2, 3] } } } Context().shell.current_path = Path('/') result = self.mgr.get('ls')(paths=['foo/ec1afeaa-8930-43b0-a60a-939f23a50724'], long=True) expected_result = "foo/ec1afeaa-8930-43b0-a60a-939f23a50724 default-project:foo:ec1afeaa-8930-43b0-a60a-939f23a50724" self.assertEqual(result, expected_result) Context().shell.current_path = Path('/foo') result = self.mgr.get('ls')(paths=['ec1afeaa-8930-43b0-a60a-939f23a50724'], long=True, fields=['prop']) expected_results = ["ec1afeaa-8930-43b0-a60a-939f23a50724 foo=False|bar=1,2,3", "ec1afeaa-8930-43b0-a60a-939f23a50724 bar=1,2,3|foo=False"] self.assertTrue(any([result == r for r in expected_results]))
def test_cd(self): self.mgr.get('cd')('foo') self.assertEqual(Context().shell.current_path, Path('/foo')) self.mgr.get('cd')('bar') self.assertEqual(Context().shell.current_path, Path('/foo/bar')) self.mgr.get('cd')('..') self.assertEqual(Context().shell.current_path, Path('/foo')) self.mgr.get('cd')('') self.assertEqual(Context().shell.current_path, Path('/foo')) self.mgr.get('cd')('/') self.assertEqual(Context().shell.current_path, Path('/'))
def test_root_collection(self, mock_session): Context().shell.current_path = Path('/') mock_session.get_json.return_value = { 'href': self.BASE, 'links': [ {'link': {'href': self.BASE + '/instance-ips', 'path': Path('/instance-ips'), 'name': 'instance-ip', 'rel': 'collection'}}, {'link': {'href': self.BASE + '/instance-ip', 'path': Path('/instance-ip'), 'name': 'instance-ip', 'rel': 'resource-base'}} ] } result = self.mgr.get('ls')() self.assertEqual('instance-ip', result) mock_session.get_json.side_effect = [ { 'href': self.BASE, 'links': [ {'link': {'href': self.BASE + '/foos', 'path': Path('/foos'), 'name': 'foo', 'rel': 'collection'}}, {'link': {'href': self.BASE + '/bars', 'path': Path('/bars'), 'name': 'bar', 'rel': 'collection'}} ] }, { 'foos': [ {'href': self.BASE + '/foo/ec1afeaa-8930-43b0-a60a-939f23a50724', 'uuid': 'ec1afeaa-8930-43b0-a60a-939f23a50724'}, {'href': self.BASE + '/foo/c2588045-d6fb-4f37-9f46-9451f653fb6a', 'uuid': 'c2588045-d6fb-4f37-9f46-9451f653fb6a'} ] }, { 'bars': [ {'href': self.BASE + '/bar/ffe8de43-a141-4336-8d70-bf970813bbf7', 'uuid': 'ffe8de43-a141-4336-8d70-bf970813bbf7'} ] } ] Context().shell.current_path = Path('/') expected_result = """foo/ec1afeaa-8930-43b0-a60a-939f23a50724 foo/c2588045-d6fb-4f37-9f46-9451f653fb6a bar/ffe8de43-a141-4336-8d70-bf970813bbf7""" result = self.mgr.get('ls')(paths=['*']) self.assertEqual(result, expected_result)
def test_count(self, mock_session): mock_session.get_json.return_value = {'foos': {'count': 3}} Context().shell.current_path = Path('/foo') result = self.mgr.get('du')() self.assertEqual(result, '3') Context().shell.current_path = Path('/') result = self.mgr.get('du')(paths=['foo']) self.assertEqual(result, '3') Context().shell.current_path = Path('/foo/%s' % uuid.uuid4()) with self.assertRaises(CommandError): self.mgr.get('du')()
def test_add_del_resource(self, mock_session): mock_document = Document(text='cat bar') comp = ShellCompleter() r1 = Resource('foo', uuid='d8eb36b4-9c57-49c5-9eac-95bedc90eb9a') r2 = Resource('bar', uuid='4c6d3711-61f1-4505-b8df-189d32b52872') completions = comp.get_completions(mock_document, None) self.assertTrue(str(r2.path.relative_to(Context().shell.current_path)) in [c.text for c in completions]) r1.delete() r2.delete() completions = comp.get_completions(mock_document, None) self.assertTrue(str(r2.path.relative_to(Context().shell.current_path)) not in [c.text for c in completions])
def test_ln(self, mock_session): Context().schema = create_schema_from_version('2.21') r1 = Resource('virtual-network', uuid='9174e7d3-865b-4faf-ab0f-c083e43fee6d') r2 = Resource('route-table', uuid='9174e7d3-865b-4faf-ab0f-c083e43fee6d') r3 = Resource('project', uuid='9174e7d3-865b-4faf-ab0f-c083e43fee6d') r5 = Resource('logical-router', uuid='9174e7d3-865b-4faf-ab0f-c083e43fee6d') self.mgr.get('ln')(resources=[r1.path, r2.path]) self.mgr.get('ln')(resources=[r1.path, r2.path], remove=True) self.mgr.get('ln')(resources=[r1.path, r5.path]) self.mgr.get('ln')(resources=[r1.path, r5.path], remove=True) with self.assertRaises(CommandError): self.mgr.get('ln')(resources=[r1.path, r3.path]) with self.assertRaises(CommandError): self.mgr.get('ln')(resources=['foo/9174e7d3-865b-4faf-ab0f-c083e43fee6d', r1.path]) Context().schema = DummySchema()
def setUp(self, mock_session): self.maxDiff = None self.BASE = "http://localhost:8082" mock_session.configure_mock(base_url=self.BASE) mock_session.get_json.return_value = { "href": self.BASE, "links": [{ "link": { "href": self.BASE + "/foos", "name": "foo", "rel": "collection" } }, { "link": { "href": self.BASE + "/bars", "name": "bar", "rel": "collection" } }, { "link": { "href": self.BASE + "/foobars", "name": "foobar", "rel": "collection" } }] } DummyResourceSchema() Context().schema = DummySchema()
def test_rm_wildcard_resources(self, mock_session): mock_session.configure_mock(base_url=self.BASE) Context().shell.current_path = Path('/foo') mock_session.get_json.return_value = { 'foos': [{ 'href': self.BASE + '/foo/ec1afeaa-8930-43b0-a60a-939f23a50724', 'uuid': 'ec1afeaa-8930-43b0-a60a-939f23a50724', 'fq_name': ['default', 'foo', '1'] }, { 'href': self.BASE + '/foo/c2588045-d6fb-4f37-9f46-9451f653fb6a', 'uuid': 'c2588045-d6fb-4f37-9f46-9451f653fb6a', 'fq_name': ['default', 'foo', '1'] }] } mock_session.delete.return_value = True t = ['ec1afeaa-8930*', 'c2588045-d6fb-4f37-9f46-9451f653fb6a'] self.mgr.get('rm')(paths=t, force=True) mock_session.delete.assert_has_calls([ mock.call(self.BASE + '/foo/c2588045-d6fb-4f37-9f46-9451f653fb6a'), mock.call(self.BASE + '/foo/ec1afeaa-8930-43b0-a60a-939f23a50724') ]) t = ['*', 'c2588045-d6fb-4f37-9f46-9451f653fb6a'] self.mgr.get('rm')(paths=t, force=True) mock_session.delete.assert_has_calls([ mock.call(self.BASE + '/foo/c2588045-d6fb-4f37-9f46-9451f653fb6a'), mock.call(self.BASE + '/foo/ec1afeaa-8930-43b0-a60a-939f23a50724') ]) t = ['default:*', 'c2588045-d6fb-4f37-9f46-9451f653fb6a'] self.mgr.get('rm')(paths=t, force=True) mock_session.delete.assert_has_calls([ mock.call(self.BASE + '/foo/c2588045-d6fb-4f37-9f46-9451f653fb6a'), mock.call(self.BASE + '/foo/ec1afeaa-8930-43b0-a60a-939f23a50724') ])
def __call__(self, paths=None, nova_api_version=None): self.nclient = nclient.Client(nova_api_version, session=Context().session) self.actions = { BackRefsExists: { # we don't want to delete virtual-router objects ('virtual-machine', 'virtual-router'): self._remove_back_ref, ('virtual-machine-interface', 'instance-ip'): self._handle_si_vm, ('virtual-network', 'virtual-machine-interface'): self._remove_vm, ('security-group', 'virtual-machine-interface'): self._remove_vm, }, ChildrenExists: { ('project', 'virtual-machine-interface'): self._remove_vm, } } resources = expand_paths(paths, predicate=lambda r: r.type == 'project') if not continue_prompt( "Do you really want to purge theses projects ? All resources will be destroyed !" ): return for project in resources: self._delete(project)
def test_notfound_fqname_ls(self, mock_session): fq_name = 'default-domain:foo' Context().shell.current_path = Path('/foo') mock_session.fqname_to_id.side_effect = client.HTTPError(http_status=404) with self.assertRaises(ResourceNotFound) as e: self.mgr.get('ls')(paths=[fq_name]) self.assertEqual("%s doesn't exists" % fq_name, str(e)) self.assertFalse(mock_session.get_json.called)
def test_resource_cat(self, mock_session, mock_highlight_json): # bind original method to mock_session mock_session.id_to_fqname = client.ContrailAPISession.id_to_fqname.__get__(mock_session) mock_session.make_url = client.ContrailAPISession.make_url.__get__(mock_session) # called by id_to_fqname def post(url, json=None): if json['uuid'] == "ec1afeaa-8930-43b0-a60a-939f23a50724": return { "type": "foo", "fq_name": [ "foo", "ec1afeaa-8930-43b0-a60a-939f23a50724" ] } if json['uuid'] == "15315402-8a21-4116-aeaa-b6a77dceb191": return { "type": "bar", "fq_name": [ "bar", "15315402-8a21-4116-aeaa-b6a77dceb191" ] } mock_session.post_json.side_effect = post mock_highlight_json.side_effect = lambda d: d mock_session.get_json.return_value = { 'foo': { 'href': self.BASE + '/foo/ec1afeaa-8930-43b0-a60a-939f23a50724', 'uuid': 'ec1afeaa-8930-43b0-a60a-939f23a50724', 'attr': None, 'fq_name': [ 'foo', 'ec1afeaa-8930-43b0-a60a-939f23a50724' ], 'bar_refs': [ { 'href': self.BASE + '/bar/15315402-8a21-4116-aeaa-b6a77dceb191', 'uuid': '15315402-8a21-4116-aeaa-b6a77dceb191', 'to': [ 'bar', '15315402-8a21-4116-aeaa-b6a77dceb191' ] } ] } } Context().shell.current_path = Path('/foo') expected_resource = Resource('foo', uuid='ec1afeaa-8930-43b0-a60a-939f23a50724', href=self.BASE + '/foo/ec1afeaa-8930-43b0-a60a-939f23a50724', attr=None, fq_name='foo:ec1afeaa-8930-43b0-a60a-939f23a50724') expected_resource['bar_refs'] = [ Resource('bar', uuid='15315402-8a21-4116-aeaa-b6a77dceb191', href=self.BASE + '/bar/15315402-8a21-4116-aeaa-b6a77dceb191', to=['bar', '15315402-8a21-4116-aeaa-b6a77dceb191']) ] result = self.mgr.get('cat')(paths=['ec1afeaa-8930-43b0-a60a-939f23a50724']) self.assertEqual(expected_resource.json(), result)
def test_rm(self, mock_continue_prompt, mock_session): mock_session.configure_mock(base_url=self.BASE) Context().shell.current_path = Path('/') t = ['foo/6b6a7f47-807e-4c39-8ac6-3adcf2f5498f'] mock_session.delete.return_value = True self.mgr.get('rm')(paths=t, force=True) mock_session.delete.assert_has_calls([ mock.call(self.BASE + '/foo/6b6a7f47-807e-4c39-8ac6-3adcf2f5498f') ]) self.assertFalse(mock_continue_prompt.called)
def test_resource_ls(self, mock_session): mock_session.get_json.return_value = { 'foo': { 'href': self.BASE + '/foo/ec1afeaa-8930-43b0-a60a-939f23a50724', 'uuid': 'ec1afeaa-8930-43b0-a60a-939f23a50724', } } Context().shell.current_path = Path('/foo') expected_result = 'ec1afeaa-8930-43b0-a60a-939f23a50724' result = self.mgr.get('ls')(paths=['ec1afeaa-8930-43b0-a60a-939f23a50724']) self.assertEqual(result, expected_result)
def test_rm_multiple_resources(self, mock_session): mock_session.configure_mock(base_url=self.BASE) Context().shell.current_path = Path('/foo') ts = ['6b6a7f47-807e-4c39-8ac6-3adcf2f5498f', '22916187-5b6f-40f1-b7b6-fc6fe9f23bce'] mock_session.delete.return_value = True self.mgr.get('rm')(paths=ts, force=True) mock_session.delete.assert_has_calls([ mock.call(self.BASE + '/foo/22916187-5b6f-40f1-b7b6-fc6fe9f23bce'), mock.call(self.BASE + '/foo/6b6a7f47-807e-4c39-8ac6-3adcf2f5498f') ])
def __call__(self, **kwargs): super(CleanSubnet, self).__call__(**kwargs) session = Context().session vn_back_refs = self._get_vn_back_refs() kv = self._get_kv_store(session) healthy_kv = self._get_healthy_kv(vn_back_refs) stale_subnets = self._get_stale_subnets(kv, healthy_kv) if self.check: return self._clean_kv(session, stale_subnets, self.dry_run) return "Clean stale subnets ended"
def test_fq_name_completion(self, mock_session): mock_document = Document(text='cat bar/default-dom') comp = ShellCompleter() r1 = Resource('bar', fq_name='default-domain:project:resource') r2 = Resource('bar', fq_name='foo:foo:foo') completions = list(comp.get_completions(mock_document, None)) self.assertEqual(len(completions), 1) self.assertTrue(str(r1.path.relative_to(Context().shell.current_path)) in [c.text for c in completions]) r1.delete() r2.delete() completions = comp.get_completions(mock_document, None) self.assertEqual(len(list(completions)), 0)
def __call__(self, subnet_uuid=None, **kwargs): super(FixSubnets, self).__call__(**kwargs) self.session = Context().session self.subnet_uuid = subnet_uuid ipam = Resource( 'network-ipam', fq_name='default-domain:default-project:default-network-ipam', fetch=True) to_check = [(vn.uuid, subnet.get('subnet_uuid'), subnet) for vn in ipam.back_refs.virtual_network for subnet in vn.get('attr', {}).get('ipam_subnets', [])] to_fix = parallel_map(self.chk, to_check, workers=50) if not self.dry_run and not self.check: parallel_map(self.fix, to_fix, workers=50)
def __call__(self): self.kclient = kclient.Client(session=Context().session) parallel_map(self._check, Collection('project', fetch=True), workers=50)
def setUp(self): Context().schema = schema.create_schema_from_version('2.21')
def test_rm_noconfirm(self, mock_continue_prompt, mock_session): Context().shell.current_path = Path('/') mock_continue_prompt.return_value = False t = ['foo/6b6a7f47-807e-4c39-8ac6-3adcf2f5498f'] self.mgr.get('rm')(paths=t) self.assertFalse(mock_session.delete.called)
def tearDown(self): Context().schema = None
def test_rm_recursive(self, mock_continue_prompt, mock_session): Context().shell.current_path = Path('/') Collection('bar') Collection('foobar') t = ['foo/6b6a7f47-807e-4c39-8ac6-3adcf2f5498f'] mock_continue_prompt.return_value = True mock_session.configure_mock(base_url=self.BASE) mock_session.get_json.side_effect = [{ 'foo': { 'href': self.BASE + '/foo/6b6a7f47-807e-4c39-8ac6-3adcf2f5498f', 'uuid': '6b6a7f47-807e-4c39-8ac6-3adcf2f5498f', 'bar_back_refs': [{ 'href': self.BASE + '/bar/22916187-5b6f-40f1-b7b6-fc6fe9f23bce', 'uuid': '22916187-5b6f-40f1-b7b6-fc6fe9f23bce', 'to': ['bar', '22916187-5b6f-40f1-b7b6-fc6fe9f23bce'] }, { 'href': self.BASE + '/bar/776bdf88-6283-4c4b-9392-93a857807307', 'uuid': '776bdf88-6283-4c4b-9392-93a857807307', 'to': ['bar', '776bdf88-6283-4c4b-9392-93a857807307'] }] } }, { 'bar': { 'href': self.BASE + '/bar/22916187-5b6f-40f1-b7b6-fc6fe9f23bce', 'uuid': '22916187-5b6f-40f1-b7b6-fc6fe9f23bce', 'foobar_back_refs': [{ 'href': self.BASE + '/foobar/1050223f-a230-4ed6-96f1-c332700c5e01', 'to': ['foobar', '1050223f-a230-4ed6-96f1-c332700c5e01'], 'uuid': '1050223f-a230-4ed6-96f1-c332700c5e01' }] } }, { 'foobar': { 'href': self.BASE + '/foobar/1050223f-a230-4ed6-96f1-c332700c5e01', 'uuid': '1050223f-a230-4ed6-96f1-c332700c5e01' } }, { 'bar': { 'href': self.BASE + '/bar/776bdf88-6283-4c4b-9392-93a857807307', 'uuid': '776bdf88-6283-4c4b-9392-93a857807307' } }] mock_session.delete.return_value = True self.mgr.get('rm')(paths=t, recursive=True) expected_calls = [ mock.call.delete(self.BASE + '/bar/776bdf88-6283-4c4b-9392-93a857807307'), mock.call.delete(self.BASE + '/foobar/1050223f-a230-4ed6-96f1-c332700c5e01'), mock.call.delete(self.BASE + '/bar/22916187-5b6f-40f1-b7b6-fc6fe9f23bce'), mock.call.delete(self.BASE + '/foo/6b6a7f47-807e-4c39-8ac6-3adcf2f5498f') ] mock_session.delete.assert_has_calls(expected_calls)