summaryrefslogtreecommitdiff
path: root/glanceclient/tests/unit/v2/test_metadefs_objects.py
blob: bc3b6694099215c091ec8a5cc2a0eeb3510cb416 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# Copyright 2012 OpenStack Foundation.
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import testtools

from glanceclient.tests.unit.v2 import base
from glanceclient.tests import utils
from glanceclient.v2 import metadefs

NAMESPACE1 = 'Namespace1'
OBJECT1 = 'Object1'
OBJECT2 = 'Object2'
OBJECTNEW = 'ObjectNew'
PROPERTY1 = 'Property1'
PROPERTY2 = 'Property2'
PROPERTY3 = 'Property3'
PROPERTY4 = 'Property4'


def _get_object_fixture(ns_name, obj_name, **kwargs):
    obj = {
        "description": "DESCRIPTION",
        "name": obj_name,
        "self": "/v2/metadefs/namespaces/%s/objects/%s" %
                (ns_name, obj_name),
        "required": [],
        "properties": {
            PROPERTY1: {
                "type": "integer",
                "description": "DESCRIPTION",
                "title": "Quota: CPU Shares"
            },
            PROPERTY2: {
                "minimum": 1000,
                "type": "integer",
                "description": "DESCRIPTION",
                "maximum": 1000000,
                "title": "Quota: CPU Period"
            }},
        "schema": "/v2/schemas/metadefs/object",
        "created_at": "2014-08-14T09:07:06Z",
        "updated_at": "2014-08-14T09:07:06Z",
    }

    obj.update(kwargs)

    return obj


data_fixtures = {
    "/v2/metadefs/namespaces/%s/objects" % NAMESPACE1: {
        "GET": (
            {},
            {
                "objects": [
                    _get_object_fixture(NAMESPACE1, OBJECT1),
                    _get_object_fixture(NAMESPACE1, OBJECT2)
                ],
                "schema": "v2/schemas/metadefs/objects"
            }
        ),
        "POST": (
            {},
            _get_object_fixture(NAMESPACE1, OBJECTNEW)
        ),
        "DELETE": (
            {},
            {}
        )
    },
    "/v2/metadefs/namespaces/%s/objects/%s" % (NAMESPACE1, OBJECT1): {
        "GET": (
            {},
            _get_object_fixture(NAMESPACE1, OBJECT1)
        ),
        "PUT": (
            {},
            _get_object_fixture(NAMESPACE1, OBJECT1)
        ),
        "DELETE": (
            {},
            {}
        )
    }
}

schema_fixtures = {
    "metadefs/object": {
        "GET": (
            {},
            {
                "additionalProperties": False,
                "definitions": {
                    "property": {
                        "additionalProperties": {
                            "required": [
                                "title",
                                "type"
                            ],
                            "type": "object",
                            "properties": {
                                "additionalItems": {
                                    "type": "boolean"
                                },
                                "enum": {
                                    "type": "array"
                                },
                                "description": {
                                    "type": "string"
                                },
                                "title": {
                                    "type": "string"
                                },
                                "default": {},
                                "minLength": {
                                    "$ref": "#/definitions/positiveInteger"
                                            "Default0"
                                },
                                "required": {
                                    "$ref": "#/definitions/stringArray"
                                },
                                "maximum": {
                                    "type": "number"
                                },
                                "minItems": {
                                    "$ref": "#/definitions/positiveInteger"
                                            "Default0"
                                },
                                "readonly": {
                                    "type": "boolean"
                                },
                                "minimum": {
                                    "type": "number"
                                },
                                "maxItems": {
                                    "$ref": "#/definitions/positiveInteger"
                                },
                                "maxLength": {
                                    "$ref": "#/definitions/positiveInteger"
                                },
                                "uniqueItems": {
                                    "default": False,
                                    "type": "boolean"
                                },
                                "pattern": {
                                    "type": "string",
                                    "format": "regex"
                                },
                                "items": {
                                    "type": "object",
                                    "properties": {
                                        "enum": {
                                            "type": "array"
                                        },
                                        "type": {
                                            "enum": [
                                                "array",
                                                "boolean",
                                                "integer",
                                                "number",
                                                "object",
                                                "string",
                                                "null"
                                            ],
                                            "type": "string"
                                        }
                                    }
                                },
                                "type": {
                                    "enum": [
                                        "array",
                                        "boolean",
                                        "integer",
                                        "number",
                                        "object",
                                        "string",
                                        "null"
                                    ],
                                    "type": "string"
                                }
                            }
                        },
                        "type": "object"
                    },
                    "positiveIntegerDefault0": {
                        "allOf": [
                            {
                                "$ref": "#/definitions/positiveInteger"
                            },
                            {
                                "default": 0
                            }
                        ]
                    },
                    "stringArray": {
                        "uniqueItems": True,
                        "items": {
                            "type": "string"
                        },
                        "type": "array"
                    },
                    "positiveInteger": {
                        "minimum": 0,
                        "type": "integer"
                    }
                },
                "required": [
                    "name"
                ],
                "name": "object",
                "properties": {
                    "created_at": {
                        "type": "string",
                        "readOnly": True,
                        "description": "Date and time of object creation ",
                        "format": "date-time"
                    },
                    "description": {
                        "type": "string"
                    },
                    "name": {
                        "type": "string"
                    },
                    "self": {
                        "type": "string"
                    },
                    "required": {
                        "$ref": "#/definitions/stringArray"
                    },
                    "properties": {
                        "$ref": "#/definitions/property"
                    },
                    "schema": {
                        "type": "string"
                    },
                    "updated_at": {
                        "type": "string",
                        "readOnly": True,
                        "description": "Date and time of the last object "
                                       "modification",
                        "format": "date-time"
                    },
                }
            }
        )
    }
}


class TestObjectController(testtools.TestCase):
    def setUp(self):
        super(TestObjectController, self).setUp()
        self.api = utils.FakeAPI(data_fixtures)
        self.schema_api = utils.FakeSchemaAPI(schema_fixtures)
        self.controller = base.BaseController(self.api, self.schema_api,
                                              metadefs.ObjectController)

    def test_list_object(self):
        objects = self.controller.list(NAMESPACE1)
        actual = [obj.name for obj in objects]
        self.assertEqual([OBJECT1, OBJECT2], actual)

    def test_get_object(self):
        obj = self.controller.get(NAMESPACE1, OBJECT1)
        self.assertEqual(OBJECT1, obj.name)
        self.assertEqual(sorted([PROPERTY1, PROPERTY2]),
                         sorted(list(obj.properties.keys())))

    def test_create_object(self):
        properties = {
            'name': OBJECTNEW,
            'description': 'DESCRIPTION'
        }
        obj = self.controller.create(NAMESPACE1, **properties)
        self.assertEqual(OBJECTNEW, obj.name)

    def test_create_object_invalid_property(self):
        properties = {
            'namespace': NAMESPACE1
        }
        self.assertRaises(TypeError, self.controller.create, **properties)

    def test_update_object(self):
        properties = {
            'description': 'UPDATED_DESCRIPTION'
        }
        obj = self.controller.update(NAMESPACE1, OBJECT1, **properties)
        self.assertEqual(OBJECT1, obj.name)

    def test_update_object_invalid_property(self):
        properties = {
            'required': 'INVALID'
        }
        self.assertRaises(TypeError, self.controller.update, NAMESPACE1,
                          OBJECT1, **properties)

    def test_update_object_disallowed_fields(self):
        properties = {
            'description': 'UPDATED_DESCRIPTION'
        }
        self.controller.update(NAMESPACE1, OBJECT1, **properties)
        actual = self.api.calls
        # API makes three calls(GET, PUT, GET) for object update.
        # PUT has the request body in the list
        '''('PUT', '/v2/metadefs/namespaces/Namespace1/objects/Object1', {},
        [('description', 'UPDATED_DESCRIPTION'),
        ('name', 'Object1'),
        ('properties', ...),
        ('required', [])])'''

        _disallowed_fields = ['self', 'schema', 'created_at', 'updated_at']
        for key in actual[1][3]:
            self.assertNotIn(key, _disallowed_fields)

    def test_delete_object(self):
        self.controller.delete(NAMESPACE1, OBJECT1)
        expect = [
            ('DELETE',
             '/v2/metadefs/namespaces/%s/objects/%s' % (NAMESPACE1, OBJECT1),
             {},
             None)]
        self.assertEqual(expect, self.api.calls)

    def test_delete_all_objects(self):
        self.controller.delete_all(NAMESPACE1)
        expect = [
            ('DELETE',
             '/v2/metadefs/namespaces/%s/objects' % NAMESPACE1,
             {},
             None)]
        self.assertEqual(expect, self.api.calls)