Example #1
0
 def setUp(self):
     super(NovaProtectionPluginTest, self).setUp()
     self.cntxt = RequestContext(user_id='demo',
                                 project_id='abcd',
                                 auth_token='efgh')
     self.plugin = NovaProtectionPlugin(cfg.CONF)
     self.glance_client = FakeGlanceClient()
     self.nova_client = FakeNovaClient()
     self.cinder_client = FakeCinderClient()
     self.neutron_client = FakeNeutronClient()
     self.checkpoint = Checkpoint()
 def setUp(self):
     super(NovaProtectionPluginTest, self).setUp()
     self.cntxt = RequestContext(user_id='admin',
                                 project_id='abcd',
                                 auth_token='efgh')
     self.plugin = NovaProtectionPlugin()
     self.glance_client = FakeGlanceClient()
     self.nova_client = FakeNovaClient()
     self.cinder_client = FakeCinderClient()
     self.neutron_client = FakeNeutronClient()
     self.checkpoint = Checkpoint()
Example #3
0
class NovaProtectionPluginTest(base.TestCase):
    def setUp(self):
        super(NovaProtectionPluginTest, self).setUp()
        self.cntxt = RequestContext(user_id='demo',
                                    project_id='abcd',
                                    auth_token='efgh')
        self.plugin = NovaProtectionPlugin(cfg.CONF)
        self.glance_client = FakeGlanceClient()
        self.nova_client = FakeNovaClient()
        self.cinder_client = FakeCinderClient()
        self.neutron_client = FakeNeutronClient()
        self.checkpoint = Checkpoint()

    def test_get_options_schema(self):
        options_schema = self.plugin.get_options_schema(
            constants.SERVER_RESOURCE_TYPE)
        self.assertEqual(options_schema, server_plugin_schemas.OPTIONS_SCHEMA)

    def test_get_restore_schema(self):
        options_schema = self.plugin.get_restore_schema(
            constants.SERVER_RESOURCE_TYPE)
        self.assertEqual(options_schema, server_plugin_schemas.RESTORE_SCHEMA)

    def test_get_saved_info_schema(self):
        options_schema = self.plugin.get_saved_info_schema(
            constants.SERVER_RESOURCE_TYPE)
        self.assertEqual(options_schema,
                         server_plugin_schemas.SAVED_INFO_SCHEMA)

    @mock.patch('karbor.services.protection.clients.neutron.create')
    @mock.patch('karbor.services.protection.clients.glance.create')
    @mock.patch('karbor.services.protection.clients.nova.create')
    @mock.patch('karbor.services.protection.clients.cinder.create')
    def test_create_backup_without_volumes(self, mock_cinder_client,
                                           mock_nova_client,
                                           mock_glance_client,
                                           mock_neutron_client):
        resource = Resource(id="vm_id_1",
                            type=constants.SERVER_RESOURCE_TYPE,
                            name="fake_vm")

        protect_operation = self.plugin.get_protect_operation(resource)
        mock_cinder_client.return_value = self.cinder_client
        mock_nova_client.return_value = self.nova_client
        mock_glance_client.return_value = self.glance_client
        mock_neutron_client.return_value = self.neutron_client

        call_hooks(protect_operation, self.checkpoint, resource, self.cntxt,
                   {})

        self.assertEqual(
            constants.RESOURCE_STATUS_AVAILABLE,
            fake_bank._plugin._objects[
                "/resource_data/checkpoint_id/vm_id_1/status"]
        )
        resource_definition = {
            "resource_id": "vm_id_1",
            "attach_metadata": {},
            'boot_metadata': {'boot_device_type': 'volume'},
            "server_metadata": {
                "availability_zone": "nova",
                "networks": ["network_id_1"],
                "floating_ips": [],
                "flavor": "flavor_id",
                "key_name": None,
                "security_groups": "default",
            },
        }
        self.assertEqual(
            resource_definition,
            fake_bank._plugin._objects[
                "/resource_data/checkpoint_id/vm_id_1/metadata"]
        )

    @mock.patch('karbor.services.protection.clients.neutron.create')
    @mock.patch('karbor.services.protection.clients.glance.create')
    @mock.patch('karbor.services.protection.clients.nova.create')
    @mock.patch('karbor.services.protection.clients.cinder.create')
    def test_create_backup_with_volumes(self, mock_cinder_client,
                                        mock_nova_client,
                                        mock_glance_client,
                                        mock_neutron_client):
        vm_resource = Resource(id="vm_id_2",
                               type=constants.SERVER_RESOURCE_TYPE,
                               name="fake_vm")

        protect_operation = self.plugin.get_protect_operation(vm_resource)
        mock_cinder_client.return_value = self.cinder_client
        mock_nova_client.return_value = self.nova_client
        mock_glance_client.return_value = self.glance_client
        mock_neutron_client.return_value = self.neutron_client
        checkpoint = Checkpoint()
        checkpoint.resource_graph = [FakeGraphNode(value=Resource(
            type='OS::Nova::Server', id='vm_id_2', name='None'),
            child_nodes=[FakeGraphNode(value=Resource(
                type='OS::Cinder::Volume', id='vol_id_1', name=None),
                child_nodes=())])]

        call_hooks(protect_operation, checkpoint, vm_resource, self.cntxt,
                   {})

        self.assertEqual(
            constants.RESOURCE_STATUS_AVAILABLE,
            fake_bank._plugin._objects[
                "/resource_data/checkpoint_id/vm_id_2/status"]
        )
        resource_definition = {
            "resource_id": "vm_id_2",
            "attach_metadata": {"vol_id_1": "/dev/vdb"},
            'boot_metadata': {'boot_device_type': 'volume'},
            "server_metadata": {
                "availability_zone": "nova",
                "networks": ["network_id_2"],
                "floating_ips": [],
                "flavor": "flavor_id",
                "key_name": None,
                "security_groups": "default",
            },
            'attach_metadata': {
                'vol_id_1': {'attachment_id': '',
                             'bootable': '',
                             'device': '/dev/vdb',
                             'host_name': '',
                             'id': 'attach_id_1',
                             'server_id': 'vm_id_2',
                             'volume_id': 'vol_id_1'}
            },
        }
        self.assertEqual(
            fake_bank._plugin._objects[
                "/resource_data/checkpoint_id/vm_id_2/metadata"],
            resource_definition
        )

    @mock.patch('karbor.services.protection.clients.glance.create')
    def test_delete_backup(self, mock_glance_client):
        resource = Resource(id="vm_id_1",
                            type=constants.SERVER_RESOURCE_TYPE,
                            name="fake_vm")

        fake_bank._plugin._objects[
            "/resource_data/checkpoint_id/vm_id_1/metadata"] = {
            "resource_id": "vm_id_1",
            "attach_metadata": {},
            "server_metadata": {
                "availability_zone": "nova",
                "networks": ["network_id_1"],
                "floating_ips": [],
                "flavor": "flavor_id",
                "key_name": None,
                "security_groups": "default",
            },
            "snapshot_id": "image_id_2",
            "snapshot_metadata": {
                "disk_format": "",
                "container_format": "",
                "name": "snapshot_checkpoint_id@vm_id_1"
            }
        }

        delete_operation = self.plugin.get_delete_operation(resource)
        mock_glance_client.return_value = self.glance_client

        fake_bank._plugin._objects[
            "/resource_data/checkpoint_id/vm_id_1/data_0"
        ] = "image_data_1"
        fake_bank._plugin._objects[
            "/resource_data/checkpoint_id/vm_id_1/data_1"
        ] = "image_data_1"

        call_hooks(delete_operation, self.checkpoint, resource, self.cntxt,
                   {})

    def test_get_supported_resources_types(self):
        types = self.plugin.get_supported_resources_types()
        self.assertEqual([constants.SERVER_RESOURCE_TYPE], types)
class NovaProtectionPluginTest(base.TestCase):
    def setUp(self):
        super(NovaProtectionPluginTest, self).setUp()
        self.cntxt = RequestContext(user_id='admin',
                                    project_id='abcd',
                                    auth_token='efgh')
        self.plugin = NovaProtectionPlugin()
        self.glance_client = FakeGlanceClient()
        self.nova_client = FakeNovaClient()
        self.cinder_client = FakeCinderClient()
        self.neutron_client = FakeNeutronClient()
        self.checkpoint = Checkpoint()

    def test_get_options_schema(self):
        options_schema = self.plugin.get_options_schema(
            constants.SERVER_RESOURCE_TYPE)
        self.assertEqual(options_schema, server_plugin_schemas.OPTIONS_SCHEMA)

    def test_get_restore_schema(self):
        options_schema = self.plugin.get_restore_schema(
            constants.SERVER_RESOURCE_TYPE)
        self.assertEqual(options_schema, server_plugin_schemas.RESTORE_SCHEMA)

    def test_get_saved_info_schema(self):
        options_schema = self.plugin.get_saved_info_schema(
            constants.SERVER_RESOURCE_TYPE)
        self.assertEqual(options_schema,
                         server_plugin_schemas.SAVED_INFO_SCHEMA)

    def test_create_backup_without_volumes(self):
        resource = Resource(id="vm_id_1",
                            type=constants.SERVER_RESOURCE_TYPE,
                            name="fake_vm")
        resource_node = ResourceNode(value=resource, child_nodes=[])
        backup_name = "fake_backup"

        self.plugin._cinder_client = mock.MagicMock()
        self.plugin._cinder_client.return_value = self.cinder_client

        self.plugin._nova_client = mock.MagicMock()
        self.plugin._nova_client.return_value = self.nova_client

        self.plugin._glance_client = mock.MagicMock()
        self.plugin._glance_client.return_value = self.glance_client

        self.plugin._neutron_client = mock.MagicMock()
        self.plugin._neutron_client.return_value = self.neutron_client

        self.plugin.create_backup(self.cntxt,
                                  self.checkpoint,
                                  node=resource_node,
                                  backup_name=backup_name)

        self.assertEqual(
            constants.RESOURCE_STATUS_PROTECTING, fake_bank._plugin.
            _objects["/resource_data/checkpoint_id/vm_id_1/status"])
        resource_definition = {
            "resource_id": "vm_id_1",
            "attach_metadata": {},
            "server_metadata": {
                "availability_zone": "nova",
                "networks": ["network_id_1"],
                "floating_ips": [],
                "flavor": "flavor_id",
                "key_name": None,
                "security_groups": "default",
            },
        }
        self.assertEqual(
            resource_definition, fake_bank._plugin.
            _objects["/resource_data/checkpoint_id/vm_id_1/metadata"])

    def test_create_backup_with_volumes(self):
        vm_resource = Resource(id="vm_id_2",
                               type=constants.SERVER_RESOURCE_TYPE,
                               name="fake_vm")
        vol_resource = Resource(id="vol_id_1",
                                type=constants.VOLUME_RESOURCE_TYPE,
                                name="fake_vol")
        vol_node = ResourceNode(value=vol_resource, child_nodes=[])
        vm_node = ResourceNode(value=vm_resource, child_nodes=[vol_node])
        backup_name = "fake_backup"

        self.plugin._cinder_client = mock.MagicMock()
        self.plugin._cinder_client.return_value = self.cinder_client

        self.plugin._nova_client = mock.MagicMock()
        self.plugin._nova_client.return_value = self.nova_client

        self.plugin._glance_client = mock.MagicMock()
        self.plugin._glance_client.return_value = self.glance_client

        self.plugin._neutron_client = mock.MagicMock()
        self.plugin._neutron_client.return_value = self.neutron_client

        self.plugin.create_backup(self.cntxt,
                                  self.checkpoint,
                                  node=vm_node,
                                  backup_name=backup_name)

        self.assertEqual(
            fake_bank._plugin.
            _objects["/resource_data/checkpoint_id/vm_id_2/status"],
            constants.RESOURCE_STATUS_PROTECTING)
        resource_definition = {
            "resource_id": "vm_id_2",
            "attach_metadata": {
                "vol_id_1": "/dev/vdb"
            },
            "server_metadata": {
                "availability_zone": "nova",
                "networks": ["network_id_2"],
                "floating_ips": [],
                "flavor": "flavor_id",
                "key_name": None,
                "security_groups": "default",
            },
        }
        self.assertEqual(
            fake_bank._plugin.
            _objects["/resource_data/checkpoint_id/vm_id_2/metadata"],
            resource_definition)

    def test_delete_backup(self):
        resource = Resource(id="vm_id_1",
                            type=constants.SERVER_RESOURCE_TYPE,
                            name="fake_vm")
        resource_node = ResourceNode(value=resource, child_nodes=[])

        fake_bank._plugin._objects[
            "/resource_data/checkpoint_id/vm_id_1/metadata"] = {
                "resource_id": "vm_id_1",
                "attach_metadata": {},
                "server_metadata": {
                    "availability_zone": "nova",
                    "networks": ["network_id_1"],
                    "floating_ips": [],
                    "flavor": "flavor_id",
                    "key_name": None,
                    "security_groups": "default",
                },
                "snapshot_id": "image_id_2",
                "snapshot_metadata": {
                    "disk_format": "",
                    "container_format": "",
                    "name": "snapshot_checkpoint_id@vm_id_1"
                }
            }

        self.plugin._glance_client = mock.MagicMock()
        self.plugin._glance_client.return_value = self.glance_client

        fake_bank._plugin._objects[
            "/resource_data/checkpoint_id/vm_id_1/data_0"] = "image_data_1"
        fake_bank._plugin._objects[
            "/resource_data/checkpoint_id/vm_id_1/data_1"] = "image_data_1"

        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.SERVER_RESOURCE_TYPE])
class NovaProtectionPluginTest(base.TestCase):
    def setUp(self):
        super(NovaProtectionPluginTest, self).setUp()
        self.cntxt = RequestContext(user_id='admin',
                                    project_id='abcd',
                                    auth_token='efgh')
        self.plugin = NovaProtectionPlugin()
        self.glance_client = FakeGlanceClient()
        self.nova_client = FakeNovaClient()
        self.cinder_client = FakeCinderClient()
        self.neutron_client = FakeNeutronClient()
        self.checkpoint = Checkpoint()

    def test_get_options_schema(self):
        options_schema = self.plugin.get_options_schema(
            constants.SERVER_RESOURCE_TYPE)
        self.assertEqual(options_schema, server_plugin_schemas.OPTIONS_SCHEMA)

    def test_get_restore_schema(self):
        options_schema = self.plugin.get_restore_schema(
            constants.SERVER_RESOURCE_TYPE)
        self.assertEqual(options_schema, server_plugin_schemas.RESTORE_SCHEMA)

    def test_get_saved_info_schema(self):
        options_schema = self.plugin.get_saved_info_schema(
            constants.SERVER_RESOURCE_TYPE)
        self.assertEqual(options_schema,
                         server_plugin_schemas.SAVED_INFO_SCHEMA)

    def test_create_backup_without_volumes(self):
        resource = Resource(id="vm_id_1",
                            type=constants.SERVER_RESOURCE_TYPE,
                            name="fake_vm")
        resource_node = ResourceNode(value=resource,
                                     child_nodes=[])
        backup_name = "fake_backup"

        self.plugin._cinder_client = mock.MagicMock()
        self.plugin._cinder_client.return_value = self.cinder_client

        self.plugin._nova_client = mock.MagicMock()
        self.plugin._nova_client.return_value = self.nova_client

        self.plugin._glance_client = mock.MagicMock()
        self.plugin._glance_client.return_value = self.glance_client

        self.plugin._neutron_client = mock.MagicMock()
        self.plugin._neutron_client.return_value = self.neutron_client

        self.plugin.create_backup(self.cntxt, self.checkpoint,
                                  node=resource_node,
                                  backup_name=backup_name)

        self.assertEqual(
            constants.RESOURCE_STATUS_PROTECTING,
            fake_bank._plugin._objects[
                "/resource_data/checkpoint_id/vm_id_1/status"]
        )
        resource_definition = {
            "resource_id": "vm_id_1",
            "attach_metadata": {},
            "server_metadata": {
                "availability_zone": "nova",
                "networks": ["network_id_1"],
                "floating_ips": [],
                "flavor": "flavor_id",
                "key_name": None,
                "security_groups": "default",
            },
        }
        self.assertEqual(
            resource_definition,
            fake_bank._plugin._objects[
                "/resource_data/checkpoint_id/vm_id_1/metadata"]
        )

    def test_create_backup_with_volumes(self):
        vm_resource = Resource(id="vm_id_2",
                               type=constants.SERVER_RESOURCE_TYPE,
                               name="fake_vm")
        vol_resource = Resource(id="vol_id_1",
                                type=constants.VOLUME_RESOURCE_TYPE,
                                name="fake_vol")
        vol_node = ResourceNode(value=vol_resource,
                                child_nodes=[])
        vm_node = ResourceNode(value=vm_resource,
                               child_nodes=[vol_node])
        backup_name = "fake_backup"

        self.plugin._cinder_client = mock.MagicMock()
        self.plugin._cinder_client.return_value = self.cinder_client

        self.plugin._nova_client = mock.MagicMock()
        self.plugin._nova_client.return_value = self.nova_client

        self.plugin._glance_client = mock.MagicMock()
        self.plugin._glance_client.return_value = self.glance_client

        self.plugin._neutron_client = mock.MagicMock()
        self.plugin._neutron_client.return_value = self.neutron_client

        self.plugin.create_backup(self.cntxt, self.checkpoint,
                                  node=vm_node,
                                  backup_name=backup_name)

        self.assertEqual(
            fake_bank._plugin._objects[
                "/resource_data/checkpoint_id/vm_id_2/status"],
            constants.RESOURCE_STATUS_PROTECTING
        )
        resource_definition = {
            "resource_id": "vm_id_2",
            "attach_metadata": {"vol_id_1": "/dev/vdb"},
            "server_metadata": {
                "availability_zone": "nova",
                "networks": ["network_id_2"],
                "floating_ips": [],
                "flavor": "flavor_id",
                "key_name": None,
                "security_groups": "default",
            },
        }
        self.assertEqual(
            fake_bank._plugin._objects[
                "/resource_data/checkpoint_id/vm_id_2/metadata"],
            resource_definition
        )

    def test_delete_backup(self):
        resource = Resource(id="vm_id_1",
                            type=constants.SERVER_RESOURCE_TYPE,
                            name="fake_vm")
        resource_node = ResourceNode(value=resource,
                                     child_nodes=[])

        fake_bank._plugin._objects[
            "/resource_data/checkpoint_id/vm_id_1/metadata"] = {
            "resource_id": "vm_id_1",
            "attach_metadata": {},
            "server_metadata": {
                "availability_zone": "nova",
                "networks": ["network_id_1"],
                "floating_ips": [],
                "flavor": "flavor_id",
                "key_name": None,
                "security_groups": "default",
            },
            "snapshot_id": "image_id_2",
            "snapshot_metadata": {
                "disk_format": "",
                "container_format": "",
                "name": "snapshot_checkpoint_id@vm_id_1"
            }
        }

        self.plugin._glance_client = mock.MagicMock()
        self.plugin._glance_client.return_value = self.glance_client

        fake_bank._plugin._objects[
            "/resource_data/checkpoint_id/vm_id_1/data_0"
        ] = "image_data_1"
        fake_bank._plugin._objects[
            "/resource_data/checkpoint_id/vm_id_1/data_1"
        ] = "image_data_1"

        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.SERVER_RESOURCE_TYPE])