def test_swap_must_be_nonnegative_db_integer(self): self.assertInvalidInput('flavor1', 64, 1, 120, swap='foo') self.assertInvalidInput('flavor1', 64, 1, 120, swap=-1) self.assertInvalidInput('flavor1', 64, 1, 120, swap=db.MAX_INT + 1) flavors.create('flavor1', 64, 1, 120, swap=0) flavors.create('flavor2', 64, 1, 120, swap=1)
def test_create_flavor_ephemeral_error(self): args = ("ephemeral_test", "1024", "1", "10", "9999999999") try: flavors.create(*args) self.fail("Be sure this will never be executed.") except exception.InvalidInput as e: self.assertIn("ephemeral", e.message)
def test_ephemeral_gb_must_be_nonnegative_db_integer(self): self.assertInvalidInput('flavor1', 64, 1, 120, ephemeral_gb='foo') self.assertInvalidInput('flavor1', 64, 1, 120, ephemeral_gb=-1) self.assertInvalidInput('flavor1', 64, 1, 120, ephemeral_gb=db.MAX_INT + 1) flavors.create('flavor1', 64, 1, 120, ephemeral_gb=0) flavors.create('flavor2', 64, 1, 120, ephemeral_gb=120)
def test_add_flavor_access_already_exists(self): user_id = "fake" project_id = "fake" ctxt = context.RequestContext(user_id, project_id, is_admin=True) flavor_id = "flavor1" flavors.create("some flavor", 256, 1, 120, 100, flavorid=flavor_id) flavors.add_flavor_access(flavor_id, project_id, ctxt=ctxt) self.assertRaises(exception.FlavorAccessExists, flavors.add_flavor_access, flavor_id, project_id, ctxt)
def test_flavorid_populated(self): flavor1 = flavors.create('flavor1', 64, 1, 120) self.assertIsNot(None, flavor1['flavorid']) flavor2 = flavors.create('flavor2', 64, 1, 120, flavorid='') self.assertIsNot(None, flavor2['flavorid']) flavor3 = flavors.create('flavor3', 64, 1, 120, flavorid='foo') self.assertEqual('foo', flavor3['flavorid'])
def test_remove_flavor_access_doesnt_exists(self): user_id = 'fake' project_id = 'fake' ctxt = context.RequestContext(user_id, project_id, is_admin=True) flavor_id = 'flavor1' flavors.create('some flavor', 256, 1, 120, 100, flavorid=flavor_id) self.assertRaises(exception.FlavorAccessNotFound, flavors.remove_flavor_access, flavor_id, project_id, ctxt=ctxt)
def test_flavorid_populated(self): flavor1 = flavors.create("flavor1", 64, 1, 120) self.assertIsNot(None, flavor1["flavorid"]) flavor2 = flavors.create("flavor2", 64, 1, 120, flavorid="") self.assertIsNot(None, flavor2["flavorid"]) flavor3 = flavors.create("flavor3", 64, 1, 120, flavorid="foo") self.assertEqual("foo", flavor3["flavorid"])
def test_flavorid_populated(self): flavor1 = flavors.create('flavor1', 64, 1, 120) self.assertIsNotNone(flavor1.flavorid) flavor2 = flavors.create('flavor2', 64, 1, 120, flavorid='') self.assertIsNotNone(flavor2.flavorid) flavor3 = flavors.create('flavor3', 64, 1, 120, flavorid='foo') self.assertEqual('foo', flavor3.flavorid)
def test_rxtx_factor_must_be_positive_float(self): self.assertInvalidInput("flavor1", 64, 1, 120, rxtx_factor="foo") self.assertInvalidInput("flavor1", 64, 1, 120, rxtx_factor=-1.0) self.assertInvalidInput("flavor1", 64, 1, 120, rxtx_factor=0.0) flavor = flavors.create("flavor1", 64, 1, 120, rxtx_factor=1.0) self.assertEqual(1.0, flavor["rxtx_factor"]) flavor = flavors.create("flavor2", 64, 1, 120, rxtx_factor=1.1) self.assertEqual(1.1, flavor["rxtx_factor"])
def test_rxtx_factor_must_be_positive_float(self): self.assertInvalidInput('flavor1', 64, 1, 120, rxtx_factor='foo') self.assertInvalidInput('flavor1', 64, 1, 120, rxtx_factor=-1.0) self.assertInvalidInput('flavor1', 64, 1, 120, rxtx_factor=0.0) flavor = flavors.create('flavor1', 64, 1, 120, rxtx_factor=1.0) self.assertEqual(1.0, flavor.rxtx_factor) flavor = flavors.create('flavor2', 64, 1, 120, rxtx_factor=1.1) self.assertEqual(1.1, flavor.rxtx_factor)
def test_name_length_checks(self): MAX_LEN = 255 # Flavor name with 255 characters or less is valid. flavors.create('a' * MAX_LEN, 64, 1, 120) # Flavor name which is more than 255 characters will cause error. self.assertInvalidInput('a' * (MAX_LEN + 1), 64, 1, 120) # Flavor name which is empty should cause an error self.assertInvalidInput('', 64, 1, 120)
def test_read_deleted_false_converting_flavorid(self): """Ensure deleted flavors are not returned when not needed (for example when creating a server and attempting to translate from flavorid to instance_type_id. """ flavors.create("instance_type1", 256, 1, 120, 100, "test1") flavors.destroy("instance_type1") flavors.create("instance_type1_redo", 256, 1, 120, 100, "test1") instance_type = flavors.get_flavor_by_flavor_id("test1", read_deleted="no") self.assertEqual("instance_type1_redo", instance_type.name)
def test_remove_flavor_access(self): user_id = "fake" project_id = "fake" ctxt = context.RequestContext(user_id, project_id, is_admin=True) flavor_id = "flavor1" flavors.create("some flavor", 256, 1, 120, 100, flavorid=flavor_id) flavors.add_flavor_access(flavor_id, project_id, ctxt) flavors.remove_flavor_access(flavor_id, project_id, ctxt) projects = flavors.get_flavor_access_by_flavor_id(flavor_id, ctxt) self.assertEqual([], projects)
def test_get_inactive_flavors_with_same_name(self): flavors.create('flavor1', 256, 1, 120, 100, "test_id1") flavors.destroy('flavor1') flavors.create('flavor1', 512, 4, 250, 100, "test_id2") returned_flavors_ids = flavors.get_all_flavors().keys() self.assertNotIn('test_id1', returned_flavors_ids) self.assertIn('test_id2', returned_flavors_ids) returned_flavors_ids = flavors.get_all_flavors(inactive=True).keys() self.assertIn('test_id1', returned_flavors_ids) self.assertIn('test_id2', returned_flavors_ids)
def test_get_inactive_flavors(self): flav1 = flavors.create('flavor1', 256, 1, 120) flav2 = flavors.create('flavor2', 512, 4, 250) flavors.destroy('flavor1') returned_flavors_ids = flavors.get_all_flavors().keys() self.assertNotIn(flav1['id'], returned_flavors_ids) self.assertIn(flav2['id'], returned_flavors_ids) returned_flavors_ids = flavors.get_all_flavors(inactive=True).keys() self.assertIn(flav1['id'], returned_flavors_ids) self.assertIn(flav2['id'], returned_flavors_ids)
def test_get_inactive_flavors_with_same_flavorid(self): flav1 = flavors.create('flavor', 256, 1, 120, 100, "flavid") flavors.destroy('flavor') flav2 = flavors.create('flavor', 512, 4, 250, 100, "flavid") returned_flavors_ids = flavors.get_all_flavors().keys() self.assertNotIn(flav1['id'], returned_flavors_ids) self.assertIn(flav2['id'], returned_flavors_ids) returned_flavors_ids = flavors.get_all_flavors(inactive=True).keys() self.assertIn(flav1['id'], returned_flavors_ids) self.assertIn(flav2['id'], returned_flavors_ids)
def test_get_inactive_flavors_with_same_name(self): flav1 = flavors.create('flavor', 256, 1, 120) flavors.destroy('flavor') flav2 = flavors.create('flavor', 512, 4, 250) returned_flavors_ids = flavors.get_all_flavors().keys() self.assertNotIn(flav1.id, returned_flavors_ids) self.assertIn(flav2.id, returned_flavors_ids) returned_flavors_ids = flavors.get_all_flavors(inactive=True).keys() self.assertIn(flav1.id, returned_flavors_ids) self.assertIn(flav2.id, returned_flavors_ids)
def test_get_inactive_flavors(self): flav1 = flavors.create("flavor1", 256, 1, 120) flav2 = flavors.create("flavor2", 512, 4, 250) flavors.destroy("flavor1") returned_flavors_ids = flavors.get_all_flavors().keys() self.assertNotIn(flav1["id"], returned_flavors_ids) self.assertIn(flav2["id"], returned_flavors_ids) returned_flavors_ids = flavors.get_all_flavors(inactive=True).keys() self.assertIn(flav1["id"], returned_flavors_ids) self.assertIn(flav2["id"], returned_flavors_ids)
def _create_flavor(self, context, flavor): """ Create and insert the flavor """ flavor_dict = flavor.__dict__ name = self.prefix + flavor.name flavorid = self.prefix + flavor.id memory = flavor.ram vcpus = flavor.vcpus root_gb = flavor.disk ephemeral_gb = flavor_dict.get('OS-FLV-EXT-DATA:ephemeral', 0) u_swap = flavor_dict.get('swap', 0) rxtx_factor = flavor_dict.get('rxtx_factor', 1.0) is_public = flavor_dict.get('os-flavor-access:is_public', True) if u_swap == "": swap = 0 else: swap = int(u_swap) try: return flavors.create(name, memory, vcpus, root_gb, ephemeral_gb=ephemeral_gb, flavorid=flavorid, swap=swap, rxtx_factor=rxtx_factor, is_public=is_public) except exception.InstanceExists as err: raise err
def _create(self, req, body): context = req.environ['nova.context'] context.can(fm_policies.BASE_POLICY_NAME) vals = body['flavor'] name = vals['name'] flavorid = vals.get('id') memory = vals['ram'] vcpus = vals['vcpus'] root_gb = vals['disk'] ephemeral_gb = vals.get('OS-FLV-EXT-DATA:ephemeral', 0) swap = vals.get('swap', 0) rxtx_factor = vals.get('rxtx_factor', 1.0) is_public = vals.get('os-flavor-access:is_public', True) try: flavor = flavors.create(name, memory, vcpus, root_gb, ephemeral_gb=ephemeral_gb, flavorid=flavorid, swap=swap, rxtx_factor=rxtx_factor, is_public=is_public) # NOTE(gmann): For backward compatibility, non public flavor # access is not being added for created tenant. Ref -bug/1209101 req.cache_db_flavor(flavor) except (exception.FlavorExists, exception.FlavorIdExists) as err: raise webob.exc.HTTPConflict(explanation=err.format_message()) except exception.ObjectActionError: raise webob.exc.HTTPConflict(explanation=_( 'Not all flavors have been migrated to the API database')) return self._view_builder.show(req, flavor)
def _create(self, req, body): context = req.environ['nova.context'] authorize(context) vals = body['flavor'] name = vals['name'] flavorid = vals.get('id') memory = vals['ram'] vcpus = vals['vcpus'] root_gb = vals['disk'] ephemeral_gb = vals.get('OS-FLV-EXT-DATA:ephemeral', 0) swap = vals.get('swap', 0) rxtx_factor = vals.get('rxtx_factor', 1.0) is_public = vals.get('os-flavor-access:is_public', True) try: flavor = flavors.create(name, memory, vcpus, root_gb, ephemeral_gb=ephemeral_gb, flavorid=flavorid, swap=swap, rxtx_factor=rxtx_factor, is_public=is_public) # NOTE(gmann): For backward compatibility, non public flavor # access is not being added for created tenant. Ref -bug/1209101 req.cache_db_flavor(flavor) except (exception.FlavorExists, exception.FlavorIdExists) as err: raise webob.exc.HTTPConflict(explanation=err.format_message()) except exception.FlavorCreateFailed as err: raise webob.exc.HTTPInternalServerError(explanation= err.format_message()) return self._view_builder.show(req, flavor)
def _create(self, req, body): context = req.environ['nova.context'] authorize(context) if not self.is_valid_body(body, 'flavor'): msg = _("Invalid request body") raise webob.exc.HTTPBadRequest(explanation=msg) vals = body['flavor'] name = vals.get('name') flavorid = vals.get('id') memory = vals.get('ram') vcpus = vals.get('vcpus') root_gb = vals.get('disk') ephemeral_gb = vals.get('OS-FLV-EXT-DATA:ephemeral', 0) swap = vals.get('swap', 0) rxtx_factor = vals.get('rxtx_factor', 1.0) is_public = vals.get('os-flavor-access:is_public', True) try: flavor = flavors.create(name, memory, vcpus, root_gb, ephemeral_gb=ephemeral_gb, flavorid=flavorid, swap=swap, rxtx_factor=rxtx_factor, is_public=is_public) if not flavor['is_public']: flavors.add_flavor_access(flavor['flavorid'], context.project_id, context) req.cache_db_flavor(flavor) except (exception.FlavorExists, exception.FlavorIdExists) as err: raise webob.exc.HTTPConflict(explanation=err.format_message()) except exception.InvalidInput as exc: raise webob.exc.HTTPBadRequest(explanation=exc.format_message()) return self._view_builder.show(req, flavor)
def test_default_values(self): flavor1 = flavors.create("flavor1", 64, 1, 120) self.assertIsNot(None, flavor1["flavorid"]) self.assertEqual(flavor1["ephemeral_gb"], 0) self.assertEqual(flavor1["swap"], 0) self.assertEqual(flavor1["rxtx_factor"], 1.0)
def test_instance_type_create_then_delete(self): # Ensure instance types can be created. name = "Small Flavor" flavorid = "flavor1" original_list = flavors.get_all_types() # create new type and make sure values stick inst_type = flavors.create(name, 256, 1, 120, 100, flavorid) inst_type_id = inst_type["id"] self.assertEqual(inst_type["flavorid"], flavorid) self.assertEqual(inst_type["name"], name) self.assertEqual(inst_type["memory_mb"], 256) self.assertEqual(inst_type["vcpus"], 1) self.assertEqual(inst_type["root_gb"], 120) self.assertEqual(inst_type["ephemeral_gb"], 100) self.assertEqual(inst_type["swap"], 0) self.assertEqual(inst_type["rxtx_factor"], 1.0) # make sure new type shows up in list new_list = flavors.get_all_types() self.assertNotEqual(len(original_list), len(new_list), "instance type was not created") flavors.destroy(name) self.assertRaises(exception.InstanceTypeNotFound, flavors.get_instance_type, inst_type_id) # deleted instance should not be in list anymoer new_list = flavors.get_all_types() self.assertEqual(original_list, new_list)
def _create(self, req, body): context = req.environ['nova.context'] authorize(context) vals = body['flavor'] name = vals['name'] flavorid = vals.get('id') memory = vals.get('ram') vcpus = vals.get('vcpus') root_gb = vals.get('disk') ephemeral_gb = vals.get('OS-FLV-EXT-DATA:ephemeral', 0) swap = vals.get('swap', 0) rxtx_factor = vals.get('rxtx_factor', 1.0) is_public = vals.get('os-flavor-access:is_public', True) try: flavor = flavors.create(name, memory, vcpus, root_gb, ephemeral_gb=ephemeral_gb, flavorid=flavorid, swap=swap, rxtx_factor=rxtx_factor, is_public=is_public) req.cache_db_flavor(flavor) except (exception.InstanceTypeExists, exception.InstanceTypeIdExists) as err: raise webob.exc.HTTPConflict(explanation=err.format_message()) return self._view_builder.show(req, flavor)
def _create_flavor(self, context, flavor): """ Create and insert the flavor """ flavor_dict = flavor.__dict__ name = self.prefix + flavor.name flavorid = self.prefix + flavor.id memory = flavor.ram vcpus = flavor.vcpus root_gb = flavor.disk ephemeral_gb = flavor_dict.get('OS-FLV-EXT-DATA:ephemeral', 0) u_swap = flavor_dict.get('swap', 0) rxtx_factor = flavor_dict.get('rxtx_factor', 1.0) is_public = flavor_dict.get('os-flavor-access:is_public', True) if u_swap == "": swap = 0 else: swap = int(u_swap) try: return flavors.create(name, memory, vcpus, root_gb, ephemeral_gb=ephemeral_gb, flavorid=flavorid, swap=swap, rxtx_factor=rxtx_factor, is_public=is_public) except Exception as exc: LOG.error(_("Unable to sync flavor " + str(name) + ". " + str(exc.format_message()))) return None
def _create(self, req, body): context = req.environ['nova.context'] authorize(context) vals = body['flavor'] name = vals['name'] flavorid = vals.get('id') memory = vals['ram'] vcpus = vals['vcpus'] root_gb = vals['disk'] ephemeral_gb = vals.get('ephemeral', 0) swap = vals.get('swap', 0) rxtx_factor = vals.get('os-flavor-rxtx:rxtx_factor', 1.0) is_public = vals.get('flavor-access:is_public', True) try: flavor = flavors.create(name, memory, vcpus, root_gb, ephemeral_gb=ephemeral_gb, flavorid=flavorid, swap=swap, rxtx_factor=rxtx_factor, is_public=is_public) if not flavor['is_public']: flavors.add_flavor_access(flavor['flavorid'], context.project_id, context) req.cache_db_flavor(flavor) except (exception.FlavorExists, exception.FlavorIdExists) as err: raise webob.exc.HTTPConflict(explanation=err.format_message()) return self._view_builder.show(req, flavor)
def test_default_values(self): flavor1 = flavors.create('flavor1', 64, 1, 120) self.assertIsNot(None, flavor1['flavorid']) self.assertEqual(flavor1['ephemeral_gb'], 0) self.assertEqual(flavor1['swap'], 0) self.assertEqual(flavor1['rxtx_factor'], 1.0)
def test_default_values(self): flavor1 = flavors.create('flavor1', 64, 1, 120) self.assertIsNotNone(flavor1.flavorid) self.assertEqual(flavor1.ephemeral_gb, 0) self.assertEqual(flavor1.swap, 0) self.assertEqual(flavor1.rxtx_factor, 1.0)
def test_remove_instance_type_access_doesnt_exists(self): user_id = "fake" project_id = "fake" ctxt = context.RequestContext(user_id, project_id, is_admin=True) flavor_id = "flavor1" type_ref = flavors.create("some flavor", 256, 1, 120, 100, flavorid=flavor_id) self.assertRaises( exception.FlavorAccessNotFound, flavors.remove_instance_type_access, flavor_id, project_id, ctxt=ctxt )
def test_root_gb_must_be_nonnegative_db_integer(self): self.assertInvalidInput('flavor1', 64, 1, 'foo') self.assertInvalidInput('flavor1', 64, 1, -1) self.assertInvalidInput('flavor1', 64, 1, db.MAX_INT + 1) flavors.create('flavor1', 64, 1, 0) flavors.create('flavor2', 64, 1, 120)
def test_name_with_special_characters(self): # Names can contain [a-zA-Z0-9_.- ] flavors.create('_foo.bar-123', 64, 1, 120) # Ensure instance types raises InvalidInput for invalid characters. self.assertInvalidInput('foobar#', 64, 1, 120)
def test_create_with_valid_name(self): # Names can contain [a-zA-Z0-9_.- ] flavors.create('azAZ09. -_', 64, 1, 120)
def test_duplicate_names_fail(self): # Ensures that name duplicates raise InstanceTypeCreateFailed. flavors.create('flavor', 256, 1, 120, 200, 'flavor1') self.assertRaises(exception.InstanceTypeExists, flavors.create, 'flavor', 64, 1, 120)
def test_memory_must_be_positive_integer(self): self.assertInvalidInput('flavor1', 'foo', 1, 120) self.assertInvalidInput('flavor1', -1, 1, 120) self.assertInvalidInput('flavor1', 0, 1, 120) self.assertInvalidInput('flavor1', sys.maxint + 1, 1, 120) flavors.create('flavor1', 1, 1, 120)
def test_duplicate_flavorids_fail(self): # Ensures that flavorid duplicates raise FlavorExists flavors.create('flavor1', 64, 1, 120, flavorid='flavorid') self.assertRaises( exception.FlavorIdExists, flavors.create, 'flavor2', 64, 1, 120, flavorid='flavorid')
def test_create_with_valid_name(self): # Names can contain alphanumeric and [_.- ] flavors.create('azAZ09. -_', 64, 1, 120) # And they are not limited to ascii characters # E.g.: m1.huge in simplified Chinese flavors.create(u'm1.\u5DE8\u5927', 6400, 100, 12000)
def test_name_with_special_characters(self): # Names can contain all printable characters flavors.create('_foo.bar-123', 64, 1, 120) # Ensure instance types raises InvalidInput for invalid characters. self.assertInvalidInput('foobar\x00', 64, 1, 120)
def test_duplicate_names_fail(self): # Ensures that name duplicates raise FlavorExists flavors.create('flavor', 256, 1, 120, 200, 'flavor1') self.assertRaises(exception.FlavorExists, flavors.create, 'flavor', 64, 1, 120)
def test_duplicate_flavorids_fail(self): # Ensures that flavorid duplicates raise InstanceTypeCreateFailed. flavors.create('flavor1', 64, 1, 120, flavorid='flavorid') self.assertRaises(exception.InstanceTypeIdExists, flavors.create, 'flavor2', 64, 1, 120, flavorid='flavorid')
def test_is_public_must_be_valid_bool_string(self): self.assertInvalidInput('flavor1', 64, 1, 120, is_public='foo') flavors.create('flavor1', 64, 1, 120, is_public='TRUE') flavors.create('flavor2', 64, 1, 120, is_public='False') flavors.create('flavor3', 64, 1, 120, is_public='Yes') flavors.create('flavor4', 64, 1, 120, is_public='No') flavors.create('flavor5', 64, 1, 120, is_public='Y') flavors.create('flavor6', 64, 1, 120, is_public='N') flavors.create('flavor7', 64, 1, 120, is_public='1') flavors.create('flavor8', 64, 1, 120, is_public='0') flavors.create('flavor9', 64, 1, 120, is_public='true')
def test_vcpus_must_be_positive_db_integer(self): self.assertInvalidInput('flavor`', 64, 'foo', 120) self.assertInvalidInput('flavor1', 64, -1, 120) self.assertInvalidInput('flavor1', 64, 0, 120) self.assertInvalidInput('flavor1', 64, db.MAX_INT + 1, 120) flavors.create('flavor1', 64, 1, 120)
def test_memory_must_be_positive_db_integer(self): self.assertInvalidInput('flavor1', 'foo', 1, 120) self.assertInvalidInput('flavor1', -1, 1, 120) self.assertInvalidInput('flavor1', 0, 1, 120) self.assertInvalidInput('flavor1', db.MAX_INT + 1, 1, 120) flavors.create('flavor1', 1, 1, 120)