def test_create_image_from_instance(self, mock_glance, mock_cinder, mock_neutron, mock_nova): nova_util = nova_helper.NovaHelper() instance = self.fake_server(self.instance_uuid) image = mock.MagicMock() setattr(instance, 'OS-EXT-SRV-ATTR:host', self.source_node) setattr(instance, 'OS-EXT-STS:vm_state', "stopped") self.fake_nova_find_list(nova_util, find=instance, list=instance) image_uuid = 'fake-image-uuid' nova_util.nova.servers.create_image.return_value = image glance_client = mock.MagicMock() mock_glance.return_value = glance_client glance_client.images = {image_uuid: image} instance = nova_util.create_image_from_instance( self.instance_uuid, "Cirros" ) self.assertIsNotNone(instance) nova_util.glance.images.get.return_value = None instance = nova_util.create_image_from_instance( self.instance_uuid, "Cirros" ) self.assertIsNone(instance)
def test_watcher_non_live_migrate_keep_image(self, mock_glance, mock_cinder, mock_neutron, mock_nova): nova_util = nova_helper.NovaHelper() nova_servers = nova_util.nova.servers instance = self.fake_server(self.instance_uuid) setattr(instance, 'OS-EXT-SRV-ATTR:host', self.source_node) setattr(instance, 'OS-EXT-STS:vm_state', "stopped") addresses = mock.MagicMock() network_type = mock.MagicMock() networks = [] networks.append(("lan", network_type)) addresses.items.return_value = networks attached_volumes = mock.MagicMock() setattr(instance, 'addresses', addresses) setattr(instance, "os-extended-volumes:volumes_attached", attached_volumes) self.fake_nova_find_list(nova_util, find=instance, list=instance) nova_servers.create_image.return_value = utils.generate_uuid() nova_util.glance.images.get.return_value = mock.MagicMock( status='active') is_success = nova_util.watcher_non_live_migrate_instance( self.instance_uuid, self.destination_node, keep_original_image_name=False) self.assertTrue(is_success)
def test_wait_for_instance_status(self, mock_glance, mock_cinder, mock_neutron, mock_nova): nova_util = nova_helper.NovaHelper() instance = self.fake_server(self.instance_uuid) # verify that the method will return True when the status of instance # is in the expected status. result = nova_util.wait_for_instance_status( instance, ('ACTIVE', 'ERROR'), 5, 10) self.assertTrue(result) # verify that the method will return False when the instance is None. result = nova_util.wait_for_instance_status( None, ('ACTIVE', 'ERROR'), 5, 10) self.assertFalse(result) # verify that the method will return False when the status of instance # is not in the expected status. self.fake_nova_find_list(nova_util, fake_find=instance, fake_list=None) result = nova_util.wait_for_instance_status( instance, ('ERROR'), 5, 10) self.assertFalse(result)
def test_abort_live_migrate_instance(self, mock_glance, mock_cinder, mock_neutron, mock_nova): nova_util = nova_helper.NovaHelper() server = self.fake_server(self.instance_uuid) setattr(server, 'OS-EXT-SRV-ATTR:host', self.source_node) setattr(server, 'OS-EXT-STS:task_state', None) migration = self.fake_migration(2) self.fake_nova_migration_list(nova_util, list=migration) self.fake_nova_find_list(nova_util, find=server, list=server) self.assertTrue( nova_util.abort_live_migrate(self.instance_uuid, self.source_node, self.destination_node)) setattr(server, 'OS-EXT-SRV-ATTR:host', self.destination_node) self.assertFalse( nova_util.abort_live_migrate(self.instance_uuid, self.source_node, self.destination_node)) setattr(server, 'status', 'ERROR') self.assertRaises(Exception, nova_util.abort_live_migrate, self.instance_uuid, self.source_node, self.destination_node)
def test_get_compute_node_list(self, mock_glance, mock_cinder, mock_neutron, mock_nova): nova_util = nova_helper.NovaHelper() hypervisor1_id = utils.generate_uuid() hypervisor1_name = "fake_hypervisor_1" hypervisor1 = self.fake_hypervisor(hypervisor1_id, hypervisor1_name, hypervisor_type="QEMU") hypervisor2_id = utils.generate_uuid() hypervisor2_name = "fake_ironic" hypervisor2 = self.fake_hypervisor(hypervisor2_id, hypervisor2_name, hypervisor_type="ironic") nova_util.nova.hypervisors.list.return_value = [ hypervisor1, hypervisor2 ] compute_nodes = nova_util.get_compute_node_list() # baremetal node should be removed self.assertEqual(1, len(compute_nodes)) self.assertEqual(hypervisor1_name, compute_nodes[0].hypervisor_hostname)
def test_get_compute_node_by_hostname( self, mock_glance, mock_cinder, mock_neutron, mock_nova): nova_util = nova_helper.NovaHelper() hypervisor_id = utils.generate_uuid() hypervisor_name = "fake_hypervisor_1" hypervisor = self.fake_hypervisor(hypervisor_id, hypervisor_name) self.fake_nova_hypervisor_list( nova_util, fake_find=hypervisor, fake_list=[hypervisor]) nova_util.nova.hypervisors.search.return_value = [hypervisor] # verify that the compute node can be obtained normally by name self.assertEqual( nova_util.get_compute_node_by_hostname(hypervisor_name), hypervisor) # verify that getting the compute node with the wrong name # will throw an exception. self.assertRaises( exception.ComputeNodeNotFound, nova_util.get_compute_node_by_hostname, "exception_hypervisor_1") # verify that when the result of getting the compute node is empty # will throw an exception. nova_util.nova.hypervisors.search.return_value = [] self.assertRaises( exception.ComputeNodeNotFound, nova_util.get_compute_node_by_hostname, hypervisor_name)
def test_create_instance(self, mock_glance, mock_cinder, mock_neutron, mock_nova): nova_util = nova_helper.NovaHelper() instance = self.fake_server(self.instance_uuid) nova_util.nova.servers.create.return_value = instance nova_util.nova.servers.get.return_value = instance create_instance = nova_util.create_instance(self.source_node) self.assertIsNotNone(create_instance) self.assertEqual(create_instance, instance) # verify that the method create_instance will return None when # the method findall raises exception. nova_util.nova.keypairs.findall.side_effect = nvexceptions.NotFound( 404) instance = nova_util.create_instance(self.source_node) self.assertIsNone(instance) nova_util.nova.keypairs.findall.side_effect = None # verify that the method create_instance will return None when # the method get raises exception. nova_util.glance.images.get.side_effect = glexceptions.NotFound(404) instance = nova_util.create_instance(self.source_node) self.assertIsNone(instance) nova_util.glance.images.get.side_effect = None # verify that the method create_instance will return None when # the method find raises exception. nova_util.nova.flavors.find.side_effect = nvexceptions.NotFound(404) instance = nova_util.create_instance(self.source_node) self.assertIsNone(instance) nova_util.nova.flavors.find.side_effect = None # verify that the method create_instance will return None when # the method get_security_group_id_from_name return None. with mock.patch.object(nova_util, 'get_security_group_id_from_name', return_value=None) as mock_security_group_id: instance = nova_util.create_instance(self.source_node) self.assertIsNone(instance) mock_security_group_id.assert_called_once_with("default") # verify that the method create_instance will return None when # the method get_network_id_from_name return None. with mock.patch.object(nova_util, 'get_network_id_from_name', return_value=None) as mock_get_network_id: instance = nova_util.create_instance(self.source_node) self.assertIsNone(instance) mock_get_network_id.assert_called_once_with("demo-net") # verify that the method create_instance will not return None when # the method wait_for_instance_status return True. with mock.patch.object(nova_util, 'wait_for_instance_status', return_value=True) as mock_instance_status: instance = nova_util.create_instance(self.source_node) self.assertIsNotNone(instance) mock_instance_status.assert_called_once_with( mock.ANY, ('ACTIVE', 'ERROR'), 5, 10)
def test_live_migrate_instance(self, mock_glance, mock_cinder, mock_neutron, mock_nova): nova_util = nova_helper.NovaHelper() server = self.fake_server(self.instance_uuid) setattr(server, 'OS-EXT-SRV-ATTR:host', self.destination_node) self.fake_nova_find_list(nova_util, fake_find=server, fake_list=server) is_success = nova_util.live_migrate_instance(self.instance_uuid, self.destination_node) self.assertTrue(is_success) setattr(server, 'OS-EXT-SRV-ATTR:host', self.source_node) self.fake_nova_find_list(nova_util, fake_find=server, fake_list=None) is_success = nova_util.live_migrate_instance(self.instance_uuid, self.destination_node) self.assertFalse(is_success) # verify that the method will return False when the instance does # not exist. setattr(server, 'OS-EXT-SRV-ATTR:host', self.source_node) self.fake_nova_find_list(nova_util, fake_find=None, fake_list=None) is_success = nova_util.live_migrate_instance(self.instance_uuid, self.destination_node) self.assertFalse(is_success) # verify that the method will return False when the instance status # is in other cases. setattr(server, 'status', 'fake_status') self.fake_nova_find_list(nova_util, fake_find=server, fake_list=None) is_success = nova_util.live_migrate_instance(self.instance_uuid, None) self.assertFalse(is_success)
def test_execute_with_failed(self): nova_util = nova_helper.NovaHelper() instance = "31b9dd5c-b1fd-4f61-9b68-a47096326dac" nova_util.nova.servers.get.return_value = instance action_plan = obj_utils.create_test_action_plan( self.context, audit_id=self.audit.id, strategy_id=self.strategy.id, state=objects.action.State.ONGOING) action = obj_utils.create_test_action( self.context, action_plan_id=action_plan.id, state=objects.action.State.ONGOING, action_type='migrate', input_parameters={ "resource_id": instance, "migration_type": "live", "destination_node": "host2", "source_node": "host1" }) action_container = tflow.TaskFlowActionContainer(db_action=action, engine=self.engine) result = action_container.execute() self.assertFalse(result) self.assertTrue(action.state, objects.action.State.FAILED)
def setUp(self): super(TestActionScheduling, self).setUp() self.goal = db_utils.create_test_goal(name="dummy") self.strategy = db_utils.create_test_strategy(name="dummy") self.audit = db_utils.create_test_audit(uuid=utils.generate_uuid(), strategy_id=self.strategy.id) self.planner = pbase.WorkloadStabilizationPlanner(mock.Mock()) self.nova_helper = nova_helper.NovaHelper(mock.Mock())
def test_set_host_offline(self, mock_glance, mock_cinder, mock_neutron, mock_nova): nova_util = nova_helper.NovaHelper() host = mock.MagicMock() nova_util.nova.hosts = mock.MagicMock() nova_util.nova.hosts.get.return_value = host result = nova_util.set_host_offline("rennes") self.assertEqual(result, True)
def __init__(self, osc): self.osc = osc self.model = None self.model_scope = dict() self.no_model_scope_flag = False self.nova = osc.nova() self.nova_helper = nova_helper.NovaHelper(osc=self.osc) self.placement_helper = placement_helper.PlacementHelper(osc=self.osc) self.executor = threading.DecisionEngineThreadPool()
def test_create_instance(self, mock_glance, mock_cinder, mock_neutron, mock_nova): nova_util = nova_helper.NovaHelper() instance = self.fake_server(self.instance_uuid) nova_util.nova.services.create.return_value = instance nova_util.nova.services.get.return_value = instance instance = nova_util.create_instance(self.source_node) self.assertIsNotNone(instance)
def test_watcher_non_live_migrate_instance_not_found( self, mock_glance, mock_cinder, mock_neutron, mock_nova): nova_util = nova_helper.NovaHelper() self.fake_nova_find_list(nova_util, find=None, list=None) is_success = nova_util.watcher_non_live_migrate_instance( self.instance_uuid, self.destination_node) self.assertFalse(is_success)
def _nova_manage_service(self, state): if state is None: raise exception.IllegalArgumentException( message=_("The target state is not defined")) nova = nova_helper.NovaHelper(osc=self.osc) if state is True: return nova.enable_service_nova_compute(self.host) else: return nova.disable_service_nova_compute(self.host)
def test_get_compute_node_by_uuid(self, mock_glance, mock_cinder, mock_neutron, mock_nova): nova_util = nova_helper.NovaHelper() hypervisor_id = utils.generate_uuid() hypervisor_name = "fake_hypervisor_1" hypervisor = self.fake_hypervisor(hypervisor_id, hypervisor_name) nova_util.nova.hypervisors.get.return_value = hypervisor # verify that the compute node can be obtained normally by id self.assertEqual(nova_util.get_compute_node_by_uuid(hypervisor_id), hypervisor)
def test_watcher_non_live_migrate_instance_not_found( self, mock_glance, mock_cinder, mock_neutron, mock_nova): nova_util = nova_helper.NovaHelper() nova_util.nova.servers.list.return_value = [] nova_util.nova.servers.find.return_value = None is_success = nova_util.watcher_non_live_migrate_instance( self.instance_uuid, self.destination_hypervisor) self.assertEqual(is_success, False)
def test_live_migrate_instance(self, mock_glance, mock_cinder, mock_neutron, mock_nova): nova_util = nova_helper.NovaHelper() server = mock.MagicMock() server.id = self.instance_uuid nova_util.nova.servers = mock.MagicMock() nova_util.nova.servers.list.return_value = [server] instance = nova_util.live_migrate_instance(self.instance_uuid, self.destination_hypervisor) self.assertIsNotNone(instance)
def test_watcher_non_live_migrate_instance_volume(self, mock_glance, mock_cinder, mock_neutron, mock_nova): nova_util = nova_helper.NovaHelper() instance = mock.MagicMock(id=self.instance_uuid) setattr(instance, 'OS-EXT-SRV-ATTR:host', self.source_hypervisor) nova_util.nova.servers.list.return_value = [instance] nova_util.nova.servers.find.return_value = instance instance = nova_util.watcher_non_live_migrate_instance( self.instance_uuid, self.destination_hypervisor) self.assertIsNotNone(instance)
def abort(self): nova = nova_helper.NovaHelper(osc=self.osc) instance = nova.find_instance(self.instance_uuid) if instance: if self.migration_type == self.COLD_MIGRATION: return self._abort_cold_migrate(nova) elif self.migration_type == self.LIVE_MIGRATION: return self._abort_live_migrate( nova, source=self.source_node, destination=self.destination_node) else: raise exception.InstanceNotFound(name=self.instance_uuid)
def test_non_live_migrate_instance_no_destination_node( self, mock_glance, mock_cinder, mock_neutron, mock_nova): nova_util = nova_helper.NovaHelper() server = self.fake_server(self.instance_uuid) setattr(server, 'OS-EXT-SRV-ATTR:host', self.source_node) self.destination_node = None self.fake_nova_find_list(nova_util, find=server, list=server) self.fake_cold_migrate(server) self.fake_confirm_resize(server) is_success = nova_util.watcher_non_live_migrate_instance( self.instance_uuid, self.destination_node) self.assertTrue(is_success)
def test_get_flavor_instance(self, mock_glance, mock_cinder, mock_neutron, mock_nova): nova_util = nova_helper.NovaHelper() instance = self.fake_server(self.instance_uuid) flavor = {'id': 1, 'name': 'm1.tiny', 'ram': 512, 'vcpus': 1, 'disk': 0, 'ephemeral': 0} instance.flavor = flavor nova_util.nova.flavors.get.return_value = flavor cache = flavor nova_util.get_flavor_instance(instance, cache) self.assertEqual(instance.flavor['name'], cache['name'])
def validate_parents(self, resource_action_map, action): nova = nova_helper.NovaHelper(osc=self.osc) instance_uuid = action['input_parameters']['resource_id'] parent_actions = resource_action_map.get(instance_uuid) host_of_instance = nova.get_hostname( nova.get_instance_by_uuid(instance_uuid)[0]) self._mapping(resource_action_map, host_of_instance, action['uuid'], 'resize') if parent_actions: return [x[0] for x in parent_actions] else: return []
def test_live_migrate_instance_no_destination_node(self, mock_glance, mock_cinder, mock_neutron, mock_nova): nova_util = nova_helper.NovaHelper() server = self.fake_server(self.instance_uuid) self.destination_node = None self.fake_nova_find_list(nova_util, find=server, list=server) self.fake_live_migrate(server) is_success = nova_util.live_migrate_instance(self.instance_uuid, self.destination_node) self.assertTrue(is_success)
def test_stop_instance(self, mock_glance, mock_cinder, mock_neutron, mock_nova): nova_util = nova_helper.NovaHelper() instance_id = utils.generate_uuid() server = mock.MagicMock() server.id = instance_id setattr(server, 'OS-EXT-STS:vm_state', 'stopped') nova_util.nova.servers = mock.MagicMock() nova_util.nova.servers.find.return_value = server nova_util.nova.servers.list.return_value = [server] result = nova_util.stop_instance(instance_id) self.assertEqual(result, True)
def test_enable_service_nova_compute(self, mock_glance, mock_cinder, mock_neutron, mock_nova): nova_util = nova_helper.NovaHelper() nova_services = nova_util.nova.services nova_services.enable.return_value = mock.MagicMock(status='enabled') result = nova_util.enable_service_nova_compute('nanjing') self.assertTrue(result) nova_services.enable.return_value = mock.MagicMock(status='disabled') result = nova_util.enable_service_nova_compute('nanjing') self.assertFalse(result)
def test_live_migrate_instance(self, mock_glance, mock_cinder, mock_neutron, mock_nova): nova_util = nova_helper.NovaHelper() server = self.fake_server(self.instance_uuid) setattr(server, 'OS-EXT-SRV-ATTR:host', self.destination_node) self.fake_nova_find_list(nova_util, find=server, list=server) is_success = nova_util.live_migrate_instance(self.instance_uuid, self.destination_node) self.assertTrue(is_success) self.fake_nova_find_list(nova_util, find=server, list=None) is_success = nova_util.live_migrate_instance(self.instance_uuid, self.destination_node) self.assertFalse(is_success)
def test_resize_instance(self, mock_glance, mock_cinder, mock_neutron, mock_nova): nova_util = nova_helper.NovaHelper() server = self.fake_server(self.instance_uuid) setattr(server, 'status', 'VERIFY_RESIZE') self.fake_nova_find_list(nova_util, find=server, list=server) is_success = nova_util.resize_instance(self.instance_uuid, self.flavor_name) self.assertTrue(is_success) setattr(server, 'status', 'SOMETHING_ELSE') is_success = nova_util.resize_instance(self.instance_uuid, self.flavor_name) self.assertFalse(is_success)
def test_confirm_resize(self, mock_glance, mock_cinder, mock_neutron, mock_nova): nova_util = nova_helper.NovaHelper() instance = self.fake_server(self.instance_uuid) self.fake_nova_find_list(nova_util, fake_find=instance, fake_list=None) # verify that the method will return True when the status of instance # is not in the expected status. result = nova_util.confirm_resize(instance, instance.status) self.assertTrue(result) # verify that the method will return False when the status of instance # is not in the expected status. result = nova_util.confirm_resize(instance, "fake_status") self.assertFalse(result)
def test_swap_volume(self, mock_glance, mock_cinder, mock_neutron, mock_nova): nova_util = nova_helper.NovaHelper() server = self.fake_server(self.instance_uuid) self.fake_nova_find_list(nova_util, find=server, list=server) old_volume = self.fake_volume(status='in-use', attachments=[{ 'server_id': self.instance_uuid }]) new_volume = self.fake_volume(id=utils.generate_uuid(), status='in-use') result = nova_util.swap_volume(old_volume, new_volume) self.assertTrue(result)