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)
     #parsed_args = cmd_parser.parse_args("")
     #cmd.run(parsed_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.end_url(path, query), '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.assertEquals('\n', _str)
 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.assertTrue(myid in _str)
     self.assertTrue('myname' in _str)
    def _test_list_router_port(self, resources, cmd,
                               myid, detail=False, tags=[],
                               fields_1=[], fields_2=[]):
        self.mox.StubOutWithMock(cmd, "get_client")
        self.mox.StubOutWithMock(self.client.httpclient, "request")
        cmd.get_client().MultipleTimes().AndReturn(self.client)
        reses = {resources: [{'id': 'myid1', },
                             {'id': 'myid2', }, ], }

        resstr = self.client.serialize(reses)

        # url method body
        query = ""
        args = detail and ['-D', ] or []

        if fields_1:
            for field in fields_1:
                args.append('--fields')
                args.append(field)
        args.append(myid)
        if tags:
            args.append('--')
            args.append("--tag")
        for tag in tags:
            args.append(tag)
        if (not tags) and fields_2:
            args.append('--')
        if fields_2:
            args.append("--fields")
            for field in fields_2:
                args.append(field)
        fields_1.extend(fields_2)
        for field in fields_1:
            if query:
                query += "&fields=" + field
            else:
                query = "fields=" + field

        for tag in tags:
            if query:
                query += "&tag=" + tag
            else:
                query = "tag=" + tag
        if detail:
            query = query and query + '&verbose=True' or 'verbose=True'
        query = query and query + '&device_id=%s' or 'device_id=%s'
        path = getattr(self.client, resources + "_path")
        self.client.httpclient.request(
            test_cli20.end_url(path, query % myid), '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.assertTrue('myid1' in _str)
    def _test_create_resource(self, resource, cmd,
                              name, myid, args,
                              position_names, position_values, tenant_id=None,
                              tags=None, admin_state_up=True, shared=False,
                              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']
        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 shared:
            body[resource].update({'shared': shared})
        if extra_body:
            body[resource].update(extra_body)
        body[resource].update(kwargs)

        for i in xrange(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
        path = getattr(self.client, resource + "s_path")
        self.client.httpclient.request(
            end_url(path, format=self.format), 'POST',
            body=MyComparator(body, self.client),
            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.assertTrue(myid in _str)
        if name:
            self.assertTrue(name in _str)
 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), 'DELETE',
         body=None,
         headers=ContainsKeyValue('X-Auth-Token',
                                  TOKEN)).AndReturn((MyResp(204), None))
     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.assertTrue(myid in _str)
    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)
        resstr = self.client.serialize(resources_out)

        path = getattr(self.client, resources_collection + "_path")
        self.client.httpclient.request(
            end_url(path), 'GET',
            body=None,
            headers=ContainsKeyValue('X-Auth-Token',
                                     TOKEN)).AndReturn((MyResp(200), resstr))
        self.mox.ReplayAll()
        cmd_parser = cmd.get_parser("list_" + resources_collection)
        shell.run_command(cmd, cmd_parser, args)
        self.mox.VerifyAll()
        self.mox.UnsetStubs()
 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.assertTrue(myid in _str)
    def _test_list_resources(self, resources, cmd, detail=False, tags=[],
                             fields_1=[], fields_2=[], page_size=None,
                             sort_key=[], sort_dir=[], response_contents=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 = detail and ['-D', ] or []
        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
        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.assertTrue('myid1' in _str)
        return _str