blob: ba43daf574f4c670b8c02907c16b93388f341d92 [file] [log] [blame]
Matthew Treinisha33037e2013-12-05 23:16:39 +00001# Copyright 2013 IBM Corp.
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may
4# not use this file except in compliance with the License. You may obtain
5# a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations
13# under the License.
14
15import httplib2
vponomaryov67b58fe2014-02-06 19:05:41 +020016import json
Matthew Treinisha33037e2013-12-05 23:16:39 +000017
18from tempest.common import rest_client
Matthew Treinish684d8992014-01-30 16:27:40 +000019from tempest import config
Matthew Treinisha33037e2013-12-05 23:16:39 +000020from tempest import exceptions
21from tempest.openstack.common.fixture import mockpatch
vponomaryov67b58fe2014-02-06 19:05:41 +020022from tempest.services.compute.xml import common as xml
Matthew Treinisha33037e2013-12-05 23:16:39 +000023from tempest.tests import base
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000024from tempest.tests import fake_auth_provider
Matthew Treinisha33037e2013-12-05 23:16:39 +000025from tempest.tests import fake_config
26from tempest.tests import fake_http
27
28
29class BaseRestClientTestClass(base.TestCase):
30
vponomaryov67b58fe2014-02-06 19:05:41 +020031 url = 'fake_endpoint'
32
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000033 def _get_region(self):
34 return 'fake region'
Matthew Treinisha33037e2013-12-05 23:16:39 +000035
36 def setUp(self):
37 super(BaseRestClientTestClass, self).setUp()
Matthew Treinish684d8992014-01-30 16:27:40 +000038 self.stubs.Set(config, 'TempestConfigPrivate', fake_config.FakeConfig)
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000039 self.rest_client = rest_client.RestClient(
40 fake_auth_provider.FakeAuthProvider())
Matthew Treinisha33037e2013-12-05 23:16:39 +000041 self.stubs.Set(httplib2.Http, 'request', self.fake_http.request)
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000042 self.useFixture(mockpatch.PatchObject(self.rest_client, '_get_region',
43 side_effect=self._get_region()))
Matthew Treinisha33037e2013-12-05 23:16:39 +000044 self.useFixture(mockpatch.PatchObject(self.rest_client,
45 '_log_response'))
46
47
48class TestRestClientHTTPMethods(BaseRestClientTestClass):
49 def setUp(self):
50 self.fake_http = fake_http.fake_httplib2()
51 super(TestRestClientHTTPMethods, self).setUp()
52 self.useFixture(mockpatch.PatchObject(self.rest_client,
53 '_error_checker'))
54
55 def test_post(self):
vponomaryov67b58fe2014-02-06 19:05:41 +020056 __, return_dict = self.rest_client.post(self.url, {}, {})
Matthew Treinisha33037e2013-12-05 23:16:39 +000057 self.assertEqual('POST', return_dict['method'])
58
59 def test_get(self):
vponomaryov67b58fe2014-02-06 19:05:41 +020060 __, return_dict = self.rest_client.get(self.url)
Matthew Treinisha33037e2013-12-05 23:16:39 +000061 self.assertEqual('GET', return_dict['method'])
62
63 def test_delete(self):
vponomaryov67b58fe2014-02-06 19:05:41 +020064 __, return_dict = self.rest_client.delete(self.url)
Matthew Treinisha33037e2013-12-05 23:16:39 +000065 self.assertEqual('DELETE', return_dict['method'])
66
67 def test_patch(self):
vponomaryov67b58fe2014-02-06 19:05:41 +020068 __, return_dict = self.rest_client.patch(self.url, {}, {})
Matthew Treinisha33037e2013-12-05 23:16:39 +000069 self.assertEqual('PATCH', return_dict['method'])
70
71 def test_put(self):
vponomaryov67b58fe2014-02-06 19:05:41 +020072 __, return_dict = self.rest_client.put(self.url, {}, {})
Matthew Treinisha33037e2013-12-05 23:16:39 +000073 self.assertEqual('PUT', return_dict['method'])
74
75 def test_head(self):
76 self.useFixture(mockpatch.PatchObject(self.rest_client,
77 'response_checker'))
vponomaryov67b58fe2014-02-06 19:05:41 +020078 __, return_dict = self.rest_client.head(self.url)
Matthew Treinisha33037e2013-12-05 23:16:39 +000079 self.assertEqual('HEAD', return_dict['method'])
80
81 def test_copy(self):
vponomaryov67b58fe2014-02-06 19:05:41 +020082 __, return_dict = self.rest_client.copy(self.url)
Matthew Treinisha33037e2013-12-05 23:16:39 +000083 self.assertEqual('COPY', return_dict['method'])
84
85
86class TestRestClientNotFoundHandling(BaseRestClientTestClass):
87 def setUp(self):
88 self.fake_http = fake_http.fake_httplib2(404)
89 super(TestRestClientNotFoundHandling, self).setUp()
90
91 def test_post(self):
92 self.assertRaises(exceptions.NotFound, self.rest_client.post,
vponomaryov67b58fe2014-02-06 19:05:41 +020093 self.url, {}, {})
94
95
96class TestRestClientHeadersJSON(TestRestClientHTTPMethods):
97 TYPE = "json"
98
99 def _verify_headers(self, resp):
100 self.assertEqual(self.rest_client._get_type(), self.TYPE)
101 resp = dict((k.lower(), v) for k, v in resp.iteritems())
102 self.assertEqual(self.header_value, resp['accept'])
103 self.assertEqual(self.header_value, resp['content-type'])
104
105 def setUp(self):
106 super(TestRestClientHeadersJSON, self).setUp()
107 self.rest_client.TYPE = self.TYPE
108 self.header_value = 'application/%s' % self.rest_client._get_type()
109
110 def test_post(self):
111 resp, __ = self.rest_client.post(self.url, {})
112 self._verify_headers(resp)
113
114 def test_get(self):
115 resp, __ = self.rest_client.get(self.url)
116 self._verify_headers(resp)
117
118 def test_delete(self):
119 resp, __ = self.rest_client.delete(self.url)
120 self._verify_headers(resp)
121
122 def test_patch(self):
123 resp, __ = self.rest_client.patch(self.url, {})
124 self._verify_headers(resp)
125
126 def test_put(self):
127 resp, __ = self.rest_client.put(self.url, {})
128 self._verify_headers(resp)
129
130 def test_head(self):
131 self.useFixture(mockpatch.PatchObject(self.rest_client,
132 'response_checker'))
133 resp, __ = self.rest_client.head(self.url)
134 self._verify_headers(resp)
135
136 def test_copy(self):
137 resp, __ = self.rest_client.copy(self.url)
138 self._verify_headers(resp)
139
140
141class TestRestClientHeadersXML(TestRestClientHeadersJSON):
142 TYPE = "xml"
143
144 # These two tests are needed in one exemplar
145 def test_send_json_accept_xml(self):
146 resp, __ = self.rest_client.get(self.url,
147 self.rest_client.get_headers("xml",
148 "json"))
149 resp = dict((k.lower(), v) for k, v in resp.iteritems())
150 self.assertEqual("application/json", resp["content-type"])
151 self.assertEqual("application/xml", resp["accept"])
152
153 def test_send_xml_accept_json(self):
154 resp, __ = self.rest_client.get(self.url,
155 self.rest_client.get_headers("json",
156 "xml"))
157 resp = dict((k.lower(), v) for k, v in resp.iteritems())
158 self.assertEqual("application/json", resp["accept"])
159 self.assertEqual("application/xml", resp["content-type"])
160
161
162class TestRestClientParseRespXML(BaseRestClientTestClass):
163 TYPE = "xml"
164
165 keys = ["fake_key1", "fake_key2"]
166 values = ["fake_value1", "fake_value2"]
167 item_expected = {key: value for key, value in zip(keys, values)}
168 list_expected = {"body_list": [
169 {keys[0]: values[0]},
170 {keys[1]: values[1]},
171 ]}
172 dict_expected = {"body_dict": {
173 keys[0]: values[0],
174 keys[1]: values[1],
175 }}
176
177 def setUp(self):
178 self.fake_http = fake_http.fake_httplib2()
179 super(TestRestClientParseRespXML, self).setUp()
180 self.rest_client.TYPE = self.TYPE
181
182 def test_parse_resp_body_item(self):
183 body_item = xml.Element("item", **self.item_expected)
184 body = self.rest_client._parse_resp(str(xml.Document(body_item)))
185 self.assertEqual(self.item_expected, body)
186
187 def test_parse_resp_body_list(self):
188 self.rest_client.list_tags = ["fake_list", ]
189 body_list = xml.Element(self.rest_client.list_tags[0])
190 for i in range(2):
191 body_list.append(xml.Element("fake_item",
192 **self.list_expected["body_list"][i]))
193 body = self.rest_client._parse_resp(str(xml.Document(body_list)))
194 self.assertEqual(self.list_expected["body_list"], body)
195
196 def test_parse_resp_body_dict(self):
197 self.rest_client.dict_tags = ["fake_dict", ]
198 body_dict = xml.Element(self.rest_client.dict_tags[0])
199
200 for i in range(2):
201 body_dict.append(xml.Element("fake_item", xml.Text(self.values[i]),
202 key=self.keys[i]))
203
204 body = self.rest_client._parse_resp(str(xml.Document(body_dict)))
205 self.assertEqual(self.dict_expected["body_dict"], body)
206
207
208class TestRestClientParseRespJSON(TestRestClientParseRespXML):
209 TYPE = "json"
210
211 def test_parse_resp_body_item(self):
212 body = self.rest_client._parse_resp(json.dumps(self.item_expected))
213 self.assertEqual(self.item_expected, body)
214
215 def test_parse_resp_body_list(self):
216 body = self.rest_client._parse_resp(json.dumps(self.list_expected))
217 self.assertEqual(self.list_expected["body_list"], body)
218
219 def test_parse_resp_body_dict(self):
220 body = self.rest_client._parse_resp(json.dumps(self.dict_expected))
221 self.assertEqual(self.dict_expected["body_dict"], body)
222
223 def test_parse_resp_two_top_keys(self):
224 dict_two_keys = self.dict_expected.copy()
225 dict_two_keys.update({"second_key": ""})
226 body = self.rest_client._parse_resp(json.dumps(dict_two_keys))
227 self.assertEqual(dict_two_keys, body)
228
229 def test_parse_resp_one_top_key_without_list_or_dict(self):
230 data = {"one_top_key": "not_list_or_dict_value"}
231 body = self.rest_client._parse_resp(json.dumps(data))
232 self.assertEqual(data, body)