Ejemplo n.º 1
0
 def _test_update_resource_action(self,
                                  resource,
                                  cmd,
                                  myid,
                                  action,
                                  args,
                                  body,
                                  retval=None):
     self.mox.StubOutWithMock(cmd, "get_client")
     self.mox.StubOutWithMock(self.client.httpclient, "request")
     cmd.get_client().MultipleTimes().AndReturn(self.client)
     path = getattr(self.client, resource + "_path")
     path_action = '%s/%s' % (myid, action)
     self.client.httpclient.request(end_url(path % path_action,
                                            format=self.format),
                                    'PUT',
                                    body=MyComparator(body, self.client),
                                    headers=mox.ContainsKeyValue(
                                        'X-Auth-Token', TOKEN)).AndReturn(
                                            (MyResp(204), retval))
     args.extend(['--request-format', self.format])
     self.mox.ReplayAll()
     cmd_parser = cmd.get_parser("delete_" + resource)
     shell.run_command(cmd, cmd_parser, args)
     self.mox.VerifyAll()
     self.mox.UnsetStubs()
     _str = self.fake_stdout.make_string()
     self.assertIn(myid, _str)
Ejemplo n.º 2
0
 def _test_show_resource(self, resource, cmd, myid, args, fields=[]):
     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 = {
         resource: {
             self.id_field: myid,
             'name': 'myname',
         },
     }
     self.client.format = self.format
     resstr = self.client.serialize(expected_res)
     path = getattr(self.client, resource + "_path")
     self.client.httpclient.request(end_url(path % myid,
                                            query,
                                            format=self.format),
                                    'GET',
                                    body=None,
                                    headers=mox.ContainsKeyValue(
                                        'X-Auth-Token', TOKEN)).AndReturn(
                                            (MyResp(200), resstr))
     args.extend(['--request-format', self.format])
     self.mox.ReplayAll()
     cmd_parser = cmd.get_parser("show_" + 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)
Ejemplo n.º 3
0
 def _test_update_resource(self, resource, cmd, myid, args, extrafields):
     self.mox.StubOutWithMock(cmd, "get_client")
     self.mox.StubOutWithMock(self.client.httpclient, "request")
     cmd.get_client().MultipleTimes().AndReturn(self.client)
     body = {resource: extrafields}
     path = getattr(self.client, resource + "_path")
     self.client.format = self.format
     # Work around for LP #1217791. XML deserializer called from
     # MyComparator does not decodes XML string correctly.
     if self.format == 'json':
         mox_body = MyComparator(body, self.client)
     else:
         mox_body = self.client.serialize(body)
     self.client.httpclient.request(
         MyUrlComparator(end_url(path % myid, format=self.format),
                         self.client),
         'PUT',
         body=mox_body,
         headers=mox.ContainsKeyValue(
             'X-Auth-Token', TOKEN)).AndReturn((MyResp(204), None))
     args.extend(['--request-format', self.format])
     self.mox.ReplayAll()
     cmd_parser = cmd.get_parser("update_" + resource)
     shell.run_command(cmd, cmd_parser, args)
     self.mox.VerifyAll()
     self.mox.UnsetStubs()
     _str = self.fake_stdout.make_string()
     self.assertIn(myid, _str)
Ejemplo n.º 4
0
 def _test_list_resources_with_pagination(self, resources, cmd):
     self.mox.StubOutWithMock(cmd, "get_client")
     self.mox.StubOutWithMock(self.client.httpclient, "request")
     cmd.get_client().MultipleTimes().AndReturn(self.client)
     path = getattr(self.client, resources + "_path")
     fake_query = "marker=myid2&limit=2"
     reses1 = {resources: [{'id': 'myid1', },
                           {'id': 'myid2', }],
               '%s_links' % resources: [{'href': end_url(path, fake_query),
                                         'rel': 'next'}]}
     reses2 = {resources: [{'id': 'myid3', },
                           {'id': 'myid4', }]}
     self.client.format = self.format
     resstr1 = self.client.serialize(reses1)
     resstr2 = self.client.serialize(reses2)
     self.client.httpclient.request(
         end_url(path, "", format=self.format), 'GET',
         body=None,
         headers=mox.ContainsKeyValue(
             'X-Auth-Token', TOKEN)).AndReturn((MyResp(200), resstr1))
     self.client.httpclient.request(
         end_url(path, fake_query, format=self.format), 'GET',
         body=None,
         headers=mox.ContainsKeyValue(
             'X-Auth-Token', TOKEN)).AndReturn((MyResp(200), resstr2))
     self.mox.ReplayAll()
     cmd_parser = cmd.get_parser("list_" + resources)
     args = ['--request-format', self.format]
     shell.run_command(cmd, cmd_parser, args)
     self.mox.VerifyAll()
     self.mox.UnsetStubs()
Ejemplo n.º 5
0
 def _test_show_resource(self, resource, cmd, myid, args, fields=[]):
     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 = {resource:
                     {self.id_field: myid,
                      'name': 'myname', }, }
     self.client.format = self.format
     resstr = self.client.serialize(expected_res)
     path = getattr(self.client, resource + "_path")
     self.client.httpclient.request(
         end_url(path % myid, query, format=self.format), 'GET',
         body=None,
         headers=mox.ContainsKeyValue(
             'X-Auth-Token', TOKEN)).AndReturn((MyResp(200), resstr))
     args.extend(['--request-format', self.format])
     self.mox.ReplayAll()
     cmd_parser = cmd.get_parser("show_" + 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)
Ejemplo n.º 6
0
 def _test_delete_resource(self, resource, cmd, myid, args, mock_get):
     deleted_msg = {'vnf': 'delete initiated'}
     mock_get.return_value = self.client
     path = getattr(self.client, resource + "_path")
     with mock.patch.object(self.client.httpclient, 'request') as mock_req:
         mock_req.return_value = (MyResp(204), None)
         args.extend(['--request-format', self.format])
         cmd_parser = cmd.get_parser("delete_" + resource)
         shell.run_command(cmd, cmd_parser, args)
         if '--force' in args:
             body_str = '{"' + resource + \
                        '": {"attributes": {"force": true}}}'
             mock_req.assert_called_once_with(
                 end_url(path % myid, format=self.format),
                 'DELETE',
                 body=body_str,
                 headers=test_utils.ContainsKeyValue('X-Auth-Token', TOKEN))
         else:
             mock_req.assert_called_once_with(
                 end_url(path % myid, format=self.format),
                 'DELETE',
                 body=None,
                 headers=test_utils.ContainsKeyValue('X-Auth-Token', TOKEN))
     mock_get.assert_called_once_with()
     _str = self.fake_stdout.make_string()
     msg = 'All specified %(resource)s(s) %(msg)s successfully\n' % {
         'msg': deleted_msg.get(resource, 'deleted'),
         'resource': resource
     }
     self.assertEqual(msg, _str)
Ejemplo n.º 7
0
 def _test_show_resource(self, resource, cmd, myid, args, fields=[]):
     with mock.patch.object(cmd, 'get_client') as mock_get:
         mock_get.return_value = self.client
         query = "&".join(["fields=%s" % field for field in fields])
         expected_res = {
             resource: {
                 self.id_field: myid,
                 'name': 'myname',
             },
         }
         self.client.format = self.format
         resstr = self.client.serialize(expected_res)
         path = getattr(self.client, resource + "_path")
         with mock.patch.object(self.client.httpclient, 'request') as\
                 mock_req:
             mock_req.return_value = (MyResp(200), resstr)
             args.extend(['--request-format', self.format])
             cmd_parser = cmd.get_parser("show_" + resource)
             shell.run_command(cmd, cmd_parser, args)
             mock_req.assert_called_once_with(
                 end_url(path % myid, query, format=self.format),
                 'GET',
                 body=None,
                 headers=test_utils.ContainsKeyValue('X-Auth-Token', TOKEN))
         _str = self.fake_stdout.make_string()
         mock_get.assert_called_once_with()
         self.assertIn(myid, _str)
         self.assertIn('myname', _str)
Ejemplo n.º 8
0
 def _test_update_resource(self,
                           resource,
                           cmd,
                           myid,
                           args,
                           extrafields,
                           mock_get,
                           get_client_called_count=1):
     mock_get.return_value = self.client
     body = {resource: extrafields}
     path = getattr(self.client, resource + "_path")
     self.client.format = self.format
     # Work around for LP #1217791. XML deserializer called from
     # MyComparator does not decodes XML string correctly.
     if self.format == 'json':
         _body = MyComparator(body, self.client)
     else:
         _body = self.client.serialize(body)
     with mock.patch.object(self.client.httpclient, 'request') as mock_req:
         comparator = MyUrlComparator(
             end_url(path % myid, format=self.format), self.client)
         mock_req.return_value = (MyResp(204), None)
         args.extend(['--request-format', self.format])
         cmd_parser = cmd.get_parser("update_" + resource)
         shell.run_command(cmd, cmd_parser, args)
         mock_req.assert_called_once_with(
             comparator,
             'PUT',
             body=_body,
             headers=test_utils.ContainsKeyValue('X-Auth-Token', TOKEN))
     self.assertEqual(get_client_called_count, mock_get.call_count)
     _str = self.fake_stdout.make_string()
     self.assertIn(myid, _str)
Ejemplo n.º 9
0
 def _test_update_resource(self, resource, cmd, myid, args, extrafields):
     self.mox.StubOutWithMock(cmd, "get_client")
     self.mox.StubOutWithMock(self.client.httpclient, "request")
     cmd.get_client().MultipleTimes().AndReturn(self.client)
     body = {resource: extrafields}
     path = getattr(self.client, resource + "_path")
     self.client.format = self.format
     # Work around for LP #1217791. XML deserializer called from
     # MyComparator does not decodes XML string correctly.
     if self.format == 'json':
         mox_body = MyComparator(body, self.client)
     else:
         mox_body = self.client.serialize(body)
     self.client.httpclient.request(
         MyUrlComparator(end_url(path % myid, format=self.format),
                         self.client),
         'PUT',
         body=mox_body,
         headers=mox.ContainsKeyValue('X-Auth-Token', TOKEN)).AndReturn(
             (MyResp(204), None))
     args.extend(['--request-format', self.format])
     self.mox.ReplayAll()
     cmd_parser = cmd.get_parser("update_" + resource)
     shell.run_command(cmd, cmd_parser, args)
     self.mox.VerifyAll()
     self.mox.UnsetStubs()
     _str = self.fake_stdout.make_string()
     self.assertIn(myid, _str)
Ejemplo n.º 10
0
 def _test_list_sub_resources_with_pagination(self, resources, api_resource,
                                              cmd, myid):
     self.mox.StubOutWithMock(cmd, "get_client")
     self.mox.StubOutWithMock(self.client.httpclient, "request")
     cmd.get_client().MultipleTimes().AndReturn(self.client)
     path = getattr(self.client, resources + "_path")
     fake_query = "marker=myid2&limit=2"
     reses1 = {
         api_resource: [{
             'id': 'myid1',
         }, {
             'id': 'myid2',
         }],
         '%s_links' % api_resource: [{
             'href':
             end_url(path % myid, fake_query),
             'rel':
             'next'
         }]
     }
     reses2 = {
         api_resource: [{
             'id': 'myid3',
         }, {
             'id': 'myid4',
         }]
     }
     self.client.format = self.format
     resstr1 = self.client.serialize(reses1)
     resstr2 = self.client.serialize(reses2)
     self.client.httpclient.request(end_url(path % myid,
                                            "",
                                            format=self.format),
                                    'GET',
                                    body=None,
                                    headers=mox.ContainsKeyValue(
                                        'X-Auth-Token', TOKEN)).AndReturn(
                                            (MyResp(200), resstr1))
     self.client.httpclient.request(MyUrlComparator(
         end_url(path % myid, fake_query, format=self.format), self.client),
                                    'GET',
                                    body=None,
                                    headers=mox.ContainsKeyValue(
                                        'X-Auth-Token', TOKEN)).AndReturn(
                                            (MyResp(200), resstr2))
     self.mox.ReplayAll()
     cmd_parser = cmd.get_parser("list_" + resources)
     args = [myid, '--request-format', self.format]
     shell.run_command(cmd, cmd_parser, args)
     self.mox.VerifyAll()
     self.mox.UnsetStubs()
Ejemplo n.º 11
0
    def _test_create_resource(self, resource, cmd, name, myid, args,
                              position_names, position_values, mock_get,
                              tenant_id=None, tags=None, admin_state_up=True,
                              extra_body=None, **kwargs):
        mock_get.return_value = self.client
        non_admin_status_resources = ['vnfd', 'vnf']
        if (resource in non_admin_status_resources):
            body = {resource: {}, }
        else:
            body = {resource: {'admin_state_up': admin_state_up, }, }
        if tenant_id:
            body[resource].update({'tenant_id': tenant_id})
        if tags:
            body[resource].update({'tags': tags})
        if extra_body:
            body[resource].update(extra_body)
        body[resource].update(kwargs)

        for i in range(len(position_names)):
            body[resource].update({position_names[i]: position_values[i]})
        ress = {resource:
                {self.id_field: myid}, }
        if name:
            ress[resource].update({'name': name})
        self.client.format = self.format
        resstr = self.client.serialize(ress)
        # url method body
        resource_plural = tackerV1_0._get_resource_plural(resource,
                                                          self.client)
        path = getattr(self.client, resource_plural + "_path")
        # Work around for LP #1217791. XML deserializer called from
        # MyComparator does not decodes XML string correctly.
        if self.format == 'json':
            _body = test_cli10.MyComparator(body, self.client)
        else:
            _body = self.client.serialize(body)
        with mock.patch.object(self.client.httpclient, 'request') as mock_req:
            mock_req.return_value = (test_cli10.MyResp(200), resstr)
            args.extend(['--request-format', self.format])
            args.extend(['--vnfd-id', 'vnfd'])
            cmd_parser = cmd.get_parser('create_' + resource)
            shell.run_command(cmd, cmd_parser, args)
            mock_req.assert_called_once_with(
                test_cli10.end_url(path, format=self.format), 'POST',
                body=_body,
                headers=test_utils.ContainsKeyValue('X-Auth-Token', TOKEN))
        mock_get.assert_any_call()
Ejemplo n.º 12
0
 def _test_delete_resource(self, resource, cmd, myid, args):
     self.mox.StubOutWithMock(cmd, "get_client")
     self.mox.StubOutWithMock(self.client.httpclient, "request")
     cmd.get_client().MultipleTimes().AndReturn(self.client)
     path = getattr(self.client, resource + "_path")
     self.client.httpclient.request(
         end_url(path % myid, format=self.format), 'DELETE',
         body=None,
         headers=mox.ContainsKeyValue(
             'X-Auth-Token', TOKEN)).AndReturn((MyResp(204), None))
     args.extend(['--request-format', self.format])
     self.mox.ReplayAll()
     cmd_parser = cmd.get_parser("delete_" + resource)
     shell.run_command(cmd, cmd_parser, args)
     self.mox.VerifyAll()
     self.mox.UnsetStubs()
     _str = self.fake_stdout.make_string()
     self.assertIn(myid, _str)
Ejemplo n.º 13
0
    def _test_list_columns(self, cmd, resources_collection,
                           resources_out, args=['-f', 'json']):
        self.mox.StubOutWithMock(cmd, "get_client")
        self.mox.StubOutWithMock(self.client.httpclient, "request")
        cmd.get_client().MultipleTimes().AndReturn(self.client)
        self.client.format = self.format
        resstr = self.client.serialize(resources_out)

        path = getattr(self.client, resources_collection + "_path")
        self.client.httpclient.request(
            end_url(path, format=self.format), 'GET',
            body=None,
            headers=mox.ContainsKeyValue(
                'X-Auth-Token', TOKEN)).AndReturn((MyResp(200), resstr))
        args.extend(['--request-format', self.format])
        self.mox.ReplayAll()
        cmd_parser = cmd.get_parser("list_" + resources_collection)
        shell.run_command(cmd, cmd_parser, args)
        self.mox.VerifyAll()
        self.mox.UnsetStubs()
Ejemplo n.º 14
0
    def _test_list_columns(self,
                           cmd,
                           resources_collection,
                           resources_out,
                           mock_get,
                           args=['-f', 'json']):
        mock_get.return_value = self.client
        self.client.format = self.format
        resstr = self.client.serialize(resources_out)

        path = getattr(self.client, resources_collection + "_path")
        with mock.patch.object(self.client.httpclient, 'request') as mock_req:
            mock_req.return_value = (MyResp(200), resstr)
            args.extend(['--request-format', self.format])
            cmd_parser = cmd.get_parser("list_" + resources_collection)
            shell.run_command(cmd, cmd_parser, args)
            mock_req.assert_called_once_with(
                end_url(path, format=self.format),
                'GET',
                body=None,
                headers=test_utils.ContainsKeyValue('X-Auth-Token', TOKEN))
        mock_get.assert_called_once_with()
Ejemplo n.º 15
0
 def _test_update_resource_action(self,
                                  resource,
                                  cmd,
                                  myid,
                                  action,
                                  args,
                                  body,
                                  mock_get,
                                  retval=None):
     mock_get.return_value = self.client
     path = getattr(self.client, resource + "_path")
     path_action = '%s/%s' % (myid, action)
     with mock.patch.object(self.client.httpclient, 'request') as mock_req:
         mock_req.return_value = (MyResp(204), retval)
         args.extend(['--request-format', self.format])
         cmd_parser = cmd.get_parser("delete_" + resource)
         shell.run_command(cmd, cmd_parser, args)
         mock_req.assert_called_once_with(
             end_url(path % path_action, format=self.format),
             'PUT',
             body=MyComparator(body, self.client),
             headers=test_utils.ContainsKeyValue('X-Auth-Token', TOKEN))
     _str = self.fake_stdout.make_string()
     self.assertIn(myid, _str)
Ejemplo n.º 16
0
    def _test_list_columns(self,
                           cmd,
                           resources_collection,
                           resources_out,
                           args=['-f', 'json']):
        self.mox.StubOutWithMock(cmd, "get_client")
        self.mox.StubOutWithMock(self.client.httpclient, "request")
        cmd.get_client().MultipleTimes().AndReturn(self.client)
        self.client.format = self.format
        resstr = self.client.serialize(resources_out)

        path = getattr(self.client, resources_collection + "_path")
        self.client.httpclient.request(end_url(path, format=self.format),
                                       'GET',
                                       body=None,
                                       headers=mox.ContainsKeyValue(
                                           'X-Auth-Token', TOKEN)).AndReturn(
                                               (MyResp(200), resstr))
        args.extend(['--request-format', self.format])
        self.mox.ReplayAll()
        cmd_parser = cmd.get_parser("list_" + resources_collection)
        shell.run_command(cmd, cmd_parser, args)
        self.mox.VerifyAll()
        self.mox.UnsetStubs()
Ejemplo n.º 17
0
    def _test_create_resource(self,
                              resource,
                              cmd,
                              name,
                              myid,
                              args,
                              position_names,
                              position_values,
                              tenant_id=None,
                              tags=None,
                              admin_state_up=True,
                              extra_body=None,
                              **kwargs):
        self.mox.StubOutWithMock(cmd, "get_client")
        self.mox.StubOutWithMock(self.client.httpclient, "request")
        cmd.get_client().MultipleTimes().AndReturn(self.client)
        non_admin_status_resources = ['vnfd', 'vnf', 'vim', 'vnffgd', 'vnffg']
        if (resource in non_admin_status_resources):
            body = {
                resource: {},
            }
        else:
            body = {
                resource: {
                    'admin_state_up': admin_state_up,
                },
            }
        if tenant_id:
            body[resource].update({'tenant_id': tenant_id})
        if tags:
            body[resource].update({'tags': tags})
        if extra_body:
            body[resource].update(extra_body)
        body[resource].update(kwargs)

        for i in range(len(position_names)):
            body[resource].update({position_names[i]: position_values[i]})
        ress = {
            resource: {
                self.id_field: myid
            },
        }
        if name:
            ress[resource].update({'name': name})
        self.client.format = self.format
        resstr = self.client.serialize(ress)
        # url method body
        resource_plural = tackerV1_0._get_resource_plural(
            resource, self.client)
        path = getattr(self.client, resource_plural + "_path")
        # Work around for LP #1217791. XML deserializer called from
        # MyComparator does not decodes XML string correctly.
        if self.format == 'json':
            mox_body = MyComparator(body, self.client)
        else:
            mox_body = self.client.serialize(body)
        self.client.httpclient.request(end_url(path, format=self.format),
                                       'POST',
                                       body=mox_body,
                                       headers=mox.ContainsKeyValue(
                                           'X-Auth-Token', TOKEN)).AndReturn(
                                               (MyResp(200), resstr))
        args.extend(['--request-format', self.format])
        self.mox.ReplayAll()
        cmd_parser = cmd.get_parser('create_' + resource)
        shell.run_command(cmd, cmd_parser, args)
        self.mox.VerifyAll()
        self.mox.UnsetStubs()
        _str = self.fake_stdout.make_string()
        self.assertIn(myid, _str)
        if name:
            self.assertIn(name, _str)
Ejemplo n.º 18
0
    def _test_list_resources(self, resources, cmd, detail=False, tags=[],
                             fields_1=[], fields_2=[], page_size=None,
                             sort_key=[], sort_dir=[], response_contents=None,
                             base_args=None, path=None):
        self.mox.StubOutWithMock(cmd, "get_client")
        self.mox.StubOutWithMock(self.client.httpclient, "request")
        cmd.get_client().MultipleTimes().AndReturn(self.client)
        if response_contents is None:
            contents = [{self.id_field: 'myid1', },
                        {self.id_field: 'myid2', }, ]
        else:
            contents = response_contents
        reses = {resources: contents}
        self.client.format = self.format
        resstr = self.client.serialize(reses)
        # url method body
        query = ""
        args = base_args if base_args is not None else []
        if detail:
            args.append('-D')
        args.extend(['--request-format', self.format])
        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 isinstance(tag, unicode):
                tag = urllib.quote(tag.encode('utf-8'))
            if query:
                query += "&tag=" + tag
            else:
                query = "tag=" + tag
        if (not tags) and fields_2:
            args.append('--')
        if fields_2:
            args.append("--fields")
            for field in fields_2:
                args.append(field)
        if detail:
            query = query and query + '&verbose=True' or 'verbose=True'
        fields_1.extend(fields_2)
        for field in fields_1:
            if query:
                query += "&fields=" + field
            else:
                query = "fields=" + field
        if page_size:
            args.append("--page-size")
            args.append(str(page_size))
            if query:
                query += "&limit=%s" % page_size
            else:
                query = "limit=%s" % page_size
        if sort_key:
            for key in sort_key:
                args.append('--sort-key')
                args.append(key)
                if query:
                    query += '&'
                query += 'sort_key=%s' % key
        if sort_dir:
            len_diff = len(sort_key) - len(sort_dir)
            if len_diff > 0:
                sort_dir += ['asc'] * len_diff
            elif len_diff < 0:
                sort_dir = sort_dir[:len(sort_key)]
            for dir in sort_dir:
                args.append('--sort-dir')
                args.append(dir)
                if query:
                    query += '&'
                query += 'sort_dir=%s' % dir
        if path is None:
            path = getattr(self.client, resources + "_path")
        self.client.httpclient.request(
            MyUrlComparator(end_url(path, query, format=self.format),
                            self.client),
            'GET',
            body=None,
            headers=mox.ContainsKeyValue(
                'X-Auth-Token', TOKEN)).AndReturn((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()
        if response_contents is None:
            self.assertIn('myid1', _str)
        return _str
Ejemplo n.º 19
0
    def _test_create_resource(self, resource, cmd,
                              name, myid, args,
                              position_names, position_values, tenant_id=None,
                              tags=None, admin_state_up=True, extra_body=None,
                              **kwargs):
        self.mox.StubOutWithMock(cmd, "get_client")
        self.mox.StubOutWithMock(self.client.httpclient, "request")
        cmd.get_client().MultipleTimes().AndReturn(self.client)
        non_admin_status_resources = ['subnet', 'floatingip', 'security_group',
                                      'security_group_rule', 'qos_queue',
                                      'network_gateway', 'gateway_device',
                                      'credential', 'network_profile',
                                      'policy_profile', 'ikepolicy',
                                      'ipsecpolicy', 'metering_label',
                                      'metering_label_rule', 'net_partition',
                                      'device_template', 'device',
                                      'service_instance']
        if (resource in non_admin_status_resources):
            body = {resource: {}, }
        else:
            body = {resource: {'admin_state_up': admin_state_up, }, }
        if tenant_id:
            body[resource].update({'tenant_id': tenant_id})
        if tags:
            body[resource].update({'tags': tags})
        if extra_body:
            body[resource].update(extra_body)
        body[resource].update(kwargs)

        for i in range(len(position_names)):
            body[resource].update({position_names[i]: position_values[i]})
        ress = {resource:
                {self.id_field: myid}, }
        if name:
            ress[resource].update({'name': name})
        self.client.format = self.format
        resstr = self.client.serialize(ress)
        # url method body
        resource_plural = tackerV1_0._get_resource_plural(resource,
                                                          self.client)
        path = getattr(self.client, resource_plural + "_path")
        # Work around for LP #1217791. XML deserializer called from
        # MyComparator does not decodes XML string correctly.
        if self.format == 'json':
            mox_body = MyComparator(body, self.client)
        else:
            mox_body = self.client.serialize(body)
        self.client.httpclient.request(
            end_url(path, format=self.format), 'POST',
            body=mox_body,
            headers=mox.ContainsKeyValue(
                'X-Auth-Token', TOKEN)).AndReturn((MyResp(200), resstr))
        args.extend(['--request-format', self.format])
        self.mox.ReplayAll()
        cmd_parser = cmd.get_parser('create_' + resource)
        shell.run_command(cmd, cmd_parser, args)
        self.mox.VerifyAll()
        self.mox.UnsetStubs()
        _str = self.fake_stdout.make_string()
        self.assertIn(myid, _str)
        if name:
            self.assertIn(name, _str)
Ejemplo n.º 20
0
    def _test_list_sub_resources(self,
                                 resources,
                                 api_resource,
                                 cmd,
                                 myid,
                                 detail=False,
                                 tags=[],
                                 fields_1=[],
                                 fields_2=[],
                                 page_size=None,
                                 sort_key=[],
                                 sort_dir=[],
                                 response_contents=None,
                                 base_args=None,
                                 path=None):
        self.mox.StubOutWithMock(cmd, "get_client")
        self.mox.StubOutWithMock(self.client.httpclient, "request")
        cmd.get_client().MultipleTimes().AndReturn(self.client)
        if response_contents is None:
            contents = [
                {
                    self.id_field: 'myid1',
                },
                {
                    self.id_field: 'myid2',
                },
            ]
        else:
            contents = response_contents
        reses = {api_resource: contents}
        self.client.format = self.format
        resstr = self.client.serialize(reses)
        # url method body
        query = ""
        args = base_args if base_args is not None else []
        if detail:
            args.append('-D')
        args.extend(['--request-format', self.format])
        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 isinstance(tag, six.string_types):
                tag = urllib.quote(tag.encode('utf-8'))
            if query:
                query += "&tag=" + tag
            else:
                query = "tag=" + tag
        if (not tags) and fields_2:
            args.append('--')
        if fields_2:
            args.append("--fields")
            for field in fields_2:
                args.append(field)
        if detail:
            query = query and query + '&verbose=True' or 'verbose=True'
        fields_1.extend(fields_2)
        for field in fields_1:
            if query:
                query += "&fields=" + field
            else:
                query = "fields=" + field
        if page_size:
            args.append("--page-size")
            args.append(str(page_size))
            if query:
                query += "&limit=%s" % page_size
            else:
                query = "limit=%s" % page_size
        if sort_key:
            for key in sort_key:
                args.append('--sort-key')
                args.append(key)
                if query:
                    query += '&'
                query += 'sort_key=%s' % key
        if sort_dir:
            len_diff = len(sort_key) - len(sort_dir)
            if len_diff > 0:
                sort_dir += ['asc'] * len_diff
            elif len_diff < 0:
                sort_dir = sort_dir[:len(sort_key)]
            for dir in sort_dir:
                args.append('--sort-dir')
                args.append(dir)
                if query:
                    query += '&'
                query += 'sort_dir=%s' % dir
        if path is None:
            path = getattr(self.client, resources + "_path")
        self.client.httpclient.request(
            MyUrlComparator(end_url(path % myid, query, format=self.format),
                            self.client),
            'GET',
            body=None,
            headers=mox.ContainsKeyValue('X-Auth-Token', TOKEN)).AndReturn(
                (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()
        if response_contents is None:
            self.assertIn('myid1', _str)
        return _str
Ejemplo n.º 21
0
    def _test_list_resources(self,
                             resources,
                             cmd,
                             detail=False,
                             tags=[],
                             fields_1=[],
                             fields_2=[],
                             page_size=None,
                             sort_key=[],
                             sort_dir=[],
                             response_contents=None,
                             base_args=None,
                             path=None,
                             template_source=None):
        if response_contents is None:
            contents = [
                {
                    self.id_field: 'myid1',
                },
                {
                    self.id_field: 'myid2',
                },
            ]
        else:
            contents = response_contents
        reses = {resources: contents}
        self.client.format = self.format
        resstr = self.client.serialize(reses)
        # url method body
        query = ""
        args = base_args if base_args is not None else []
        if detail:
            args.append('-D')
        args.extend(['--request-format', self.format])
        if fields_1:
            for field in fields_1:
                args.append('--fields')
                args.append(field)
        if template_source is not None:
            args.append("--template-source")
            args.append(template_source)
            query += 'template_source=' + template_source

        if tags:
            args.append('--')
            args.append("--tag")
        for tag in tags:
            args.append(tag)
            if isinstance(tag, six.string_types):
                tag = urllib.quote(tag.encode('utf-8'))
            if query:
                query += "&tag=" + tag
            else:
                query = "tag=" + tag
        if (not tags) and fields_2:
            args.append('--')
        if fields_2:
            args.append("--fields")
            for field in fields_2:
                args.append(field)
        if detail:
            query = query and query + '&verbose=True' or 'verbose=True'
        fields_1.extend(fields_2)
        for field in fields_1:
            if query:
                query += "&fields=" + field
            else:
                query = "fields=" + field
        if page_size:
            args.append("--page-size")
            args.append(str(page_size))
            if query:
                query += "&limit=%s" % page_size
            else:
                query = "limit=%s" % page_size
        if sort_key:
            for key in sort_key:
                args.append('--sort-key')
                args.append(key)
                if query:
                    query += '&'
                query += 'sort_key=%s' % key
        if sort_dir:
            len_diff = len(sort_key) - len(sort_dir)
            if len_diff > 0:
                sort_dir += ['asc'] * len_diff
            elif len_diff < 0:
                sort_dir = sort_dir[:len(sort_key)]
            for dir in sort_dir:
                args.append('--sort-dir')
                args.append(dir)
                if query:
                    query += '&'
                query += 'sort_dir=%s' % dir
        if path is None:
            path = getattr(self.client, resources + "_path")
        with mock.patch.object(self.client.httpclient, 'request') as mock_req:
            mock_req.return_value = (MyResp(200), resstr)
            with mock.patch.object(TackerCommand, 'get_client') as mock_get:
                mock_get.return_value = self.client
                cmd_parser = cmd.get_parser("list_" + resources)
                shell.run_command(cmd, cmd_parser, args)
                mock_req.assert_called_once_with(
                    MyUrlComparator(end_url(path, query, format=self.format),
                                    self.client),
                    'GET',
                    body=None,
                    headers=test_utils.ContainsKeyValue('X-Auth-Token', TOKEN))
        _str = self.fake_stdout.make_string()
        if response_contents is None:
            self.assertIn('myid1', _str)
        return _str