Objectify test_instance_type_extra_specs

This module was still using low-level DB functions for flavors,
which makes it break when we move them.

Change-Id: I248a9f09be6d9b6bf395a024d092f15dd1b2bbc3
This commit is contained in:
Dan Smith 2016-03-22 15:26:06 -07:00
parent 1906aa4214
commit dbbcf38e02

View File

@ -16,8 +16,7 @@ Unit Tests for instance types extra specs code
from nova.compute import arch from nova.compute import arch
from nova import context from nova import context
from nova import db from nova import objects
from nova import exception
from nova import test from nova import test
@ -26,61 +25,49 @@ class InstanceTypeExtraSpecsTestCase(test.TestCase):
def setUp(self): def setUp(self):
super(InstanceTypeExtraSpecsTestCase, self).setUp() super(InstanceTypeExtraSpecsTestCase, self).setUp()
self.context = context.get_admin_context() self.context = context.get_admin_context()
values = dict(name="cg1.4xlarge", flavor = objects.Flavor(context=self.context,
memory_mb=22000, name="cg1.4xlarge",
vcpus=8, memory_mb=22000,
root_gb=1690, vcpus=8,
ephemeral_gb=2000, root_gb=1690,
flavorid=105) ephemeral_gb=2000,
flavorid=105)
self.specs = dict(cpu_arch=arch.X86_64, self.specs = dict(cpu_arch=arch.X86_64,
cpu_model="Nehalem", cpu_model="Nehalem",
xpu_arch="fermi", xpu_arch="fermi",
xpus="2", xpus="2",
xpu_model="Tesla 2050") xpu_model="Tesla 2050")
values['extra_specs'] = self.specs flavor.extra_specs = self.specs
ref = db.flavor_create(self.context, flavor.create()
values) self.flavor = flavor
self.instance_type_id = ref["id"] self.instance_type_id = flavor.id
self.flavorid = ref["flavorid"] self.flavorid = flavor.flavorid
def tearDown(self): def tearDown(self):
# Remove the instance type from the database # Remove the instance type from the database
db.flavor_destroy(self.context, "cg1.4xlarge") self.flavor.destroy()
super(InstanceTypeExtraSpecsTestCase, self).tearDown() super(InstanceTypeExtraSpecsTestCase, self).tearDown()
def test_instance_type_specs_get(self): def test_instance_type_specs_get(self):
actual_specs = db.flavor_extra_specs_get( flavor = objects.Flavor.get_by_flavor_id(self.context,
self.context, self.flavorid)
self.flavorid) self.assertEqual(self.specs, flavor.extra_specs)
self.assertEqual(self.specs, actual_specs)
def test_flavor_extra_specs_delete(self): def test_flavor_extra_specs_delete(self):
del self.specs["xpu_model"] del self.specs["xpu_model"]
db.flavor_extra_specs_delete(self.context, del self.flavor.extra_specs['xpu_model']
self.flavorid, self.flavor.save()
"xpu_model") flavor = objects.Flavor.get_by_flavor_id(self.context,
actual_specs = db.flavor_extra_specs_get( self.flavorid)
self.context, self.assertEqual(self.specs, flavor.extra_specs)
self.flavorid)
self.assertEqual(self.specs, actual_specs)
def test_instance_type_extra_specs_update(self): def test_instance_type_extra_specs_update(self):
self.specs["cpu_model"] = "Sandy Bridge" self.specs["cpu_model"] = "Sandy Bridge"
db.flavor_extra_specs_update_or_create( self.flavor.extra_specs["cpu_model"] = "Sandy Bridge"
self.context, self.flavor.save()
self.flavorid, flavor = objects.Flavor.get_by_flavor_id(self.context,
dict(cpu_model="Sandy Bridge")) self.flavorid)
actual_specs = db.flavor_extra_specs_get( self.assertEqual(self.specs, flavor.extra_specs)
self.context,
self.flavorid)
self.assertEqual(self.specs, actual_specs)
def test_instance_type_extra_specs_update_with_nonexisting_flavor(self):
extra_specs = dict(cpu_arch=arch.X86_64)
nonexisting_flavorid = "some_flavor_that_does_not_exist"
self.assertRaises(exception.FlavorNotFound,
db.flavor_extra_specs_update_or_create,
self.context, nonexisting_flavorid, extra_specs)
def test_instance_type_extra_specs_create(self): def test_instance_type_extra_specs_create(self):
net_attrs = { net_attrs = {
@ -88,55 +75,35 @@ class InstanceTypeExtraSpecsTestCase(test.TestCase):
"net_mbps": "10000" "net_mbps": "10000"
} }
self.specs.update(net_attrs) self.specs.update(net_attrs)
db.flavor_extra_specs_update_or_create( self.flavor.extra_specs.update(net_attrs)
self.context, self.flavor.save()
self.flavorid, flavor = objects.Flavor.get_by_flavor_id(self.context,
net_attrs) self.flavorid)
actual_specs = db.flavor_extra_specs_get( self.assertEqual(self.specs, flavor.extra_specs)
self.context,
self.flavorid)
self.assertEqual(self.specs, actual_specs)
def test_instance_type_get_with_extra_specs(self): def test_instance_type_get_with_extra_specs(self):
instance_type = db.flavor_get( flavor = objects.Flavor.get_by_id(self.context, 5)
self.context, self.assertEqual(flavor.extra_specs, {})
self.instance_type_id)
self.assertEqual(instance_type['extra_specs'],
self.specs)
instance_type = db.flavor_get(
self.context,
5)
self.assertEqual(instance_type['extra_specs'], {})
def test_instance_type_get_by_name_with_extra_specs(self): def test_instance_type_get_by_name_with_extra_specs(self):
instance_type = db.flavor_get_by_name( flavor = objects.Flavor.get_by_name(self.context,
self.context, "cg1.4xlarge")
"cg1.4xlarge") self.assertEqual(flavor.extra_specs, self.specs)
self.assertEqual(instance_type['extra_specs'], flavor = objects.Flavor.get_by_name(self.context,
self.specs) "m1.small")
instance_type = db.flavor_get_by_name( self.assertEqual(flavor.extra_specs, {})
self.context,
"m1.small")
self.assertEqual(instance_type['extra_specs'], {})
def test_instance_type_get_by_flavor_id_with_extra_specs(self): def test_instance_type_get_by_flavor_id_with_extra_specs(self):
instance_type = db.flavor_get_by_flavor_id( flavor = objects.Flavor.get_by_flavor_id(self.context, 105)
self.context, self.assertEqual(flavor.extra_specs, self.specs)
105) flavor = objects.Flavor.get_by_flavor_id(self.context, 2)
self.assertEqual(instance_type['extra_specs'], self.assertEqual(flavor.extra_specs, {})
self.specs)
instance_type = db.flavor_get_by_flavor_id(
self.context,
2)
self.assertEqual(instance_type['extra_specs'], {})
def test_instance_type_get_all(self): def test_instance_type_get_all(self):
types = db.flavor_get_all(self.context) flavors = objects.FlavorList.get_all(self.context)
name2specs = {} name2specs = {flavor.name: flavor.extra_specs
for instance_type in types: for flavor in flavors}
name = instance_type['name']
name2specs[name] = instance_type['extra_specs']
self.assertEqual(name2specs['cg1.4xlarge'], self.specs) self.assertEqual(name2specs['cg1.4xlarge'], self.specs)
self.assertEqual(name2specs['m1.small'], {}) self.assertEqual(name2specs['m1.small'], {})