blob: 88b8129829a1ca758e51c4e31d51b287f8c1e4e8 [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
16import httplib
17import json
18import mock
19import six
20import socket
21
22from tempest.common import glance_http
23from tempest import exceptions
24from tempest.openstack.common.fixture import mockpatch
25from tempest.tests import base
26from tempest.tests import fake_auth_provider
27from tempest.tests import fake_http
28
29
30class TestGlanceHTTPClient(base.TestCase):
31
32 def setUp(self):
33 super(TestGlanceHTTPClient, self).setUp()
34 self.fake_http = fake_http.fake_httplib2(return_type=200)
35 # NOTE(maurosr): using http here implies that we will be using httplib
36 # directly. With https glance_client would use an httpS version, but
37 # the real backend would still be httplib anyway and since we mock it
38 # that there is no reason to care.
39 self.endpoint = 'http://fake_url.com'
40 self.fake_auth = fake_auth_provider.FakeAuthProvider()
41
42 self.fake_auth.base_url = mock.MagicMock(return_value=self.endpoint)
43
44 self.useFixture(mockpatch.PatchObject(httplib.HTTPConnection,
45 'request',
46 side_effect=self.fake_http.request(self.endpoint)[1]))
47 self.client = glance_http.HTTPClient(self.fake_auth, {})
48
49 def _set_response_fixture(self, header, status, resp_body):
50 resp = fake_http.fake_httplib(header, status=status,
51 body=six.StringIO(resp_body))
52 self.useFixture(mockpatch.PatchObject(httplib.HTTPConnection,
53 'getresponse',
54 return_value=resp))
55 return resp
56
Mauro S. M. Rodrigues5403b792014-05-06 15:24:54 -040057 def test_json_request_without_content_type_header_in_response(self):
Mauro S. M. Rodrigues790a96d2014-03-30 10:41:30 -040058 self._set_response_fixture({}, 200, 'fake_response_body')
Mauro S. M. Rodrigues5403b792014-05-06 15:24:54 -040059 self.assertRaises(exceptions.InvalidContentType,
60 self.client.json_request, 'GET', '/images')
Mauro S. M. Rodrigues790a96d2014-03-30 10:41:30 -040061
Mauro S. M. Rodrigues5403b792014-05-06 15:24:54 -040062 def test_json_request_with_xml_content_type_header_in_request(self):
63 self.assertRaises(exceptions.InvalidContentType,
64 self.client.json_request, 'GET', '/images',
65 headers={'Content-Type': 'application/xml'})
66
67 def test_json_request_with_xml_content_type_header_in_response(self):
Mauro S. M. Rodrigues790a96d2014-03-30 10:41:30 -040068 self._set_response_fixture({'content-type': 'application/xml'},
69 200, 'fake_response_body')
Mauro S. M. Rodrigues5403b792014-05-06 15:24:54 -040070 self.assertRaises(exceptions.InvalidContentType,
71 self.client.json_request, 'GET', '/images')
Mauro S. M. Rodrigues790a96d2014-03-30 10:41:30 -040072
Mauro S. M. Rodrigues5403b792014-05-06 15:24:54 -040073 def test_json_request_with_json_content_type_header_only_in_resp(self):
Mauro S. M. Rodrigues790a96d2014-03-30 10:41:30 -040074 self._set_response_fixture({'content-type': 'application/json'},
75 200, 'fake_response_body')
76 resp, body = self.client.json_request('GET', '/images')
77 self.assertEqual(200, resp.status)
78 self.assertEqual('fake_response_body', body)
79
Mauro S. M. Rodrigues5403b792014-05-06 15:24:54 -040080 def test_json_request_with_json_content_type_header_in_req_and_resp(self):
81 self._set_response_fixture({'content-type': 'application/json'},
82 200, 'fake_response_body')
83 resp, body = self.client.json_request('GET', '/images', headers={
84 'Content-Type': 'application/json'})
85 self.assertEqual(200, resp.status)
86 self.assertEqual('fake_response_body', body)
87
Mauro S. M. Rodrigues790a96d2014-03-30 10:41:30 -040088 def test_json_request_fails_to_json_loads(self):
89 self._set_response_fixture({'content-type': 'application/json'},
90 200, 'fake_response_body')
91 self.useFixture(mockpatch.PatchObject(json, 'loads',
92 side_effect=ValueError()))
93 resp, body = self.client.json_request('GET', '/images')
94 self.assertEqual(200, resp.status)
95 self.assertEqual(body, 'fake_response_body')
96
97 def test_json_request_socket_timeout(self):
98 self.useFixture(mockpatch.PatchObject(httplib.HTTPConnection,
99 'request',
100 side_effect=socket.timeout()))
101 self.assertRaises(exceptions.TimeoutException,
102 self.client.json_request, 'GET', '/images')
103
104 def test_json_request_endpoint_not_found(self):
105 self.useFixture(mockpatch.PatchObject(httplib.HTTPConnection,
106 'request',
107 side_effect=socket.gaierror()))
108 self.assertRaises(exceptions.EndpointNotFound,
109 self.client.json_request, 'GET', '/images')
110
111 def test_raw_request(self):
112 self._set_response_fixture({}, 200, 'fake_response_body')
113 resp, body = self.client.raw_request('GET', '/images')
114 self.assertEqual(200, resp.status)
115 self.assertEqual('fake_response_body', body.read())
116
117 def test_raw_request_with_response_chunked(self):
118 self._set_response_fixture({}, 200, 'fake_response_body')
119 self.useFixture(mockpatch.PatchObject(glance_http,
120 'CHUNKSIZE', 1))
121 resp, body = self.client.raw_request('GET', '/images')
122 self.assertEqual(200, resp.status)
123 self.assertEqual('fake_response_body', body.read())
124
125 def test_raw_request_chunked(self):
126 self.useFixture(mockpatch.PatchObject(glance_http,
127 'CHUNKSIZE', 1))
128 self.useFixture(mockpatch.PatchObject(httplib.HTTPConnection,
129 'endheaders'))
130 self.useFixture(mockpatch.PatchObject(httplib.HTTPConnection,
131 'send'))
132
133 self._set_response_fixture({}, 200, 'fake_response_body')
134 req_body = six.StringIO('fake_request_body')
135 resp, body = self.client.raw_request('PUT', '/images', body=req_body)
136 self.assertEqual(200, resp.status)
137 self.assertEqual('fake_response_body', body.read())
138 httplib.HTTPConnection.send.assert_call_count(req_body.len)
139
140 def test_get_connection_class_for_https(self):
141 conn_class = self.client.get_connection_class('https')
142 self.assertEqual(glance_http.VerifiedHTTPSConnection, conn_class)
143
144 def test_get_connection_class_for_http(self):
145 conn_class = (self.client.get_connection_class('http'))
146 self.assertEqual(httplib.HTTPConnection, conn_class)
147
148 def test_get_connection_http(self):
149 self.assertTrue(isinstance(self.client.get_connection(),
150 httplib.HTTPConnection))
151
152 def test_get_connection_https(self):
153 endpoint = 'https://fake_url.com'
154 self.fake_auth.base_url = mock.MagicMock(return_value=endpoint)
155 self.client = glance_http.HTTPClient(self.fake_auth, {})
156 self.assertTrue(isinstance(self.client.get_connection(),
157 glance_http.VerifiedHTTPSConnection))
158
159 def test_get_connection_url_not_fount(self):
160 self.useFixture(mockpatch.PatchObject(self.client, 'connection_class',
161 side_effect=httplib.InvalidURL()
162 ))
163 self.assertRaises(exceptions.EndpointNotFound,
164 self.client.get_connection)
165
166 def test_get_connection_kwargs_default_for_http(self):
167 kwargs = self.client.get_connection_kwargs('http')
168 self.assertEqual(600, kwargs['timeout'])
169 self.assertEqual(1, len(kwargs.keys()))
170
171 def test_get_connection_kwargs_set_timeout_for_http(self):
172 kwargs = self.client.get_connection_kwargs('http', timeout=10,
173 cacert='foo')
174 self.assertEqual(10, kwargs['timeout'])
175 # nothing more than timeout is evaluated for http connections
176 self.assertEqual(1, len(kwargs.keys()))
177
178 def test_get_connection_kwargs_default_for_https(self):
179 kwargs = self.client.get_connection_kwargs('https')
180 self.assertEqual(600, kwargs['timeout'])
181 self.assertEqual(None, kwargs['cacert'])
182 self.assertEqual(None, kwargs['cert_file'])
183 self.assertEqual(None, kwargs['key_file'])
184 self.assertEqual(False, kwargs['insecure'])
185 self.assertEqual(True, kwargs['ssl_compression'])
186 self.assertEqual(6, len(kwargs.keys()))
187
188 def test_get_connection_kwargs_set_params_for_https(self):
189 kwargs = self.client.get_connection_kwargs('https', timeout=10,
190 cacert='foo',
191 cert_file='/foo/bar.cert',
192 key_file='/foo/key.pem',
193 insecure=True,
194 ssl_compression=False)
195 self.assertEqual(10, kwargs['timeout'])
196 self.assertEqual('foo', kwargs['cacert'])
197 self.assertEqual('/foo/bar.cert', kwargs['cert_file'])
198 self.assertEqual('/foo/key.pem', kwargs['key_file'])
199 self.assertEqual(True, kwargs['insecure'])
200 self.assertEqual(False, kwargs['ssl_compression'])
201 self.assertEqual(6, len(kwargs.keys()))
202
203
204class TestResponseBodyIterator(base.TestCase):
205
206 def test_iter_default_chunk_size_64k(self):
207 resp = fake_http.fake_httplib({}, six.StringIO(
208 'X' * (glance_http.CHUNKSIZE + 1)))
209 iterator = glance_http.ResponseBodyIterator(resp)
210 chunks = list(iterator)
211 self.assertEqual(chunks, ['X' * glance_http.CHUNKSIZE, 'X'])