blob: ed886da5d063384e06e97a8409aeff0f014dfc89 [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 mock
Doug Hellmann583ce2c2015-03-11 14:55:46 +000017from oslotest import mockpatch
Mauro S. M. Rodrigues790a96d2014-03-30 10:41:30 -040018import six
Matthew Treinish6421af82015-04-23 09:47:50 -040019from six.moves import http_client as httplib
Mauro S. M. Rodrigues790a96d2014-03-30 10:41:30 -040020
21from tempest.common import glance_http
22from tempest import exceptions
Mauro S. M. Rodrigues790a96d2014-03-30 10:41:30 -040023from tempest.tests import base
24from tempest.tests import fake_auth_provider
25from tempest.tests import fake_http
26
27
28class 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 Treinish1d14c542014-06-17 20:25:40 -040042 self.useFixture(mockpatch.PatchObject(
43 httplib.HTTPConnection,
44 'request',
45 side_effect=self.fake_http.request(self.endpoint)[1]))
Mauro S. M. Rodrigues790a96d2014-03-30 10:41:30 -040046 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 Treinish1d14c542014-06-17 20:25:40 -040052 'getresponse', return_value=resp))
Mauro S. M. Rodrigues790a96d2014-03-30 10:41:30 -040053 return resp
54
Mauro S. M. Rodrigues790a96d2014-03-30 10:41:30 -040055 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 Huangd9504152015-07-10 09:38:44 +000082 call_count = httplib.HTTPConnection.send.call_count
83 self.assertEqual(call_count - 1, req_body.tell())
Mauro S. M. Rodrigues790a96d2014-03-30 10:41:30 -040084
85 def test_get_connection_class_for_https(self):
Ken'ichi Ohmichi8f6cf5e2015-11-30 12:24:31 +000086 conn_class = self.client._get_connection_class('https')
Mauro S. M. Rodrigues790a96d2014-03-30 10:41:30 -040087 self.assertEqual(glance_http.VerifiedHTTPSConnection, conn_class)
88
89 def test_get_connection_class_for_http(self):
Ken'ichi Ohmichi8f6cf5e2015-11-30 12:24:31 +000090 conn_class = (self.client._get_connection_class('http'))
Mauro S. M. Rodrigues790a96d2014-03-30 10:41:30 -040091 self.assertEqual(httplib.HTTPConnection, conn_class)
92
93 def test_get_connection_http(self):
Ken'ichi Ohmichi8f6cf5e2015-11-30 12:24:31 +000094 self.assertTrue(isinstance(self.client._get_connection(),
Mauro S. M. Rodrigues790a96d2014-03-30 10:41:30 -040095 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 Ohmichi8f6cf5e2015-11-30 12:24:31 +0000101 self.assertTrue(isinstance(self.client._get_connection(),
Mauro S. M. Rodrigues790a96d2014-03-30 10:41:30 -0400102 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 Ohmichi8f6cf5e2015-11-30 12:24:31 +0000109 self.client._get_connection)
Mauro S. M. Rodrigues790a96d2014-03-30 10:41:30 -0400110
111 def test_get_connection_kwargs_default_for_http(self):
Ken'ichi Ohmichi8f6cf5e2015-11-30 12:24:31 +0000112 kwargs = self.client._get_connection_kwargs('http')
Mauro S. M. Rodrigues790a96d2014-03-30 10:41:30 -0400113 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 Ohmichi8f6cf5e2015-11-30 12:24:31 +0000117 kwargs = self.client._get_connection_kwargs('http', timeout=10,
118 ca_certs='foo')
Mauro S. M. Rodrigues790a96d2014-03-30 10:41:30 -0400119 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 Ohmichi8f6cf5e2015-11-30 12:24:31 +0000124 kwargs = self.client._get_connection_kwargs('https')
Mauro S. M. Rodrigues790a96d2014-03-30 10:41:30 -0400125 self.assertEqual(600, kwargs['timeout'])
Joseph Lanouxc9c06be2015-01-21 09:03:30 +0000126 self.assertEqual(None, kwargs['ca_certs'])
Mauro S. M. Rodrigues790a96d2014-03-30 10:41:30 -0400127 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 Ohmichi8f6cf5e2015-11-30 12:24:31 +0000134 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. Rodrigues790a96d2014-03-30 10:41:30 -0400140 self.assertEqual(10, kwargs['timeout'])
Joseph Lanouxc9c06be2015-01-21 09:03:30 +0000141 self.assertEqual('foo', kwargs['ca_certs'])
Mauro S. M. Rodrigues790a96d2014-03-30 10:41:30 -0400142 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
149class 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'])