def test_rpc_errors(self): api = create_api() req = webob.Request.blank('/rpc') req.method = 'POST' req.content_type = 'application/json' # Body is not a list, it should fail req.body = jsonutils.dumps({}) res = req.get_response(api) self.assertEqual(res.status_int, 400) # cmd is not dict, it should fail. req.body = jsonutils.dumps([None]) res = req.get_response(api) self.assertEqual(res.status_int, 400) # No command key, it should fail. req.body = jsonutils.dumps([{}]) res = req.get_response(api) self.assertEqual(res.status_int, 400) # kwargs not dict, it should fail. req.body = jsonutils.dumps([{"command": "test", "kwargs": 200}]) res = req.get_response(api) self.assertEqual(res.status_int, 400) # Command does not exist, it should fail. req.body = jsonutils.dumps([{"command": "test"}]) res = req.get_response(api) self.assertEqual(res.status_int, 404)
def test_create_new_task(self): # 0. POST /tasks # Create a new task with valid input and type task_data = _new_task_fixture() task_owner = 'tenant1' body_content = json.dumps(task_data) path = "/v2/tasks" response, content = self.http.request( path, 'POST', headers=minimal_task_headers(task_owner), body=body_content) self.assertEqual(response.status, 201) data = json.loads(content) task_id = data['id'] self.assertIsNotNone(task_id) self.assertEqual(task_owner, data['owner']) self.assertEqual(task_data['type'], data['type']) self.assertEqual(task_data['input'], data['input']) # 1. POST /tasks # Create a new task with invalid type # Expect BadRequest(400) Error as response task_data = _new_task_fixture(type='invalid') task_owner = 'tenant1' body_content = json.dumps(task_data) path = "/v2/tasks" response, content = self.http.request( path, 'POST', headers=minimal_task_headers(task_owner), body=body_content) self.assertEqual(response.status, 400) # 1. POST /tasks # Create a new task with invalid input for type 'import' # Expect BadRequest(400) Error as response task_data = _new_task_fixture(task_input='{something: invalid}') task_owner = 'tenant1' body_content = json.dumps(task_data) path = "/v2/tasks" response, content = self.http.request( path, 'POST', headers=minimal_task_headers(task_owner), body=body_content) self.assertEqual(response.status, 400) # NOTE(nikhil): wait for all task executions to finish before exiting # else there is a risk of running into deadlock self._wait_on_task_execution()
def test_create_new_task(self): # 0. POST /tasks # Create a new task with valid input and type task_data = _new_task_fixture() task_owner = 'tenant1' body_content = json.dumps(task_data) path = "/v2/tasks" response, content = self.http.request( path, 'POST', headers=minimal_task_headers(task_owner), body=body_content) self.assertEqual(response.status, 201) data = json.loads(content) task_id = data['id'] self.assertIsNotNone(task_id) self.assertEqual(task_owner, data['owner']) self.assertEqual(task_data['type'], data['type']) self.assertEqual(task_data['input'], data['input']) # 1. POST /tasks # Create a new task with invalid type # Expect BadRequest(400) Error as response task_data = _new_task_fixture(type='invalid') task_owner = 'tenant1' body_content = json.dumps(task_data) path = "/v2/tasks" response, content = self.http.request( path, 'POST', headers=minimal_task_headers(task_owner), body=body_content) self.assertEqual(response.status, 400) # 1. POST /tasks # Create a new task with invalid input for type 'import' # Expect BadRequest(400) Error as response task_data = _new_task_fixture(input='{something: invalid}') task_owner = 'tenant1' body_content = json.dumps(task_data) path = "/v2/tasks" response, content = self.http.request( path, 'POST', headers=minimal_task_headers(task_owner), body=body_content) self.assertEqual(response.status, 400)
def _check_http(brain, match_kind, match, target_dict, cred_dict): """Check http: rules by calling to a remote server. This example implementation simply verifies that the response is exactly 'True'. A custom brain using response codes could easily be implemented. """ url = "http:" + (match % target_dict) data = {"target": jsonutils.dumps(target_dict), "credentials": jsonutils.dumps(cred_dict)} post_data = urllib.urlencode(data) f = urllib2.urlopen(url, post_data) return f.read() == "True"
def __call__(self, target, creds): """ Check http: rules by calling to a remote server. This example implementation simply verifies that the response is exactly 'True'. """ url = ('http:' + self.match) % target data = {'target': jsonutils.dumps(target), 'credentials': jsonutils.dumps(creds)} post_data = urllib.urlencode(data) f = urllib2.urlopen(url, post_data) return f.read() == "True"
def test_create_new_task(self): # 0. POST /tasks # Create a new task with valid input and type task_data = _new_task_fixture() task_owner = 'tenant1' body_content = json.dumps(task_data) path = "/v2/tasks" response, content = self.http.request(path, 'POST', headers= minimal_task_headers(task_owner), body=body_content) self.assertEqual(response.status, 201) data = json.loads(content) task_id = data['id'] self.assertIsNotNone(task_id) self.assertEqual(task_owner, data['owner']) self.assertEqual(task_data['type'], data['type']) self.assertEqual(task_data['input'], data['input']) # 1. POST /tasks # Create a new task with invalid type # Expect BadRequest(400) Error as response task_data = _new_task_fixture(type='invalid') task_owner = 'tenant1' body_content = json.dumps(task_data) path = "/v2/tasks" response, content = self.http.request(path, 'POST', headers= minimal_task_headers(task_owner), body=body_content) self.assertEqual(response.status, 400) # 1. POST /tasks # Create a new task with invalid input for type 'import' # Expect BadRequest(400) Error as response task_data = _new_task_fixture(input='{something: invalid}') task_owner = 'tenant1' body_content = json.dumps(task_data) path = "/v2/tasks" response, content = self.http.request(path, 'POST', headers= minimal_task_headers(task_owner), body=body_content) self.assertEqual(response.status, 400)
def test_create(self): request = unit_test_utils.get_fake_request() request.body = jsonutils.dumps({ 'type': 'import', 'input': { 'import_from': 'swift://cloud.foo/myaccount/mycontainer/path', 'import_from_format': 'qcow2', 'image_properties': { 'name': 'fake1' } }, }) output = self.deserializer.create(request) properties = { 'type': 'import', 'input': { 'import_from': 'swift://cloud.foo/myaccount/mycontainer/path', 'import_from_format': 'qcow2', 'image_properties': { 'name': 'fake1' } }, } self.maxDiff = None expected = {'task': properties} self.assertEqual(expected, output)
def test_get_index_marker_and_container_format_desc(self): """Test marker and null container format descending Tests that the registry API returns 200 when a marker and a null container_format are combined descending order """ UUID3 = _gen_uuid() extra_fixture = {'id': UUID3, 'status': 'active', 'is_public': True, 'disk_format': 'vhd', 'container_format': None, 'name': 'Fake image', 'size': 19, 'checksum': None} db_api.image_create(self.context, extra_fixture) req = webob.Request.blank('/rpc') req.method = "POST" cmd = [{ 'command': 'image_get_all', 'kwargs': {'marker': UUID3, 'sort_key': 'container_format', 'sort_dir': 'desc'}, }] req.body = jsonutils.dumps(cmd) res = req.get_response(self.api) self.assertEqual(res.status_int, 200) images = jsonutils.loads(res.body)[0] self.assertEqual(len(images), 0)
def __render(self, json_data, response, response_status=None): body = json.dumps(json_data, ensure_ascii=False) response.unicode_body = six.text_type(body) response.content_type = 'application/json' if response_status: response.status_int = response_status return response
def _to_property_dict(self, name, value): # Convert the model PropertyTypes dict to a JSON string json_data = tojson(PropertyType, value) db_property_type_dict = dict() db_property_type_dict['schema'] = json.dumps(json_data) db_property_type_dict['name'] = name return db_property_type_dict
def test_delete_task(self): # 0. POST /tasks # Create a new task with valid input and type task_data = _new_task_fixture() task_owner = 'tenant1' body_content = json.dumps(task_data) path = "/v2/tasks" response, content = self.http.request( path, 'POST', headers=minimal_task_headers(task_owner), body=body_content) self.assertEqual(response.status, 201) data = json.loads(content) task_id = data['id'] # 1. DELETE on /tasks/{task_id} # Attempt to delete a task path = "/v2/tasks/%s" % task_id response, content = self.http.request(path, 'DELETE', headers=minimal_task_headers()) self.assertEqual(response.status, 405) self.assertEqual('GET', response.webob_resp.headers.get('Allow')) self.assertEqual(('GET',), response.webob_resp.allow) self.assertEqual(('GET',), response.allow) # 2. GET /tasks/{task_id} # Ensure that methods mentioned in the Allow header work path = "/v2/tasks/%s" % task_id response, content = self.http.request(path, 'GET', headers=minimal_task_headers()) self.assertEqual(response.status, 200) self.assertIsNotNone(content)
def test_get_index(self): """ Tests that the image_get_all command returns list of images """ fixture = {'id': UUID2, 'name': 'fake image #2', 'size': 19, 'checksum': None} req = webob.Request.blank('/rpc') req.method = "POST" cmd = [{ 'command': 'image_get_all', 'kwargs': {'filters': fixture}, }] req.body = jsonutils.dumps(cmd) res = req.get_response(self.api) self.assertEqual(res.status_int, 200) images = jsonutils.loads(res.body)[0] self.assertEqual(len(images), 1) for k, v in six.iteritems(fixture): self.assertEqual(v, images[0][k])
def index(self, req): """Respond to a request for all OpenStack API versions.""" def build_version_object(version, path, status): return { 'id': 'v%s' % version, 'status': status, 'links': [ { 'rel': 'self', 'href': '%s/%s/' % (req.host_url, path), }, ], } version_objs = [] if CONF.enable_v2_api: version_objs.extend([ build_version_object(2.2, 'v2', 'CURRENT'), build_version_object(2.1, 'v2', 'SUPPORTED'), build_version_object(2.0, 'v2', 'SUPPORTED'), ]) if CONF.enable_v1_api: version_objs.extend([ build_version_object(1.1, 'v1', 'CURRENT'), build_version_object(1.0, 'v1', 'SUPPORTED'), ]) response = webob.Response(request=req, status=httplib.MULTIPLE_CHOICES, content_type='application/json') response.body = jsonutils.dumps(dict(versions=version_objs)) return response
def _patch(self, image_id, body, expected_status): path = "/v2/images/%s" % image_id media_type = "application/openstack-images-v2.1-json-patch" headers = self._headers({"content-type": media_type}) rsp, content = self.http.request(path, "PATCH", headers=headers, body=jsonutils.dumps(body)) self.assertEqual(expected_status, rsp.status, content) return content
def test_get_index(self): """ Tests that the image_get_all command returns list of images """ fixture = { 'id': UUID2, 'name': 'fake image #2', 'size': 19, 'checksum': None } req = webob.Request.blank('/rpc') req.method = "POST" cmd = [{ 'command': 'image_get_all', 'kwargs': { 'filters': fixture }, }] req.body = jsonutils.dumps(cmd) res = req.get_response(self.api) self.assertEqual(res.status_int, 200) images = jsonutils.loads(res.body)[0] self.assertEqual(len(images), 1) for k, v in six.iteritems(fixture): self.assertEqual(v, images[0][k])
def _check_http(brain, match_kind, match, target_dict, cred_dict): """Check http: rules by calling to a remote server. This example implementation simply verifies that the response is exactly 'True'. A custom brain using response codes could easily be implemented. """ url = 'http:' + (match % target_dict) data = { 'target': jsonutils.dumps(target_dict), 'credentials': jsonutils.dumps(cred_dict) } post_data = urllib.urlencode(data) f = urllib2.urlopen(url, post_data) return f.read() == "True"
def test_update_image(self): """Tests that the registry API updates the image""" fixture = {'name': 'fake public image #2', 'min_disk': 5, 'min_ram': 256, 'disk_format': 'raw'} req = webob.Request.blank('/rpc') req.method = "POST" cmd = [{ 'command': 'image_update', 'kwargs': {'values': fixture, 'image_id': UUID2} }] req.body = jsonutils.dumps(cmd) res = req.get_response(self.api) self.assertEqual(res.status_int, 200) res_dict = jsonutils.loads(res.body)[0] self.assertNotEqual(res_dict['created_at'], res_dict['updated_at']) for k, v in six.iteritems(fixture): self.assertEqual(v, res_dict[k])
def _post_new_task(self, **kwargs): task_owner = kwargs.get('owner') headers = minimal_task_headers(task_owner) task_data = _new_task_fixture() task_data['input']['import_from'] = "http://example.com" body_content = json.dumps(task_data) path = "/v2/tasks" response, content = self.http.request(path, 'POST', headers=headers, body=body_content) self.assertEqual(response.status, 201) task = json.loads(content) task_id = task['id'] self.assertIsNotNone(task_id) self.assertEqual(task_owner, task['owner']) self.assertEqual(task_data['type'], task['type']) self.assertEqual(task_data['input'], task['input']) return task, task_data
def test_create_image(self): """Tests that the registry API creates the image""" fixture = {'name': 'fake public image', 'status': 'active', 'is_public': True, 'disk_format': 'vhd', 'container_format': 'ovf'} req = webob.Request.blank('/rpc') req.method = "POST" cmd = [{ 'command': 'image_create', 'kwargs': {'values': fixture} }] req.body = jsonutils.dumps(cmd) res = req.get_response(self.api) self.assertEqual(res.status_int, 200) res_dict = jsonutils.loads(res.body)[0] for k, v in six.iteritems(fixture): self.assertEqual(v, res_dict[k]) # Test status was updated properly self.assertEqual('active', res_dict['status'])
def format(self, record): message = { 'message': record.getMessage(), 'asctime': self.formatTime(record, self.datefmt), 'name': record.name, 'msg': record.msg, 'args': record.args, 'levelname': record.levelname, 'levelno': record.levelno, 'pathname': record.pathname, 'filename': record.filename, 'module': record.module, 'lineno': record.lineno, 'funcname': record.funcName, 'created': record.created, 'msecs': record.msecs, 'relative_created': record.relativeCreated, 'thread': record.thread, 'thread_name': record.threadName, 'process_name': record.processName, 'process': record.process, 'traceback': None } if hasattr(record, 'extra'): message['extra'] = record.extra if record.exc_info: message['traceback'] = self.formatException(record.exc_info) return jsonutils.dumps(message)
def _to_dict(self, model_property_type): # Convert the model PropertyTypes dict to a JSON string json_data = tojson(PropertyType, model_property_type) db_property_type_dict = dict() db_property_type_dict['schema'] = json.dumps(json_data) db_property_type_dict['name'] = model_property_type.name return db_property_type_dict
def test_create_image(self): """Tests that the registry API creates the image""" fixture = { 'name': 'fake public image', 'status': 'active', 'is_public': True, 'disk_format': 'vhd', 'container_format': 'ovf' } req = webob.Request.blank('/rpc') req.method = "POST" cmd = [{'command': 'image_create', 'kwargs': {'values': fixture}}] req.body = jsonutils.dumps(cmd) res = req.get_response(self.api) self.assertEqual(res.status_int, 200) res_dict = jsonutils.loads(res.body)[0] for k, v in six.iteritems(fixture): self.assertEqual(v, res_dict[k]) # Test status was updated properly self.assertEqual('active', res_dict['status'])
def test_get_index_sort_updated_at_desc(self): """ Tests that the registry API returns list of public images sorted by updated_at in descending order. """ uuid4_time = timeutils.utcnow() + datetime.timedelta(seconds=10) uuid3_time = uuid4_time + datetime.timedelta(seconds=5) UUID3 = _gen_uuid() extra_fixture = { 'id': UUID3, 'status': 'active', 'is_public': True, 'disk_format': 'vhd', 'container_format': 'ovf', 'name': 'new name! #123', 'size': 19, 'checksum': None, 'created_at': None, 'updated_at': uuid3_time } db_api.image_create(self.context, extra_fixture) UUID4 = _gen_uuid() extra_fixture = { 'id': UUID4, 'status': 'active', 'is_public': True, 'disk_format': 'vhd', 'container_format': 'ovf', 'name': 'new name! #123', 'size': 20, 'checksum': None, 'created_at': None, 'updated_at': uuid4_time } db_api.image_create(self.context, extra_fixture) req = webob.Request.blank('/rpc') req.method = "POST" cmd = [{ 'command': 'image_get_all', 'kwargs': { 'sort_key': 'updated_at', 'sort_dir': 'desc' } }] req.body = jsonutils.dumps(cmd) res = req.get_response(self.api) self.assertEqual(res.status_int, 200) res_dict = jsonutils.loads(res.body)[0] images = res_dict self.assertEqual(len(images), 4) self.assertEqual(images[0]['id'], UUID3) self.assertEqual(images[1]['id'], UUID4) self.assertEqual(images[2]['id'], UUID2) self.assertEqual(images[3]['id'], UUID1)
def test_update_image(self): """Tests that the registry API updates the image""" fixture = { 'name': 'fake public image #2', 'min_disk': 5, 'min_ram': 256, 'disk_format': 'raw' } req = webob.Request.blank('/rpc') req.method = "POST" cmd = [{ 'command': 'image_update', 'kwargs': { 'values': fixture, 'image_id': UUID2 } }] req.body = jsonutils.dumps(cmd) res = req.get_response(self.api) self.assertEqual(res.status_int, 200) res_dict = jsonutils.loads(res.body)[0] self.assertNotEqual(res_dict['created_at'], res_dict['updated_at']) for k, v in six.iteritems(fixture): self.assertEqual(v, res_dict[k])
def test_show(self): """ Tests that registry API endpoint returns the expected image """ fixture = { 'id': UUID2, 'name': 'fake image #2', 'size': 19, 'min_ram': 256, 'min_disk': 5, 'checksum': None } req = webob.Request.blank('/rpc') req.method = "POST" cmd = [{ 'command': 'image_get', 'kwargs': { 'image_id': UUID2 }, }] req.body = jsonutils.dumps(cmd) res = req.get_response(self.api) self.assertEqual(res.status_int, 200) res_dict = jsonutils.loads(res.body)[0] image = res_dict for k, v in six.iteritems(fixture): self.assertEqual(v, image[k])
def format(self, record): message = {'message': record.getMessage(), 'asctime': self.formatTime(record, self.datefmt), 'name': record.name, 'msg': record.msg, 'args': record.args, 'levelname': record.levelname, 'levelno': record.levelno, 'pathname': record.pathname, 'filename': record.filename, 'module': record.module, 'lineno': record.lineno, 'funcname': record.funcName, 'created': record.created, 'msecs': record.msecs, 'relative_created': record.relativeCreated, 'thread': record.thread, 'thread_name': record.threadName, 'process_name': record.processName, 'process': record.process, 'traceback': None} if hasattr(record, 'extra'): message['extra'] = record.extra if record.exc_info: message['traceback'] = self.formatException(record.exc_info) return jsonutils.dumps(message)
def _v2_auth(self, token_url): creds = self.creds creds = { "auth": { "tenantName": creds["tenant"], "passwordCredentials": {"username": creds["username"], "password": creds["password"]}, } } headers = {} headers["Content-Type"] = "application/json" req_body = jsonutils.dumps(creds) resp, resp_body = self._do_request(token_url, "POST", headers=headers, body=req_body) if resp.status == 200: resp_auth = jsonutils.loads(resp_body)["access"] creds_region = self.creds.get("region") if self.configure_via_auth: endpoint = get_endpoint(resp_auth["serviceCatalog"], endpoint_region=creds_region) self.management_url = endpoint self.auth_token = resp_auth["token"]["id"] elif resp.status == 305: raise exception.RedirectException(resp["location"]) elif resp.status == 400: raise exception.AuthBadRequest(url=token_url) elif resp.status == 401: raise exception.NotAuthenticated() elif resp.status == 404: raise exception.AuthUrlNotFound(url=token_url) else: raise Exception(_("Unexpected response: %s") % resp.status)
def test_create(self): request = unit_test_utils.get_fake_request() request.body = jsonutils.dumps({'member': TENANT1}) image_id = UUID1 output = self.deserializer.create(request) expected = {'member_id': TENANT1} self.assertEqual(expected, output)
def _json_request(self, auth_host, auth_port, method, path, body=None, additional_headers=None): """HTTP request helper used to make json requests. :param method: http method :param path: relative request url :param body: dict to encode to json as request body. Optional. :param additional_headers: dict of additional headers to send with http request. Optional. :return (http response object, response body parsed as json) :raise ServerError when unable to communicate with keystone """ kwargs = { 'headers': { 'Content-type': 'application/json', 'Accept': 'application/json', }, } if additional_headers: kwargs['headers'].update(additional_headers) if body: kwargs['body'] = jsonutils.dumps(body) path = self.auth_admin_prefix + path response, body = self._http_request(auth_host, auth_port, method, path, **kwargs) try: data = jsonutils.loads(body) except ValueError: self.LOG.debug('Keystone did not return json-encoded body') data = {} return response, data
def test_update_invalid(self): request = unit_test_utils.get_fake_request() image_id = UUID1 member_id = TENANT1 request.body = jsonutils.dumps({'mem': TENANT1}) self.assertRaises(webob.exc.HTTPBadRequest, self.deserializer.update, request)
def update_image(self, image_id, image_metadata, purge_props=False, from_state=None): """ Updates Registry's information about an image """ if 'image' not in image_metadata: image_metadata = dict(image=image_metadata) encrypted_metadata = self.encrypt_metadata(image_metadata['image']) image_metadata['image'] = encrypted_metadata image_metadata['from_state'] = from_state body = jsonutils.dumps(image_metadata) headers = { 'Content-Type': 'application/json', } if purge_props: headers["X-Glance-Registry-Purge-Props"] = "true" res = self.do_request("PUT", "/images/%s" % image_id, body=body, headers=headers) data = jsonutils.loads(res.read()) image = data['image'] return self.decrypt_metadata(image)
def test_get_index_sort_name_asc(self): """ Tests that the registry API returns list of public images sorted alphabetically by name in ascending order. """ UUID3 = _gen_uuid() extra_fixture = {'id': UUID3, 'status': 'active', 'is_public': True, 'disk_format': 'vhd', 'container_format': 'ovf', 'name': 'asdf', 'size': 19, 'checksum': None} db_api.image_create(self.context, extra_fixture) UUID4 = _gen_uuid() extra_fixture = {'id': UUID4, 'status': 'active', 'is_public': True, 'disk_format': 'vhd', 'container_format': 'ovf', 'name': 'xyz', 'size': 20, 'checksum': None} db_api.image_create(self.context, extra_fixture) UUID5 = _gen_uuid() extra_fixture = {'id': UUID5, 'status': 'active', 'is_public': True, 'disk_format': 'vhd', 'container_format': 'ovf', 'name': None, 'size': 20, 'checksum': None} db_api.image_create(self.context, extra_fixture) req = webob.Request.blank('/rpc') req.method = "POST" cmd = [{ 'command': 'image_get_all', 'kwargs': {'sort_key': 'name', 'sort_dir': 'asc'} }] req.body = jsonutils.dumps(cmd) res = req.get_response(self.api) self.assertEqual(res.status_int, 200) res_dict = jsonutils.loads(res.body)[0] images = res_dict self.assertEqual(len(images), 5) self.assertEqual(images[0]['id'], UUID5) self.assertEqual(images[1]['id'], UUID3) self.assertEqual(images[2]['id'], UUID1) self.assertEqual(images[3]['id'], UUID2) self.assertEqual(images[4]['id'], UUID4)
def test_task_lifecycle(self): # Task list should be empty path = self._url('/v2/tasks') response = requests.get(path, headers=self._headers()) self.assertEqual(200, response.status_code) tasks = jsonutils.loads(response.text)['tasks'] self.assertEqual(0, len(tasks)) # Create a task path = self._url('/v2/tasks') headers = self._headers({'content-type': 'application/json'}) data = jsonutils.dumps({ "type": "import", "input": { "import_from": self.file_path, "import_from_format": "qcow2", "image_properties": { 'disk_format': 'vhd', 'container_format': 'ovf' } } }) response = requests.post(path, headers=headers, data=data) self.assertEqual(201, response.status_code) # Returned task entity should have a generated id and status task = jsonutils.loads(response.text) task_id = task['id'] checked_keys = set([ u'created_at', u'id', u'input', u'owner', u'schema', u'self', u'status', u'type', u'updated_at' ]) self.assertEqual(set(task.keys()), checked_keys) expected_task = { 'status': 'pending', 'type': 'import', 'input': { "import_from": self.file_path, "import_from_format": "qcow2", "image_properties": { 'disk_format': 'vhd', 'container_format': 'ovf' } }, 'schema': '/v2/schemas/task', } for key, value in expected_task.items(): self.assertEqual(task[key], value, key) # Tasks list should now have one entry path = self._url('/v2/tasks') response = requests.get(path, headers=self._headers()) self.assertEqual(200, response.status_code) tasks = jsonutils.loads(response.text)['tasks'] self.assertEqual(1, len(tasks)) self.assertEqual(tasks[0]['id'], task_id) self.stop_servers()
def test_cache_middleware_trans_v2_without_download_image_policy(self): """ Ensure the image v2 API image transfer applied 'download_image' policy enforcement. """ self.cleanup() self.start_servers(**self.__dict__.copy()) # Add an image and verify success path = "http://%s:%d/v2/images" % ("0.0.0.0", self.api_port) http = httplib2.Http() headers = {'content-type': 'application/json'} image_entity = { 'name': 'Image1', 'visibility': 'public', 'container_format': 'bare', 'disk_format': 'raw', } response, content = http.request(path, 'POST', headers=headers, body=jsonutils.dumps(image_entity)) self.assertEqual(response.status, 201) data = jsonutils.loads(content) image_id = data['id'] path = "http://%s:%d/v2/images/%s/file" % ("0.0.0.0", self.api_port, image_id) headers = {'content-type': 'application/octet-stream'} image_data = "*" * FIVE_KB response, content = http.request(path, 'PUT', headers=headers, body=image_data) self.assertEqual(response.status, 204) # Verify image not in cache image_cached_path = os.path.join(self.api_server.image_cache_dir, image_id) self.assertFalse(os.path.exists(image_cached_path)) rules = {"context_is_admin": "role:admin", "default": "", "download_image": "!"} self.set_policy_rules(rules) # Grab the image http = httplib2.Http() response, content = http.request(path, 'GET') self.assertEqual(response.status, 403) # Now, we delete the image from the server and verify that # the image cache no longer contains the deleted image path = "http://%s:%d/v2/images/%s" % ("0.0.0.0", self.api_port, image_id) http = httplib2.Http() response, content = http.request(path, 'DELETE') self.assertEqual(response.status, 204) self.assertFalse(os.path.exists(image_cached_path)) self.stop_servers()
def notify(_context, message): """Notifies the recipient of the desired event given the model. Log notifications using openstack's default logging system""" priority = message.get("priority", CONF.default_notification_level) priority = priority.lower() logger = logging.getLogger("glance.openstack.common.notification.%s" % message["event_type"]) getattr(logger, priority)(jsonutils.dumps(message))
def test_rest_get_images(self): c = glance_replicator.ImageService(FakeHTTPConnection(), 'noauth') # Two images, one of which is queued resp = {'images': [IMG_RESPONSE_ACTIVE, IMG_RESPONSE_QUEUED]} c.conn.prime_request('GET', 'v1/images/detail?is_public=None', '', {'x-auth-token': 'noauth'}, 200, jsonutils.dumps(resp), {}) c.conn.prime_request('GET', ('v1/images/detail?marker=%s&is_public=None' % IMG_RESPONSE_QUEUED['id']), '', {'x-auth-token': 'noauth'}, 200, jsonutils.dumps({'images': []}), {}) imgs = list(c.get_images()) self.assertEqual(len(imgs), 2) self.assertEqual(c.conn.count, 2)
def write_image(img, data): imgfile = os.path.join(tempdir, img['id']) with open(imgfile, 'w') as f: f.write(jsonutils.dumps(img)) if data: with open('%s.img' % imgfile, 'w') as f: f.write(data)
def test_update(self): request = unit_test_utils.get_fake_request() request.body = jsonutils.dumps({'status': 'accepted'}) image_id = UUID1 member_id = TENANT1 output = self.deserializer.update(request) expected = {'status': 'accepted'} self.assertEqual(expected, output)
def sync_data(self, image_id, **kwargs): headers = { 'Content-Type': 'application/json', } body = jsonutils.dumps(kwargs) res = self.do_request("PUT", "/v1/images/%s" % (image_id), body=body, headers=headers) return res
def test_replication_load(self): tempdir = self.useFixture(fixtures.TempDir()).path def write_image(img, data): imgfile = os.path.join(tempdir, img['id']) with open(imgfile, 'w') as f: f.write(jsonutils.dumps(img)) if data: with open('%s.img' % imgfile, 'w') as f: f.write(data) for img in FAKEIMAGES: cimg = copy.copy(img) # We need at least one image where the stashed metadata on disk # is newer than what the fake has if cimg['id'] == '5dcddce0-cba5-4f18-9cf4-9853c7b207a6': cimg['extra'] = 'thisissomeextra' # This is an image where the metadata change should be ignored if cimg['id'] == 'f4da1d2a-40e8-4710-b3aa-0222a4cc887b': cimg['dontrepl'] = 'thisisyetmoreextra' write_image(cimg, 'kjdhfkjshdfkjhsdkfd') # And an image which isn't on the destination at all new_id = str(uuid.uuid4()) cimg['id'] = new_id write_image(cimg, 'dskjfhskjhfkfdhksjdhf') # And an image which isn't on the destination, but lacks image # data new_id_missing_data = str(uuid.uuid4()) cimg['id'] = new_id_missing_data write_image(cimg, None) # A file which should be ignored badfile = os.path.join(tempdir, 'kjdfhf') with open(badfile, 'w') as f: f.write(jsonutils.dumps([1, 2, 3, 4, 5])) # Finally, we're ready to test options = UserDict.UserDict() options.dontreplicate = 'dontrepl dontreplabsent' options.slavetoken = 'slavetoken' args = ['localhost:9292', tempdir] orig_img_service = glance_replicator.get_image_service try: glance_replicator.get_image_service = get_image_service updated = glance_replicator.replication_load(options, args) finally: glance_replicator.get_image_service = orig_img_service self.assertIn('5dcddce0-cba5-4f18-9cf4-9853c7b207a6', updated) self.assertNotIn('f4da1d2a-40e8-4710-b3aa-0222a4cc887b', updated) self.assertIn(new_id, updated) self.assertNotIn(new_id_missing_data, updated)
def test_replication_load(self): tempdir = self.useFixture(fixtures.TempDir()).path def write_image(img, data): imgfile = os.path.join(tempdir, img['id']) with open(imgfile, 'w') as f: f.write(jsonutils.dumps(img)) if data: with open('%s.img' % imgfile, 'w') as f: f.write(data) for img in FAKEIMAGES: cimg = copy.copy(img) # We need at least one image where the stashed metadata on disk # is newer than what the fake has if cimg['id'] == '5dcddce0-cba5-4f18-9cf4-9853c7b207a6': cimg['extra'] = 'thisissomeextra' # This is an image where the metadata change should be ignored if cimg['id'] == 'f4da1d2a-40e8-4710-b3aa-0222a4cc887b': cimg['dontrepl'] = 'thisisyetmoreextra' write_image(cimg, 'kjdhfkjshdfkjhsdkfd') # And an image which isn't on the destination at all new_id = str(uuid.uuid4()) cimg['id'] = new_id write_image(cimg, 'dskjfhskjhfkfdhksjdhf') # And an image which isn't on the destination, but lacks image # data new_id_missing_data = str(uuid.uuid4()) cimg['id'] = new_id_missing_data write_image(cimg, None) # A file which should be ignored badfile = os.path.join(tempdir, 'kjdfhf') with open(badfile, 'w') as f: f.write(jsonutils.dumps([1, 2, 3, 4, 5])) # Finally, we're ready to test options = UserDict.UserDict() options.dontreplicate = 'dontrepl dontreplabsent' options.slavetoken = 'slavetoken' args = ['localhost:9292', tempdir] orig_img_service = glance_replicator.get_image_service try: glance_replicator.get_image_service = get_image_service updated = glance_replicator.replication_load(options, args) finally: glance_replicator.get_image_service = orig_img_service self.assertTrue('5dcddce0-cba5-4f18-9cf4-9853c7b207a6' in updated) self.assertFalse('f4da1d2a-40e8-4710-b3aa-0222a4cc887b' in updated) self.assertTrue(new_id in updated) self.assertFalse(new_id_missing_data in updated)
def test_get_index_limit_marker(self): """ Tests that the registry API returns list of public images that conforms to limit and marker query params """ uuid3_time = timeutils.utcnow() + datetime.timedelta(seconds=10) uuid4_time = uuid3_time + datetime.timedelta(seconds=5) UUID3 = _gen_uuid() extra_fixture = { 'id': UUID3, 'status': 'active', 'is_public': True, 'disk_format': 'vhd', 'container_format': 'ovf', 'name': 'new name! #123', 'size': 19, 'checksum': None, 'created_at': uuid3_time, 'updated_at': uuid3_time } db_api.image_create(self.context, extra_fixture) extra_fixture = { 'id': _gen_uuid(), 'status': 'active', 'is_public': True, 'disk_format': 'vhd', 'container_format': 'ovf', 'name': 'new name! #123', 'size': 20, 'checksum': None, 'created_at': uuid4_time, 'updated_at': uuid4_time } db_api.image_create(self.context, extra_fixture) req = webob.Request.blank('/rpc') req.method = "POST" cmd = [{ 'command': 'image_get_all', 'kwargs': { 'marker': UUID3, 'limit': 1 }, }] req.body = jsonutils.dumps(cmd) res = req.get_response(self.api) res_dict = jsonutils.loads(res.body)[0] self.assertEqual(res.status_int, 200) images = res_dict self.assertEqual(len(images), 1) # expect list to be sorted by created_at desc self.assertEqual(images[0]['id'], UUID2)
def notify(_context, message): """Notifies the recipient of the desired event given the model. Log notifications using openstack's default logging system""" priority = message.get('priority', CONF.default_notification_level) priority = priority.lower() logger = logging.getLogger('glance.openstack.common.notification.%s' % message['event_type']) getattr(logger, priority)(jsonutils.dumps(message))
def _create_image(self, body): path = '/v2/images' headers = self._headers({'content-type': 'application/json'}) rsp, content = self.http.request(path, 'POST', headers=headers, body=jsonutils.dumps(body)) self.assertEqual(201, rsp.status) return jsonutils.loads(content)
def get_cascaded_endpoints(self, regions=[]): headers = { 'Content-Type': 'application/json', } body = jsonutils.dumps({'regions': regions}) res = self.do_request('POST', '/v1/cascaded-eps', body=body, headers=headers) return jsonutils.loads(res.read())['eps']
def test_delete_image(self): """Tests that the registry API deletes the image""" # Grab the original number of images req = webob.Request.blank('/rpc') req.method = "POST" cmd = [{ 'command': 'image_get_all', 'kwargs': { 'filters': { 'deleted': False } } }] req.body = jsonutils.dumps(cmd) res = req.get_response(self.api) res_dict = jsonutils.loads(res.body)[0] self.assertEqual(res.status_int, 200) orig_num_images = len(res_dict) # Delete image #2 cmd = [{'command': 'image_destroy', 'kwargs': {'image_id': UUID2}}] req.body = jsonutils.dumps(cmd) res = req.get_response(self.api) self.assertEqual(res.status_int, 200) # Verify one less image cmd = [{ 'command': 'image_get_all', 'kwargs': { 'filters': { 'deleted': False } } }] req.body = jsonutils.dumps(cmd) res = req.get_response(self.api) res_dict = jsonutils.loads(res.body)[0] self.assertEqual(res.status_int, 200) new_num_images = len(res_dict) self.assertEqual(new_num_images, orig_num_images - 1)
def sync_locations(self, image_id, action=None, locs=None, **kwargs): headers = { 'Content-Type': 'application/json', } kwargs['action'] = action kwargs['locations'] = locs body = jsonutils.dumps(kwargs) res = self.do_request("PUT", "/v1/images/%s/location" % (image_id), body=body, headers=headers) return res