class GlanceProtectionPluginTest(base.TestCase): def setUp(self): super(GlanceProtectionPluginTest, self).setUp() self.plugin = GlanceProtectionPlugin() cfg.CONF.set_default("glance_endpoint", "http://127.0.0.1:9292", "glance_client") self.cntxt = RequestContext(user_id="admin", project_id="abcd", auth_token="efgh") self.glance_client = ClientFactory.create_client("glance", self.cntxt) self.checkpoint = CheckpointCollection() def test_get_options_schema(self): options_schema = self.plugin.get_options_schema(constants.IMAGE_RESOURCE_TYPE) self.assertEqual(options_schema, image_plugin_schemas.OPTIONS_SCHEMA) def test_get_restore_schema(self): options_schema = self.plugin.get_restore_schema(constants.IMAGE_RESOURCE_TYPE) self.assertEqual(options_schema, image_plugin_schemas.RESTORE_SCHEMA) def test_get_saved_info_schema(self): options_schema = self.plugin.get_saved_info_schema(constants.IMAGE_RESOURCE_TYPE) self.assertEqual(options_schema, image_plugin_schemas.SAVED_INFO_SCHEMA) def test_create_backup(self): resource = Resource(id="123", type=constants.IMAGE_RESOURCE_TYPE, name="fake") resource_node = ResourceNode(value=resource, child_nodes=[]) fake_bank_section.create_object = mock.MagicMock() self.plugin._glance_client = mock.MagicMock() self.plugin._glance_client.return_value = self.glance_client self.glance_client.images.get = mock.MagicMock() self.glance_client.images.return_value = Image(disk_format="", container_format="", status="active") fake_bank_section.update_object = mock.MagicMock() self.glance_client.images.data = mock.MagicMock() self.glance_client.images.data.return_value = "image-data" self.plugin.create_backup(self.cntxt, self.checkpoint, node=resource_node) def test_delete_backup(self): resource = Resource(id="123", type=constants.IMAGE_RESOURCE_TYPE, name="fake") resource_node = ResourceNode(value=resource, child_nodes=[]) fake_bank_section.list_objects = mock.MagicMock() fake_bank_section.list_objects.return_value = ["data_1", "data_2"] fake_bank_section.delete_object = mock.MagicMock() self.plugin.delete_backup(self.cntxt, self.checkpoint, node=resource_node) def test_get_supported_resources_types(self): types = self.plugin.get_supported_resources_types() self.assertEqual(types, [constants.IMAGE_RESOURCE_TYPE])
class GlanceProtectionPluginTest(base.TestCase): def setUp(self): super(GlanceProtectionPluginTest, self).setUp() self.plugin = GlanceProtectionPlugin() cfg.CONF.set_default('glance_endpoint', 'http://127.0.0.1:9292', 'glance_client') self.cntxt = RequestContext(user_id='admin', project_id='abcd', auth_token='efgh') self.glance_client = ClientFactory.create_client("glance", self.cntxt) self.checkpoint = CheckpointCollection() def test_get_resource_stats(self): fake_resource_id = "123" fake_bank_section.get_object = mock.MagicMock() fake_bank_section.get_object.return_value = \ constants.RESOURCE_STATUS_PROTECTING status = self.plugin.get_resource_stats(self.checkpoint, fake_resource_id) self.assertEqual(status, constants.RESOURCE_STATUS_PROTECTING) def test_get_options_schema(self): options_schema = self.plugin.get_options_schema( constants.IMAGE_RESOURCE_TYPE) self.assertEqual(options_schema, image_plugin_schemas.OPTIONS_SCHEMA) def test_get_restore_schema(self): options_schema = self.plugin.get_restore_schema( constants.IMAGE_RESOURCE_TYPE) self.assertEqual(options_schema, image_plugin_schemas.RESTORE_SCHEMA) def test_get_saved_info_schema(self): options_schema = self.plugin.get_saved_info_schema( constants.IMAGE_RESOURCE_TYPE) self.assertEqual(options_schema, image_plugin_schemas.SAVED_INFO_SCHEMA) def test_create_backup(self): resource = Resource(id="123", type=constants.IMAGE_RESOURCE_TYPE, name='fake') resource_node = ResourceNode(value=resource, child_nodes=[]) fake_bank_section.create_object = mock.MagicMock() self.plugin._glance_client = mock.MagicMock() self.plugin._glance_client.return_value = self.glance_client self.glance_client.images.get = mock.MagicMock() self.glance_client.images.return_value = Image(disk_format="", container_format="", status="active") fake_bank_section.update_object = mock.MagicMock() self.glance_client.images.data = mock.MagicMock() self.glance_client.images.data.return_value = "image-data" self.plugin.create_backup(self.cntxt, self.checkpoint, node=resource_node) def test_delete_backup(self): resource = Resource(id="123", type=constants.IMAGE_RESOURCE_TYPE, name='fake') resource_node = ResourceNode(value=resource, child_nodes=[]) fake_bank_section.list_objects = mock.MagicMock() fake_bank_section.list_objects.return_value = ["data_1", "data_2"] fake_bank_section.delete_object = mock.MagicMock() self.plugin.delete_backup(self.cntxt, self.checkpoint, node=resource_node) def test_get_supported_resources_types(self): types = self.plugin.get_supported_resources_types() self.assertEqual(types, [constants.IMAGE_RESOURCE_TYPE])
class GlanceProtectionPluginTest(base.TestCase): def setUp(self): super(GlanceProtectionPluginTest, self).setUp() plugin_config = cfg.ConfigOpts() plugin_config_fixture = self.useFixture(fixture.Config(plugin_config)) plugin_config_fixture.load_raw_values( group='image_backup_plugin', poll_interval=0, ) plugin_config_fixture.load_raw_values( group='image_backup_plugin', backup_image_object_size=65536, ) self.plugin = GlanceProtectionPlugin(plugin_config) cfg.CONF.set_default('glance_endpoint', 'http://127.0.0.1:9292', 'glance_client') self.cntxt = RequestContext(user_id='demo', project_id='abcd', auth_token='efgh') self.glance_client = client_factory.ClientFactory.create_client( "glance", self.cntxt) self.checkpoint = FakeCheckpoint() def test_get_options_schema(self): options_schema = self.plugin.get_options_schema( constants.IMAGE_RESOURCE_TYPE) self.assertEqual(options_schema, image_plugin_schemas.OPTIONS_SCHEMA) def test_get_restore_schema(self): options_schema = self.plugin.get_restore_schema( constants.IMAGE_RESOURCE_TYPE) self.assertEqual(options_schema, image_plugin_schemas.RESTORE_SCHEMA) def test_get_saved_info_schema(self): options_schema = self.plugin.get_saved_info_schema( constants.IMAGE_RESOURCE_TYPE) self.assertEqual(options_schema, image_plugin_schemas.SAVED_INFO_SCHEMA) @mock.patch('karbor.services.protection.protection_plugins.image.' 'image_protection_plugin.utils.status_poll') @mock.patch('karbor.services.protection.clients.glance.create') def test_create_backup(self, mock_glance_create, mock_status_poll): resource = Resource(id="123", type=constants.IMAGE_RESOURCE_TYPE, name='fake') fake_bank_section.update_object = mock.MagicMock() protect_operation = self.plugin.get_protect_operation(resource) mock_glance_create.return_value = self.glance_client self.glance_client.images.get = mock.MagicMock() self.glance_client.images.return_value = Image(disk_format="", container_format="", status="active") fake_bank_section.update_object = mock.MagicMock() self.glance_client.images.data = mock.MagicMock() self.glance_client.images.data.return_value = [] mock_status_poll.return_value = True call_hooks(protect_operation, self.checkpoint, resource, self.cntxt, {}) def test_delete_backup(self): resource = Resource(id="123", type=constants.IMAGE_RESOURCE_TYPE, name='fake') fake_bank_section.list_objects = mock.MagicMock() fake_bank_section.list_objects.return_value = ["data_1", "data_2"] fake_bank_section.delete_object = mock.MagicMock() delete_operation = self.plugin.get_delete_operation(resource) call_hooks(delete_operation, self.checkpoint, resource, self.cntxt, {}) @mock.patch('karbor.services.protection.protection_plugins.utils.' 'update_resource_verify_result') def test_verify_backup(self, mock_update_verify): resource = Resource(id="123", type=constants.IMAGE_RESOURCE_TYPE, name='fake') fake_bank_section.get_object = mock.MagicMock() fake_bank_section.get_object.return_value = 'available' verify_operation = self.plugin.get_verify_operation(resource) call_hooks(verify_operation, self.checkpoint, resource, self.cntxt, {}) mock_update_verify.assert_called_with(None, resource.type, resource.id, 'available') def test_get_supported_resources_types(self): types = self.plugin.get_supported_resources_types() self.assertEqual([constants.IMAGE_RESOURCE_TYPE], types)