Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 1 | # 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 Cuong | 54555c1 | 2017-05-04 18:12:02 +0700 | [diff] [blame] | 15 | import fixtures |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 16 | from oslo_serialization import jsonutils as json |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 17 | |
Matthew Treinish | ffad78a | 2016-04-16 14:39:52 -0400 | [diff] [blame] | 18 | from tempest.tests import base |
Jordan Pittier | 00f2596 | 2016-03-18 17:10:07 +0100 | [diff] [blame] | 19 | from tempest.tests.lib import fake_http |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 20 | |
| 21 | |
Ken'ichi Ohmichi | ce1552f | 2016-06-06 13:05:19 -0700 | [diff] [blame] | 22 | class BaseServiceTest(base.TestCase): |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 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') |
Jordan Pittier | 00f2596 | 2016-03-18 17:10:07 +0100 | [diff] [blame] | 29 | resp = fake_http.fake_http_response(headers, status=status), json_body |
| 30 | return resp |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 31 | |
| 32 | def check_service_client_function(self, function, function2mock, |
| 33 | body, to_utf=False, status=200, |
Felipe Monteiro | a4dadd3 | 2017-06-05 21:20:13 +0100 | [diff] [blame] | 34 | headers=None, mock_args=None, |
ghanshyam | 609298c | 2017-07-20 09:15:33 +0000 | [diff] [blame^] | 35 | resp_as_string=False, |
Felipe Monteiro | a4dadd3 | 2017-06-05 21:20:13 +0100 | [diff] [blame] | 36 | **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 Igawa | 25dc03c | 2017-07-05 12:26:09 +0900 | [diff] [blame] | 44 | :param to_utf: Whether to use UTF-8 encoding for response. |
Felipe Monteiro | a4dadd3 | 2017-06-05 21:20:13 +0100 | [diff] [blame] | 45 | :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. |
ghanshyam | 609298c | 2017-07-20 09:15:33 +0000 | [diff] [blame^] | 57 | :param resp_as_string: Whether response body is retruned as string. |
| 58 | This is for service client methods which return ResponseBodyData |
| 59 | object. |
Felipe Monteiro | a4dadd3 | 2017-06-05 21:20:13 +0100 | [diff] [blame] | 60 | :param kwargs: kwargs that are passed to function. |
| 61 | """ |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 62 | mocked_response = self.create_response(body, to_utf, status, headers) |
Felipe Monteiro | a4dadd3 | 2017-06-05 21:20:13 +0100 | [diff] [blame] | 63 | fixture = self.useFixture(fixtures.MockPatch( |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 64 | function2mock, return_value=mocked_response)) |
| 65 | if kwargs: |
| 66 | resp = function(**kwargs) |
| 67 | else: |
| 68 | resp = function() |
ghanshyam | 609298c | 2017-07-20 09:15:33 +0000 | [diff] [blame^] | 69 | if resp_as_string: |
| 70 | resp = resp.data |
ghanshyam | 9507e6b | 2016-09-15 17:34:24 +0900 | [diff] [blame] | 71 | self.assertEqual(body, resp) |
Felipe Monteiro | a4dadd3 | 2017-06-05 21:20:13 +0100 | [diff] [blame] | 72 | 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) |