blob: bb2df43a5a9a42173734e0f9e7a9c305221ac918 [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
57 def test_json_request_without_content_type_header(self):
58 self._set_response_fixture({}, 200, 'fake_response_body')
59 resp, body = self.client.json_request('GET', '/images')
60 self.assertEqual(200, resp.status)
61 self.assertIsNone(body)
62
63 def test_json_request_with_xml_content_type_header(self):
64 self._set_response_fixture({'content-type': 'application/xml'},
65 200, 'fake_response_body')
66 resp, body = self.client.json_request('GET', '/images')
67 self.assertEqual(200, resp.status)
68 self.assertIsNone(body)
69
70 def test_json_request_with_content_type_header(self):
71 self._set_response_fixture({'content-type': 'application/json'},
72 200, 'fake_response_body')
73 resp, body = self.client.json_request('GET', '/images')
74 self.assertEqual(200, resp.status)
75 self.assertEqual('fake_response_body', body)
76
77 def test_json_request_fails_to_json_loads(self):
78 self._set_response_fixture({'content-type': 'application/json'},
79 200, 'fake_response_body')
80 self.useFixture(mockpatch.PatchObject(json, 'loads',
81 side_effect=ValueError()))
82 resp, body = self.client.json_request('GET', '/images')
83 self.assertEqual(200, resp.status)
84 self.assertEqual(body, 'fake_response_body')
85
86 def test_json_request_socket_timeout(self):
87 self.useFixture(mockpatch.PatchObject(httplib.HTTPConnection,
88 'request',
89 side_effect=socket.timeout()))
90 self.assertRaises(exceptions.TimeoutException,
91 self.client.json_request, 'GET', '/images')
92
93 def test_json_request_endpoint_not_found(self):
94 self.useFixture(mockpatch.PatchObject(httplib.HTTPConnection,
95 'request',
96 side_effect=socket.gaierror()))
97 self.assertRaises(exceptions.EndpointNotFound,
98 self.client.json_request, 'GET', '/images')
99
100 def test_raw_request(self):
101 self._set_response_fixture({}, 200, 'fake_response_body')
102 resp, body = self.client.raw_request('GET', '/images')
103 self.assertEqual(200, resp.status)
104 self.assertEqual('fake_response_body', body.read())
105
106 def test_raw_request_with_response_chunked(self):
107 self._set_response_fixture({}, 200, 'fake_response_body')
108 self.useFixture(mockpatch.PatchObject(glance_http,
109 'CHUNKSIZE', 1))
110 resp, body = self.client.raw_request('GET', '/images')
111 self.assertEqual(200, resp.status)
112 self.assertEqual('fake_response_body', body.read())
113
114 def test_raw_request_chunked(self):
115 self.useFixture(mockpatch.PatchObject(glance_http,
116 'CHUNKSIZE', 1))
117 self.useFixture(mockpatch.PatchObject(httplib.HTTPConnection,
118 'endheaders'))
119 self.useFixture(mockpatch.PatchObject(httplib.HTTPConnection,
120 'send'))
121
122 self._set_response_fixture({}, 200, 'fake_response_body')
123 req_body = six.StringIO('fake_request_body')
124 resp, body = self.client.raw_request('PUT', '/images', body=req_body)
125 self.assertEqual(200, resp.status)
126 self.assertEqual('fake_response_body', body.read())
127 httplib.HTTPConnection.send.assert_call_count(req_body.len)
128
129 def test_get_connection_class_for_https(self):
130 conn_class = self.client.get_connection_class('https')
131 self.assertEqual(glance_http.VerifiedHTTPSConnection, conn_class)
132
133 def test_get_connection_class_for_http(self):
134 conn_class = (self.client.get_connection_class('http'))
135 self.assertEqual(httplib.HTTPConnection, conn_class)
136
137 def test_get_connection_http(self):
138 self.assertTrue(isinstance(self.client.get_connection(),
139 httplib.HTTPConnection))
140
141 def test_get_connection_https(self):
142 endpoint = 'https://fake_url.com'
143 self.fake_auth.base_url = mock.MagicMock(return_value=endpoint)
144 self.client = glance_http.HTTPClient(self.fake_auth, {})
145 self.assertTrue(isinstance(self.client.get_connection(),
146 glance_http.VerifiedHTTPSConnection))
147
148 def test_get_connection_url_not_fount(self):
149 self.useFixture(mockpatch.PatchObject(self.client, 'connection_class',
150 side_effect=httplib.InvalidURL()
151 ))
152 self.assertRaises(exceptions.EndpointNotFound,
153 self.client.get_connection)
154
155 def test_get_connection_kwargs_default_for_http(self):
156 kwargs = self.client.get_connection_kwargs('http')
157 self.assertEqual(600, kwargs['timeout'])
158 self.assertEqual(1, len(kwargs.keys()))
159
160 def test_get_connection_kwargs_set_timeout_for_http(self):
161 kwargs = self.client.get_connection_kwargs('http', timeout=10,
162 cacert='foo')
163 self.assertEqual(10, kwargs['timeout'])
164 # nothing more than timeout is evaluated for http connections
165 self.assertEqual(1, len(kwargs.keys()))
166
167 def test_get_connection_kwargs_default_for_https(self):
168 kwargs = self.client.get_connection_kwargs('https')
169 self.assertEqual(600, kwargs['timeout'])
170 self.assertEqual(None, kwargs['cacert'])
171 self.assertEqual(None, kwargs['cert_file'])
172 self.assertEqual(None, kwargs['key_file'])
173 self.assertEqual(False, kwargs['insecure'])
174 self.assertEqual(True, kwargs['ssl_compression'])
175 self.assertEqual(6, len(kwargs.keys()))
176
177 def test_get_connection_kwargs_set_params_for_https(self):
178 kwargs = self.client.get_connection_kwargs('https', timeout=10,
179 cacert='foo',
180 cert_file='/foo/bar.cert',
181 key_file='/foo/key.pem',
182 insecure=True,
183 ssl_compression=False)
184 self.assertEqual(10, kwargs['timeout'])
185 self.assertEqual('foo', kwargs['cacert'])
186 self.assertEqual('/foo/bar.cert', kwargs['cert_file'])
187 self.assertEqual('/foo/key.pem', kwargs['key_file'])
188 self.assertEqual(True, kwargs['insecure'])
189 self.assertEqual(False, kwargs['ssl_compression'])
190 self.assertEqual(6, len(kwargs.keys()))
191
192
193class TestResponseBodyIterator(base.TestCase):
194
195 def test_iter_default_chunk_size_64k(self):
196 resp = fake_http.fake_httplib({}, six.StringIO(
197 'X' * (glance_http.CHUNKSIZE + 1)))
198 iterator = glance_http.ResponseBodyIterator(resp)
199 chunks = list(iterator)
200 self.assertEqual(chunks, ['X' * glance_http.CHUNKSIZE, 'X'])