예제 #1
0
 def test_notfound_fqname_ls(self, mock_session):
     fq_name = 'default-domain:foo'
     ShellContext.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)
예제 #2
0
 def test_root_collection(self, mock_session):
     ShellContext.current_path = Path('/')
     mock_session.get_json.return_value = {
         'href': BASE,
         'links': [
             {'link': {'href': BASE + '/instance-ips',
                       'path': Path('/instance-ips'),
                       'name': 'instance-ip',
                       'rel': 'collection'}},
             {'link': {'href': BASE + '/instance-ip',
                       'path': Path('/instance-ip'),
                       'name': 'instance-ip',
                       'rel': 'resource-base'}}
         ]
     }
     result = self.mgr.get('ls')()
     self.assertEqual('instance-ip', result)
예제 #3
0
    def test_resource_cat(self, mock_session, mock_colorize):
        # 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_colorize.side_effect = lambda d: d
        mock_session.get_json.return_value = {
            'foo': {
                'href': 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': BASE + '/bar/15315402-8a21-4116-aeaa-b6a77dceb191',
                        'uuid': '15315402-8a21-4116-aeaa-b6a77dceb191',
                        'to': [
                            'bar',
                            '15315402-8a21-4116-aeaa-b6a77dceb191'
                        ]
                    }
                ]
            }
        }
        ShellContext.current_path = Path('/foo')
        expected_resource = Resource('foo', uuid='ec1afeaa-8930-43b0-a60a-939f23a50724',
                                     href=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=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)
예제 #4
0
    def test_count(self, mock_session):
        mock_session.get_json.return_value = {
            'foos': {
                'count': 3
            }
        }

        ShellContext.current_path = Path('/foo')
        result = self.mgr.get('du')()
        self.assertEqual(result, '3')

        ShellContext.current_path = Path('/')
        result = self.mgr.get('du')(paths=['foo'])
        self.assertEqual(result, '3')

        ShellContext.current_path = Path('/foo/%s' % uuid.uuid4())
        with self.assertRaises(CommandError):
            self.mgr.get('du')()
예제 #5
0
 def test_rm(self, mock_continue_prompt, mock_session):
     mock_session.configure_mock(base_url=BASE)
     ShellContext.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(BASE + '/foo/6b6a7f47-807e-4c39-8ac6-3adcf2f5498f')])
     self.assertFalse(mock_continue_prompt.called)
예제 #6
0
 def test_rm_multiple_resources(self, mock_session):
     mock_session.configure_mock(base_url=BASE)
     ShellContext.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(BASE + '/foo/22916187-5b6f-40f1-b7b6-fc6fe9f23bce'),
         mock.call(BASE + '/foo/6b6a7f47-807e-4c39-8ac6-3adcf2f5498f')
     ])
예제 #7
0
 def test_resource_ls(self, mock_session):
     mock_session.get_json.return_value = {
         'foo': {
             'href': BASE + '/foo/ec1afeaa-8930-43b0-a60a-939f23a50724',
             'uuid': 'ec1afeaa-8930-43b0-a60a-939f23a50724',
         }
     }
     ShellContext.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)
예제 #8
0
    def test_resource_collection(self, mock_session):
        mock_session.get_json.return_value = {
            'instance-ips': [
                {'href': BASE + '/instance-ip/ec1afeaa-8930-43b0-a60a-939f23a50724',
                 'uuid': 'ec1afeaa-8930-43b0-a60a-939f23a50724'},
                {'href': BASE + '/instance-ip/c2588045-d6fb-4f37-9f46-9451f653fb6a',
                 'uuid': 'c2588045-d6fb-4f37-9f46-9451f653fb6a'}
            ]
        }

        ShellContext.current_path = Path('/instance-ip')
        result = self.mgr.get('ls')()
        self.assertEqual('\n'.join(['ec1afeaa-8930-43b0-a60a-939f23a50724',
                                    'c2588045-d6fb-4f37-9f46-9451f653fb6a']),
                         result)

        ShellContext.current_path = Path('/')
        result = self.mgr.get('ls')(paths=['instance-ip'])
        self.assertEqual('\n'.join(['instance-ip/ec1afeaa-8930-43b0-a60a-939f23a50724',
                                    'instance-ip/c2588045-d6fb-4f37-9f46-9451f653fb6a']),
                         result)
예제 #9
0
    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'], int=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'],
            int=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]))
예제 #10
0
 def test_add_same_resource(self, mock_session):
     ShellContext.current_path = Path('/')
     mock_document = mock.Mock()
     mock_document.get_word_before_cursor.return_value = 'bar'
     comp = ResourceCompleter()
     r1 = Resource('bar', uuid='4c6d3711-61f1-4505-b8df-189d32b52872')
     r2 = Resource('bar', uuid='4c6d3711-61f1-4505-b8df-189d32b52872')
     completions = comp.get_completions(mock_document, None)
     self.assertEqual(len(list(completions)), 1)
     r1.delete()
     completions = comp.get_completions(mock_document, None)
     self.assertEqual(len(list(completions)), 0)
     r2.delete()
예제 #11
0
    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)
예제 #12
0
    def test_add_del_resource(self, mock_session):
        ShellContext.current_path = Path('/')
        mock_document = mock.Mock()
        mock_document.get_word_before_cursor.return_value = 'bar'
        comp = ResourceCompleter()

        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(ShellContext.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(ShellContext.current_path)) not in
                        [c.text for c in completions])
예제 #13
0
    def test_fq_name_completion(self, mock_session):
        ShellContext.current_path = Path('/')
        mock_document = mock.Mock()
        mock_document.get_word_before_cursor.return_value = 'default-dom'

        comp = ResourceCompleter()
        r1 = Resource('bar', fq_name='default-domain:project:resource')
        r2 = Resource('foo', 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(ShellContext.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)
예제 #14
0
 def test_relative(self):
     p1 = Path()
     p2 = Path("foo/bar")
     p2_rel = p2.relative(p1)
     self.assertEqual(str(p2_rel), "foo/bar")
     self.assertFalse(p2_rel.absolute)
     p1 = Path("foo")
     p2 = Path("foo/bar")
     p2_rel = p2.relative(p1)
     self.assertEqual(str(p2_rel), "bar")
     self.assertFalse(p2_rel.absolute)
     p1 = Path("bar")
     p2 = Path("foo/bar")
     p2_rel = p2.relative(p1)
     self.assertEqual(str(p2_rel), "/foo/bar")
     self.assertTrue(p2_rel.absolute)
예제 #15
0
 def test_cd(self):
     p = Path("/")
     p = p / "foo" / "bar"
     self.assertEqual(str(p), "/foo/bar")
     p = p / ".." / "foo"
     self.assertEqual(str(p), "/foo/foo")
예제 #16
0
 def test_uuid(self):
     p = Path("/foo/%s" % str(uuid4()))
     self.assertTrue(p.is_uuid)
     p = Path("/foo/bar")
     self.assertFalse(p.is_uuid)
예제 #17
0
 def test_fq_name(self):
     p = Path("/foo/%s" % str(uuid4()))
     self.assertFalse(p.is_fq_name)
     p = Path("/foo/bar")
     self.assertTrue(p.is_fq_name)
예제 #18
0
 def _decode_paths(self, obj):
     for attr, value in obj.items():
         if attr in ('href', 'parent_href'):
             obj[attr] = Path(value[len(self.base_url):])
             obj[attr].meta["fq_name"] = ":".join(obj.get('to', obj.get('fq_name', '')))
     return obj
예제 #19
0
 def test_cd(self):
     p = Path()
     p.cd("foo")
     self.assertEqual(str(p), "/foo")
     p.cd("foo/bar")
     self.assertEqual(str(p), "/foo/foo/bar")
     p.cd("/foo/bar")
     self.assertEqual(str(p), "/foo/bar")
     p.cd("..")
     self.assertEqual(str(p), "/foo")
     p.cd(".")
     self.assertEqual(str(p), "/foo")
     p.cd()
     self.assertEqual(str(p), "/")
예제 #20
0
 def test_unicode(self):
     p = Path("/éà")
     self.assertEqual(p.base, "??")
예제 #21
0
 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)
예제 #22
0
 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)
예제 #23
0
 def test_root(self):
     p = Path("/")
     self.assertTrue(p.is_root)
     p = Path("/foo")
     self.assertFalse(p.is_root)