~hudson-openstack/nova/trunk

« back to all changes in this revision

Viewing changes to nova/tests/api/openstack/test_flavors.py

  • Committer: Tarmac
  • Author(s): Rick Harris
  • Date: 2011-09-22 16:42:20 UTC
  • mfrom: (1590.1.11 flavor_min_filter)
  • Revision ID: tarmac-20110922164220-ndscikwberuj3c7f
This patch adds flavor filtering, specifically the ability to flavor on minRam, minDisk, or both, per the 1.1 OSAPI spec.

In addition, this patch refactors instance_type_get_all to return a *list* of instance_types instead of a *dict*. This makes it more consistent with the rest of the DB API.

Show diffs side-by-side

added added

removed removed

Lines of Context:
32
32
ATOMNS = "{http://www.w3.org/2005/Atom}"
33
33
 
34
34
 
35
 
def stub_flavor(flavorid, name, memory_mb="256", local_gb="10"):
36
 
    return {
37
 
        "flavorid": str(flavorid),
38
 
        "name": name,
39
 
        "memory_mb": memory_mb,
40
 
        "local_gb": local_gb,
41
 
    }
42
 
 
43
 
 
44
 
def return_instance_type_by_flavor_id(context, flavorid):
45
 
    return stub_flavor(flavorid, "flavor %s" % (flavorid,))
46
 
 
47
 
 
48
 
def return_instance_types(context, num=2):
49
 
    instance_types = {}
50
 
    for i in xrange(1, num + 1):
51
 
        name = "flavor %s" % (i,)
52
 
        instance_types[name] = stub_flavor(i, name)
53
 
    return instance_types
 
35
FAKE_FLAVORS = {
 
36
    'flavor 1': {
 
37
        "flavorid": '1',
 
38
        "name": 'flavor 1',
 
39
        "memory_mb": '256',
 
40
        "local_gb": '10'
 
41
    },
 
42
    'flavor 2': {
 
43
        "flavorid": '2',
 
44
        "name": 'flavor 2',
 
45
        "memory_mb": '512',
 
46
        "local_gb": '20'
 
47
    },
 
48
}
 
49
 
 
50
 
 
51
def fake_instance_type_get_by_flavor_id(context, flavorid):
 
52
    return FAKE_FLAVORS['flavor %s' % flavorid]
 
53
 
 
54
 
 
55
def fake_instance_type_get_all(context, inactive=False, filters=None):
 
56
    def reject_min(db_attr, filter_attr):
 
57
        return filter_attr in filters and\
 
58
               int(flavor[db_attr]) < int(filters[filter_attr])
 
59
 
 
60
    filters = filters or {}
 
61
    for flavor in FAKE_FLAVORS.values():
 
62
        if reject_min('memory_mb', 'min_memory_mb'):
 
63
            continue
 
64
        elif reject_min('local_gb', 'min_local_gb'):
 
65
            continue
 
66
 
 
67
        yield flavor
 
68
 
 
69
 
 
70
def empty_instance_type_get_all(context, inactive=False, filters=None):
 
71
    return {}
54
72
 
55
73
 
56
74
def return_instance_type_not_found(context, flavor_id):
63
81
        fakes.stub_out_networking(self.stubs)
64
82
        fakes.stub_out_rate_limiting(self.stubs)
65
83
        self.stubs.Set(nova.db.api, "instance_type_get_all",
66
 
                       return_instance_types)
 
84
                       fake_instance_type_get_all)
67
85
        self.stubs.Set(nova.db.api, "instance_type_get_by_flavor_id",
68
 
                       return_instance_type_by_flavor_id)
 
86
                       fake_instance_type_get_by_flavor_id)
69
87
 
70
88
    def tearDown(self):
71
89
        self.stubs.UnsetAll()
89
107
        self.assertEqual(flavors, expected)
90
108
 
91
109
    def test_get_empty_flavor_list_v1_0(self):
92
 
        def _return_empty(self):
93
 
            return {}
94
110
        self.stubs.Set(nova.db.api, "instance_type_get_all",
95
 
                       _return_empty)
 
111
                       empty_instance_type_get_all)
96
112
 
97
113
        req = webob.Request.blank('/v1.0/flavors')
98
114
        res = req.get_response(fakes.wsgi_app())
120
136
            {
121
137
                "id": "2",
122
138
                "name": "flavor 2",
123
 
                "ram": "256",
124
 
                "disk": "10",
 
139
                "ram": "512",
 
140
                "disk": "20",
125
141
                "rxtx_cap": "",
126
142
                "rxtx_quota": "",
127
143
                "swap": "",
131
147
        self.assertEqual(flavors, expected)
132
148
 
133
149
    def test_get_flavor_by_id_v1_0(self):
134
 
        req = webob.Request.blank('/v1.0/flavors/12')
 
150
        req = webob.Request.blank('/v1.0/flavors/1')
135
151
        res = req.get_response(fakes.wsgi_app())
136
152
        self.assertEqual(res.status_int, 200)
137
153
        flavor = json.loads(res.body)["flavor"]
138
154
        expected = {
139
 
            "id": "12",
140
 
            "name": "flavor 12",
 
155
            "id": "1",
 
156
            "name": "flavor 1",
141
157
            "ram": "256",
142
158
            "disk": "10",
143
159
            "rxtx_cap": "",
155
171
        self.assertEqual(res.status_int, 404)
156
172
 
157
173
    def test_get_flavor_by_id_v1_1(self):
158
 
        req = webob.Request.blank('/v1.1/fake/flavors/12')
 
174
        req = webob.Request.blank('/v1.1/fake/flavors/1')
159
175
        req.environ['api.version'] = '1.1'
160
176
        res = req.get_response(fakes.wsgi_app())
161
177
        self.assertEqual(res.status_int, 200)
162
178
        flavor = json.loads(res.body)
163
179
        expected = {
164
180
            "flavor": {
165
 
                "id": "12",
166
 
                "name": "flavor 12",
 
181
                "id": "1",
 
182
                "name": "flavor 1",
167
183
                "ram": "256",
168
184
                "disk": "10",
169
185
                "rxtx_cap": "",
173
189
                "links": [
174
190
                    {
175
191
                        "rel": "self",
176
 
                        "href": "http://localhost/v1.1/fake/flavors/12",
 
192
                        "href": "http://localhost/v1.1/fake/flavors/1",
177
193
                    },
178
194
                    {
179
195
                        "rel": "bookmark",
180
 
                        "href": "http://localhost/fake/flavors/12",
 
196
                        "href": "http://localhost/fake/flavors/1",
181
197
                    },
182
198
                ],
183
199
            },
255
271
                {
256
272
                    "id": "2",
257
273
                    "name": "flavor 2",
258
 
                    "ram": "256",
259
 
                    "disk": "10",
 
274
                    "ram": "512",
 
275
                    "disk": "20",
260
276
                    "rxtx_cap": "",
261
277
                    "rxtx_quota": "",
262
278
                    "swap": "",
277
293
        self.assertEqual(flavor, expected)
278
294
 
279
295
    def test_get_empty_flavor_list_v1_1(self):
280
 
        def _return_empty(self):
281
 
            return {}
282
 
        self.stubs.Set(nova.db.api, "instance_type_get_all", _return_empty)
 
296
        self.stubs.Set(nova.db.api, "instance_type_get_all",
 
297
                       empty_instance_type_get_all)
283
298
 
284
299
        req = webob.Request.blank('/v1.1/fake/flavors')
285
300
        res = req.get_response(fakes.wsgi_app())
288
303
        expected = []
289
304
        self.assertEqual(flavors, expected)
290
305
 
 
306
    def test_get_flavor_list_filter_min_ram_v1_1(self):
 
307
        """Flavor lists may be filtered by minRam"""
 
308
        req = webob.Request.blank('/v1.1/fake/flavors?minRam=512')
 
309
        req.environ['api.version'] = '1.1'
 
310
        res = req.get_response(fakes.wsgi_app())
 
311
        self.assertEqual(res.status_int, 200)
 
312
        flavor = json.loads(res.body)
 
313
        expected = {
 
314
            "flavors": [
 
315
                {
 
316
                    "id": "2",
 
317
                    "name": "flavor 2",
 
318
                    "links": [
 
319
                        {
 
320
                            "rel": "self",
 
321
                            "href": "http://localhost/v1.1/fake/flavors/2",
 
322
                        },
 
323
                        {
 
324
                            "rel": "bookmark",
 
325
                            "href": "http://localhost/fake/flavors/2",
 
326
                        },
 
327
                    ],
 
328
                },
 
329
            ],
 
330
        }
 
331
        self.assertEqual(flavor, expected)
 
332
 
 
333
    def test_get_flavor_list_filter_min_disk(self):
 
334
        """Flavor lists may be filtered by minRam"""
 
335
        req = webob.Request.blank('/v1.1/fake/flavors?minDisk=20')
 
336
        req.environ['api.version'] = '1.1'
 
337
        res = req.get_response(fakes.wsgi_app())
 
338
        self.assertEqual(res.status_int, 200)
 
339
        flavor = json.loads(res.body)
 
340
        expected = {
 
341
            "flavors": [
 
342
                {
 
343
                    "id": "2",
 
344
                    "name": "flavor 2",
 
345
                    "links": [
 
346
                        {
 
347
                            "rel": "self",
 
348
                            "href": "http://localhost/v1.1/fake/flavors/2",
 
349
                        },
 
350
                        {
 
351
                            "rel": "bookmark",
 
352
                            "href": "http://localhost/fake/flavors/2",
 
353
                        },
 
354
                    ],
 
355
                },
 
356
            ],
 
357
        }
 
358
        self.assertEqual(flavor, expected)
 
359
 
 
360
    def test_get_flavor_list_detail_min_ram_and_min_disk_v1_1(self):
 
361
        """Tests that filtering work on flavor details and that minRam and
 
362
        minDisk filters can be combined
 
363
        """
 
364
        req = webob.Request.blank(
 
365
            '/v1.1/fake/flavors/detail?minRam=256&minDisk=20')
 
366
        req.environ['api.version'] = '1.1'
 
367
        res = req.get_response(fakes.wsgi_app())
 
368
        self.assertEqual(res.status_int, 200)
 
369
        flavor = json.loads(res.body)
 
370
        expected = {
 
371
            "flavors": [
 
372
                {
 
373
                    "id": "2",
 
374
                    "name": "flavor 2",
 
375
                    "ram": "512",
 
376
                    "disk": "20",
 
377
                    "rxtx_cap": "",
 
378
                    "rxtx_quota": "",
 
379
                    "swap": "",
 
380
                    "vcpus": "",
 
381
                    "links": [
 
382
                        {
 
383
                            "rel": "self",
 
384
                            "href": "http://localhost/v1.1/fake/flavors/2",
 
385
                        },
 
386
                        {
 
387
                            "rel": "bookmark",
 
388
                            "href": "http://localhost/fake/flavors/2",
 
389
                        },
 
390
                    ],
 
391
                },
 
392
            ],
 
393
        }
 
394
        self.assertEqual(flavor, expected)
 
395
 
 
396
    def test_get_flavor_list_detail_bogus_min_ram_v1_1(self):
 
397
        """Tests that bogus minRam filtering values are ignored"""
 
398
        req = webob.Request.blank(
 
399
            '/v1.1/fake/flavors/detail?minRam=16GB')
 
400
        req.environ['api.version'] = '1.1'
 
401
        res = req.get_response(fakes.wsgi_app())
 
402
        self.assertEqual(res.status_int, 200)
 
403
        flavor = json.loads(res.body)
 
404
        expected = {
 
405
            "flavors": [
 
406
                {
 
407
                    "id": "1",
 
408
                    "name": "flavor 1",
 
409
                    "ram": "256",
 
410
                    "disk": "10",
 
411
                    "rxtx_cap": "",
 
412
                    "rxtx_quota": "",
 
413
                    "swap": "",
 
414
                    "vcpus": "",
 
415
                    "links": [
 
416
                        {
 
417
                            "rel": "self",
 
418
                            "href": "http://localhost/v1.1/fake/flavors/1",
 
419
                        },
 
420
                        {
 
421
                            "rel": "bookmark",
 
422
                            "href": "http://localhost/fake/flavors/1",
 
423
                        },
 
424
                    ],
 
425
                },
 
426
                {
 
427
                    "id": "2",
 
428
                    "name": "flavor 2",
 
429
                    "ram": "512",
 
430
                    "disk": "20",
 
431
                    "rxtx_cap": "",
 
432
                    "rxtx_quota": "",
 
433
                    "swap": "",
 
434
                    "vcpus": "",
 
435
                    "links": [
 
436
                        {
 
437
                            "rel": "self",
 
438
                            "href": "http://localhost/v1.1/fake/flavors/2",
 
439
                        },
 
440
                        {
 
441
                            "rel": "bookmark",
 
442
                            "href": "http://localhost/fake/flavors/2",
 
443
                        },
 
444
                    ],
 
445
                },
 
446
            ],
 
447
        }
 
448
        self.assertEqual(flavor, expected)
 
449
 
 
450
    def test_get_flavor_list_detail_bogus_min_disk_v1_1(self):
 
451
        """Tests that bogus minDisk filtering values are ignored"""
 
452
        req = webob.Request.blank(
 
453
            '/v1.1/fake/flavors/detail?minDisk=16GB')
 
454
        req.environ['api.version'] = '1.1'
 
455
        res = req.get_response(fakes.wsgi_app())
 
456
        self.assertEqual(res.status_int, 200)
 
457
        flavor = json.loads(res.body)
 
458
        expected = {
 
459
            "flavors": [
 
460
                {
 
461
                    "id": "1",
 
462
                    "name": "flavor 1",
 
463
                    "ram": "256",
 
464
                    "disk": "10",
 
465
                    "rxtx_cap": "",
 
466
                    "rxtx_quota": "",
 
467
                    "swap": "",
 
468
                    "vcpus": "",
 
469
                    "links": [
 
470
                        {
 
471
                            "rel": "self",
 
472
                            "href": "http://localhost/v1.1/fake/flavors/1",
 
473
                        },
 
474
                        {
 
475
                            "rel": "bookmark",
 
476
                            "href": "http://localhost/fake/flavors/1",
 
477
                        },
 
478
                    ],
 
479
                },
 
480
                {
 
481
                    "id": "2",
 
482
                    "name": "flavor 2",
 
483
                    "ram": "512",
 
484
                    "disk": "20",
 
485
                    "rxtx_cap": "",
 
486
                    "rxtx_quota": "",
 
487
                    "swap": "",
 
488
                    "vcpus": "",
 
489
                    "links": [
 
490
                        {
 
491
                            "rel": "self",
 
492
                            "href": "http://localhost/v1.1/fake/flavors/2",
 
493
                        },
 
494
                        {
 
495
                            "rel": "bookmark",
 
496
                            "href": "http://localhost/fake/flavors/2",
 
497
                        },
 
498
                    ],
 
499
                },
 
500
            ],
 
501
        }
 
502
        self.assertEqual(flavor, expected)
 
503
 
291
504
 
292
505
class FlavorsXMLSerializationTest(test.TestCase):
293
506