blob: 56020443daa298820cfb3d455edab974ff786cbc [file] [log] [blame]
Matthew Treinish9e26ca82016-02-23 11:43:20 -05001# Copyright 2015 Deutsche Telekom AG. All rights reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may
4# not use this file except in compliance with the License. You may obtain
5# a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations
13# under the License.
14
15import httplib2
16from oslo_serialization import jsonutils as json
17from oslotest import mockpatch
18
19from tempest.tests.lib import base
20
21
22class BaseComputeServiceTest(base.TestCase):
23 def create_response(self, body, to_utf=False, status=200, headers=None):
24 json_body = {}
25 if body:
26 json_body = json.dumps(body)
27 if to_utf:
28 json_body = json_body.encode('utf-8')
29 resp_dict = {'status': status}
30 if headers:
31 resp_dict.update(headers)
32 response = (httplib2.Response(resp_dict), json_body)
33 return response
34
35 def check_service_client_function(self, function, function2mock,
36 body, to_utf=False, status=200,
37 headers=None, **kwargs):
38 mocked_response = self.create_response(body, to_utf, status, headers)
39 self.useFixture(mockpatch.Patch(
40 function2mock, return_value=mocked_response))
41 if kwargs:
42 resp = function(**kwargs)
43 else:
44 resp = function()
45 self.assertEqual(body, resp)