Mauro S. M. Rodrigues | 790a96d | 2014-03-30 10:41:30 -0400 | [diff] [blame] | 1 | # Copyright 2014 IBM Corp. |
| 2 | # All Rights Reserved. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 | # not use this file except in compliance with the License. You may obtain |
| 6 | # a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | # License for the specific language governing permissions and limitations |
| 14 | # under the License. |
| 15 | |
Mauro S. M. Rodrigues | 790a96d | 2014-03-30 10:41:30 -0400 | [diff] [blame] | 16 | import json |
Matthew Treinish | 96e9e88 | 2014-06-09 18:37:19 -0400 | [diff] [blame] | 17 | import socket |
| 18 | |
Mauro S. M. Rodrigues | 790a96d | 2014-03-30 10:41:30 -0400 | [diff] [blame] | 19 | import mock |
Doug Hellmann | 583ce2c | 2015-03-11 14:55:46 +0000 | [diff] [blame] | 20 | from oslotest import mockpatch |
Mauro S. M. Rodrigues | 790a96d | 2014-03-30 10:41:30 -0400 | [diff] [blame] | 21 | import six |
Matthew Treinish | 6421af8 | 2015-04-23 09:47:50 -0400 | [diff] [blame] | 22 | from six.moves import http_client as httplib |
Masayuki Igawa | d5f3b6c | 2015-01-20 14:40:45 +0900 | [diff] [blame] | 23 | from tempest_lib import exceptions as lib_exc |
Mauro S. M. Rodrigues | 790a96d | 2014-03-30 10:41:30 -0400 | [diff] [blame] | 24 | |
| 25 | from tempest.common import glance_http |
| 26 | from tempest import exceptions |
Mauro S. M. Rodrigues | 790a96d | 2014-03-30 10:41:30 -0400 | [diff] [blame] | 27 | from tempest.tests import base |
| 28 | from tempest.tests import fake_auth_provider |
| 29 | from tempest.tests import fake_http |
| 30 | |
| 31 | |
| 32 | class TestGlanceHTTPClient(base.TestCase): |
| 33 | |
| 34 | def setUp(self): |
| 35 | super(TestGlanceHTTPClient, self).setUp() |
| 36 | self.fake_http = fake_http.fake_httplib2(return_type=200) |
| 37 | # NOTE(maurosr): using http here implies that we will be using httplib |
| 38 | # directly. With https glance_client would use an httpS version, but |
| 39 | # the real backend would still be httplib anyway and since we mock it |
| 40 | # that there is no reason to care. |
| 41 | self.endpoint = 'http://fake_url.com' |
| 42 | self.fake_auth = fake_auth_provider.FakeAuthProvider() |
| 43 | |
| 44 | self.fake_auth.base_url = mock.MagicMock(return_value=self.endpoint) |
| 45 | |
Matthew Treinish | 1d14c54 | 2014-06-17 20:25:40 -0400 | [diff] [blame] | 46 | self.useFixture(mockpatch.PatchObject( |
| 47 | httplib.HTTPConnection, |
| 48 | 'request', |
| 49 | side_effect=self.fake_http.request(self.endpoint)[1])) |
Mauro S. M. Rodrigues | 790a96d | 2014-03-30 10:41:30 -0400 | [diff] [blame] | 50 | self.client = glance_http.HTTPClient(self.fake_auth, {}) |
| 51 | |
| 52 | def _set_response_fixture(self, header, status, resp_body): |
| 53 | resp = fake_http.fake_httplib(header, status=status, |
| 54 | body=six.StringIO(resp_body)) |
| 55 | self.useFixture(mockpatch.PatchObject(httplib.HTTPConnection, |
Matthew Treinish | 1d14c54 | 2014-06-17 20:25:40 -0400 | [diff] [blame] | 56 | 'getresponse', return_value=resp)) |
Mauro S. M. Rodrigues | 790a96d | 2014-03-30 10:41:30 -0400 | [diff] [blame] | 57 | return resp |
| 58 | |
Mauro S. M. Rodrigues | 5403b79 | 2014-05-06 15:24:54 -0400 | [diff] [blame] | 59 | def test_json_request_without_content_type_header_in_response(self): |
Mauro S. M. Rodrigues | 790a96d | 2014-03-30 10:41:30 -0400 | [diff] [blame] | 60 | self._set_response_fixture({}, 200, 'fake_response_body') |
Masayuki Igawa | d5f3b6c | 2015-01-20 14:40:45 +0900 | [diff] [blame] | 61 | self.assertRaises(lib_exc.InvalidContentType, |
Mauro S. M. Rodrigues | 5403b79 | 2014-05-06 15:24:54 -0400 | [diff] [blame] | 62 | self.client.json_request, 'GET', '/images') |
Mauro S. M. Rodrigues | 790a96d | 2014-03-30 10:41:30 -0400 | [diff] [blame] | 63 | |
Mauro S. M. Rodrigues | 5403b79 | 2014-05-06 15:24:54 -0400 | [diff] [blame] | 64 | def test_json_request_with_xml_content_type_header_in_request(self): |
Masayuki Igawa | d5f3b6c | 2015-01-20 14:40:45 +0900 | [diff] [blame] | 65 | self.assertRaises(lib_exc.InvalidContentType, |
Mauro S. M. Rodrigues | 5403b79 | 2014-05-06 15:24:54 -0400 | [diff] [blame] | 66 | self.client.json_request, 'GET', '/images', |
| 67 | headers={'Content-Type': 'application/xml'}) |
| 68 | |
| 69 | def test_json_request_with_xml_content_type_header_in_response(self): |
Mauro S. M. Rodrigues | 790a96d | 2014-03-30 10:41:30 -0400 | [diff] [blame] | 70 | self._set_response_fixture({'content-type': 'application/xml'}, |
| 71 | 200, 'fake_response_body') |
Masayuki Igawa | d5f3b6c | 2015-01-20 14:40:45 +0900 | [diff] [blame] | 72 | self.assertRaises(lib_exc.InvalidContentType, |
Mauro S. M. Rodrigues | 5403b79 | 2014-05-06 15:24:54 -0400 | [diff] [blame] | 73 | self.client.json_request, 'GET', '/images') |
Mauro S. M. Rodrigues | 790a96d | 2014-03-30 10:41:30 -0400 | [diff] [blame] | 74 | |
Mauro S. M. Rodrigues | 5403b79 | 2014-05-06 15:24:54 -0400 | [diff] [blame] | 75 | def test_json_request_with_json_content_type_header_only_in_resp(self): |
Mauro S. M. Rodrigues | 790a96d | 2014-03-30 10:41:30 -0400 | [diff] [blame] | 76 | self._set_response_fixture({'content-type': 'application/json'}, |
| 77 | 200, 'fake_response_body') |
| 78 | resp, body = self.client.json_request('GET', '/images') |
| 79 | self.assertEqual(200, resp.status) |
| 80 | self.assertEqual('fake_response_body', body) |
| 81 | |
Mauro S. M. Rodrigues | 5403b79 | 2014-05-06 15:24:54 -0400 | [diff] [blame] | 82 | def test_json_request_with_json_content_type_header_in_req_and_resp(self): |
| 83 | self._set_response_fixture({'content-type': 'application/json'}, |
| 84 | 200, 'fake_response_body') |
| 85 | resp, body = self.client.json_request('GET', '/images', headers={ |
| 86 | 'Content-Type': 'application/json'}) |
| 87 | self.assertEqual(200, resp.status) |
| 88 | self.assertEqual('fake_response_body', body) |
| 89 | |
Mauro S. M. Rodrigues | 790a96d | 2014-03-30 10:41:30 -0400 | [diff] [blame] | 90 | def test_json_request_fails_to_json_loads(self): |
| 91 | self._set_response_fixture({'content-type': 'application/json'}, |
| 92 | 200, 'fake_response_body') |
| 93 | self.useFixture(mockpatch.PatchObject(json, 'loads', |
| 94 | side_effect=ValueError())) |
| 95 | resp, body = self.client.json_request('GET', '/images') |
| 96 | self.assertEqual(200, resp.status) |
| 97 | self.assertEqual(body, 'fake_response_body') |
| 98 | |
| 99 | def test_json_request_socket_timeout(self): |
| 100 | self.useFixture(mockpatch.PatchObject(httplib.HTTPConnection, |
| 101 | 'request', |
| 102 | side_effect=socket.timeout())) |
| 103 | self.assertRaises(exceptions.TimeoutException, |
| 104 | self.client.json_request, 'GET', '/images') |
| 105 | |
| 106 | def test_json_request_endpoint_not_found(self): |
| 107 | self.useFixture(mockpatch.PatchObject(httplib.HTTPConnection, |
| 108 | 'request', |
| 109 | side_effect=socket.gaierror())) |
| 110 | self.assertRaises(exceptions.EndpointNotFound, |
| 111 | self.client.json_request, 'GET', '/images') |
| 112 | |
| 113 | def test_raw_request(self): |
| 114 | self._set_response_fixture({}, 200, 'fake_response_body') |
| 115 | resp, body = self.client.raw_request('GET', '/images') |
| 116 | self.assertEqual(200, resp.status) |
| 117 | self.assertEqual('fake_response_body', body.read()) |
| 118 | |
| 119 | def test_raw_request_with_response_chunked(self): |
| 120 | self._set_response_fixture({}, 200, 'fake_response_body') |
| 121 | self.useFixture(mockpatch.PatchObject(glance_http, |
| 122 | 'CHUNKSIZE', 1)) |
| 123 | resp, body = self.client.raw_request('GET', '/images') |
| 124 | self.assertEqual(200, resp.status) |
| 125 | self.assertEqual('fake_response_body', body.read()) |
| 126 | |
| 127 | def test_raw_request_chunked(self): |
| 128 | self.useFixture(mockpatch.PatchObject(glance_http, |
| 129 | 'CHUNKSIZE', 1)) |
| 130 | self.useFixture(mockpatch.PatchObject(httplib.HTTPConnection, |
| 131 | 'endheaders')) |
| 132 | self.useFixture(mockpatch.PatchObject(httplib.HTTPConnection, |
| 133 | 'send')) |
| 134 | |
| 135 | self._set_response_fixture({}, 200, 'fake_response_body') |
| 136 | req_body = six.StringIO('fake_request_body') |
| 137 | resp, body = self.client.raw_request('PUT', '/images', body=req_body) |
| 138 | self.assertEqual(200, resp.status) |
| 139 | self.assertEqual('fake_response_body', body.read()) |
| 140 | httplib.HTTPConnection.send.assert_call_count(req_body.len) |
| 141 | |
| 142 | def test_get_connection_class_for_https(self): |
| 143 | conn_class = self.client.get_connection_class('https') |
| 144 | self.assertEqual(glance_http.VerifiedHTTPSConnection, conn_class) |
| 145 | |
| 146 | def test_get_connection_class_for_http(self): |
| 147 | conn_class = (self.client.get_connection_class('http')) |
| 148 | self.assertEqual(httplib.HTTPConnection, conn_class) |
| 149 | |
| 150 | def test_get_connection_http(self): |
| 151 | self.assertTrue(isinstance(self.client.get_connection(), |
| 152 | httplib.HTTPConnection)) |
| 153 | |
| 154 | def test_get_connection_https(self): |
| 155 | endpoint = 'https://fake_url.com' |
| 156 | self.fake_auth.base_url = mock.MagicMock(return_value=endpoint) |
| 157 | self.client = glance_http.HTTPClient(self.fake_auth, {}) |
| 158 | self.assertTrue(isinstance(self.client.get_connection(), |
| 159 | glance_http.VerifiedHTTPSConnection)) |
| 160 | |
| 161 | def test_get_connection_url_not_fount(self): |
| 162 | self.useFixture(mockpatch.PatchObject(self.client, 'connection_class', |
| 163 | side_effect=httplib.InvalidURL() |
| 164 | )) |
| 165 | self.assertRaises(exceptions.EndpointNotFound, |
| 166 | self.client.get_connection) |
| 167 | |
| 168 | def test_get_connection_kwargs_default_for_http(self): |
| 169 | kwargs = self.client.get_connection_kwargs('http') |
| 170 | self.assertEqual(600, kwargs['timeout']) |
| 171 | self.assertEqual(1, len(kwargs.keys())) |
| 172 | |
| 173 | def test_get_connection_kwargs_set_timeout_for_http(self): |
| 174 | kwargs = self.client.get_connection_kwargs('http', timeout=10, |
Joseph Lanoux | c9c06be | 2015-01-21 09:03:30 +0000 | [diff] [blame] | 175 | ca_certs='foo') |
Mauro S. M. Rodrigues | 790a96d | 2014-03-30 10:41:30 -0400 | [diff] [blame] | 176 | self.assertEqual(10, kwargs['timeout']) |
| 177 | # nothing more than timeout is evaluated for http connections |
| 178 | self.assertEqual(1, len(kwargs.keys())) |
| 179 | |
| 180 | def test_get_connection_kwargs_default_for_https(self): |
| 181 | kwargs = self.client.get_connection_kwargs('https') |
| 182 | self.assertEqual(600, kwargs['timeout']) |
Joseph Lanoux | c9c06be | 2015-01-21 09:03:30 +0000 | [diff] [blame] | 183 | self.assertEqual(None, kwargs['ca_certs']) |
Mauro S. M. Rodrigues | 790a96d | 2014-03-30 10:41:30 -0400 | [diff] [blame] | 184 | self.assertEqual(None, kwargs['cert_file']) |
| 185 | self.assertEqual(None, kwargs['key_file']) |
| 186 | self.assertEqual(False, kwargs['insecure']) |
| 187 | self.assertEqual(True, kwargs['ssl_compression']) |
| 188 | self.assertEqual(6, len(kwargs.keys())) |
| 189 | |
| 190 | def test_get_connection_kwargs_set_params_for_https(self): |
| 191 | kwargs = self.client.get_connection_kwargs('https', timeout=10, |
Joseph Lanoux | c9c06be | 2015-01-21 09:03:30 +0000 | [diff] [blame] | 192 | ca_certs='foo', |
Mauro S. M. Rodrigues | 790a96d | 2014-03-30 10:41:30 -0400 | [diff] [blame] | 193 | cert_file='/foo/bar.cert', |
| 194 | key_file='/foo/key.pem', |
| 195 | insecure=True, |
| 196 | ssl_compression=False) |
| 197 | self.assertEqual(10, kwargs['timeout']) |
Joseph Lanoux | c9c06be | 2015-01-21 09:03:30 +0000 | [diff] [blame] | 198 | self.assertEqual('foo', kwargs['ca_certs']) |
Mauro S. M. Rodrigues | 790a96d | 2014-03-30 10:41:30 -0400 | [diff] [blame] | 199 | self.assertEqual('/foo/bar.cert', kwargs['cert_file']) |
| 200 | self.assertEqual('/foo/key.pem', kwargs['key_file']) |
| 201 | self.assertEqual(True, kwargs['insecure']) |
| 202 | self.assertEqual(False, kwargs['ssl_compression']) |
| 203 | self.assertEqual(6, len(kwargs.keys())) |
| 204 | |
| 205 | |
| 206 | class TestResponseBodyIterator(base.TestCase): |
| 207 | |
| 208 | def test_iter_default_chunk_size_64k(self): |
| 209 | resp = fake_http.fake_httplib({}, six.StringIO( |
| 210 | 'X' * (glance_http.CHUNKSIZE + 1))) |
| 211 | iterator = glance_http.ResponseBodyIterator(resp) |
| 212 | chunks = list(iterator) |
| 213 | self.assertEqual(chunks, ['X' * glance_http.CHUNKSIZE, 'X']) |