def test_launch_default_security_group(self): sg = utils.create_security_group(self.context, {"name": "test-sg", "description": "test security group"}) instance_uuid = utils.create_instance(self.context, {"security_groups": [sg["name"]]}) blessed_instance = self.cobalt_api.bless_instance(self.context, instance_uuid) blessed_instance_uuid = blessed_instance["uuid"] inst = self.cobalt_api.launch_instance(self.context, blessed_instance_uuid, params={}) self.assertEqual(inst["security_groups"][0].id, sg.id)
def test_launch_instance_twice(self): instance_uuid = utils.create_instance(self.context) blessed_instance = self.cobalt_api.bless_instance(self.context, instance_uuid) blessed_instance_uuid = blessed_instance["uuid"] launched_instance = self.cobalt_api.launch_instance(self.context, blessed_instance_uuid) launched_instance_uuid = launched_instance["uuid"] metadata = db.instance_metadata_get(self.context, launched_instance["uuid"]) self.assertTrue( metadata.has_key("launched_from"), "The instance should have a 'launched from' metadata after being launched.", ) self.assertTrue( metadata["launched_from"] == "%s" % (blessed_instance_uuid), "The instance should have the 'launched from' metadata set to blessed instanced id after being launched. " + "(value=%s)" % (metadata["launched_from"]), ) launched_instance = self.cobalt_api.launch_instance(self.context, blessed_instance_uuid) launched_instance_uuid = launched_instance["uuid"] metadata = db.instance_metadata_get(self.context, launched_instance["uuid"]) self.assertTrue( metadata.has_key("launched_from"), "The instance should have a 'launched from' metadata after being launched.", ) self.assertTrue( metadata["launched_from"] == "%s" % (blessed_instance_uuid), "The instance should have the 'launched from' metadata set to blessed instanced id after being launched. " + "(value=%s)" % (metadata["launched_from"]), )
def test_bless_instance(self): instance_uuid = utils.create_instance(self.context) num_instance_before = len(db.instance_get_all(self.context)) blessed_instance = self.cobalt_api.bless_instance(self.context, instance_uuid) self.assertEquals(vm_states.BUILDING, blessed_instance['vm_state']) # Ensure that we have a 2nd instance in the database that is a "clone" # of our original instance. instances = db.instance_get_all(self.context) self.assertTrue(len(instances) == (num_instance_before + 1), "There should be one new instance after blessing.") # The virtual machine should be marked that it is now blessed. metadata = db.instance_metadata_get(self.context, blessed_instance['uuid']) self.assertTrue(metadata.has_key('blessed_from'), "The instance should have a bless metadata after being blessed.") self.assertTrue(metadata['blessed_from'] == '%s' % instance_uuid, "The instance should have the blessed_from metadata set to true after being blessed. " \ + "(value=%s)" % (metadata['blessed_from'])) system_metadata = db.instance_system_metadata_get(self.context, blessed_instance['uuid']) self.assertTrue(system_metadata.has_key('blessed_from'), "The instance should have a bless system_metadata after being blessed.") self.assertTrue(system_metadata['blessed_from'] == '%s' % instance_uuid, "The instance should have the blessed_from system_metadata set to true after being blessed. "\ + "(value=%s)" % (system_metadata['blessed_from'])) db_blessed_instance = db.instance_get_by_uuid(self.context, blessed_instance['uuid']) self.assertTrue(db_blessed_instance['info_cache']) self.assertIsNotNone(db_blessed_instance['info_cache']['network_info'])
def test_check_delete_already_deleted(self): instance_uuid = utils.create_instance(self.context, {'deleted': True}) # This should not fail for an instance that is already deleted because # there is a race were the instance can be deleted by nova-compute # before nova-api call this part of the extension. self.cobalt_api.check_delete(self.context, instance_uuid)
def test_launch_without_user_data(self): instance_uuid = utils.create_instance(self.context) blessed_instance = self.cobalt_api.bless_instance(self.context, instance_uuid) blessed_instance_uuid = blessed_instance['uuid'] launched_instance = self.cobalt_api.launch_instance(self.context, blessed_instance_uuid, params={}) user_data = launched_instance['user_data'] self.assertEqual(user_data, '')
def test_bless_instance_migrate(self): self.vmsconn.set_return_val( "bless", ("newname", "migration_url", ["file1", "file2", "file3"], [])) self.vmsconn.set_return_val("post_bless", ["file1_ref", "file2_ref", "file3_ref"]) self.vmsconn.set_return_val("bless_cleanup", None) self.vmsconn.set_return_val("get_instance_info", {'state': power_state.RUNNING}) blessed_uuid = utils.create_instance(self.context) pre_bless_instance = db.instance_get_by_uuid(self.context, blessed_uuid) migration_url, instance_ref = self.cobalt.bless_instance( self.context, instance_uuid=blessed_uuid, migration_url="mcdist://migrate_addr") post_bless_instance = db.instance_get_by_uuid(self.context, blessed_uuid) self.assertEquals(pre_bless_instance['vm_state'], post_bless_instance['vm_state']) self.assertEquals("migration_url", migration_url) system_metadata = db.instance_system_metadata_get( self.context, blessed_uuid) self.assertEquals("file1_ref,file2_ref,file3_ref", system_metadata['images']) self.assertEquals(pre_bless_instance['launched_at'], post_bless_instance['launched_at']) self.assertFalse(pre_bless_instance.get('disable_terminate', None), post_bless_instance.get('disable_terminate', None))
def test_vms_policy_generation_custom_flavor(self): flavor = utils.create_flavor() instance_uuid = utils.create_instance(self.context, {'instance_type_id': flavor['id']}) instance = db.instance_get_by_uuid(self.context, instance_uuid) vms_policy = self.cobalt._generate_vms_policy_name(self.context, instance, instance) expected_policy = ';blessed=%s;;flavor=%s;;tenant=%s;;uuid=%s;' \ %(instance['uuid'], flavor['name'], self.context.project_id, instance['uuid']) self.assertEquals(expected_policy, vms_policy)
def test_launch_no_name(self): instance_uuid = utils.create_instance(self.context) blessed_instance = self.cobalt_api.bless_instance(self.context, instance_uuid) blessed_instance_uuid = blessed_instance["uuid"] launched_instance = self.cobalt_api.launch_instance(self.context, blessed_instance_uuid, params={}) name = launched_instance["display_name"] self.assertEqual(name, "None-0-clone") self.assertEqual(launched_instance["hostname"], "none-0-clone")
def test_migrate_bad_destination(self): instance_uuid = utils.create_instance(self.context, {"task_state":task_states.MIGRATING}) try: self.cobalt_api.migrate_instance(self.context, instance_uuid, "no_destination") self.fail("Should not be able to migrate a non-existent destination.") except exception.NovaException: pass
def test_launch_set_name(self): instance_uuid = utils.create_instance(self.context) blessed_instance = self.cobalt_api.bless_instance(self.context, instance_uuid) blessed_instance_uuid = blessed_instance['uuid'] launched_instance = self.cobalt_api.launch_instance(self.context, blessed_instance_uuid, params={'name': 'test instance'}) name = launched_instance['display_name'] self.assertEqual(name, 'test instance') self.assertEqual(launched_instance['hostname'], 'test-instance')
def test_launch_no_name(self): instance_uuid = utils.create_instance(self.context) blessed_instance = self.cobalt_api.bless_instance(self.context, instance_uuid) blessed_instance_uuid = blessed_instance['uuid'] launched_instance = self.cobalt_api.launch_instance(self.context, blessed_instance_uuid, params={}) name = launched_instance['display_name'] self.assertEqual(name, 'None-0-clone') self.assertEqual(launched_instance['hostname'], 'none-0-clone')
def test_launch_instance_filter_props(self): instance_uuid = utils.create_instance(self.context) blessed_instance = self.cobalt_api.bless_instance(self.context, instance_uuid) blessed_instance_uuid = blessed_instance["uuid"] launched_instance = self.cobalt_api.launch_instance( self.context, blessed_instance_uuid, params={"scheduler_hints": {"a": "b", "c": "d"}} ) launched_instance_uuid = launched_instance["uuid"] self.assertTrue({"scheduler_hints": {"a": "b", "c": "d"}} in utils.stored_hints[launched_instance_uuid])
def test_launch_instance_host_az(self): instance_uuid = utils.create_instance(self.context) blessed_instance = self.cobalt_api.bless_instance(self.context, instance_uuid) blessed_instance_uuid = blessed_instance["uuid"] launched_instance = self.cobalt_api.launch_instance( self.context, blessed_instance_uuid, params={"availability_zone": "nova:myhost"} ) launched_instance_uuid = launched_instance["uuid"] self.assertTrue(len(self.mock_rpc.cast_log["launch_instance"]["cobalt.myhost"][launched_instance_uuid]) > 0)
def test_launch_with_user_data(self): instance_uuid = utils.create_instance(self.context) blessed_instance = self.cobalt_api.bless_instance(self.context, instance_uuid) blessed_instance_uuid = blessed_instance['uuid'] test_data = "here is some test user data" test_data_encoded = base64.b64encode(test_data) launched_instance = self.cobalt_api.launch_instance(self.context, blessed_instance_uuid, params={'user_data': test_data_encoded}) user_data = launched_instance['user_data'] self.assertEqual(user_data, test_data_encoded)
def test_launch_not_blessed_image(self): instance_uuid = utils.create_instance(self.context) try: self.cobalt_api.launch_instance(self.context, instance_uuid) self.fail("Should not be able to launch and instance that has not been blessed.") except exception.NovaException, e: pass # Success!
def test_reset_host_non_migrating_instance(self): host = "test-host" instance_uuid = utils.create_instance(self.context, {'host': host}) self.cobalt.host = host self.cobalt._refresh_host(self.context) instance = db.instance_get_by_uuid(self.context, instance_uuid) self.assertEquals(host, instance['host']) self.assertEquals(None, instance['task_state'])
def test_vms_policy_generation_custom_flavor(self): flavor = utils.create_flavor() instance_uuid = utils.create_instance( self.context, {'instance_type_id': flavor['id']}) instance = db.instance_get_by_uuid(self.context, instance_uuid) vms_policy = self.cobalt._generate_vms_policy_name( self.context, instance, instance) expected_policy = ';blessed=%s;;flavor=%s;;tenant=%s;;uuid=%s;' \ %(instance['uuid'], flavor['name'], self.context.project_id, instance['uuid']) self.assertEquals(expected_policy, vms_policy)
def test_migrate_instance_no_destination(self): instance_uuid = utils.create_instance(self.context, {"vm_state":vm_states.ACTIVE}) # Create a service so that one can be found by the api. utils.create_cobalt_service(self.context) self.cobalt_api.migrate_instance(self.context, instance_uuid, None) instance_ref = db.instance_get_by_uuid(self.context, instance_uuid) self.assertEquals(task_states.MIGRATING, instance_ref['task_state']) self.assertEquals(vm_states.ACTIVE, instance_ref['vm_state'])
def test_migrate_instance_with_destination(self): instance_uuid = utils.create_instance(self.context, {"vm_state":vm_states.ACTIVE}) gc_service = utils.create_cobalt_service(self.context) dest = gc_service['host'] self.cobalt_api.migrate_instance(self.context, instance_uuid, dest) instance_ref = db.instance_get_by_uuid(self.context, instance_uuid) self.assertEquals(task_states.MIGRATING, instance_ref['task_state']) self.assertEquals(vm_states.ACTIVE, instance_ref['vm_state'])
def test_migrate_migrating_instance(self): instance_uuid = utils.create_instance(self.context, {"task_state":task_states.MIGRATING}) # Create a service so that one can be found by the api. utils.create_cobalt_service(self.context) try: self.cobalt_api.migrate_instance(self.context, instance_uuid, None) self.fail("Should not be able to migrate a migrating instance.") except exception.NovaException: pass
def test_migrate_inactive_instance(self): instance_uuid = utils.create_instance(self.context, {"vm_state":vm_states.BUILDING}) # Create a service so that one can be found by the api. utils.create_cobalt_service(self.context) try: self.cobalt_api.migrate_instance(self.context, instance_uuid, None) self.fail("Should not be able to migrate an inactive instance.") except exception.NovaException: pass
def test_launch_set_name(self): instance_uuid = utils.create_instance(self.context) blessed_instance = self.cobalt_api.bless_instance(self.context, instance_uuid) blessed_instance_uuid = blessed_instance["uuid"] launched_instance = self.cobalt_api.launch_instance( self.context, blessed_instance_uuid, params={"name": "test instance"} ) name = launched_instance["display_name"] self.assertEqual(name, "test instance") self.assertEqual(launched_instance["hostname"], "test-instance")
def test_launch_with_security_groups(self): instance_uuid = utils.create_instance(self.context) blessed_instance = self.cobalt_api.bless_instance(self.context, instance_uuid) blessed_instance_uuid = blessed_instance["uuid"] sg = utils.create_security_group(self.context, {"name": "test-sg", "description": "test security group"}) inst = self.cobalt_api.launch_instance( self.context, blessed_instance_uuid, params={"security_groups": ["test-sg"]} ) self.assertEqual(inst["security_groups"][0].id, sg.id) self.assertEqual(1, len(inst["security_groups"]))
def test_launch_with_key(self): instance_uuid = utils.create_instance(self.context) blessed_instance = self.cobalt_api.bless_instance(self.context, instance_uuid) blessed_instance_uuid = blessed_instance["uuid"] db.key_pair_create( self.context, {"name": "foo", "public_key": "bar", "user_id": self.context.user_id, "fingerprint": ""} ) inst = self.cobalt_api.launch_instance(self.context, blessed_instance_uuid, params={"key_name": "foo"}) self.assertEqual(inst["key_name"], "foo") self.assertEqual(inst["key_data"], "bar")
def test_launch_instance_filter_props(self): instance_uuid = utils.create_instance(self.context) blessed_instance = self.cobalt_api.bless_instance(self.context, instance_uuid) blessed_instance_uuid = blessed_instance['uuid'] launched_instance = self.cobalt_api.launch_instance(self.context, blessed_instance_uuid, params = { 'scheduler_hints' : {'a':'b','c':'d'}, }) launched_instance_uuid = launched_instance['uuid'] self.assertTrue({'scheduler_hints': {'a':'b','c':'d'}} in utils.stored_hints[launched_instance_uuid])
def test_launch_instance_host_az(self): instance_uuid = utils.create_instance(self.context) blessed_instance = self.cobalt_api.bless_instance(self.context, instance_uuid) blessed_instance_uuid = blessed_instance['uuid'] launched_instance = self.cobalt_api.launch_instance(self.context, blessed_instance_uuid, params = { 'availability_zone' : 'nova:myhost', }) launched_instance_uuid = launched_instance['uuid'] self.assertTrue(len(self.mock_rpc.cast_log['launch_instance']['cobalt.myhost'][launched_instance_uuid]) > 0)
def test_bless_instance_twice(self): instance_uuid = utils.create_instance(self.context) num_instance_before = len(db.instance_get_all(self.context)) self.cobalt_api.bless_instance(self.context, instance_uuid) self.cobalt_api.bless_instance(self.context, instance_uuid) instances = db.instance_get_all(self.context) self.assertTrue(len(instances) == num_instance_before + 2, "There should be 2 more instances because we blessed twice.")
def test_launch_with_nonexistent_key(self): instance_uuid = utils.create_instance(self.context) blessed_instance = self.cobalt_api.bless_instance(self.context, instance_uuid) blessed_instance_uuid = blessed_instance["uuid"] try: inst = self.cobalt_api.launch_instance( self.context, blessed_instance_uuid, params={"key_name": "nonexistent"} ) self.fail("Expected KeypairNotFound") except exception.KeypairNotFound: pass
def test_launch_with_nonexistent_key(self): instance_uuid = utils.create_instance(self.context) blessed_instance = self.cobalt_api.bless_instance(self.context, instance_uuid) blessed_instance_uuid = blessed_instance['uuid'] try: inst = self.cobalt_api.launch_instance(self.context, blessed_instance_uuid, params={'key_name': 'nonexistent'}) self.fail('Expected KeypairNotFound') except exception.KeypairNotFound: pass
def test_launch_with_security_groups(self): instance_uuid = utils.create_instance(self.context) blessed_instance = self.cobalt_api.bless_instance(self.context, instance_uuid) blessed_instance_uuid = blessed_instance['uuid'] sg = utils.create_security_group(self.context, {'name': 'test-sg', 'description': 'test security group'}) inst = self.cobalt_api.launch_instance(self.context, blessed_instance_uuid, params={'security_groups': ['test-sg']}) self.assertEqual(inst['security_groups'][0].id, sg.id) self.assertEqual(1, len(inst['security_groups']))
def test_bless_quota(self): def assert_quotas(expected_increased): _pre_usages = dict(pre_usages) _pre_usages.pop('project_id', None) post_usages = sqlalchemy_db.quota_usage_get_all_by_project(self.context, self.context.project_id) # Need to assert something about the quota consumption for quota_key in ['instances', 'ram', 'cores']: pre_usage = _pre_usages.pop(quota_key) self.assertEquals(pre_usage.get('in_use',0) + expected_increased[quota_key], post_usages[quota_key].get('in_use',0)) self.assertEquals(pre_usage.get('reserved',0), post_usages[quota_key].get('reserved',0)) for key, quota_usage in _pre_usages.iteritems(): self.assertEquals(quota_usage.get('reserved', 0), post_usages[key].get('reserved', 0)) self.assertEquals(quota_usage.get('in_use', 0), post_usages[key].get('in_use', 0)) instance_uuid = utils.create_instance(self.context) instance = db.instance_get_by_uuid(self.context, instance_uuid) # Set the quota so that we can have two blessed instances (+ the instance we are blessing). db.quota_create(self.context, self.context.project_id, 'ram', 3 * instance['memory_mb']) pre_usages = sqlalchemy_db.quota_usage_get_all_by_project(self.context, self.context.project_id) # The amount of resources a blessed instance consumes. blessed_uuids = [] for i in range(1, 3): blessed_uuids.append( self.cobalt_api.bless_instance(self.context, instance_uuid)['uuid']) expected = dict(zip(['instances', 'ram', 'cores'], map(lambda x: i * x, [1, instance['memory_mb'], instance['vcpus']]))) assert_quotas(expected) try: self.cobalt_api.bless_instance(self.context, instance_uuid) self.fail("We should not have the quota to bless one more instance.") except exception.TooManyInstances: pass # Discard the blessed uuid and ensure that the quota increases. remaining = len(blessed_uuids) for blessed_uuid in blessed_uuids: self.cobalt_api.discard_instance(self.context, blessed_uuid) remaining -= 1 expected = dict(zip(['instances', 'ram', 'cores'], map(lambda x: remaining * x, [1, instance['memory_mb'], instance['vcpus']]))) assert_quotas(expected)
def test_check_delete(self): instance_uuid = utils.create_instance(self.context) # This should not raise an error because the instance is not blessed. self.cobalt_api.check_delete(self.context, instance_uuid) blessed_instance_uuid = utils.create_blessed_instance(self.context) try: self.cobalt_api.check_delete(self.context, blessed_instance_uuid) self.fail("Check delete should fail for a blessed instance.") except exception.NovaException: pass
def test_launch_with_key(self): instance_uuid = utils.create_instance(self.context) blessed_instance = self.cobalt_api.bless_instance(self.context, instance_uuid) blessed_instance_uuid = blessed_instance['uuid'] db.key_pair_create(self.context, {'name': 'foo', 'public_key': 'bar', 'user_id': self.context.user_id, 'fingerprint': ''}) inst = self.cobalt_api.launch_instance(self.context, blessed_instance_uuid, params={'key_name': 'foo'}) self.assertEqual(inst['key_name'], 'foo') self.assertEqual(inst['key_data'], 'bar')
def test_bless_a_non_active_instance(self): instance_uuid = utils.create_instance(self.context, {'vm_state':vm_states.BUILDING}) no_exception = False try: self.cobalt_api.bless_instance(self.context, instance_uuid) no_exception = True except: pass # success if no_exception: self.fail("Should not be able to bless an instance in a non-active state")
def test_reset_host_different_host_instance(self): host = "test-host" instance_uuid = utils.create_instance(self.context, { 'task_state': task_states.MIGRATING, 'host': host }) self.cobalt.host = 'different-host' self.cobalt._refresh_host(self.context) instance = db.instance_get_by_uuid(self.context, instance_uuid) self.assertEquals(host, instance['host']) self.assertEquals(task_states.MIGRATING, instance['task_state'])
def test_bless_a_blessed_instance(self): instance_uuid = utils.create_instance(self.context) blessed_instance = self.cobalt_api.bless_instance(self.context, instance_uuid) blessed_uuid = blessed_instance['uuid'] no_exception = False try: self.cobalt_api.bless_instance(self.context, blessed_uuid) no_exception = True except Exception: pass # success if no_exception: self.fail("Should not be able to bless a blessed instance.")
def test_launch_instance_filter_and_az(self): instance_uuid = utils.create_instance(self.context) blessed_instance = self.cobalt_api.bless_instance(self.context, instance_uuid) blessed_instance_uuid = blessed_instance['uuid'] launched_instance = self.cobalt_api.launch_instance(self.context, blessed_instance_uuid, params = { 'availability_zone' : 'nova:filter_host', 'scheduler_hints' : {'a':'b','c':'d'}, }) launched_instance_uuid = launched_instance['uuid'] self.assertTrue({'scheduler_hints' : {'a': 'b', 'c': 'd'}, 'force_hosts' : ['filter_host'], 'availability_zone' : 'nova' } in utils.stored_hints[launched_instance_uuid]) self.assertTrue(len(self.mock_rpc.cast_log['launch_instance']['cobalt.filter_host'][launched_instance_uuid]) > 0)
def test_discard_a_blessed_instance_with_remaining_launched_ones(self): instance_uuid = utils.create_instance(self.context) bless_instance = self.cobalt_api.bless_instance(self.context, instance_uuid) blessed_uuid = bless_instance['uuid'] self.cobalt_api.launch_instance(self.context, blessed_uuid) no_exception = False try: self.cobalt_api.discard_instance(self.context, blessed_uuid) no_exception = True except: pass # success if no_exception: self.fail("Should not be able to discard a blessed instance while launched ones still remain.")
def test_reset_host_not_local_src(self): src_host = "src-test-host" dst_host = "dst-test-host" instance_uuid = utils.create_instance( self.context, { 'task_state': task_states.MIGRATING, 'host': src_host, 'system_metadata': { 'gc_src_host': src_host, 'gc_dst_host': dst_host } }) self.cobalt.host = src_host self.cobalt._refresh_host(self.context) instance = db.instance_get_by_uuid(self.context, instance_uuid) self.assertEquals(dst_host, instance['host']) self.assertEquals(task_states.MIGRATING, instance['task_state'])
def test_launch_default_security_group(self): sg = utils.create_security_group(self.context, {'name': 'test-sg', 'description': 'test security group'}) instance_uuid = utils.create_instance(self.context, {'security_groups': [sg['name']]}) blessed_instance = self.cobalt_api.bless_instance(self.context, instance_uuid) blessed_instance_uuid = blessed_instance['uuid'] inst = self.cobalt_api.launch_instance(self.context, blessed_instance_uuid, params={}) self.assertEqual(len(inst['security_groups']), 1) # Ensure that with not security group provided, the existing one is not # inherited, but the 'default' security group is applied to the # instance. self.assertNotEqual(inst['security_groups'][0].id, sg.id) default_sg = db.security_group_get_by_name(self.context, self.context.project_id, 'default') self.assertEqual(inst['security_groups'][0].id, default_sg.id)