def test_create_client_by_endpoint(self):
     cfg.CONF.set_default('cinder_endpoint', 'http://127.0.0.1:8776/v2',
                          'cinder_client')
     plugin = VolumeProtectablePlugin(self._context)
     self.assertEqual('volumev2',
                      plugin._client(self._context).client.service_type)
     self.assertEqual('http://127.0.0.1:8776/v2/abcd',
                      plugin._client(self._context).client.management_url)
    def test_list_resources(self, mock_volume_list):
        plugin = VolumeProtectablePlugin(self._context)

        mock_volume_list.return_value = \
            [vol_info('123', [], 'name123'), vol_info('456', [], 'name456')]
        self.assertEqual([Resource('OS::Cinder::Volume', '123', 'name123'),
                          Resource('OS::Cinder::Volume', '456', 'name456')],
                         plugin.list_resources(self._context))
    def test_show_resource(self, mock_volume_get):
        plugin = VolumeProtectablePlugin(self._context)

        vol_info = namedtuple('vol_info', ['id', 'name'])
        mock_volume_get.return_value = \
            vol_info(id='123', name='name123')
        self.assertEqual(Resource('OS::Cinder::Volume', '123', 'name123'),
                         plugin.show_resource(self._context, "123"))
    def test_show_resource(self, mock_volume_get):
        plugin = VolumeProtectablePlugin(self._context)

        vol_info = namedtuple('vol_info', ['id', 'name'])
        mock_volume_get.return_value = \
            vol_info(id='123', name='name123')
        self.assertEqual(Resource('OS::Cinder::Volume', '123', 'name123'),
                         plugin.show_resource(self._context, "123"))
    def test_list_resources(self):
        plugin = VolumeProtectablePlugin(self._context)
        plugin._client.volumes.list = mock.MagicMock()

        plugin._client.volumes.list.return_value = [vol_info('123', []),
                                                    vol_info('456', [])]
        self.assertEqual([Resource('OS::Cinder::Volume', '123'),
                          Resource('OS::Cinder::Volume', '456')],
                         plugin.list_resources())
    def test_list_resources(self, mock_volume_list):
        plugin = VolumeProtectablePlugin(self._context)

        mock_volume_list.return_value = \
            [vol_info('123', [], 'name123'), vol_info('456', [], 'name456')]
        self.assertEqual([
            Resource('OS::Cinder::Volume', '123', 'name123'),
            Resource('OS::Cinder::Volume', '456', 'name456')
        ], plugin.list_resources(self._context))
 def test_create_client_by_endpoint(self):
     cfg.CONF.set_default('cinder_endpoint',
                          'http://127.0.0.1:8776/v2',
                          'cinder_client')
     plugin = VolumeProtectablePlugin(self._context)
     self.assertEqual('volumev2',
                      plugin._client(self._context).client.service_type)
     self.assertEqual('http://127.0.0.1:8776/v2/abcd',
                      plugin._client(self._context).client.management_url)
    def test_show_resource(self):
        plugin = VolumeProtectablePlugin(self._context)
        plugin._client.volumes.get = mock.MagicMock()

        vol_info = namedtuple('vol_info', ['id', 'name'])
        plugin._client.volumes.get.return_value = vol_info(id='123',
                                                           name='name123')
        self.assertEqual(Resource('OS::Cinder::Volume', '123', 'name123'),
                         plugin.show_resource("123"))
    def test_get_server_dependent_resources(self, mock_volume_list):
        plugin = VolumeProtectablePlugin(self._context)

        attached = [{'server_id': 'abcdef', 'name': 'name'}]
        mock_volume_list.return_value = [
            vol_info('123', attached, 'name123'),
            vol_info('456', [], 'name456'),
        ]
        self.assertEqual([Resource('OS::Cinder::Volume', '123', 'name123')],
                         plugin.get_dependent_resources(
                             self._context,
                             Resource("OS::Nova::Server", 'abcdef', 'name')))
    def test_get_server_dependent_resources(self, mock_volume_list):
        plugin = VolumeProtectablePlugin(self._context)

        attached = [{'server_id': 'abcdef', 'name': 'name'}]
        mock_volume_list.return_value = [
            vol_info('123', attached, 'name123'),
            vol_info('456', [], 'name456'),
        ]
        self.assertEqual([Resource('OS::Cinder::Volume', '123', 'name123')],
                         plugin.get_dependent_resources(
                             self._context,
                             Resource("OS::Nova::Server", 'abcdef', 'name')))
    def test_get_server_dependent_resources(self):
        plugin = VolumeProtectablePlugin(self._context)
        plugin._client.volumes.list = mock.MagicMock()

        attached = [{'server_id': 'abcdef'}]
        plugin._client.volumes.list.return_value = [
            vol_info('123', attached),
            vol_info('456', []),
        ]
        self.assertEqual([Resource('OS::Cinder::Volume', '123')],
                         plugin.get_dependent_resources(Resource(
                             "OS::Nova::Server", 'abcdef'
                         )))
    def test_get_project_dependent_resources(self):
        project = project_info('abcd', constants.PROJECT_RESOURCE_TYPE)
        plugin = VolumeProtectablePlugin(self._context)
        plugin._client.volumes.list = mock.MagicMock()

        volumes = [
            mock.Mock(name='Volume', id='123'),
            mock.Mock(name='Volume', id='456'),
        ]
        setattr(volumes[0], 'os-vol-tenant-attr:tenant_id', 'abcd')
        setattr(volumes[1], 'os-vol-tenant-attr:tenant_id', 'efgh')

        plugin._client.volumes.list.return_value = volumes
        self.assertEqual(plugin.get_dependent_resources(project),
                         [Resource('OS::Cinder::Volume', '123')])
    def test_get_project_dependent_resources(self, mock_volume_list):
        project = project_info('abcd', constants.PROJECT_RESOURCE_TYPE,
                               'nameabcd')
        plugin = VolumeProtectablePlugin(self._context)

        volumes = [
            mock.Mock(name='Volume', id='123'),
            mock.Mock(name='Volume', id='456'),
        ]
        setattr(volumes[0], 'os-vol-tenant-attr:tenant_id', 'abcd')
        setattr(volumes[1], 'os-vol-tenant-attr:tenant_id', 'efgh')
        setattr(volumes[0], 'name', 'name123')
        setattr(volumes[1], 'name', 'name456')

        mock_volume_list.return_value = volumes
        self.assertEqual(
            plugin.get_dependent_resources(self._context, project),
            [Resource('OS::Cinder::Volume', '123', 'name123')])
 def test_get_parent_resource_types(self):
     plugin = VolumeProtectablePlugin(self._context)
     self.assertItemsEqual(("OS::Nova::Server", "OS::Keystone::Project"),
                           plugin.get_parent_resource_types())
 def test_get_resource_type(self):
     plugin = VolumeProtectablePlugin(self._context)
     self.assertEqual("OS::Cinder::Volume", plugin.get_resource_type())
 def test_create_client_by_catalog(self):
     plugin = VolumeProtectablePlugin(self._context)
     self.assertEqual('volumev2',
                      plugin._client(self._context).client.service_type)
     self.assertEqual('http://127.0.0.1:8776/v2/abcd',
                      plugin._client(self._context).client.management_url)
 def test_get_resource_type(self):
     plugin = VolumeProtectablePlugin(self._context)
     self.assertEqual("OS::Cinder::Volume", plugin.get_resource_type())
 def test_get_parent_resource_types(self):
     plugin = VolumeProtectablePlugin(self._context)
     self.assertItemsEqual(("OS::Nova::Server", "OS::Keystone::Project"),
                           plugin.get_parent_resource_types())
 def test_create_client_by_catalog(self):
     plugin = VolumeProtectablePlugin(self._context)
     self.assertEqual('volumev2',
                      plugin._client(self._context).client.service_type)
     self.assertEqual('http://127.0.0.1:8776/v2/abcd',
                      plugin._client(self._context).client.management_url)