From 576cbb32d505ea1a9df49ed7c427931e0215c107 Mon Sep 17 00:00:00 2001 From: chenying Date: Wed, 25 Oct 2017 17:01:29 +0800 Subject: [PATCH] Add verify action for the pod backup protection plugin Change-Id: If93372d0371b2cda89552e158dcca1dad1ed0a96 Implements: blueprint support-verify-the-checkpoint-api --- .../pod/pod_plugin_schemas.py | 6 ++++ .../pod/pod_protection_plugin.py | 36 +++++++++++++++++++ .../protection/test_pod_protection_plugin.py | 16 +++++++++ 3 files changed, 58 insertions(+) diff --git a/karbor/services/protection/protection_plugins/pod/pod_plugin_schemas.py b/karbor/services/protection/protection_plugins/pod/pod_plugin_schemas.py index ff8f2e26..bae6e5b5 100644 --- a/karbor/services/protection/protection_plugins/pod/pod_plugin_schemas.py +++ b/karbor/services/protection/protection_plugins/pod/pod_plugin_schemas.py @@ -30,6 +30,12 @@ RESTORE_SCHEMA = { "required": ["restore_name"] } +VERIFY_SCHEMA = { + "title": "Pod Protection Verify", + "type": "object", + "properties": {} +} + SAVED_INFO_SCHEMA = { "title": "Pod Protection Saved Info", "type": "object", diff --git a/karbor/services/protection/protection_plugins/pod/pod_protection_plugin.py b/karbor/services/protection/protection_plugins/pod/pod_protection_plugin.py index 6ddfbc0b..e96e1361 100644 --- a/karbor/services/protection/protection_plugins/pod/pod_protection_plugin.py +++ b/karbor/services/protection/protection_plugins/pod/pod_protection_plugin.py @@ -185,6 +185,35 @@ class DeleteOperation(protection_plugin.Operation): resource_type=constants.POD_RESOURCE_TYPE) +class VerifyOperation(protection_plugin.Operation): + def __init__(self): + super(VerifyOperation, self).__init__() + + def on_main(self, checkpoint, resource, context, parameters, **kwargs): + original_pod_id = resource.id + bank_section = checkpoint.get_resource_bank_section( + original_pod_id) + LOG.info('Verifying the pod backup, pod_id: %s.', original_pod_id) + + update_method = partial( + utils.update_resource_verify_result, + kwargs.get('verify'), resource.type, original_pod_id) + + backup_status = bank_section.get_object("status") + + if backup_status == constants.RESOURCE_STATUS_AVAILABLE: + update_method(constants.RESOURCE_STATUS_AVAILABLE) + else: + reason = ('The status of pod backup status is %s.' + % backup_status) + update_method(backup_status, reason) + raise exception.VerifyResourceFailed( + name="Pod backup", + reason=reason, + resource_id=original_pod_id, + resource_type=resource.type) + + class RestoreOperation(protection_plugin.Operation): def __init__(self, poll_interval): super(RestoreOperation, self).__init__() @@ -312,6 +341,10 @@ class PodProtectionPlugin(protection_plugin.ProtectionPlugin): def get_restore_schema(cls, resource_type): return pod_plugin_schemas.RESTORE_SCHEMA + @classmethod + def get_verify_schema(cls, resources_type): + return pod_plugin_schemas.VERIFY_SCHEMA + @classmethod def get_saved_info_schema(cls, resource_type): return pod_plugin_schemas.SAVED_INFO_SCHEMA @@ -326,5 +359,8 @@ class PodProtectionPlugin(protection_plugin.ProtectionPlugin): def get_restore_operation(self, resource): return RestoreOperation(self._poll_interval) + def get_verify_operation(self, resource): + return VerifyOperation() + def get_delete_operation(self, resource): return DeleteOperation() diff --git a/karbor/tests/unit/protection/test_pod_protection_plugin.py b/karbor/tests/unit/protection/test_pod_protection_plugin.py index ba66cc00..ef815e9c 100644 --- a/karbor/tests/unit/protection/test_pod_protection_plugin.py +++ b/karbor/tests/unit/protection/test_pod_protection_plugin.py @@ -157,6 +157,22 @@ class PodProtectionPluginTest(base.TestCase): call_hooks(protect_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="c88b92a8-e8b4-504c-bad4-343d92061871", + type=constants.POD_RESOURCE_TYPE, + name='default:busybox-test') + + 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_delete_backup(self): resource = Resource(id="c88b92a8-e8b4-504c-bad4-343d92061871", type=constants.POD_RESOURCE_TYPE,