Merge "Support service client unit tests for headers also."
This commit is contained in:
@@ -20,19 +20,22 @@ from tempest_lib.tests import base
|
|||||||
|
|
||||||
|
|
||||||
class BaseComputeServiceTest(base.TestCase):
|
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 = {}
|
json_body = {}
|
||||||
if body:
|
if body:
|
||||||
json_body = json.dumps(body)
|
json_body = json.dumps(body)
|
||||||
if to_utf:
|
if to_utf:
|
||||||
json_body = json_body.encode('utf-8')
|
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
|
return response
|
||||||
|
|
||||||
def check_service_client_function(self, function, function2mock,
|
def check_service_client_function(self, function, function2mock,
|
||||||
body, to_utf=False, status=200,
|
body, to_utf=False, status=200,
|
||||||
**kwargs):
|
headers=None, **kwargs):
|
||||||
mocked_response = self.create_response(body, to_utf, status)
|
mocked_response = self.create_response(body, to_utf, status, headers)
|
||||||
self.useFixture(mockpatch.Patch(
|
self.useFixture(mockpatch.Patch(
|
||||||
function2mock, return_value=mocked_response))
|
function2mock, return_value=mocked_response))
|
||||||
if kwargs:
|
if kwargs:
|
||||||
|
@@ -85,6 +85,7 @@ class TestImagesClient(base.BaseComputeServiceTest):
|
|||||||
"progress": 100,
|
"progress": 100,
|
||||||
"status": "ACTIVE",
|
"status": "ACTIVE",
|
||||||
"updated": "2011-01-01T01:02:03Z"}},
|
"updated": "2011-01-01T01:02:03Z"}},
|
||||||
|
"create": {},
|
||||||
"delete": {}
|
"delete": {}
|
||||||
}
|
}
|
||||||
func2mock = {
|
func2mock = {
|
||||||
@@ -94,6 +95,7 @@ class TestImagesClient(base.BaseComputeServiceTest):
|
|||||||
'delete': 'tempest_lib.common.rest_client.RestClient.delete'}
|
'delete': 'tempest_lib.common.rest_client.RestClient.delete'}
|
||||||
# Variable definition
|
# Variable definition
|
||||||
FAKE_IMAGE_ID = FAKE_IMAGE_DATA['show']['image']['id']
|
FAKE_IMAGE_ID = FAKE_IMAGE_DATA['show']['image']['id']
|
||||||
|
FAKE_SERVER_ID = "80a599e0-31e7-49b7-b260-868f441e343f"
|
||||||
FAKE_CREATE_INFO = {'location': 'None'}
|
FAKE_CREATE_INFO = {'location': 'None'}
|
||||||
FAKE_METADATA = FAKE_IMAGE_METADATA['show_item']['meta']
|
FAKE_METADATA = FAKE_IMAGE_METADATA['show_item']['meta']
|
||||||
|
|
||||||
@@ -108,10 +110,27 @@ class TestImagesClient(base.BaseComputeServiceTest):
|
|||||||
mock_operation = self.func2mock['get']
|
mock_operation = self.func2mock['get']
|
||||||
expected_op = self.FAKE_IMAGE_DATA[operation]
|
expected_op = self.FAKE_IMAGE_DATA[operation]
|
||||||
params = {"image_id": self.FAKE_IMAGE_ID}
|
params = {"image_id": self.FAKE_IMAGE_ID}
|
||||||
|
headers = None
|
||||||
if operation == 'list':
|
if operation == 'list':
|
||||||
function = self.client.list_images
|
function = self.client.list_images
|
||||||
elif operation == 'show':
|
elif operation == 'show':
|
||||||
function = self.client.show_image
|
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:
|
else:
|
||||||
function = self.client.delete_image
|
function = self.client.delete_image
|
||||||
mock_operation = self.func2mock['delete']
|
mock_operation = self.func2mock['delete']
|
||||||
@@ -119,7 +138,7 @@ class TestImagesClient(base.BaseComputeServiceTest):
|
|||||||
|
|
||||||
self.check_service_client_function(
|
self.check_service_client_function(
|
||||||
function, mock_operation, expected_op,
|
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):
|
def _test_image_metadata(self, operation="set_item", bytes_body=False):
|
||||||
response_code = 200
|
response_code = 200
|
||||||
@@ -191,6 +210,12 @@ class TestImagesClient(base.BaseComputeServiceTest):
|
|||||||
def test_show_image_with_bytes_body(self):
|
def test_show_image_with_bytes_body(self):
|
||||||
self._test_image_operation('show', True)
|
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):
|
def test_delete_image_with_str_body(self):
|
||||||
self._test_image_operation('delete')
|
self._test_image_operation('delete')
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user