Matthew Treinish | a33037e | 2013-12-05 23:16:39 +0000 | [diff] [blame] | 1 | # Copyright 2013 IBM Corp. |
| 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 | |
| 15 | import httplib2 |
| 16 | |
| 17 | from tempest.common import rest_client |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame^] | 18 | from tempest import config |
Matthew Treinish | a33037e | 2013-12-05 23:16:39 +0000 | [diff] [blame] | 19 | from tempest import exceptions |
| 20 | from tempest.openstack.common.fixture import mockpatch |
| 21 | from tempest.tests import base |
| 22 | from tempest.tests import fake_config |
| 23 | from tempest.tests import fake_http |
| 24 | |
| 25 | |
| 26 | class BaseRestClientTestClass(base.TestCase): |
| 27 | |
| 28 | def _set_token(self): |
| 29 | self.rest_client.token = 'fake token' |
| 30 | |
| 31 | def setUp(self): |
| 32 | super(BaseRestClientTestClass, self).setUp() |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame^] | 33 | self.stubs.Set(config, 'TempestConfigPrivate', fake_config.FakeConfig) |
| 34 | self.rest_client = rest_client.RestClient('fake_user', 'fake_pass', |
Matthew Treinish | a33037e | 2013-12-05 23:16:39 +0000 | [diff] [blame] | 35 | 'http://fake_url/v2.0') |
| 36 | self.stubs.Set(httplib2.Http, 'request', self.fake_http.request) |
| 37 | self.useFixture(mockpatch.PatchObject(self.rest_client, '_set_auth', |
| 38 | side_effect=self._set_token())) |
| 39 | self.useFixture(mockpatch.PatchObject(self.rest_client, |
| 40 | '_log_response')) |
| 41 | |
| 42 | |
| 43 | class TestRestClientHTTPMethods(BaseRestClientTestClass): |
| 44 | def setUp(self): |
| 45 | self.fake_http = fake_http.fake_httplib2() |
| 46 | super(TestRestClientHTTPMethods, self).setUp() |
| 47 | self.useFixture(mockpatch.PatchObject(self.rest_client, |
| 48 | '_error_checker')) |
| 49 | |
| 50 | def test_post(self): |
| 51 | __, return_dict = self.rest_client.post('fake_endpoint', {}, |
| 52 | {}) |
| 53 | self.assertEqual('POST', return_dict['method']) |
| 54 | |
| 55 | def test_get(self): |
| 56 | __, return_dict = self.rest_client.get('fake_endpoint') |
| 57 | self.assertEqual('GET', return_dict['method']) |
| 58 | |
| 59 | def test_delete(self): |
| 60 | __, return_dict = self.rest_client.delete('fake_endpoint') |
| 61 | self.assertEqual('DELETE', return_dict['method']) |
| 62 | |
| 63 | def test_patch(self): |
| 64 | __, return_dict = self.rest_client.patch('fake_endpoint', {}, |
| 65 | {}) |
| 66 | self.assertEqual('PATCH', return_dict['method']) |
| 67 | |
| 68 | def test_put(self): |
| 69 | __, return_dict = self.rest_client.put('fake_endpoint', {}, |
| 70 | {}) |
| 71 | self.assertEqual('PUT', return_dict['method']) |
| 72 | |
| 73 | def test_head(self): |
| 74 | self.useFixture(mockpatch.PatchObject(self.rest_client, |
| 75 | 'response_checker')) |
| 76 | __, return_dict = self.rest_client.head('fake_endpoint') |
| 77 | self.assertEqual('HEAD', return_dict['method']) |
| 78 | |
| 79 | def test_copy(self): |
| 80 | __, return_dict = self.rest_client.copy('fake_endpoint') |
| 81 | self.assertEqual('COPY', return_dict['method']) |
| 82 | |
| 83 | |
| 84 | class TestRestClientNotFoundHandling(BaseRestClientTestClass): |
| 85 | def setUp(self): |
| 86 | self.fake_http = fake_http.fake_httplib2(404) |
| 87 | super(TestRestClientNotFoundHandling, self).setUp() |
| 88 | |
| 89 | def test_post(self): |
| 90 | self.assertRaises(exceptions.NotFound, self.rest_client.post, |
| 91 | 'fake_endpoint', {}, {}) |