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