blob: 924f9f20624b7a9ba4c754b75dc45a3a8b54c890 [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
Ngo Quoc Cuong54555c12017-05-04 18:12:02 +070015import fixtures
Matthew Treinish9e26ca82016-02-23 11:43:20 -050016from oslo_serialization import jsonutils as json
Matthew Treinish9e26ca82016-02-23 11:43:20 -050017
Matthew Treinishffad78a2016-04-16 14:39:52 -040018from tempest.tests import base
Jordan Pittier00f25962016-03-18 17:10:07 +010019from tempest.tests.lib import fake_http
Matthew Treinish9e26ca82016-02-23 11:43:20 -050020
21
Ken'ichi Ohmichice1552f2016-06-06 13:05:19 -070022class BaseServiceTest(base.TestCase):
Matthew Treinish9e26ca82016-02-23 11:43:20 -050023 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')
Jordan Pittier00f25962016-03-18 17:10:07 +010029 resp = fake_http.fake_http_response(headers, status=status), json_body
30 return resp
Matthew Treinish9e26ca82016-02-23 11:43:20 -050031
32 def check_service_client_function(self, function, function2mock,
33 body, to_utf=False, status=200,
Felipe Monteiroa4dadd32017-06-05 21:20:13 +010034 headers=None, mock_args=None,
ghanshyam609298c2017-07-20 09:15:33 +000035 resp_as_string=False,
Felipe Monteiroa4dadd32017-06-05 21:20:13 +010036 **kwargs):
37 """Mock a service client function for unit testing.
38
39 :param function: The service client function to call.
40 :param function2mock: The REST call to mock inside the service client
41 function.
42 :param body: Expected response body returned by the service client
43 function.
Masayuki Igawa25dc03c2017-07-05 12:26:09 +090044 :param to_utf: Whether to use UTF-8 encoding for response.
Felipe Monteiroa4dadd32017-06-05 21:20:13 +010045 :param status: Expected response status returned by the service client
46 function.
47 :param headers: Expected headers returned by the service client
48 function.
49 :param mock_args: List/dict/value of expected args/kwargs called by
50 function2mock. For example:
51 * If mock_args=['foo'] then ``assert_called_once_with('foo')``
52 is called.
53 * If mock_args={'foo': 'bar'} then
54 ``assert_called_once_with(foo='bar')`` is called.
55 * If mock_args='foo' then ``assert_called_once_with('foo')``
56 is called.
ghanshyam609298c2017-07-20 09:15:33 +000057 :param resp_as_string: Whether response body is retruned as string.
58 This is for service client methods which return ResponseBodyData
59 object.
Felipe Monteiroa4dadd32017-06-05 21:20:13 +010060 :param kwargs: kwargs that are passed to function.
61 """
Matthew Treinish9e26ca82016-02-23 11:43:20 -050062 mocked_response = self.create_response(body, to_utf, status, headers)
Felipe Monteiroa4dadd32017-06-05 21:20:13 +010063 fixture = self.useFixture(fixtures.MockPatch(
Matthew Treinish9e26ca82016-02-23 11:43:20 -050064 function2mock, return_value=mocked_response))
65 if kwargs:
66 resp = function(**kwargs)
67 else:
68 resp = function()
ghanshyam609298c2017-07-20 09:15:33 +000069 if resp_as_string:
70 resp = resp.data
ghanshyam9507e6b2016-09-15 17:34:24 +090071 self.assertEqual(body, resp)
Felipe Monteiroa4dadd32017-06-05 21:20:13 +010072 if isinstance(mock_args, list):
73 fixture.mock.assert_called_once_with(*mock_args)
74 elif isinstance(mock_args, dict):
75 fixture.mock.assert_called_once_with(**mock_args)
76 elif mock_args is not None:
77 fixture.mock.assert_called_once_with(mock_args)