blob: b132f6072d61b837b736fd48fcda3bbfe68b44cd [file] [log] [blame]
Mauro S. M. Rodrigues790a96d2014-03-30 10:41:30 -04001# 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. Rodrigues790a96d2014-03-30 10:41:30 -040016import json
Matthew Treinish96e9e882014-06-09 18:37:19 -040017import socket
18
Mauro S. M. Rodrigues790a96d2014-03-30 10:41:30 -040019import mock
Doug Hellmann583ce2c2015-03-11 14:55:46 +000020from oslotest import mockpatch
Mauro S. M. Rodrigues790a96d2014-03-30 10:41:30 -040021import six
Matthew Treinish6421af82015-04-23 09:47:50 -040022from six.moves import http_client as httplib
Masayuki Igawad5f3b6c2015-01-20 14:40:45 +090023from tempest_lib import exceptions as lib_exc
Mauro S. M. Rodrigues790a96d2014-03-30 10:41:30 -040024
25from tempest.common import glance_http
26from tempest import exceptions
Mauro S. M. Rodrigues790a96d2014-03-30 10:41:30 -040027from tempest.tests import base
28from tempest.tests import fake_auth_provider
29from tempest.tests import fake_http
30
31
32class 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 Treinish1d14c542014-06-17 20:25:40 -040046 self.useFixture(mockpatch.PatchObject(
47 httplib.HTTPConnection,
48 'request',
49 side_effect=self.fake_http.request(self.endpoint)[1]))
Mauro S. M. Rodrigues790a96d2014-03-30 10:41:30 -040050 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 Treinish1d14c542014-06-17 20:25:40 -040056 'getresponse', return_value=resp))
Mauro S. M. Rodrigues790a96d2014-03-30 10:41:30 -040057 return resp
58
Mauro S. M. Rodrigues5403b792014-05-06 15:24:54 -040059 def test_json_request_without_content_type_header_in_response(self):
Mauro S. M. Rodrigues790a96d2014-03-30 10:41:30 -040060 self._set_response_fixture({}, 200, 'fake_response_body')
Masayuki Igawad5f3b6c2015-01-20 14:40:45 +090061 self.assertRaises(lib_exc.InvalidContentType,
Mauro S. M. Rodrigues5403b792014-05-06 15:24:54 -040062 self.client.json_request, 'GET', '/images')
Mauro S. M. Rodrigues790a96d2014-03-30 10:41:30 -040063
Mauro S. M. Rodrigues5403b792014-05-06 15:24:54 -040064 def test_json_request_with_xml_content_type_header_in_request(self):
Masayuki Igawad5f3b6c2015-01-20 14:40:45 +090065 self.assertRaises(lib_exc.InvalidContentType,
Mauro S. M. Rodrigues5403b792014-05-06 15:24:54 -040066 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. Rodrigues790a96d2014-03-30 10:41:30 -040070 self._set_response_fixture({'content-type': 'application/xml'},
71 200, 'fake_response_body')
Masayuki Igawad5f3b6c2015-01-20 14:40:45 +090072 self.assertRaises(lib_exc.InvalidContentType,
Mauro S. M. Rodrigues5403b792014-05-06 15:24:54 -040073 self.client.json_request, 'GET', '/images')
Mauro S. M. Rodrigues790a96d2014-03-30 10:41:30 -040074
Mauro S. M. Rodrigues5403b792014-05-06 15:24:54 -040075 def test_json_request_with_json_content_type_header_only_in_resp(self):
Mauro S. M. Rodrigues790a96d2014-03-30 10:41:30 -040076 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. Rodrigues5403b792014-05-06 15:24:54 -040082 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. Rodrigues790a96d2014-03-30 10:41:30 -040090 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 Lanouxc9c06be2015-01-21 09:03:30 +0000175 ca_certs='foo')
Mauro S. M. Rodrigues790a96d2014-03-30 10:41:30 -0400176 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 Lanouxc9c06be2015-01-21 09:03:30 +0000183 self.assertEqual(None, kwargs['ca_certs'])
Mauro S. M. Rodrigues790a96d2014-03-30 10:41:30 -0400184 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 Lanouxc9c06be2015-01-21 09:03:30 +0000192 ca_certs='foo',
Mauro S. M. Rodrigues790a96d2014-03-30 10:41:30 -0400193 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 Lanouxc9c06be2015-01-21 09:03:30 +0000198 self.assertEqual('foo', kwargs['ca_certs'])
Mauro S. M. Rodrigues790a96d2014-03-30 10:41:30 -0400199 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
206class 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'])