Debian Codegraph @

glanceclient/tests/unit/v2/test_metadefs_objects.py

raw symbols SCIP
1 # Copyright 2012 OpenStack Foundation.
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
16 import testtools
17
18 from glanceclient.tests.unit.v2 import base
19 from glanceclient.tests import utils
20 from glanceclient.v2 import metadefs
21
22 NAMESPACE1 = 'Namespace1'
23 OBJECT1 = 'Object1'
24 OBJECT2 = 'Object2'
25 OBJECTNEW = 'ObjectNew'
26 PROPERTY1 = 'Property1'
27 PROPERTY2 = 'Property2'
28 PROPERTY3 = 'Property3'
29 PROPERTY4 = 'Property4'
30
31
32 def _get_object_fixture(ns_name, obj_name, **kwargs):
33 obj = {
34 "description": "DESCRIPTION",
35 "name": obj_name,
36 "self": "/v2/metadefs/namespaces/%s/objects/%s" %
37 (ns_name, obj_name),
38 "required": [],
39 "properties": {
40 PROPERTY1: {
41 "type": "integer",
42 "description": "DESCRIPTION",
43 "title": "Quota: CPU Shares"
44 },
45 PROPERTY2: {
46 "minimum": 1000,
47 "type": "integer",
48 "description": "DESCRIPTION",
49 "maximum": 1000000,
50 "title": "Quota: CPU Period"
51 }},
52 "schema": "/v2/schemas/metadefs/object",
53 "created_at": "2014-08-14T09:07:06Z",
54 "updated_at": "2014-08-14T09:07:06Z",
55 }
56
57 obj.update(kwargs)
58
59 return obj
60
61
62 data_fixtures = {
63 "/v2/metadefs/namespaces/%s/objects" % NAMESPACE1: {
64 "GET": (
65 {},
66 {
67 "objects": [
68 _get_object_fixture(NAMESPACE1, OBJECT1),
69 _get_object_fixture(NAMESPACE1, OBJECT2)
70 ],
71 "schema": "v2/schemas/metadefs/objects"
72 }
73 ),
74 "POST": (
75 {},
76 _get_object_fixture(NAMESPACE1, OBJECTNEW)
77 ),
78 "DELETE": (
79 {},
80 {}
81 )
82 },
83 "/v2/metadefs/namespaces/%s/objects/%s" % (NAMESPACE1, OBJECT1): {
84 "GET": (
85 {},
86 _get_object_fixture(NAMESPACE1, OBJECT1)
87 ),
88 "PUT": (
89 {},
90 _get_object_fixture(NAMESPACE1, OBJECT1)
91 ),
92 "DELETE": (
93 {},
94 {}
95 )
96 }
97 }
98
99 schema_fixtures = {
100 "metadefs/object": {
101 "GET": (
102 {},
103 {
104 "additionalProperties": False,
105 "definitions": {
106 "property": {
107 "additionalProperties": {
108 "required": [
109 "title",
110 "type"
111 ],
112 "type": "object",
113 "properties": {
114 "additionalItems": {
115 "type": "boolean"
116 },
117 "enum": {
118 "type": "array"
119 },
120 "description": {
121 "type": "string"
122 },
123 "title": {
124 "type": "string"
125 },
126 "default": {},
127 "minLength": {
128 "$ref": "#/definitions/positiveInteger"
129 "Default0"
130 },
131 "required": {
132 "$ref": "#/definitions/stringArray"
133 },
134 "maximum": {
135 "type": "number"
136 },
137 "minItems": {
138 "$ref": "#/definitions/positiveInteger"
139 "Default0"
140 },
141 "readonly": {
142 "type": "boolean"
143 },
144 "minimum": {
145 "type": "number"
146 },
147 "maxItems": {
148 "$ref": "#/definitions/positiveInteger"
149 },
150 "maxLength": {
151 "$ref": "#/definitions/positiveInteger"
152 },
153 "uniqueItems": {
154 "default": False,
155 "type": "boolean"
156 },
157 "pattern": {
158 "type": "string",
159 "format": "regex"
160 },
161 "items": {
162 "type": "object",
163 "properties": {
164 "enum": {
165 "type": "array"
166 },
167 "type": {
168 "enum": [
169 "array",
170 "boolean",
171 "integer",
172 "number",
173 "object",
174 "string",
175 "null"
176 ],
177 "type": "string"
178 }
179 }
180 },
181 "type": {
182 "enum": [
183 "array",
184 "boolean",
185 "integer",
186 "number",
187 "object",
188 "string",
189 "null"
190 ],
191 "type": "string"
192 }
193 }
194 },
195 "type": "object"
196 },
197 "positiveIntegerDefault0": {
198 "allOf": [
199 {
200 "$ref": "#/definitions/positiveInteger"
201 },
202 {
203 "default": 0
204 }
205 ]
206 },
207 "stringArray": {
208 "uniqueItems": True,
209 "items": {
210 "type": "string"
211 },
212 "type": "array"
213 },
214 "positiveInteger": {
215 "minimum": 0,
216 "type": "integer"
217 }
218 },
219 "required": [
220 "name"
221 ],
222 "name": "object",
223 "properties": {
224 "created_at": {
225 "type": "string",
226 "readOnly": True,
227 "description": "Date and time of object creation ",
228 "format": "date-time"
229 },
230 "description": {
231 "type": "string"
232 },
233 "name": {
234 "type": "string"
235 },
236 "self": {
237 "type": "string"
238 },
239 "required": {
240 "$ref": "#/definitions/stringArray"
241 },
242 "properties": {
243 "$ref": "#/definitions/property"
244 },
245 "schema": {
246 "type": "string"
247 },
248 "updated_at": {
249 "type": "string",
250 "readOnly": True,
251 "description": "Date and time of the last object "
252 "modification",
253 "format": "date-time"
254 },
255 }
256 }
257 )
258 }
259 }
260
261
262 class TestObjectController(testtools.TestCase):
263 def setUp(self):
264 super(TestObjectController, self).setUp()
265 self.api = utils.FakeAPI(data_fixtures)
266 self.schema_api = utils.FakeSchemaAPI(schema_fixtures)
267 self.controller = base.BaseController(self.api, self.schema_api,
268 metadefs.ObjectController)
269
270 def test_list_object(self):
271 objects = self.controller.list(NAMESPACE1)
272 actual = [obj.name for obj in objects]
273 self.assertEqual([OBJECT1, OBJECT2], actual)
274
275 def test_get_object(self):
276 obj = self.controller.get(NAMESPACE1, OBJECT1)
277 self.assertEqual(OBJECT1, obj.name)
278 self.assertEqual(sorted([PROPERTY1, PROPERTY2]),
279 sorted(list(obj.properties.keys())))
280
281 def test_create_object(self):
282 properties = {
283 'name': OBJECTNEW,
284 'description': 'DESCRIPTION'
285 }
286 obj = self.controller.create(NAMESPACE1, **properties)
287 self.assertEqual(OBJECTNEW, obj.name)
288
289 def test_create_object_invalid_property(self):
290 properties = {
291 'namespace': NAMESPACE1
292 }
293 self.assertRaises(TypeError, self.controller.create, **properties)
294
295 def test_update_object(self):
296 properties = {
297 'description': 'UPDATED_DESCRIPTION'
298 }
299 obj = self.controller.update(NAMESPACE1, OBJECT1, **properties)
300 self.assertEqual(OBJECT1, obj.name)
301
302 def test_update_object_invalid_property(self):
303 properties = {
304 'required': 'INVALID'
305 }
306 self.assertRaises(TypeError, self.controller.update, NAMESPACE1,
307 OBJECT1, **properties)
308
309 def test_update_object_disallowed_fields(self):
310 properties = {
311 'description': 'UPDATED_DESCRIPTION'
312 }
313 self.controller.update(NAMESPACE1, OBJECT1, **properties)
314 actual = self.api.calls
315 # API makes three calls(GET, PUT, GET) for object update.
316 # PUT has the request body in the list
317 '''('PUT', '/v2/metadefs/namespaces/Namespace1/objects/Object1', {},
318 [('description', 'UPDATED_DESCRIPTION'),
319 ('name', 'Object1'),
320 ('properties', ...),
321 ('required', [])])'''
322
323 _disallowed_fields = ['self', 'schema', 'created_at', 'updated_at']
324 for key in actual[1][3]:
325 self.assertNotIn(key, _disallowed_fields)
326
327 def test_delete_object(self):
328 self.controller.delete(NAMESPACE1, OBJECT1)
329 expect = [
330 ('DELETE',
331 '/v2/metadefs/namespaces/%s/objects/%s' % (NAMESPACE1, OBJECT1),
332 {},
333 None)]
334 self.assertEqual(expect, self.api.calls)
335
336 def test_delete_all_objects(self):
337 self.controller.delete_all(NAMESPACE1)
338 expect = [
339 ('DELETE',
340 '/v2/metadefs/namespaces/%s/objects' % NAMESPACE1,
341 {},
342 None)]
343 self.assertEqual(expect, self.api.calls)