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_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_container_wait(self): instance = stubs._fake_instance() operation_id = mock.Mock() self.ml.wait_container_operation.return_value = True self.assertIsNone(self.session.operation_wait(operation_id, instance)) self.ml.wait_container_operation.assert_called_with(operation_id, 200, -1)
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_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_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_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_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_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_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_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_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_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_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_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', 'optional': 'True'}}, config)
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_container_power_on(self): context = mock.Mock() instance = stubs._fake_instance() network_info = mock.Mock() with test.nested( mock.patch.object(self.connection.container_ops, 'power_on')) as (power_on): self.connection.power_on(context, instance, network_info) self.assertTrue(power_on)
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_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_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_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_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_create_network(self): instance = stubs._fake_instance() instance_name = 'fake_instance' network_info = fake_network.fake_get_instance_nw_info(self) config = self.config.create_network(instance_name, instance, network_info) self.assertEqual({'fake_br1': {'hwaddr': 'DE:AD:BE:EF:00:01', 'nictype': 'bridged', 'parent': 'fake_br1', 'type': 'nic'}}, config)
def test_disk_quota_total_iops(self): instance = stubs._fake_instance() instance.flavor.extra_specs = { 'quota:disk_total_iops_sec': 10000 } config = self.config.configure_container_root(instance) self.assertEqual({'root': {'path': '/', 'type': 'disk', 'size': '10GB', 'limits.max': '10000iops'}}, config)
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_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)
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_container_reboot(self): instance = stubs._fake_instance() context = mock.Mock() network_info = mock.Mock() reboot_type = 'SOFT' with test.nested( mock.patch.object(self.connection.container_ops, 'reboot')) as (reboot): self.connection.reboot(context, instance, network_info, reboot_type) self.assertTrue(reboot)
def test_container_power_on(self): context = mock.Mock() instance = stubs._fake_instance() network_info = mock.Mock() with test.nested( mock.patch.object(self.connection.container_ops, 'power_on') ) as ( power_on ): self.connection.power_on(context, instance, network_info) self.assertTrue(power_on)
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))