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 mock |
Doug Hellmann | 583ce2c | 2015-03-11 14:55:46 +0000 | [diff] [blame] | 17 | from oslotest import mockpatch |
Mauro S. M. Rodrigues | 790a96d | 2014-03-30 10:41:30 -0400 | [diff] [blame] | 18 | import six |
Matthew Treinish | 6421af8 | 2015-04-23 09:47:50 -0400 | [diff] [blame] | 19 | from six.moves import http_client as httplib |
Mauro S. M. Rodrigues | 790a96d | 2014-03-30 10:41:30 -0400 | [diff] [blame] | 20 | |
| 21 | from tempest.common import glance_http |
| 22 | from tempest import exceptions |
Mauro S. M. Rodrigues | 790a96d | 2014-03-30 10:41:30 -0400 | [diff] [blame] | 23 | from tempest.tests import base |
| 24 | from tempest.tests import fake_auth_provider |
| 25 | from tempest.tests import fake_http |
| 26 | |
| 27 | |
| 28 | class TestGlanceHTTPClient(base.TestCase): |
| 29 | |
| 30 | def setUp(self): |
| 31 | super(TestGlanceHTTPClient, self).setUp() |
| 32 | self.fake_http = fake_http.fake_httplib2(return_type=200) |
| 33 | # NOTE(maurosr): using http here implies that we will be using httplib |
| 34 | # directly. With https glance_client would use an httpS version, but |
| 35 | # the real backend would still be httplib anyway and since we mock it |
| 36 | # that there is no reason to care. |
| 37 | self.endpoint = 'http://fake_url.com' |
| 38 | self.fake_auth = fake_auth_provider.FakeAuthProvider() |
| 39 | |
| 40 | self.fake_auth.base_url = mock.MagicMock(return_value=self.endpoint) |
| 41 | |
Matthew Treinish | 1d14c54 | 2014-06-17 20:25:40 -0400 | [diff] [blame] | 42 | self.useFixture(mockpatch.PatchObject( |
| 43 | httplib.HTTPConnection, |
| 44 | 'request', |
| 45 | side_effect=self.fake_http.request(self.endpoint)[1])) |
Mauro S. M. Rodrigues | 790a96d | 2014-03-30 10:41:30 -0400 | [diff] [blame] | 46 | self.client = glance_http.HTTPClient(self.fake_auth, {}) |
| 47 | |
| 48 | def _set_response_fixture(self, header, status, resp_body): |
| 49 | resp = fake_http.fake_httplib(header, status=status, |
| 50 | body=six.StringIO(resp_body)) |
| 51 | self.useFixture(mockpatch.PatchObject(httplib.HTTPConnection, |
Matthew Treinish | 1d14c54 | 2014-06-17 20:25:40 -0400 | [diff] [blame] | 52 | 'getresponse', return_value=resp)) |
Mauro S. M. Rodrigues | 790a96d | 2014-03-30 10:41:30 -0400 | [diff] [blame] | 53 | return resp |
| 54 | |
Mauro S. M. Rodrigues | 790a96d | 2014-03-30 10:41:30 -0400 | [diff] [blame] | 55 | def test_raw_request(self): |
| 56 | self._set_response_fixture({}, 200, 'fake_response_body') |
| 57 | resp, body = self.client.raw_request('GET', '/images') |
| 58 | self.assertEqual(200, resp.status) |
| 59 | self.assertEqual('fake_response_body', body.read()) |
| 60 | |
| 61 | def test_raw_request_with_response_chunked(self): |
| 62 | self._set_response_fixture({}, 200, 'fake_response_body') |
| 63 | self.useFixture(mockpatch.PatchObject(glance_http, |
| 64 | 'CHUNKSIZE', 1)) |
| 65 | resp, body = self.client.raw_request('GET', '/images') |
| 66 | self.assertEqual(200, resp.status) |
| 67 | self.assertEqual('fake_response_body', body.read()) |
| 68 | |
| 69 | def test_raw_request_chunked(self): |
| 70 | self.useFixture(mockpatch.PatchObject(glance_http, |
| 71 | 'CHUNKSIZE', 1)) |
| 72 | self.useFixture(mockpatch.PatchObject(httplib.HTTPConnection, |
| 73 | 'endheaders')) |
| 74 | self.useFixture(mockpatch.PatchObject(httplib.HTTPConnection, |
| 75 | 'send')) |
| 76 | |
| 77 | self._set_response_fixture({}, 200, 'fake_response_body') |
| 78 | req_body = six.StringIO('fake_request_body') |
| 79 | resp, body = self.client.raw_request('PUT', '/images', body=req_body) |
| 80 | self.assertEqual(200, resp.status) |
| 81 | self.assertEqual('fake_response_body', body.read()) |
Shuquan Huang | d950415 | 2015-07-10 09:38:44 +0000 | [diff] [blame] | 82 | call_count = httplib.HTTPConnection.send.call_count |
| 83 | self.assertEqual(call_count - 1, req_body.tell()) |
Mauro S. M. Rodrigues | 790a96d | 2014-03-30 10:41:30 -0400 | [diff] [blame] | 84 | |
| 85 | def test_get_connection_class_for_https(self): |
Ken'ichi Ohmichi | 8f6cf5e | 2015-11-30 12:24:31 +0000 | [diff] [blame] | 86 | conn_class = self.client._get_connection_class('https') |
Mauro S. M. Rodrigues | 790a96d | 2014-03-30 10:41:30 -0400 | [diff] [blame] | 87 | self.assertEqual(glance_http.VerifiedHTTPSConnection, conn_class) |
| 88 | |
| 89 | def test_get_connection_class_for_http(self): |
Ken'ichi Ohmichi | 8f6cf5e | 2015-11-30 12:24:31 +0000 | [diff] [blame] | 90 | conn_class = (self.client._get_connection_class('http')) |
Mauro S. M. Rodrigues | 790a96d | 2014-03-30 10:41:30 -0400 | [diff] [blame] | 91 | self.assertEqual(httplib.HTTPConnection, conn_class) |
| 92 | |
| 93 | def test_get_connection_http(self): |
Ken'ichi Ohmichi | 8f6cf5e | 2015-11-30 12:24:31 +0000 | [diff] [blame] | 94 | self.assertTrue(isinstance(self.client._get_connection(), |
Mauro S. M. Rodrigues | 790a96d | 2014-03-30 10:41:30 -0400 | [diff] [blame] | 95 | httplib.HTTPConnection)) |
| 96 | |
| 97 | def test_get_connection_https(self): |
| 98 | endpoint = 'https://fake_url.com' |
| 99 | self.fake_auth.base_url = mock.MagicMock(return_value=endpoint) |
| 100 | self.client = glance_http.HTTPClient(self.fake_auth, {}) |
Ken'ichi Ohmichi | 8f6cf5e | 2015-11-30 12:24:31 +0000 | [diff] [blame] | 101 | self.assertTrue(isinstance(self.client._get_connection(), |
Mauro S. M. Rodrigues | 790a96d | 2014-03-30 10:41:30 -0400 | [diff] [blame] | 102 | glance_http.VerifiedHTTPSConnection)) |
| 103 | |
| 104 | def test_get_connection_url_not_fount(self): |
| 105 | self.useFixture(mockpatch.PatchObject(self.client, 'connection_class', |
| 106 | side_effect=httplib.InvalidURL() |
| 107 | )) |
| 108 | self.assertRaises(exceptions.EndpointNotFound, |
Ken'ichi Ohmichi | 8f6cf5e | 2015-11-30 12:24:31 +0000 | [diff] [blame] | 109 | self.client._get_connection) |
Mauro S. M. Rodrigues | 790a96d | 2014-03-30 10:41:30 -0400 | [diff] [blame] | 110 | |
| 111 | def test_get_connection_kwargs_default_for_http(self): |
Ken'ichi Ohmichi | 8f6cf5e | 2015-11-30 12:24:31 +0000 | [diff] [blame] | 112 | kwargs = self.client._get_connection_kwargs('http') |
Mauro S. M. Rodrigues | 790a96d | 2014-03-30 10:41:30 -0400 | [diff] [blame] | 113 | self.assertEqual(600, kwargs['timeout']) |
| 114 | self.assertEqual(1, len(kwargs.keys())) |
| 115 | |
| 116 | def test_get_connection_kwargs_set_timeout_for_http(self): |
Ken'ichi Ohmichi | 8f6cf5e | 2015-11-30 12:24:31 +0000 | [diff] [blame] | 117 | kwargs = self.client._get_connection_kwargs('http', timeout=10, |
| 118 | ca_certs='foo') |
Mauro S. M. Rodrigues | 790a96d | 2014-03-30 10:41:30 -0400 | [diff] [blame] | 119 | self.assertEqual(10, kwargs['timeout']) |
| 120 | # nothing more than timeout is evaluated for http connections |
| 121 | self.assertEqual(1, len(kwargs.keys())) |
| 122 | |
| 123 | def test_get_connection_kwargs_default_for_https(self): |
Ken'ichi Ohmichi | 8f6cf5e | 2015-11-30 12:24:31 +0000 | [diff] [blame] | 124 | kwargs = self.client._get_connection_kwargs('https') |
Mauro S. M. Rodrigues | 790a96d | 2014-03-30 10:41:30 -0400 | [diff] [blame] | 125 | self.assertEqual(600, kwargs['timeout']) |
Joseph Lanoux | c9c06be | 2015-01-21 09:03:30 +0000 | [diff] [blame] | 126 | self.assertEqual(None, kwargs['ca_certs']) |
Mauro S. M. Rodrigues | 790a96d | 2014-03-30 10:41:30 -0400 | [diff] [blame] | 127 | self.assertEqual(None, kwargs['cert_file']) |
| 128 | self.assertEqual(None, kwargs['key_file']) |
| 129 | self.assertEqual(False, kwargs['insecure']) |
| 130 | self.assertEqual(True, kwargs['ssl_compression']) |
| 131 | self.assertEqual(6, len(kwargs.keys())) |
| 132 | |
| 133 | def test_get_connection_kwargs_set_params_for_https(self): |
Ken'ichi Ohmichi | 8f6cf5e | 2015-11-30 12:24:31 +0000 | [diff] [blame] | 134 | kwargs = self.client._get_connection_kwargs('https', timeout=10, |
| 135 | ca_certs='foo', |
| 136 | cert_file='/foo/bar.cert', |
| 137 | key_file='/foo/key.pem', |
| 138 | insecure=True, |
| 139 | ssl_compression=False) |
Mauro S. M. Rodrigues | 790a96d | 2014-03-30 10:41:30 -0400 | [diff] [blame] | 140 | self.assertEqual(10, kwargs['timeout']) |
Joseph Lanoux | c9c06be | 2015-01-21 09:03:30 +0000 | [diff] [blame] | 141 | self.assertEqual('foo', kwargs['ca_certs']) |
Mauro S. M. Rodrigues | 790a96d | 2014-03-30 10:41:30 -0400 | [diff] [blame] | 142 | self.assertEqual('/foo/bar.cert', kwargs['cert_file']) |
| 143 | self.assertEqual('/foo/key.pem', kwargs['key_file']) |
| 144 | self.assertEqual(True, kwargs['insecure']) |
| 145 | self.assertEqual(False, kwargs['ssl_compression']) |
| 146 | self.assertEqual(6, len(kwargs.keys())) |
| 147 | |
| 148 | |
| 149 | class TestResponseBodyIterator(base.TestCase): |
| 150 | |
| 151 | def test_iter_default_chunk_size_64k(self): |
| 152 | resp = fake_http.fake_httplib({}, six.StringIO( |
| 153 | 'X' * (glance_http.CHUNKSIZE + 1))) |
| 154 | iterator = glance_http.ResponseBodyIterator(resp) |
| 155 | chunks = list(iterator) |
| 156 | self.assertEqual(chunks, ['X' * glance_http.CHUNKSIZE, 'X']) |