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