| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 |
PROPERTY1 = 'Property1' |
| 24 |
PROPERTY2 = 'Property2' |
| 25 |
PROPERTYNEW = 'PropertyNew' |
| 26 |
|
| 27 |
data_fixtures = { |
| 28 |
"/v2/metadefs/namespaces/%s/properties" % NAMESPACE1: { |
| 29 |
"GET": ( |
| 30 |
{}, |
| 31 |
{ |
| 32 |
"properties": { |
| 33 |
PROPERTY1: { |
| 34 |
"default": "1", |
| 35 |
"type": "integer", |
| 36 |
"description": "Number of cores.", |
| 37 |
"title": "cores" |
| 38 |
}, |
| 39 |
PROPERTY2: { |
| 40 |
"items": { |
| 41 |
"enum": [ |
| 42 |
"Intel", |
| 43 |
"AMD" |
| 44 |
], |
| 45 |
"type": "string" |
| 46 |
}, |
| 47 |
"type": "array", |
| 48 |
"description": "Specifies the CPU manufacturer.", |
| 49 |
"title": "Vendor" |
| 50 |
}, |
| 51 |
} |
| 52 |
} |
| 53 |
), |
| 54 |
"POST": ( |
| 55 |
{}, |
| 56 |
{ |
| 57 |
"items": { |
| 58 |
"enum": [ |
| 59 |
"Intel", |
| 60 |
"AMD" |
| 61 |
], |
| 62 |
"type": "string" |
| 63 |
}, |
| 64 |
"type": "array", |
| 65 |
"description": "UPDATED_DESCRIPTION", |
| 66 |
"title": "Vendor", |
| 67 |
"name": PROPERTYNEW |
| 68 |
} |
| 69 |
), |
| 70 |
"DELETE": ( |
| 71 |
{}, |
| 72 |
{} |
| 73 |
) |
| 74 |
}, |
| 75 |
"/v2/metadefs/namespaces/%s/properties/%s" % (NAMESPACE1, PROPERTY1): { |
| 76 |
"GET": ( |
| 77 |
{}, |
| 78 |
{ |
| 79 |
"items": { |
| 80 |
"enum": [ |
| 81 |
"Intel", |
| 82 |
"AMD" |
| 83 |
], |
| 84 |
"type": "string" |
| 85 |
}, |
| 86 |
"type": "array", |
| 87 |
"description": "Specifies the CPU manufacturer.", |
| 88 |
"title": "Vendor" |
| 89 |
} |
| 90 |
), |
| 91 |
"PUT": ( |
| 92 |
{}, |
| 93 |
{ |
| 94 |
"items": { |
| 95 |
"enum": [ |
| 96 |
"Intel", |
| 97 |
"AMD" |
| 98 |
], |
| 99 |
"type": "string" |
| 100 |
}, |
| 101 |
"type": "array", |
| 102 |
"description": "UPDATED_DESCRIPTION", |
| 103 |
"title": "Vendor" |
| 104 |
} |
| 105 |
), |
| 106 |
"DELETE": ( |
| 107 |
{}, |
| 108 |
{} |
| 109 |
) |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
schema_fixtures = { |
| 114 |
"metadefs/property": { |
| 115 |
"GET": ( |
| 116 |
{}, |
| 117 |
{ |
| 118 |
"additionalProperties": False, |
| 119 |
"definitions": { |
| 120 |
"positiveIntegerDefault0": { |
| 121 |
"allOf": [ |
| 122 |
{ |
| 123 |
"$ref": "#/definitions/positiveInteger" |
| 124 |
}, |
| 125 |
{ |
| 126 |
"default": 0 |
| 127 |
} |
| 128 |
] |
| 129 |
}, |
| 130 |
"stringArray": { |
| 131 |
"minItems": 1, |
| 132 |
"items": { |
| 133 |
"type": "string" |
| 134 |
}, |
| 135 |
"uniqueItems": True, |
| 136 |
"type": "array" |
| 137 |
}, |
| 138 |
"positiveInteger": { |
| 139 |
"minimum": 0, |
| 140 |
"type": "integer" |
| 141 |
} |
| 142 |
}, |
| 143 |
"required": [ |
| 144 |
"name", |
| 145 |
"title", |
| 146 |
"type" |
| 147 |
], |
| 148 |
"name": "property", |
| 149 |
"properties": { |
| 150 |
"description": { |
| 151 |
"type": "string" |
| 152 |
}, |
| 153 |
"minLength": { |
| 154 |
"$ref": "#/definitions/positiveIntegerDefault0" |
| 155 |
}, |
| 156 |
"enum": { |
| 157 |
"type": "array" |
| 158 |
}, |
| 159 |
"minimum": { |
| 160 |
"type": "number" |
| 161 |
}, |
| 162 |
"maxItems": { |
| 163 |
"$ref": "#/definitions/positiveInteger" |
| 164 |
}, |
| 165 |
"maxLength": { |
| 166 |
"$ref": "#/definitions/positiveInteger" |
| 167 |
}, |
| 168 |
"uniqueItems": { |
| 169 |
"default": False, |
| 170 |
"type": "boolean" |
| 171 |
}, |
| 172 |
"additionalItems": { |
| 173 |
"type": "boolean" |
| 174 |
}, |
| 175 |
"name": { |
| 176 |
"type": "string" |
| 177 |
}, |
| 178 |
"title": { |
| 179 |
"type": "string" |
| 180 |
}, |
| 181 |
"default": {}, |
| 182 |
"pattern": { |
| 183 |
"type": "string", |
| 184 |
"format": "regex" |
| 185 |
}, |
| 186 |
"required": { |
| 187 |
"$ref": "#/definitions/stringArray" |
| 188 |
}, |
| 189 |
"maximum": { |
| 190 |
"type": "number" |
| 191 |
}, |
| 192 |
"minItems": { |
| 193 |
"$ref": "#/definitions/positiveIntegerDefault0" |
| 194 |
}, |
| 195 |
"readonly": { |
| 196 |
"type": "boolean" |
| 197 |
}, |
| 198 |
"items": { |
| 199 |
"type": "object", |
| 200 |
"properties": { |
| 201 |
"enum": { |
| 202 |
"type": "array" |
| 203 |
}, |
| 204 |
"type": { |
| 205 |
"enum": [ |
| 206 |
"array", |
| 207 |
"boolean", |
| 208 |
"integer", |
| 209 |
"number", |
| 210 |
"object", |
| 211 |
"string", |
| 212 |
"null" |
| 213 |
], |
| 214 |
"type": "string" |
| 215 |
} |
| 216 |
} |
| 217 |
}, |
| 218 |
"type": { |
| 219 |
"enum": [ |
| 220 |
"array", |
| 221 |
"boolean", |
| 222 |
"integer", |
| 223 |
"number", |
| 224 |
"object", |
| 225 |
"string", |
| 226 |
"null" |
| 227 |
], |
| 228 |
"type": "string" |
| 229 |
} |
| 230 |
} |
| 231 |
} |
| 232 |
) |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
class TestPropertyController(testtools.TestCase): |
| 238 |
def setUp(self): |
| 239 |
super(TestPropertyController, self).setUp() |
| 240 |
self.api = utils.FakeAPI(data_fixtures) |
| 241 |
self.schema_api = utils.FakeSchemaAPI(schema_fixtures) |
| 242 |
self.controller = base.BaseController(self.api, self.schema_api, |
| 243 |
metadefs.PropertyController) |
| 244 |
|
| 245 |
def test_list_property(self): |
| 246 |
properties = self.controller.list(NAMESPACE1) |
| 247 |
actual = [prop.name for prop in properties] |
| 248 |
self.assertEqual(sorted([PROPERTY1, PROPERTY2]), sorted(actual)) |
| 249 |
|
| 250 |
def test_get_property(self): |
| 251 |
prop = self.controller.get(NAMESPACE1, PROPERTY1) |
| 252 |
self.assertEqual(PROPERTY1, prop.name) |
| 253 |
|
| 254 |
def test_create_property(self): |
| 255 |
properties = { |
| 256 |
'name': PROPERTYNEW, |
| 257 |
'title': 'TITLE', |
| 258 |
'type': 'string' |
| 259 |
} |
| 260 |
obj = self.controller.create(NAMESPACE1, **properties) |
| 261 |
self.assertEqual(PROPERTYNEW, obj.name) |
| 262 |
|
| 263 |
def test_create_property_invalid_property(self): |
| 264 |
properties = { |
| 265 |
'namespace': NAMESPACE1 |
| 266 |
} |
| 267 |
self.assertRaises(TypeError, self.controller.create, **properties) |
| 268 |
|
| 269 |
def test_update_property(self): |
| 270 |
properties = { |
| 271 |
'description': 'UPDATED_DESCRIPTION' |
| 272 |
} |
| 273 |
prop = self.controller.update(NAMESPACE1, PROPERTY1, **properties) |
| 274 |
self.assertEqual(PROPERTY1, prop.name) |
| 275 |
|
| 276 |
def test_update_property_invalid_property(self): |
| 277 |
properties = { |
| 278 |
'type': 'INVALID' |
| 279 |
} |
| 280 |
self.assertRaises(TypeError, self.controller.update, NAMESPACE1, |
| 281 |
PROPERTY1, **properties) |
| 282 |
|
| 283 |
def test_update_property_disallowed_fields(self): |
| 284 |
properties = { |
| 285 |
'description': 'UPDATED_DESCRIPTION' |
| 286 |
} |
| 287 |
self.controller.update(NAMESPACE1, PROPERTY1, **properties) |
| 288 |
actual = self.api.calls |
| 289 |
_disallowed_fields = ['created_at', 'updated_at'] |
| 290 |
for key in actual[1][3]: |
| 291 |
self.assertNotIn(key, _disallowed_fields) |
| 292 |
|
| 293 |
def test_delete_property(self): |
| 294 |
self.controller.delete(NAMESPACE1, PROPERTY1) |
| 295 |
expect = [ |
| 296 |
('DELETE', |
| 297 |
'/v2/metadefs/namespaces/%s/properties/%s' % (NAMESPACE1, |
| 298 |
PROPERTY1), |
| 299 |
{}, |
| 300 |
None)] |
| 301 |
self.assertEqual(expect, self.api.calls) |
| 302 |
|
| 303 |
def test_delete_all_properties(self): |
| 304 |
self.controller.delete_all(NAMESPACE1) |
| 305 |
expect = [ |
| 306 |
('DELETE', |
| 307 |
'/v2/metadefs/namespaces/%s/properties' % NAMESPACE1, |
| 308 |
{}, |
| 309 |
None)] |
| 310 |
self.assertEqual(expect, self.api.calls) |