diff options
| author | Rick Harris <rconradharris@gmail.com> | 2011-09-21 17:49:35 -0500 |
|---|---|---|
| committer | Rick Harris <rconradharris@gmail.com> | 2011-09-21 17:49:35 -0500 |
| commit | 57a67cf27a51e6849bff6236f896ecbee6345250 (patch) | |
| tree | 23d0a6f6ae75680ff311a123a837929dfbbe120d | |
| parent | be8b40a02858a0f054c96d97a9881a9e3e4a1f49 (diff) | |
Adding OSAPI tests for flavor filtering
| -rw-r--r-- | nova/api/openstack/flavors.py | 11 | ||||
| -rw-r--r-- | nova/db/sqlalchemy/api.py | 1 | ||||
| -rw-r--r-- | nova/tests/api/openstack/test_flavors.py | 223 |
3 files changed, 225 insertions, 10 deletions
diff --git a/nova/api/openstack/flavors.py b/nova/api/openstack/flavors.py index 0fd31abb5..b9dd96f4e 100644 --- a/nova/api/openstack/flavors.py +++ b/nova/api/openstack/flavors.py @@ -45,9 +45,16 @@ class Controller(object): """Helper function that returns a list of flavor dicts.""" filters = {} if 'minRam' in req.params: - filters['min_memory_mb'] = req.params['minRam'] + try: + filters['min_memory_mb'] = int(req.params['minRam']) + except ValueError: + pass # ignore bogus values per spec + if 'minDisk' in req.params: - filters['min_local_gb'] = req.params['minDisk'] + try: + filters['min_local_gb'] = int(req.params['minDisk']) + except ValueError: + pass # ignore bogus values per spec ctxt = req.environ['nova.context'] inst_types = db.api.instance_type_get_all(ctxt, filters=filters) diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index 8126686b0..c91fa8c0d 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -3346,7 +3346,6 @@ def instance_type_get_all(context, inactive=False, filters=None): return [_dict_with_extra_specs(i) for i in inst_types] -#TODO(sirp): refactor below to use parital query? @require_context def instance_type_get(context, id): """Returns a dict describing specific instance_type""" diff --git a/nova/tests/api/openstack/test_flavors.py b/nova/tests/api/openstack/test_flavors.py index f0b14968f..743dddfc4 100644 --- a/nova/tests/api/openstack/test_flavors.py +++ b/nova/tests/api/openstack/test_flavors.py @@ -42,8 +42,8 @@ FAKE_FLAVORS = { 'flavor 2': { "flavorid": '2', "name": 'flavor 2', - "memory_mb": '256', - "local_gb": '10' + "memory_mb": '512', + "local_gb": '20' }, } @@ -53,7 +53,18 @@ def fake_instance_type_get_by_flavor_id(context, flavorid): def fake_instance_type_get_all(context, inactive=False, filters=None): - return FAKE_FLAVORS.values() + def reject_min(db_attr, filter_attr): + return filter_attr in filters and\ + int(flavor[db_attr]) < int(filters[filter_attr]) + + filters = filters or {} + for flavor in FAKE_FLAVORS.values(): + if reject_min('memory_mb', 'min_memory_mb'): + continue + elif reject_min('local_gb', 'min_local_gb'): + continue + + yield flavor def empty_instance_type_get_all(context, inactive=False, filters=None): @@ -125,8 +136,8 @@ class FlavorsTest(test.TestCase): { "id": "2", "name": "flavor 2", - "ram": "256", - "disk": "10", + "ram": "512", + "disk": "20", "rxtx_cap": "", "rxtx_quota": "", "swap": "", @@ -260,8 +271,8 @@ class FlavorsTest(test.TestCase): { "id": "2", "name": "flavor 2", - "ram": "256", - "disk": "10", + "ram": "512", + "disk": "20", "rxtx_cap": "", "rxtx_quota": "", "swap": "", @@ -292,6 +303,204 @@ class FlavorsTest(test.TestCase): expected = [] self.assertEqual(flavors, expected) + def test_get_flavor_list_filter_min_ram_v1_1(self): + """Flavor lists may be filtered by minRam""" + req = webob.Request.blank('/v1.1/fake/flavors?minRam=512') + req.environ['api.version'] = '1.1' + res = req.get_response(fakes.wsgi_app()) + self.assertEqual(res.status_int, 200) + flavor = json.loads(res.body) + expected = { + "flavors": [ + { + "id": "2", + "name": "flavor 2", + "links": [ + { + "rel": "self", + "href": "http://localhost/v1.1/fake/flavors/2", + }, + { + "rel": "bookmark", + "href": "http://localhost/fake/flavors/2", + }, + ], + }, + ], + } + self.assertEqual(flavor, expected) + + def test_get_flavor_list_filter_min_disk(self): + """Flavor lists may be filtered by minRam""" + req = webob.Request.blank('/v1.1/fake/flavors?minDisk=20') + req.environ['api.version'] = '1.1' + res = req.get_response(fakes.wsgi_app()) + self.assertEqual(res.status_int, 200) + flavor = json.loads(res.body) + expected = { + "flavors": [ + { + "id": "2", + "name": "flavor 2", + "links": [ + { + "rel": "self", + "href": "http://localhost/v1.1/fake/flavors/2", + }, + { + "rel": "bookmark", + "href": "http://localhost/fake/flavors/2", + }, + ], + }, + ], + } + self.assertEqual(flavor, expected) + + def test_get_flavor_list_detail_min_ram_and_min_disk_v1_1(self): + """Tests that filtering work on flavor details and that minRam and + minDisk filters can be combined + """ + req = webob.Request.blank( + '/v1.1/fake/flavors/detail?minRam=256&minDisk=20') + req.environ['api.version'] = '1.1' + res = req.get_response(fakes.wsgi_app()) + self.assertEqual(res.status_int, 200) + flavor = json.loads(res.body) + expected = { + "flavors": [ + { + "id": "2", + "name": "flavor 2", + "ram": "512", + "disk": "20", + "rxtx_cap": "", + "rxtx_quota": "", + "swap": "", + "vcpus": "", + "links": [ + { + "rel": "self", + "href": "http://localhost/v1.1/fake/flavors/2", + }, + { + "rel": "bookmark", + "href": "http://localhost/fake/flavors/2", + }, + ], + }, + ], + } + self.assertEqual(flavor, expected) + + def test_get_flavor_list_detail_bogus_min_ram_v1_1(self): + """Tests that bogus minRam filtering values are ignored""" + req = webob.Request.blank( + '/v1.1/fake/flavors/detail?minRam=16GB') + req.environ['api.version'] = '1.1' + res = req.get_response(fakes.wsgi_app()) + self.assertEqual(res.status_int, 200) + flavor = json.loads(res.body) + expected = { + "flavors": [ + { + "id": "1", + "name": "flavor 1", + "ram": "256", + "disk": "10", + "rxtx_cap": "", + "rxtx_quota": "", + "swap": "", + "vcpus": "", + "links": [ + { + "rel": "self", + "href": "http://localhost/v1.1/fake/flavors/1", + }, + { + "rel": "bookmark", + "href": "http://localhost/fake/flavors/1", + }, + ], + }, + { + "id": "2", + "name": "flavor 2", + "ram": "512", + "disk": "20", + "rxtx_cap": "", + "rxtx_quota": "", + "swap": "", + "vcpus": "", + "links": [ + { + "rel": "self", + "href": "http://localhost/v1.1/fake/flavors/2", + }, + { + "rel": "bookmark", + "href": "http://localhost/fake/flavors/2", + }, + ], + }, + ], + } + self.assertEqual(flavor, expected) + + def test_get_flavor_list_detail_bogus_min_disk_v1_1(self): + """Tests that bogus minDisk filtering values are ignored""" + req = webob.Request.blank( + '/v1.1/fake/flavors/detail?minDisk=16GB') + req.environ['api.version'] = '1.1' + res = req.get_response(fakes.wsgi_app()) + self.assertEqual(res.status_int, 200) + flavor = json.loads(res.body) + expected = { + "flavors": [ + { + "id": "1", + "name": "flavor 1", + "ram": "256", + "disk": "10", + "rxtx_cap": "", + "rxtx_quota": "", + "swap": "", + "vcpus": "", + "links": [ + { + "rel": "self", + "href": "http://localhost/v1.1/fake/flavors/1", + }, + { + "rel": "bookmark", + "href": "http://localhost/fake/flavors/1", + }, + ], + }, + { + "id": "2", + "name": "flavor 2", + "ram": "512", + "disk": "20", + "rxtx_cap": "", + "rxtx_quota": "", + "swap": "", + "vcpus": "", + "links": [ + { + "rel": "self", + "href": "http://localhost/v1.1/fake/flavors/2", + }, + { + "rel": "bookmark", + "href": "http://localhost/fake/flavors/2", + }, + ], + }, + ], + } + self.assertEqual(flavor, expected) + class FlavorsXMLSerializationTest(test.TestCase): |
