def test_str_with_meta(self): resp = fakes.create_response() obj = resource.StrWithMeta('test-str', resp) self.assertEqual('test-str', obj) # Check request_id attribute is added to obj self.assertTrue(hasattr(obj, 'request_id')) self.assertEqual(fakes.FAKE_REQUEST_ID, obj.request_id)
def _update_all(self, url, json, key=None, raw=False, headers={}): """update resource with PUT method""" resp, body = self.http_client.put(url, json=json, headers=headers) # PATCH requests may not return a body if body: # get required body part content = self.get_data(body, key) if raw: return self.mixin_meta(content, resp) else: return self.resource_class(self, content, resp=resp) else: return resource.StrWithMeta(resp.text, resp)
def mixin_meta(item, resp): if isinstance(item, six.string_types): if six.PY2 and isinstance(item, six.text_type): return resource.UnicodeWithMeta(item, resp) else: return resource.StrWithMeta(item, resp) elif isinstance(item, six.binary_type): return resource.BytesWithMeta(item, resp) elif isinstance(item, list): return resource.ListWithMeta(item, resp) elif isinstance(item, tuple): return resource.TupleWithMeta(item, resp) elif item is None: return resource.TupleWithMeta((), resp) else: return resource.DictWithMeta(item, resp)
def test_remove_as_instance(self, mock_create, mock_list): args = [ "--group", "group-id", "--instance", "dacd968b-2602-470d-a0e2-92a20c2f2b8b", "--instance", "as-config-TEO_2MKT59WO", "--delete", ] verify_args = [ ("group", "group-id"), ("instances", [ "dacd968b-2602-470d-a0e2-92a20c2f2b8b", "as-config-TEO_2MKT59WO", ]), ("delete", True), ] parsed_args = self.check_parser(self.cmd, args, verify_args) with self.mocked_group_find as mocked_fg: instances = [ resource.AutoScalingInstance(None, i) for i in self._instances ] mock_list.return_value = br.ListWithMeta(instances, "Request-ID") mock_create.return_value = br.StrWithMeta('', 'Request-ID-2') result = self.cmd.take_action(parsed_args) mocked_fg.assert_called_once_with("group-id") mock_list.assert_called_once_with(self._group.id) url = "/scaling_group_instance/%s/action" % self._group.id json = { "action": "REMOVE", "instances_id": [ "dacd968b-2602-470d-a0e2-92a20c2f2b8b", "35d9225d-ca47-4d55-bc5d-3858c34610a5", ], "instance_delete": "yes" } mock_create.assert_called_once_with(url, json=json, raw=True) self.assertEquals('done', result)
def _get(self, url, params={}, key=None, raw=False, resource_class=None, headers={}): resp, body = self.http_client.get(url, params=params, headers=headers) # get required body part if body: content = self.get_data(body, key) if raw: return self.mixin_meta(content, resp) else: rc = resource_class if resource_class else self.resource_class return self.resource_class(self, content, resp=resp, attached=True) else: return resource.StrWithMeta(resp.text, resp)
def test_add_as_instance(self, mock_create, mock_find_resource): args = [ "--group", "group-id", "--instance", "dacd968b-2602-470d-a0e2-92a20c2f2b8b", "--instance", "35d9225d-ca47-4d55-bc5d-3858c34610a5", ] verify_args = [ ("group", "group-id"), ("instances", [ "dacd968b-2602-470d-a0e2-92a20c2f2b8b", "35d9225d-ca47-4d55-bc5d-3858c34610a5", ]), ] parsed_args = self.check_parser(self.cmd, args, verify_args) with self.mocked_group_find as mocked_fg: mock_find_resource.side_effect = [ br.Resource(None, dict(id=parsed_args.instances[0])), br.Resource(None, dict(id=parsed_args.instances[1])), ] mock_create.return_value = br.StrWithMeta('', 'Request-ID-2') result = self.cmd.take_action(parsed_args) mocked_fg.assert_called_once_with("group-id") url = "/scaling_group_instance/%s/action" % self._group.id json = { "action": "ADD", "instances_id": [ "dacd968b-2602-470d-a0e2-92a20c2f2b8b", "35d9225d-ca47-4d55-bc5d-3858c34610a5", ], } mock_create.assert_called_once_with(url, json=json, raw=True) self.assertEquals('done', result)