From 8e73ab78fa0fc81997768a566dc7d01b5401e20a Mon Sep 17 00:00:00 2001 From: ghanshyam Date: Tue, 13 Oct 2015 15:07:55 +0900 Subject: [PATCH] Support service client unit tests for headers also. Some APIs like create image, returns only headers with empty response body. We should have unit tests for those API also which required to add header support in base service client test class. This commit support header testing for service client and implement create image unit tests which only return info in headers. Change-Id: I9a79f862288e5fa827414ce1f26d80bcca4db687 --- tempest_lib/tests/services/compute/base.py | 11 +++++--- .../services/compute/test_images_client.py | 27 ++++++++++++++++++- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/tempest_lib/tests/services/compute/base.py b/tempest_lib/tests/services/compute/base.py index fb3313b..3e4a200 100644 --- a/tempest_lib/tests/services/compute/base.py +++ b/tempest_lib/tests/services/compute/base.py @@ -20,19 +20,22 @@ from tempest_lib.tests import base class BaseComputeServiceTest(base.TestCase): - def create_response(self, body, to_utf=False, status=200): + def create_response(self, body, to_utf=False, status=200, headers=None): json_body = {} if body: json_body = json.dumps(body) if to_utf: json_body = json_body.encode('utf-8') - response = (httplib2.Response({'status': status}), json_body) + resp_dict = {'status': status} + if headers: + resp_dict.update(headers) + response = (httplib2.Response(resp_dict), json_body) return response def check_service_client_function(self, function, function2mock, body, to_utf=False, status=200, - **kwargs): - mocked_response = self.create_response(body, to_utf, status) + headers=None, **kwargs): + mocked_response = self.create_response(body, to_utf, status, headers) self.useFixture(mockpatch.Patch( function2mock, return_value=mocked_response)) if kwargs: diff --git a/tempest_lib/tests/services/compute/test_images_client.py b/tempest_lib/tests/services/compute/test_images_client.py index 2172842..f0079c0 100644 --- a/tempest_lib/tests/services/compute/test_images_client.py +++ b/tempest_lib/tests/services/compute/test_images_client.py @@ -85,6 +85,7 @@ class TestImagesClient(base.BaseComputeServiceTest): "progress": 100, "status": "ACTIVE", "updated": "2011-01-01T01:02:03Z"}}, + "create": {}, "delete": {} } func2mock = { @@ -94,6 +95,7 @@ class TestImagesClient(base.BaseComputeServiceTest): 'delete': 'tempest_lib.common.rest_client.RestClient.delete'} # Variable definition FAKE_IMAGE_ID = FAKE_IMAGE_DATA['show']['image']['id'] + FAKE_SERVER_ID = "80a599e0-31e7-49b7-b260-868f441e343f" FAKE_CREATE_INFO = {'location': 'None'} FAKE_METADATA = FAKE_IMAGE_METADATA['show_item']['meta'] @@ -108,10 +110,27 @@ class TestImagesClient(base.BaseComputeServiceTest): mock_operation = self.func2mock['get'] expected_op = self.FAKE_IMAGE_DATA[operation] params = {"image_id": self.FAKE_IMAGE_ID} + headers = None if operation == 'list': function = self.client.list_images elif operation == 'show': function = self.client.show_image + elif operation == 'create': + function = self.client.create_image + mock_operation = self.func2mock['post'] + params = {"server_id": self.FAKE_SERVER_ID} + response_code = 202 + headers = { + 'connection': 'keep-alive', + 'content-length': '0', + 'content-type': 'application/json', + 'status': '202', + 'x-compute-request-id': 'req-fake', + 'vary': 'accept-encoding', + 'x-openstack-nova-api-version': 'v2.1', + 'date': '13 Oct 2015 05:55:36 GMT', + 'location': 'http://fake.com/images/fake' + } else: function = self.client.delete_image mock_operation = self.func2mock['delete'] @@ -119,7 +138,7 @@ class TestImagesClient(base.BaseComputeServiceTest): self.check_service_client_function( function, mock_operation, expected_op, - bytes_body, response_code, **params) + bytes_body, response_code, headers, **params) def _test_image_metadata(self, operation="set_item", bytes_body=False): response_code = 200 @@ -191,6 +210,12 @@ class TestImagesClient(base.BaseComputeServiceTest): def test_show_image_with_bytes_body(self): self._test_image_operation('show', True) + def test_create_image_with_str_body(self): + self._test_image_operation('create') + + def test_create_image_with_bytes_body(self): + self._test_image_operation('create', True) + def test_delete_image_with_str_body(self): self._test_image_operation('delete')