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)
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)
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 = ['mead', 'mea'] 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 = apmecV1_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(['--mead-id', 'mead']) 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()
def _test_list_sub_resources(self, resources, api_resource, cmd, myid, mock_get, detail=False, tags=[], fields_1=[], fields_2=[], page_size=None, sort_key=[], sort_dir=[], response_contents=None, base_args=None, path=None): mock_get.return_value = 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") with mock.patch.object(self.client.httpclient, 'request') as mock_req: mock_req.return_value = (MyResp(200), resstr) comparator = MyUrlComparator( end_url(path % myid, query=query, format=self.format), self.client) args.extend(['--request-format', self.format]) cmd_parser = cmd.get_parser("list_" + resources) shell.run_command(cmd, cmd_parser, args) mock_req.assert_called_once_with( comparator, '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