def test_container_pause_fail(self, tag, side_effect, expected): """ container_pause pauses a contianer on a LXD host. Verify that an exception.NovaException is raised when there is an APIError. """ instance = stubs._fake_instance() instance = stubs._fake_instance() self.ml.container_suspend.side_effect = side_effect self.assertRaises(expected, self.session.container_pause, instance.name, instance)
def test_container_publish(self, tag, side_effect): image = mock.Mock() instance = stubs._fake_instance() self.ml.image_export.return_value = True self.assertTrue(self.session.container_publish(image, instance)) calls = [mock.call.container_publish(image)] self.assertEqual(calls, self.ml.method_calls)
def test_configure_network_devices(self): instance = stubs._fake_instance() self.assertEqual( None, self.container_config.configure_network_devices({}, instance, network_info=[]))
def test_container_snapshot_fail(self, tag, side_effect, expected): snapshot = mock.Mock() instance = stubs._fake_instance() self.ml.container_snapshot_create.side_effect = side_effect self.assertRaises(expected, self.session.container_snapshot, instance.name, snapshot)
def test_fetch_image(self, mock_download): context = mock.Mock() image_meta = mock.Mock() instance = stubs._fake_instance() self.assertEqual(None, self.image._fetch_image(context, image_meta, instance))
def test_finish_migration(self): context = mock.Mock() migration = {'source_compute': 'fake-source', 'dest_compute': 'fake-dest'} instance = stubs._fake_instance() bdevice_info = mock.Mock() disk_info = mock.Mock() network_info = mock.Mock() with contextlib.nested( mock.patch.object(container_client.LXDContainerClient, 'client'), mock.patch.object(container_utils.LXDContainerUtils, 'container_stop'), mock.patch.object(container_utils.LXDContainerUtils, 'container_init'), mock.patch.object(container_utils.LXDContainerUtils, 'container_destroy'), ) as ( container_defined, container_stop, container_init, container_destroy ): self.assertEqual(None, (self.migrate.finish_migration(context, migration, instance, disk_info, network_info, bdevice_info)))
def test_container_rescue(self): context = mock.Mock() instance = stubs._fake_instance() network_info = mock.Mock() image_meta = mock.Mock() rescue_password = mock.Mock() with test.nested( mock.patch.object(session.LXDAPISession, 'container_defined'), mock.patch.object(session.LXDAPISession, 'container_stop'), mock.patch.object(container_ops.LXDContainerOperations, '_container_local_copy'), mock.patch.object(session.LXDAPISession, 'container_destroy'), mock.patch.object(container_ops.LXDContainerOperations, 'spawn'), ) as ( mock_container_defined, mock_container_stop, mock_container_copy, mock_container_destroy, mock_spawn ): self.assertEqual(None, self.operations.rescue(context, instance, network_info, image_meta, rescue_password)) mock_container_defined.assert_called_once_with(instance.name, instance)
def test_create_container(self, tag, rescue, network_info): instance = stubs._fake_instance() injected_files = mock.Mock() block_device_info = mock.Mock() need_vif_plugged = mock.Mock() self.ml.container_defined.return_value = True with contextlib.nested( mock.patch.object(container_config.LXDContainerConfig, 'create_container'), mock.patch.object(container_utils.LXDContainerUtils, 'container_init'), mock.patch.object(self.container_ops, 'start_container') ) as ( create_container, container_init, start_container ): self.assertEqual(None, self.container_ops.create_container( instance, injected_files, network_info, block_device_info, rescue, need_vif_plugged)) create_container.called_assert_called_once_with( instance, injected_files, block_device_info, rescue) print(container_init.method_calls) container_init.called_assert_called_once_with( container_config, instance.host)
def test_create_instance_initfail(self): instance = stubs._fake_instance() self.ml.container_init.side_effect = ( lxd_exception.APIError('Fake', 500)) self.assertEqual(None, self.container_ops.create_container( instance, [], [], {}, None, True))
def test_container_init_failure(self): config = mock.Mock() instance = stubs._fake_instance() host = mock.Mock() with contextlib.nested( mock.patch.object(container_client.LXDContainerClient, 'container_init'), mock.patch.object(container_client.LXDContainerClient, 'container_wait'), mock.patch.object(container_client.LXDContainerClient, 'container_operation_info'), ) as ( container_init, container_wait, container_operation_info, ): container_init.return_value = (200, fake_api.fake_operation()) container_operation_info.return_value = ( 200, fake_api.fake_operation_info_failed()) self.assertRaises(exception.NovaException, self.container_utils.container_init, config, instance, host) self.assertTrue(container_init) self.assertTrue(container_wait) self.assertTrue(container_operation_info)
def test_profile_delete(self): instance = stubs._fake_instance() self.ml.profile_defined.return_value = True self.ml.profile_delete.return_value = \ (200, fake_api.fake_standard_return()) self.assertEqual(None, self.session.profile_delete(instance))
def test_destroy(self): instance = stubs._fake_instance() context = mock.Mock() network_info = mock.Mock() with contextlib.nested( mock.patch.object(container_utils.LXDContainerUtils, 'container_stop'), mock.patch.object(container_utils.LXDContainerUtils, 'container_destroy'), mock.patch.object(self.connection, 'cleanup'), mock.patch.object(container_ops.LXDContainerOperations, '_unplug_vifs'), ) as ( container_stop, container_destroy, cleanup, unplug_vifs ): self.connection.destroy(context, instance, network_info) self.assertTrue(container_stop) self.assertTrue(container_destroy) self.assertTrue(cleanup) unplug_vifs.assert_called_with(instance, network_info, True)
def test_finish_migration(self, mo): context = mock.Mock() migration = {'source_compute': 'fake-source', 'dest_compute': 'fake-dest'} instance = stubs._fake_instance() bdevice_info = mock.Mock() disk_info = mock.Mock() network_info = mock.Mock() with test.nested( mock.patch.object(session.LXDAPISession, 'container_defined'), mock.patch.object(session.LXDAPISession, 'container_stop'), mock.patch.object(session.LXDAPISession, 'container_init'), ) as ( container_defined, container_stop, container_init ): def side_effect(*args, **kwargs): # XXX: rockstar (7 Dec 2015) - This mock is a little greedy, # and hits too many interfaces. It should become more specific # to the single places it needs to fully mocked. Truthiness of # the mock changes in py3. if args[0] == 'defined': return False container_defined.side_effect = side_effect self.assertEqual(None, (self.migrate.finish_migration(context, migration, instance, disk_info, network_info, bdevice_info)))
def test_get_config_bridge(self): instance = stubs._fake_instance() vif_data = copy.deepcopy(self.vif_data) vif_type = self.vif_driver.get_config(instance, vif_data) self.assertEqual(vif_type, {'bridge': 'qbr0123456789a', 'mac_address': '00:11:22:33:44:55'})
def test_create_container(self, tag, rescue, network_info): instance = stubs._fake_instance() injected_files = mock.Mock() block_device_info = mock.Mock() need_vif_plugged = mock.Mock() self.ml.container_defined.return_value = True with contextlib.nested( mock.patch.object(container_config.LXDContainerConfig, 'create_container'), mock.patch.object(container_utils.LXDContainerUtils, 'container_init'), mock.patch.object(self.container_ops, 'start_container')) as (create_container, container_init, start_container): self.assertEqual( None, self.container_ops.create_container(instance, injected_files, network_info, block_device_info, rescue, need_vif_plugged)) create_container.called_assert_called_once_with( instance, injected_files, block_device_info, rescue) print(container_init.method_calls) container_init.called_assert_called_once_with( container_config, instance.host)
def test_create_profile(self): """Test the create_profile method, Ensure that the correct method calls are preformed when creating a container profile. """ instance = stubs._fake_instance() rescue = False network_info = test_utils.get_test_network_info() config = mock.Mock() with test.nested( mock.patch.object(config.LXDContainerConfig, '_create_config'), mock.patch.object(config.LXDContainerConfig, '_create_network'), mock.patch.object(session.LXDAPISession, 'profile_create') ) as ( mock_create_config, mock_create_network, mock_profile_create ): (self.assertEqual(None, self.config.create_profile(instance, network_info, rescue)))
def test_fetch_imagei_fail(self, mock_execute): context = mock.Mock() instance = stubs._fake_instance() image_meta = {'name': 'new_image', 'id': 'fake_image'} with contextlib.nested( mock.patch.object(container_image.LXDContainerImage, '_image_defined'), mock.patch.object(container_image.IMAGE_API, 'download'), mock.patch.object(container_image.LXDContainerImage, '_get_lxd_manifest'), mock.patch.object(container_image.LXDContainerImage, '_image_upload'), mock.patch.object(container_image.LXDContainerImage, '_setup_alias'), mock.patch.object(os, 'unlink') ) as ( mock_image_defined, mock_image_download, mock_image_manifest, image_upload, setup_alias, os_unlink ): mock_image_defined.return_value = True self.assertEqual(None, self.container_image.setup_image(context, instance, image_meta)) self.assertFalse(mock_image_manifest.called)
def test_create_container_source(self): """Test the create_config mehtod. Ensure that the right image is used when creating a container. """ instance = stubs._fake_instance() config = self.config._get_container_source(instance) self.assertEqual(config, {'type': 'image', 'alias': 'fake_image'})
def test_migrate_disk_power_off_resize(self): self.flags(my_ip='fakeip') instance = stubs._fake_instance() network_info = mock.Mock() flavor = mock.Mock() context = mock.Mock() dest = 'fakeip' with test.nested( mock.patch.object(session.LXDAPISession, 'container_defined'), mock.patch.object(config.LXDContainerConfig, 'create_profile'), mock.patch.object(session.LXDAPISession, 'profile_update') ) as ( mock_container_defined, mock_create_profile, mock_profile_update ): self.assertEqual('', self.migrate.migrate_disk_and_power_off( context, instance, dest, flavor, network_info)) mock_container_defined.assert_called_once_with(instance.name, instance) mock_create_profile.assert_called_once_with(instance, network_info)
def test_image_defined(self): """Test the image is defined in the LXD hypervisor.""" instance = stubs._fake_instance() self.ml.alias_defined.return_value = True self.assertTrue(self.session.image_defined(instance)) calls = [mock.call.alias_defined(instance.image_ref)] self.assertEqual(calls, self.ml.method_calls)
def test_confirm_migration(self): migration = mock.Mock() instance = stubs._fake_instance() network_info = mock.Mock() with test.nested( mock.patch.object(session.LXDAPISession, 'container_defined'), mock.patch.object(session.LXDAPISession, 'profile_delete'), mock.patch.object(session.LXDAPISession, 'container_destroy'), mock.patch.object(operations.LXDContainerOperations, 'unplug_vifs'), ) as ( mock_container_defined, mock_profile_delete, mock_container_destroy, mock_unplug_vifs): self.assertEqual(None, self.migrate.confirm_migration(migration, instance, network_info)) mock_container_defined.assert_called_once_with(instance.name, instance) mock_profile_delete.assert_called_once_with(instance) mock_unplug_vifs.assert_called_once_with(instance, network_info)
def test_container_destroy(self, tag, container_defined, side_effect): """ container_destroy delete a container from the LXD Host. Check that the approiate pylxd calls are made. """ instance = stubs._fake_instance() if container_defined: self.ml.container_defined.return_value = container_defined self.ml.container_stop.return_value = side_effect self.ml.container_destroy.return_value = side_effect self.assertEqual(None, self.session.container_destroy(instance.name, instance)) calls = [ mock.call.container_defined(instance.name), mock.call.container_defined(instance.name), mock.call.container_stop(instance.name, -1), mock.call.wait_container_operation("/1.0/operation/1234", 200, -1), mock.call.container_destroy(instance.name), mock.call.wait_container_operation("/1.0/operation/1234", 200, -1), ] self.assertEqual(calls, self.ml.method_calls) if not container_defined: self.ml.container_defined.return_value = container_defined self.assertEqual(None, self.session.container_destroy(instance.name, instance)) calls = [mock.call.container_defined(instance.name)] self.assertEqual(calls, self.ml.method_calls)
def test_fetch_image(self, mock_execute): context = mock.Mock() instance = stubs._fake_instance() image_meta = {'name': 'new_image', 'id': 'fake_image'} with contextlib.nested( mock.patch.object(container_image.LXDContainerImage, '_image_defined'), mock.patch.object(container_image.IMAGE_API, 'download'), mock.patch.object(container_image.LXDContainerImage, '_get_lxd_manifest'), mock.patch.object(container_image.LXDContainerImage, '_image_upload'), mock.patch.object(container_image.LXDContainerImage, '_setup_alias'), mock.patch.object( os, 'unlink')) as (mock_image_defined, mock_image_download, mock_image_manifest, image_upload, setup_alias, os_unlink): mock_image_defined.return_value = False mock_image_manifest.return_value = \ '/fake/image/cache/fake_image-manifest.tar' self.assertEqual( None, self.container_image.setup_image(context, instance, image_meta)) mock_execute.assert_called_once_with( 'xz', '-9', '/fake/image/cache/' 'fake_image-manifest.tar')
def test_fetch_image(self, mock_execute): context = mock.Mock() instance = stubs._fake_instance() image_meta = {'name': 'new_image', 'id': 'fake_image'} with contextlib.nested( mock.patch.object(container_image.LXDContainerImage, '_image_defined'), mock.patch.object(container_image.IMAGE_API, 'download'), mock.patch.object(container_image.LXDContainerImage, '_get_lxd_manifest'), mock.patch.object(container_image.LXDContainerImage, '_image_upload'), mock.patch.object(container_image.LXDContainerImage, '_setup_alias'), mock.patch.object(os, 'unlink') ) as ( mock_image_defined, mock_image_download, mock_image_manifest, image_upload, setup_alias, os_unlink ): mock_image_defined.return_value = False mock_image_manifest.return_value = \ '/fake/image/cache/fake_image-manifest.tar' self.assertEqual(None, self.container_image.setup_image(context, instance, image_meta)) mock_execute.assert_called_once_with('xz', '-9', '/fake/image/cache/' 'fake_image-manifest.tar')
def test_container_destroy(self, tag, container_defined, side_effect): """ container_destroy delete a container from the LXD Host. Check that the approiate pylxd calls are made. """ instance = stubs._fake_instance() if container_defined: self.ml.container_defined.return_value = container_defined self.ml.container_stop.return_value = side_effect self.ml.container_destroy.return_value = side_effect self.assertEqual( None, self.session.container_destroy(instance.name, instance)) calls = [ mock.call.container_defined(instance.name), mock.call.container_defined(instance.name), mock.call.container_stop(instance.name, -1), mock.call.wait_container_operation('/1.0/operation/1234', 200, -1), mock.call.container_destroy(instance.name), mock.call.wait_container_operation('/1.0/operation/1234', 200, -1) ] self.assertEqual(calls, self.ml.method_calls) if not container_defined: self.ml.container_defined.return_value = container_defined self.assertEqual( None, self.session.container_destroy(instance.name, instance)) calls = [mock.call.container_defined(instance.name)] self.assertEqual(calls, self.ml.method_calls)
def test_container_nested_container(self): instance = stubs._fake_instance() instance.flavor.extra_specs = {'lxd_nested_allowed': True} config = self.config.config_instance_options({}, instance) self.assertEqual({ 'security.nesting': 'True', 'boot.autostart': 'True' }, config)
def test_create_container(self, tag, key, expected): """Tests the create_container methond on LXDContainerConfig. Inspect that the correct dictionary is returned for a given instance. """ instance = stubs._fake_instance() container_config = self.config.create_container(instance) self.assertEqual(container_config[key], expected)
def test_create_disk_path(self): instance = stubs._fake_instance() config = self.config.configure_disk_path('/fake/src_path', '/fake/dest_path', 'fake_disk', instance) self.assertEqual({'fake_disk': {'path': '/fake/dest_path', 'source': '/fake/src_path', 'type': 'disk'}}, config)
def test_container_wait(self): instance = stubs._fake_instance() operation_id = mock.Mock() self.ml.wait_container_operation.return_value = True self.assertEqual(None, self.session.operation_wait(operation_id, instance)) self.ml.wait_container_operation.assert_called_with( operation_id, 200, -1)
def test_container_wait(self): instance = stubs._fake_instance() operation_id = mock.Mock() self.ml.wait_container_operation.return_value = True self.assertEqual(None, self.session.operation_wait(operation_id, instance)) self.ml.wait_container_operation.assert_called_with(operation_id, 200, -1)
def test_create_instance_initfail(self): instance = stubs._fake_instance() self.ml.container_init.side_effect = (lxd_exception.APIError( 'Fake', 500)) self.assertEqual( None, self.container_ops.create_container(instance, [], [], {}, None, True))
def test_alias_create(self): """Test the alias is created.""" instance = stubs._fake_instance() alias = mock.Mock() self.ml.alias_create.return_value = True self.assertTrue(self.session.create_alias(alias, instance)) calls = [mock.call.alias_create(alias)] self.assertEqual(calls, self.ml.method_calls)
def test_container_root_zfs(self): instance = stubs._fake_instance() config = self.config.configure_container_root(instance) self.assertEqual( {'root': { 'path': '/', 'type': 'disk', 'size': '10GB' }}, config)
def test_container_reboot_fail(self, tag, side_effect, expected): """ container_reboot reboots a container on a given LXD host. Check that an exception.NovaException is raised when there is an LXD API error. """ instance = stubs._fake_instance() self.ml.container_reboot.side_effect = side_effect self.assertRaises(expected, self.session.container_reboot, instance)
def test_get_container_config_rescue(self, tag, key, expected): """Test the get_container_config method. Ensure the right correct dictionary is created when using a rescue container. """ instance = stubs._fake_instance() rescue = True container_config = self.config.get_container_config( instance, rescue) self.assertEqual(container_config[key], expected)
def test_get_container_config(self, tag, key, expected): """Test the get_container_config method. Ensure that the correct dicitonary keys and data is returned correctly. """ instance = stubs._fake_instance() rescue = False container_config = self.config.get_container_config( instance, rescue) self.assertEqual(container_config[key], expected)
def test_container_suspend(self): instance = stubs._fake_instance() context = mock.Mock() with test.nested( mock.patch.object( session.LXDAPISession, 'container_pause')) as (mock_container_suspend): self.assertEqual(None, self.operations.suspend(context, instance)) self.assertTrue(mock_container_suspend)
def test_container_unpause_fail(self, tag, side_effect, expected): """ container_unpause resumes a previously suespended container. Validate that an exception.NovaException is raised when a APIError is sent by the API. """ instance = stubs._fake_instance() self.ml.container_resume.side_effect = side_effect self.assertRaises(expected, self.session.container_unpause, instance.name, instance)
def test_get_config_bridge(self): instance = stubs._fake_instance() vif_data = copy.deepcopy(self.vif_data) vif_type = self.vif_driver.get_config(instance, vif_data) self.assertEqual(vif_type, { 'bridge': 'qbr0123456789a', 'mac_address': '00:11:22:33:44:55' })
def test_get_info(self, tag, side_effect, expected): instance = stubs._fake_instance() with mock.patch.object(session.LXDAPISession, "container_state", ) as state: state.return_value = side_effect info = self.connection.get_info(instance) self.assertEqual(dir(hardware.InstanceInfo(state=expected, num_cpu=2)), dir(info))
def test_container_config(self): """ container_config returns a dictionary representation of the LXD container. Verify that the funciton returns a container_config """ instance = stubs._fake_instance() self.ml.get_container_config.return_value = (200, fake_api.fake_container_config()) self.assertEqual((200, fake_api.fake_container_config()), self.session.container_config(instance))
def test_container_stop_fail(self, tag, side_effect, expected): """ contianer_stop stops a container on a given LXD host. Verifty that we raise an exception.NovaException when there is an APIError. """ instance = stubs._fake_instance() self.ml.container_stop.side_effect = side_effect self.assertRaises(expected, self.session.container_stop, instance.name, instance)
def test_setup_profile(self, mock_profile_create, mock_create_profile): instance = stubs._fake_instance() network_info = mock.Mock() container_profile = mock.Mock() self.operations._setup_profile(instance.name, instance, network_info) mock_profile_create.assert_has_calls( [mock.call(instance, network_info)]) container_profile = mock_profile_create.return_value mock_create_profile.assert_has_calls( [mock.call(container_profile, instance)])
def test_profile_create(self): instance = stubs._fake_instance() config = mock.Mock() self.ml.profile_defined.return_value = True self.ml.profile_create.return_value = \ (200, fake_api.fake_standard_return()) self.assertEqual((200, fake_api.fake_standard_return()), self.session.profile_create(config, instance)) calls = [mock.call.profile_list(), mock.call.profile_create(config)] self.assertEqual(calls, self.ml.method_calls)
def test_container_power_off(self): instance = stubs._fake_instance() with test.nested( mock.patch.object(self.connection.container_ops, 'power_off') ) as ( power_off ): self.connection.power_off(instance) self.assertTrue(power_off)
def test_container_snapshot(self, tag, side_effect): snapshot = mock.Mock() instance = stubs._fake_instance() self.ml.container_snapshot_create.return_value = side_effect self.assertEqual(None, self.session.container_snapshot(snapshot, instance)) calls = [ mock.call.container_snapshot_create(instance.name, snapshot), mock.call.wait_container_operation('/1.0/operation/1234', 200, -1) ] self.assertEqual(calls, self.ml.method_calls)
def test_image(self, tag, sucess, image_data, expected): context = mock.Mock instance = stubs._fake_instance() with mock.patch.object(image.IMAGE_API, 'get', return_value=image_data): if sucess: self.assertEqual(expected, self.image._verify_image(context, instance)) else: self.assertRaises(expected, self.image._verify_image, context, instance)
def test_container_config(self): """ container_config returns a dictionary representation of the LXD container. Verify that the funciton returns a container_config """ instance = stubs._fake_instance() self.ml.get_container_config.return_value = \ (200, fake_api.fake_container_config()) self.assertEqual((200, fake_api.fake_container_config()), self.session.container_config(instance))
def test_container_init_fail(self, tag, side_effect, expected): """ continer_init create as container on a given LXD host. Make sure that we reaise an exception.NovaException if there is an APIError from the LXD API. """ config = mock.Mock() instance = stubs._fake_instance() self.ml.container_init.side_effect = side_effect self.assertRaises(expected, self.session.container_init, config, instance)
def test_power_off(self): """Test the power_off method. Ensure that the proper calls are made when the container is powered off. """ instance = stubs._fake_instance() with test.nested( mock.patch.object(session.LXDAPISession, 'container_stop')) as (mock_container_stop): self.assertEqual(None, self.operations.power_off(instance)) self.assertTrue(mock_container_stop)
def test_unpause_container(self): """Test the unapuse continaer. Ensure that the proper calls are made when unpausing a container. """ instance = stubs._fake_instance() with test.nested( mock.patch.object( session.LXDAPISession, 'container_unpause')) as (mock_container_unpause): self.assertEqual(None, self.operations.unpause(instance)) self.assertTrue(mock_container_unpause)
def test_container_resume(self): instance = stubs._fake_instance() context = mock.Mock() network_info = mock.Mock() with test.nested( mock.patch.object( session.LXDAPISession, 'container_unpause')) as (mock_container_resume): self.assertEqual( None, self.operations.resume(context, instance, network_info)) self.assertTrue(mock_container_resume)