Esempio n. 1
0
    def test_initialize_connection_exception_no_code(self, mock_cinderclient,
                                                     mock_log):
        mock_cinderclient.return_value.volumes. \
            initialize_connection.side_effect = (
                cinder_exception.ClientException(500, "500"))
        mock_cinderclient.return_value.volumes. \
            terminate_connection.side_effect = (
                test.TestingException)

        connector = {'host': 'fakehost1'}
        self.assertRaises(cinder_exception.ClientException,
                          self.api.initialize_connection, self.ctx, 'id1',
                          connector)
        self.assertIsNone(mock_log.error.call_args_list[1][0][1]['code'])
Esempio n. 2
0
    def test_initialize_connection_rollback(self, mock_cinderclient):
        mock_cinderclient.return_value.volumes.\
            initialize_connection.side_effect = (
                cinder_exception.ClientException(500, "500"))

        connector = {'host': 'host1'}
        ex = self.assertRaises(cinder_exception.ClientException,
                               self.api.initialize_connection,
                               self.ctx,
                               'id1',
                               connector)
        self.assertEqual(500, ex.code)
        mock_cinderclient.return_value.volumes.\
            terminate_connection.assert_called_once_with('id1', connector)
Esempio n. 3
0
def do_backup_update(cs, args):
    """Renames a backup."""
    kwargs = {}

    if args.name is not None:
        kwargs['name'] = args.name

    if args.description is not None:
        kwargs['description'] = args.description

    if not kwargs:
        msg = 'Must supply either name or description.'
        raise exceptions.ClientException(code=1, message=msg)

    shell_utils.find_backup(cs, args.backup).update(**kwargs)
Esempio n. 4
0
class TestCinderConnector(base.TestCase):
    def setUp(self):
        base.TestCase.setUp(self)
        self.connector = openstack.CinderConnector()
        self.connector.cinderclient = fake_client.FakeCinderClient()
        self.connector.novaclient = fake_client.FakeNovaClient()

    def test_connect_volume(self):
        pass

    @mock.patch.object(utils, 'get_instance_uuid', return_value='fake-123')
    @mock.patch.object(utils, 'execute')
    @mock.patch.object(state_monitor.StateMonitor,
                       'monitor_cinder_volume',
                       return_value=None)
    def test_disconnect_volume(self, mock_inst_id, mock_execute, mock_monitor):
        fake_cinder_volume = fake_object.FakeCinderVolume()
        result = self.connector.disconnect_volume(fake_cinder_volume)
        self.assertIsNone(result)

    @mock.patch('fuxi.tests.unit.fake_client.FakeCinderClient.Volumes.get',
                side_effect=cinder_exception.ClientException(404))
    @mock.patch.object(utils, 'execute')
    @mock.patch.object(state_monitor.StateMonitor, 'monitor_cinder_volume')
    def test_disconnect_volume_for_not_found(self, mock_get, mock_execute,
                                             mocK_monitor):
        fake_cinder_volume = fake_object.FakeCinderVolume()
        self.assertRaises(cinder_exception.ClientException,
                          self.connector.disconnect_volume, fake_cinder_volume)

    @mock.patch('fuxi.tests.unit.fake_client.FakeNovaClient.Volumes'
                '.delete_server_volume',
                side_effect=nova_exception.ClientException(500))
    @mock.patch.object(utils, 'get_instance_uuid', return_value='fake-123')
    @mock.patch.object(utils, 'execute')
    @mock.patch.object(state_monitor.StateMonitor, 'monitor_cinder_volume')
    def test_disconnect_volume_for_delete_server_volume_failed(
            self, mock_delete, mock_inst_id, mock_execute, mock_monitor):
        fake_cinder_volume = fake_object.FakeCinderVolume()
        self.assertRaises(nova_exception.ClientException,
                          self.connector.disconnect_volume, fake_cinder_volume)

    def test_get_device_path(self):
        fake_cinder_volume = fake_object.FakeCinderVolume()
        fake_devpath = os.path.join(constants.VOLUME_LINK_DIR,
                                    fake_cinder_volume.id)
        self.assertEqual(fake_devpath,
                         self.connector.get_device_path(fake_cinder_volume))
Esempio n. 5
0
def do_rename(cs, args):
    """Rename a volume."""
    kwargs = {}

    if args.name is not None:
        kwargs['name'] = args.name
    if args.display_description is not None:
        kwargs['description'] = args.display_description
    elif args.description is not None:
        kwargs['description'] = args.description

    if not any(kwargs):
        msg = 'Must supply either name or description.'
        raise exceptions.ClientException(code=1, message=msg)

    utils.find_volume(cs, args.volume).update(**kwargs)
Esempio n. 6
0
def quota_update(manager, identifier, args):
    updates = {}
    for resource in _quota_resources:
        val = getattr(args, resource, None)
        if val is not None:
            if args.volume_type:
                resource = resource + '_%s' % args.volume_type
            updates[resource] = val

    if updates:
        skip_validation = getattr(args, 'skip_validation', True)
        if not skip_validation:
            updates['skip_validation'] = skip_validation
        quota_show(manager.update(identifier, **updates))
    else:
        msg = 'Must supply at least one quota field to update.'
        raise exceptions.ClientException(code=1, message=msg)
Esempio n. 7
0
 def wait_volume_state(self, volume_id, state):
     attempts = 0
     vol = self.get_cinder().volumes.get(volume_id)
     state = state.lower()
     while (attempts < int(self.params['poll_max'])
            and vol.status.lower() != state
            and vol.status.lower() != 'error'):
         self.logger.debug("Volume '%s' has status '%s', waiting %ds",
                           volume_id, vol.status,
                           int(self.params['poll_secs']))
         attempts += 1
         time.sleep(int(self.params['poll_secs']))
         vol = self.get_cinder().volumes.get(volume_id)
     if vol.status.lower() != state:
         raise cinder_exceptions.ClientException(
             "Volume '%s' unexpectedly has status '%s'" %
             (volume_id, vol.status))
     self.logger.info("Volume '%s' has status '%s'", volume_id, state)
Esempio n. 8
0
    def _do_resize(self, new_size):
        try:
            self.volume_client.volumes.extend(self.volume_id, new_size)
        except cinder_exceptions.ClientException:
            LOG.exception(
                _("Error encountered trying to rescan or resize the "
                  "attached volume filesystem for volume: "
                  "%s") % self.volume_id)
            raise

        try:
            volume = self.volume_client.volumes.get(self.volume_id)
            if not volume:
                raise (cinder_exceptions.ClientException(
                    _('Failed to get volume with '
                      'id: %(id)s') % {'id': self.volume_id}))
            utils.poll_until(
                lambda: self.volume_client.volumes.get(self.volume_id),
                lambda volume: volume.size == int(new_size),
                sleep_time=2,
                time_out=CONF.volume_time_out)
            self.update_db(volume_size=new_size)
        except PollTimeOut:
            LOG.error(
                _("Timeout trying to rescan or resize the attached "
                  "volume filesystem for volume %(vol_id)s of "
                  "instance: %(id)s") % {
                      'vol_id': self.volume_id,
                      'id': self.id
                  })
        except Exception as e:
            LOG.exception(
                _("Error encountered trying to rescan or resize the "
                  "attached volume filesystem of volume %(vol_id)s of "
                  "instance %(id)s: %(e)s") % {
                      'vol_id': self.volume_id,
                      'id': self.id,
                      'e': e
                  })
        finally:
            self.update_db(task_status=inst_models.InstanceTasks.NONE)
Esempio n. 9
0
def do_group_update(cs, args):
    """Updates a group."""
    kwargs = {}

    if args.name is not None:
        kwargs['name'] = args.name

    if args.description is not None:
        kwargs['description'] = args.description

    if args.add_volumes is not None:
        kwargs['add_volumes'] = args.add_volumes

    if args.remove_volumes is not None:
        kwargs['remove_volumes'] = args.remove_volumes

    if not kwargs:
        msg = ('At least one of the following args must be supplied: '
               'name, description, add-volumes, remove-volumes.')
        raise exceptions.ClientException(code=1, message=msg)

    shell_utils.find_group(cs, args.group).update(**kwargs)
Esempio n. 10
0
class TestIsNotFound(common.HeatTestCase):

    scenarios = [
        ('ceilometer_not_found', dict(
            is_not_found=True,
            is_over_limit=False,
            is_client_exception=True,
            is_conflict=False,
            plugin='ceilometer',
            exception=lambda: ceil_exc.HTTPNotFound(details='gone'),
        )),
        ('ceilometer_not_found_apiclient', dict(
            is_not_found=True,
            is_over_limit=False,
            is_client_exception=True,
            is_conflict=False,
            plugin='ceilometer',
            exception=lambda: c_a_exc.NotFound(details='gone'),
        )),
        ('ceilometer_exception', dict(
            is_not_found=False,
            is_over_limit=False,
            is_client_exception=False,
            is_conflict=False,
            plugin='ceilometer',
            exception=lambda: Exception()
        )),
        ('ceilometer_overlimit', dict(
            is_not_found=False,
            is_over_limit=True,
            is_client_exception=True,
            is_conflict=False,
            plugin='ceilometer',
            exception=lambda: ceil_exc.HTTPOverLimit(details='over'),
        )),
        ('ceilometer_conflict', dict(
            is_not_found=False,
            is_over_limit=False,
            is_client_exception=True,
            is_conflict=True,
            plugin='ceilometer',
            exception=lambda: ceil_exc.HTTPConflict(),
        )),
        ('cinder_not_found', dict(
            is_not_found=True,
            is_over_limit=False,
            is_client_exception=True,
            is_conflict=False,
            plugin='cinder',
            exception=lambda: cinder_exc.NotFound(code=404),
        )),
        ('cinder_exception', dict(
            is_not_found=False,
            is_over_limit=False,
            is_client_exception=False,
            is_conflict=False,
            plugin='cinder',
            exception=lambda: Exception()
        )),
        ('cinder_overlimit', dict(
            is_not_found=False,
            is_over_limit=True,
            is_client_exception=True,
            is_conflict=False,
            plugin='cinder',
            exception=lambda: cinder_exc.OverLimit(code=413),
        )),
        ('cinder_conflict', dict(
            is_not_found=False,
            is_over_limit=False,
            is_client_exception=True,
            is_conflict=True,
            plugin='cinder',
            exception=lambda: cinder_exc.ClientException(code=409),
        )),
        ('glance_not_found', dict(
            is_not_found=True,
            is_over_limit=False,
            is_client_exception=True,
            is_conflict=False,
            plugin='glance',
            exception=lambda: glance_exc.HTTPNotFound(details='gone'),
        )),
        ('glance_exception', dict(
            is_not_found=False,
            is_over_limit=False,
            is_client_exception=False,
            is_conflict=False,
            plugin='glance',
            exception=lambda: Exception()
        )),
        ('glance_overlimit', dict(
            is_not_found=False,
            is_over_limit=True,
            is_client_exception=True,
            is_conflict=False,
            plugin='glance',
            exception=lambda: glance_exc.HTTPOverLimit(details='over'),
        )),
        ('glance_conflict', dict(
            is_not_found=False,
            is_over_limit=False,
            is_client_exception=True,
            is_conflict=True,
            plugin='glance',
            exception=lambda: glance_exc.HTTPConflict(),
        )),
        ('heat_not_found', dict(
            is_not_found=True,
            is_over_limit=False,
            is_client_exception=True,
            is_conflict=False,
            plugin='heat',
            exception=lambda: heat_exc.HTTPNotFound(message='gone'),
        )),
        ('heat_exception', dict(
            is_not_found=False,
            is_over_limit=False,
            is_client_exception=False,
            is_conflict=False,
            plugin='heat',
            exception=lambda: Exception()
        )),
        ('heat_overlimit', dict(
            is_not_found=False,
            is_over_limit=True,
            is_client_exception=True,
            is_conflict=False,
            plugin='heat',
            exception=lambda: heat_exc.HTTPOverLimit(message='over'),
        )),
        ('heat_conflict', dict(
            is_not_found=False,
            is_over_limit=False,
            is_client_exception=True,
            is_conflict=True,
            plugin='heat',
            exception=lambda: heat_exc.HTTPConflict(),
        )),
        ('keystone_not_found', dict(
            is_not_found=True,
            is_over_limit=False,
            is_client_exception=True,
            is_conflict=False,
            plugin='keystone',
            exception=lambda: keystone_exc.NotFound(details='gone'),
        )),
        ('keystone_exception', dict(
            is_not_found=False,
            is_over_limit=False,
            is_client_exception=False,
            is_conflict=False,
            plugin='keystone',
            exception=lambda: Exception()
        )),
        ('keystone_overlimit', dict(
            is_not_found=False,
            is_over_limit=True,
            is_client_exception=True,
            is_conflict=False,
            plugin='keystone',
            exception=lambda: keystone_exc.RequestEntityTooLarge(
                details='over'),
        )),
        ('keystone_conflict', dict(
            is_not_found=False,
            is_over_limit=False,
            is_client_exception=True,
            is_conflict=True,
            plugin='keystone',
            exception=lambda: keystone_exc.Conflict(
                message='Conflict'),
        )),
        ('neutron_not_found', dict(
            is_not_found=True,
            is_over_limit=False,
            is_client_exception=True,
            is_conflict=False,
            plugin='neutron',
            exception=lambda: neutron_exc.NotFound,
        )),
        ('neutron_network_not_found', dict(
            is_not_found=True,
            is_over_limit=False,
            is_client_exception=True,
            is_conflict=False,
            plugin='neutron',
            exception=lambda: neutron_exc.NetworkNotFoundClient(),
        )),
        ('neutron_port_not_found', dict(
            is_not_found=True,
            is_over_limit=False,
            is_client_exception=True,
            is_conflict=False,
            plugin='neutron',
            exception=lambda: neutron_exc.PortNotFoundClient(),
        )),
        ('neutron_status_not_found', dict(
            is_not_found=True,
            is_over_limit=False,
            is_client_exception=True,
            is_conflict=False,
            plugin='neutron',
            exception=lambda: neutron_exc.NeutronClientException(
                status_code=404),
        )),
        ('neutron_exception', dict(
            is_not_found=False,
            is_over_limit=False,
            is_client_exception=False,
            is_conflict=False,
            plugin='neutron',
            exception=lambda: Exception()
        )),
        ('neutron_overlimit', dict(
            is_not_found=False,
            is_over_limit=True,
            is_client_exception=True,
            is_conflict=False,
            plugin='neutron',
            exception=lambda: neutron_exc.NeutronClientException(
                status_code=413),
        )),
        ('neutron_conflict', dict(
            is_not_found=False,
            is_over_limit=False,
            is_client_exception=True,
            is_conflict=True,
            plugin='neutron',
            exception=lambda: neutron_exc.Conflict(),
        )),
        ('nova_not_found', dict(
            is_not_found=True,
            is_over_limit=False,
            is_client_exception=True,
            is_conflict=False,
            is_unprocessable_entity=False,
            plugin='nova',
            exception=lambda: fakes_nova.fake_exception(),
        )),
        ('nova_exception', dict(
            is_not_found=False,
            is_over_limit=False,
            is_client_exception=False,
            is_conflict=False,
            is_unprocessable_entity=False,
            plugin='nova',
            exception=lambda: Exception()
        )),
        ('nova_overlimit', dict(
            is_not_found=False,
            is_over_limit=True,
            is_client_exception=True,
            is_conflict=False,
            is_unprocessable_entity=False,
            plugin='nova',
            exception=lambda: fakes_nova.fake_exception(413),
        )),
        ('nova_unprocessable_entity', dict(
            is_not_found=False,
            is_over_limit=False,
            is_client_exception=True,
            is_conflict=False,
            is_unprocessable_entity=True,
            plugin='nova',
            exception=lambda: fakes_nova.fake_exception(422),
        )),
        ('nova_conflict', dict(
            is_not_found=False,
            is_over_limit=False,
            is_client_exception=True,
            is_conflict=True,
            is_unprocessable_entity=False,
            plugin='nova',
            exception=lambda: fakes_nova.fake_exception(409),
        )),
        ('swift_not_found', dict(
            is_not_found=True,
            is_over_limit=False,
            is_client_exception=True,
            is_conflict=False,
            plugin='swift',
            exception=lambda: swift_exc.ClientException(
                msg='gone', http_status=404),
        )),
        ('swift_exception', dict(
            is_not_found=False,
            is_over_limit=False,
            is_client_exception=False,
            is_conflict=False,
            plugin='swift',
            exception=lambda: Exception()
        )),
        ('swift_overlimit', dict(
            is_not_found=False,
            is_over_limit=True,
            is_client_exception=True,
            is_conflict=False,
            plugin='swift',
            exception=lambda: swift_exc.ClientException(
                msg='ouch', http_status=413),
        )),
        ('swift_conflict', dict(
            is_not_found=False,
            is_over_limit=False,
            is_client_exception=True,
            is_conflict=True,
            plugin='swift',
            exception=lambda: swift_exc.ClientException(
                msg='conflict', http_status=409),
        )),
        ('trove_not_found', dict(
            is_not_found=True,
            is_over_limit=False,
            is_client_exception=True,
            is_conflict=False,
            plugin='trove',
            exception=lambda: troveclient.exceptions.NotFound(message='gone'),
        )),
        ('trove_exception', dict(
            is_not_found=False,
            is_over_limit=False,
            is_client_exception=False,
            is_conflict=False,
            plugin='trove',
            exception=lambda: Exception()
        )),
        ('trove_overlimit', dict(
            is_not_found=False,
            is_over_limit=True,
            is_client_exception=True,
            is_conflict=False,
            plugin='trove',
            exception=lambda: troveclient.exceptions.RequestEntityTooLarge(
                message='over'),
        )),
        ('trove_conflict', dict(
            is_not_found=False,
            is_over_limit=False,
            is_client_exception=True,
            is_conflict=True,
            plugin='trove',
            exception=lambda: troveclient.exceptions.Conflict(
                message='Conflict'),
        )),
        ('sahara_not_found', dict(
            is_not_found=True,
            is_over_limit=False,
            is_client_exception=True,
            is_conflict=False,
            plugin='sahara',
            exception=lambda: sahara_base.APIException(
                error_message='gone1', error_code=404),
        )),
        ('sahara_exception', dict(
            is_not_found=False,
            is_over_limit=False,
            is_client_exception=False,
            is_conflict=False,
            plugin='sahara',
            exception=lambda: Exception()
        )),
        ('sahara_overlimit', dict(
            is_not_found=False,
            is_over_limit=True,
            is_client_exception=True,
            is_conflict=False,
            plugin='sahara',
            exception=lambda: sahara_base.APIException(
                error_message='over1', error_code=413),
        )),
        ('sahara_conflict', dict(
            is_not_found=False,
            is_over_limit=False,
            is_client_exception=True,
            is_conflict=True,
            plugin='sahara',
            exception=lambda: sahara_base.APIException(
                error_message='conflict1', error_code=409),
        )),
    ]

    def test_is_not_found(self):
        con = mock.Mock()
        c = clients.Clients(con)
        client_plugin = c.client_plugin(self.plugin)
        try:
            raise self.exception()
        except Exception as e:
            if self.is_not_found != client_plugin.is_not_found(e):
                raise

    def test_ignore_not_found(self):
        con = mock.Mock()
        c = clients.Clients(con)
        client_plugin = c.client_plugin(self.plugin)
        try:
            exp = self.exception()
            exp_class = exp.__class__
            raise exp
        except Exception as e:
            if self.is_not_found:
                client_plugin.ignore_not_found(e)
            else:
                self.assertRaises(exp_class,
                                  client_plugin.ignore_not_found,
                                  e)

    def test_ignore_conflict_and_not_found(self):
        con = mock.Mock()
        c = clients.Clients(con)
        client_plugin = c.client_plugin(self.plugin)
        try:
            exp = self.exception()
            exp_class = exp.__class__
            raise exp
        except Exception as e:
            if self.is_conflict or self.is_not_found:
                client_plugin.ignore_conflict_and_not_found(e)
            else:
                self.assertRaises(exp_class,
                                  client_plugin.ignore_conflict_and_not_found,
                                  e)

    def test_is_over_limit(self):
        con = mock.Mock()
        c = clients.Clients(con)
        client_plugin = c.client_plugin(self.plugin)
        try:
            raise self.exception()
        except Exception as e:
            if self.is_over_limit != client_plugin.is_over_limit(e):
                raise

    def test_is_client_exception(self):
        con = mock.Mock()
        c = clients.Clients(con)
        client_plugin = c.client_plugin(self.plugin)
        try:
            raise self.exception()
        except Exception as e:
            ice = self.is_client_exception
            actual = client_plugin.is_client_exception(e)
            if ice != actual:
                raise

    def test_is_conflict(self):
        con = mock.Mock()
        c = clients.Clients(con)
        client_plugin = c.client_plugin(self.plugin)
        try:
            raise self.exception()
        except Exception as e:
            if self.is_conflict != client_plugin.is_conflict(e):
                raise

    def test_is_unprocessable_entity(self):
        con = mock.Mock()
        c = clients.Clients(con)
        # only 'nova' client plugin need to check this exception
        if self.plugin == 'nova':
            client_plugin = c.client_plugin(self.plugin)
            try:
                raise self.exception()
            except Exception as e:
                iue = self.is_unprocessable_entity
                if iue != client_plugin.is_unprocessable_entity(e):
                    raise
Esempio n. 11
0
class TestCinderConnector(base.TestCase):
    def setUp(self):
        base.TestCase.setUp(self)
        self.connector = osbrickconnector.CinderConnector()
        self.connector.cinderclient = fake_client.FakeCinderClient()

    def test_connect_volume(self):
        fake_cinder_volume = fake_object.FakeCinderVolume()
        self.connector._connect_volume = mock.MagicMock()
        self.connector.connect_volume(fake_cinder_volume)
        self.assertEqual(1, len(fake_cinder_volume.attachments))

    @mock.patch.object(osbrickconnector,
                       'brick_get_connector',
                       return_value=fake_client.FakeOSBrickConnector())
    @mock.patch.object(utils, 'execute')
    def test_disconnect_volume(self, mock_brick_connector, mock_execute):
        attachments = [{
            u'server_id': u'123',
            u'attachment_id': u'123',
            u'attached_at': u'2016-05-20T09:19:57.000000',
            u'host_name': utils.get_hostname(),
            u'device': None,
            u'id': u'123'
        }]
        fake_cinder_volume = \
            fake_object.FakeCinderVolume(attachments=attachments)

        self.connector._get_connection_info = mock.MagicMock()
        self.connector.cinderclient.volumes.detach = mock.MagicMock()
        self.assertIsNone(self.connector.disconnect_volume(fake_cinder_volume))

    @mock.patch.object(osbrickconnector, 'brick_get_connector_properties',
                       mock_get_connector_properties)
    @mock.patch.object(utils, 'execute')
    @mock.patch('fuxi.tests.unit.fake_client.FakeCinderClient.Volumes'
                '.initialize_connection',
                side_effect=cinder_exception.ClientException(500))
    def test_disconnect_volume_no_connection_info(self, mock_execute,
                                                  mock_init_conn):
        attachments = [{
            u'server_id': u'123',
            u'attachment_id': u'123',
            u'attached_at': u'2016-05-20T09:19:57.000000',
            u'host_name': utils.get_hostname(),
            u'device': None,
            u'id': u'123'
        }]
        fake_cinder_volume = \
            fake_object.FakeCinderVolume(attachments=attachments)
        self.assertRaises(cinder_exception.ClientException,
                          self.connector.disconnect_volume, fake_cinder_volume)

    @mock.patch.object(osbrickconnector,
                       'brick_get_connector',
                       return_value=fake_client.FakeOSBrickConnector())
    @mock.patch.object(osbrickconnector.CinderConnector,
                       '_get_connection_info',
                       return_value={
                           'driver_volume_type': 'fake_proto',
                           'data': {
                               'path': '/dev/0'
                           }
                       })
    @mock.patch.object(utils, 'execute')
    @mock.patch('fuxi.tests.unit.fake_client.FakeOSBrickConnector'
                '.disconnect_volume',
                side_effect=processutils.ProcessExecutionError())
    def test_disconnect_volume_osbrick_disconnect_failed(
            self, mock_connector, mock_init_conn, mock_execute,
            mock_disconnect_vol):
        attachments = [{
            u'server_id': u'123',
            u'attachment_id': u'123',
            u'attached_at': u'2016-05-20T09:19:57.000000',
            u'host_name': utils.get_hostname(),
            u'device': None,
            u'id': u'123'
        }]
        fake_cinder_volume = \
            fake_object.FakeCinderVolume(attachments=attachments)
        self.assertRaises(processutils.ProcessExecutionError,
                          self.connector.disconnect_volume, fake_cinder_volume)

    @mock.patch('fuxi.tests.unit.fake_client.FakeCinderClient.Volumes.detach',
                side_effect=cinder_exception.ClientException(500))
    @mock.patch.object(osbrickconnector,
                       'brick_get_connector',
                       return_value=fake_client.FakeOSBrickConnector())
    @mock.patch.object(utils, 'execute')
    @mock.patch.object(osbrickconnector.CinderConnector,
                       '_get_connection_info',
                       return_value={
                           'driver_volume_type': 'fake_proto',
                           'data': {
                               'path': '/dev/0'
                           }
                       })
    def test_disconnect_volume_detach_failed(self, mock_detach,
                                             mock_brick_connector,
                                             mock_execute, mock_conn_info):
        attachments = [{
            u'server_id': u'123',
            u'attachment_id': u'123',
            u'attached_at': u'2016-05-20T09:19:57.000000',
            u'host_name': utils.get_hostname(),
            u'device': None,
            u'id': u'123'
        }]
        fake_cinder_volume = \
            fake_object.FakeCinderVolume(attachments=attachments)
        self.assertRaises(cinder_exception.ClientException,
                          self.connector.disconnect_volume, fake_cinder_volume)

    def test_get_device_path(self):
        fake_cinder_volume = fake_object.FakeCinderVolume()
        self.assertEqual(
            os.path.join(constants.VOLUME_LINK_DIR, fake_cinder_volume.id),
            self.connector.get_device_path(fake_cinder_volume))
Esempio n. 12
0
class TestCinder(base.TestCase):
    volume_provider_type = 'cinder'

    def setUp(self):
        base.TestCase.setUp(self)
        self.cinderprovider = cinder.Cinder()
        self.cinderprovider.cinderclient = fake_client.FakeCinderClient()

    @mock.patch.object(cinder.Cinder, '_get_connector', mock_connector)
    @mock.patch.object(cinder.Cinder,
                       '_get_docker_volume',
                       return_value=(None, consts.UNKNOWN))
    def test_create_with_volume_not_exist(self, mock_docker_volume):
        self.assertEqual(os.path.join(volume_link_dir, DEFAULT_VOLUME_ID),
                         self.cinderprovider.create('fake-vol', {})['path'])

    @mock.patch.object(cinder.Cinder, '_get_connector', mock_connector)
    @mock.patch.object(
        cinder.Cinder,
        '_get_docker_volume',
        return_value=(fake_object.FakeCinderVolume(status='unknown'),
                      consts.UNKNOWN))
    @mock.patch.object(state_monitor.StateMonitor, 'monitor_cinder_volume',
                       mock_monitor_cinder_volume)
    def test_create_from_volume_id(self, mock_docker_volume):
        fake_volume_name = 'fake_vol'
        fake_volume_opts = {'volume_id': DEFAULT_VOLUME_ID}
        result = self.cinderprovider.create(fake_volume_name, fake_volume_opts)
        self.assertEqual(
            os.path.join(consts.VOLUME_LINK_DIR, DEFAULT_VOLUME_ID),
            result['path'])

    @mock.patch.object(cinder.Cinder, '_get_connector', mock_connector)
    @mock.patch.object(
        cinder.Cinder,
        '_get_docker_volume',
        return_value=(fake_object.FakeCinderVolume(status='unknown'),
                      consts.UNKNOWN))
    @mock.patch('fuxi.tests.unit.fake_client.FakeCinderClient.Volumes.get',
                side_effect=cinder_exception.ClientException(404))
    def test_create_from_volume_id_with_volume_not_exist(
            self, mocK_docker_volume, mock_volume_get):
        fake_volume_name = 'fake_vol'
        fake_volume_opts = {'volume_id': DEFAULT_VOLUME_ID}
        self.assertRaises(cinder_exception.ClientException,
                          self.cinderprovider.create, fake_volume_name,
                          fake_volume_opts)

    @mock.patch.object(cinder.Cinder, '_get_connector', mock_connector)
    @mock.patch.object(
        cinder.Cinder,
        '_get_docker_volume',
        return_value=(fake_object.FakeCinderVolume(status='unknown'),
                      consts.UNKNOWN))
    def test_create_from_volume_id_with_unexpected_status_1(
            self, mock_docker_volume):
        fake_volume_name = 'fake_vol'
        fake_volume_args = {
            'volume_id': DEFAULT_VOLUME_ID,
            'status': 'attaching'
        }
        fake_cinder_volume = fake_object.FakeCinderVolume(**fake_volume_args)
        self.cinderprovider._get_docker_volume = mock.MagicMock()
        self.cinderprovider._get_docker_volume.return_value \
            = (fake_cinder_volume,
               consts.UNKNOWN)
        self.cinderprovider.cinderclient.volumes.get = mock.MagicMock()
        self.cinderprovider.cinderclient.volumes.get.return_value = \
            fake_cinder_volume
        self.assertRaises(exceptions.FuxiException, self.cinderprovider.create,
                          fake_volume_name, {'volume_id': DEFAULT_VOLUME_ID})

    @mock.patch.object(cinder.Cinder, '_get_connector', mock_connector)
    def test_create_from_volume_id_with_unexpected_status_2(self):
        fake_server_id = 'fake_server_123'
        fake_host_name = 'attached_to_other'
        fake_volume_name = 'fake_vol'
        fake_volume_args = {
            'volume_id':
            DEFAULT_VOLUME_ID,
            'status':
            'in-use',
            'multiattach':
            False,
            'attachments': [{
                'server_id': fake_server_id,
                'host_name': fake_host_name
            }]
        }
        fake_cinder_volume = fake_object.FakeCinderVolume(**fake_volume_args)
        self.cinderprovider._get_docker_volume = mock.MagicMock()
        self.cinderprovider._get_docker_volume.return_value \
            = (fake_cinder_volume,
               consts.UNKNOWN)
        self.cinderprovider.cinderclient.volumes.get = mock.MagicMock()
        self.cinderprovider.cinderclient.volumes.get.return_value = \
            fake_cinder_volume
        self.assertRaises(exceptions.FuxiException, self.cinderprovider.create,
                          fake_volume_name, {'volume_id': DEFAULT_VOLUME_ID})

    @mock.patch.object(cinder.Cinder, '_get_connector', mock_connector)
    def test_create_with_volume_attach_to_this(self):
        fake_server_id = 'fake_server_123'
        fake_host_name = 'attached_to_this'
        fake_volume_args = {
            'id':
            DEFAULT_VOLUME_ID,
            'status':
            'in-use',
            'attachments': [{
                'server_id': fake_server_id,
                'host_name': fake_host_name
            }]
        }
        fake_cinder_volume = fake_object.FakeCinderVolume(**fake_volume_args)
        self.cinderprovider._get_docker_volume = mock.MagicMock()
        self.cinderprovider._get_docker_volume.return_value \
            = (fake_cinder_volume,
               consts.ATTACH_TO_THIS)
        self.cinderprovider.cinderclient.volumes.get = mock.MagicMock()
        self.cinderprovider.cinderclient.volumes.get.return_value = \
            fake_cinder_volume
        fake_result = self.cinderprovider.create('fake-vol', {})
        self.assertEqual(os.path.join(volume_link_dir, DEFAULT_VOLUME_ID),
                         fake_result['path'])

    @mock.patch.object(cinder.Cinder, '_get_connector', mock_connector)
    def test_create_with_volume_no_attach(self):
        fake_cinder_volume = fake_object.FakeCinderVolume()
        self.cinderprovider._get_docker_volume = mock.MagicMock()
        self.cinderprovider._get_docker_volume.return_value \
            = (fake_cinder_volume,
               consts.NOT_ATTACH)
        fake_result = self.cinderprovider.create('fake-vol', {})
        self.assertEqual(os.path.join(volume_link_dir, DEFAULT_VOLUME_ID),
                         fake_result['path'])

    @mock.patch.object(cinder.Cinder, '_get_connector', mock_connector)
    @mock.patch.object(
        cinder.Cinder,
        '_get_docker_volume',
        return_value=(fake_object.FakeCinderVolume(multiattach=True),
                      consts.ATTACH_TO_OTHER))
    def test_create_with_multiable_vol_attached_to_other(
            self, mock_docker_volume):
        self.assertEqual(
            os.path.join(volume_link_dir, fake_object.DEFAULT_VOLUME_ID),
            self.cinderprovider.create('fake-vol', {})['path'])

    @mock.patch.object(cinder.Cinder, '_get_connector', mock_connector)
    @mock.patch.object(
        cinder.Cinder,
        '_get_docker_volume',
        return_value=(fake_object.FakeCinderVolume(multiattach=False),
                      consts.ATTACH_TO_OTHER))
    def test_create_with_volume_attached_to_other(self, mock_docker_volume):
        self.assertRaises(exceptions.FuxiException, self.cinderprovider.create,
                          'fake-vol', {})

    def test_create_with_multi_matched_volumes(self):
        fake_vol_name = 'fake-vol'
        fake_vols = [
            fake_object.FakeCinderVolume(name=fake_vol_name),
            fake_object.FakeCinderVolume(name=fake_vol_name)
        ]
        with mock.patch.object(fake_client.FakeCinderClient.Volumes,
                               'list',
                               return_value=fake_vols):
            self.assertRaises(exceptions.TooManyResources,
                              self.cinderprovider.create, fake_vol_name, {})

    @mock.patch.object(cinder.Cinder, '_get_connector', mock_connector)
    @mock.patch.object(utils, 'execute')
    @mock.patch.object(FakeCinderConnector, 'get_device_path',
                       mock_device_path_for_delete)
    def test_delete(self, mock_execute):
        fd, tmpfname = tempfile.mkstemp()
        attachments = [{
            u'server_id': u'123',
            u'attachment_id': u'123',
            u'attached_at': u'2016-05-20T09:19:57.000000',
            u'host_name': utils.get_hostname(),
            u'device': None,
            u'id': u'123'
        }]

        self.cinderprovider._get_docker_volume = mock.MagicMock()
        self.cinderprovider._get_docker_volume.return_value = (
            fake_object.FakeCinderVolume(id=tmpfname, attachments=attachments),
            consts.ATTACH_TO_THIS)
        self.cinderprovider._delete_volume = mock.MagicMock()

        self.assertTrue(self.cinderprovider.delete('fake-vol'))

    @mock.patch.object(cinder.Cinder, '_get_connector', mock_connector)
    @mock.patch.object(cinder.Cinder,
                       '_get_docker_volume',
                       return_value=(fake_object.FakeCinderVolume(),
                                     consts.NOT_ATTACH))
    def test_delete_not_attach(self, mock_docker_volume):
        self.cinderprovider._delete_volume = mock.MagicMock()
        self.assertTrue(self.cinderprovider.delete('fake-vol'))

    @mock.patch.object(cinder.Cinder, '_get_connector', mock_connector)
    @mock.patch.object(cinder.Cinder,
                       '_get_docker_volume',
                       return_value=(fake_object.FakeCinderVolume(),
                                     consts.ATTACH_TO_OTHER))
    def test_delete_attach_to_other(self, mock_docker_volume):
        self.assertTrue(self.cinderprovider.delete('fake-vol'))

    @mock.patch.object(cinder.Cinder, '_get_connector', mock_connector)
    @mock.patch.object(cinder.Cinder,
                       '_get_docker_volume',
                       return_value=(fake_object.FakeCinderVolume(status=None),
                                     None))
    def test_delete_not_match_state(self, mock_docker_volume):
        self.assertRaises(exceptions.NotMatchedState,
                          self.cinderprovider.delete, 'fake-vol')

    @mock.patch.object(cinder.Cinder, '_get_connector', mock_connector)
    @mock.patch.object(utils, 'execute')
    @mock.patch.object(FakeCinderConnector, 'get_device_path',
                       mock_device_path_for_delete)
    @mock.patch('fuxi.tests.unit.fake_client.FakeCinderClient.Volumes.delete',
                side_effect=cinder_exception.ClientException(500))
    def test_delete_failed(self, mock_execute, mock_delete):
        fd, tmpfname = tempfile.mkstemp()
        attachments = [{
            u'server_id': u'123',
            u'attachment_id': u'123',
            u'attached_at': u'2016-05-20T09:19:57.000000',
            u'host_name': utils.get_hostname(),
            u'device': None,
            u'id': u'123'
        }]

        self.cinderprovider._get_docker_volume = mock.MagicMock()
        self.cinderprovider._get_docker_volume.return_value = (
            fake_object.FakeCinderVolume(id=tmpfname, attachments=attachments),
            consts.ATTACH_TO_THIS)

        self.assertRaises(cinder_exception.ClientException,
                          self.cinderprovider.delete, 'fake-vol')

    @mock.patch.object(cinder.Cinder, '_get_connector', mock_connector)
    @mock.patch.object(utils, 'execute')
    @mock.patch.object(FakeCinderConnector, 'get_device_path',
                       mock_device_path_for_delete)
    def test_delete_timeout(self, mock_execute):
        consts.DESTROY_VOLUME_TIMEOUT = 4
        fd, tmpfname = tempfile.mkstemp()
        attachments = [{
            u'server_id': u'123',
            u'attachment_id': u'123',
            u'attached_at': u'2016-05-20T09:19:57.000000',
            u'host_name': utils.get_hostname(),
            u'device': None,
            u'id': u'123'
        }]

        self.cinderprovider._get_docker_volume = mock.MagicMock()
        self.cinderprovider._get_docker_volume.return_value = (
            fake_object.FakeCinderVolume(id=tmpfname, attachments=attachments),
            consts.ATTACH_TO_THIS)

        self.assertRaises(exceptions.TimeoutException,
                          self.cinderprovider.delete, 'fake-vol')

    @mock.patch.object(cinder.Cinder, '_get_connector', mock_connector)
    def test_list(self):
        fake_vols = [fake_object.FakeCinderVolume(name='fake-vol1')]
        with mock.patch.object(fake_client.FakeCinderClient.Volumes,
                               'list',
                               return_value=fake_vols):
            self.assertEqual([{
                'Name': 'fake-vol1',
                'Mountpoint': ''
            }], self.cinderprovider.list())

    @mock.patch.object(cinder.Cinder, '_get_connector', mock_connector)
    @mock.patch('fuxi.tests.unit.fake_client.FakeCinderClient.Volumes.list',
                side_effect=cinder_exception.ClientException(500))
    def test_list_failed(self, mock_list):
        self.assertRaises(cinder_exception.ClientException,
                          self.cinderprovider.list)

    @mock.patch.object(cinder.Cinder, '_get_connector', mock_connector)
    @mock.patch.object(utils, 'execute')
    @mock.patch.object(cinder.Cinder,
                       '_get_docker_volume',
                       return_value=(fake_object.FakeCinderVolume(),
                                     consts.ATTACH_TO_THIS))
    def test_show_state_attach_to_this(self, mock_execute, mock_docker_volume):
        self.assertEqual({
            'Name': 'fake-vol',
            'Mountpoint': ''
        }, self.cinderprovider.show('fake-vol'))

    @mock.patch.object(cinder.Cinder, '_get_connector', mock_connector)
    @mock.patch.object(
        cinder.Cinder,
        '_get_docker_volume',
        return_value=(fake_object.FakeCinderVolume(status='unknown'),
                      consts.UNKNOWN))
    def test_show_state_unknown(self, mock_docker_volume):
        self.assertRaises(exceptions.NotFound, self.cinderprovider.show,
                          'fake-vol')

    @mock.patch.object(cinder.Cinder, '_get_connector', mock_connector)
    @mock.patch.object(cinder.Cinder,
                       '_get_docker_volume',
                       return_value=(fake_object.FakeCinderVolume(status=None),
                                     None))
    def test_show_state_not_match(self, mock_docker_volume):
        self.assertRaises(exceptions.FuxiException, self.cinderprovider.show,
                          'fake-vol')

    @mock.patch.object(cinder.Cinder, '_get_connector', mock_connector)
    @mock.patch.object(cinder.Cinder,
                       '_get_docker_volume',
                       return_value=(fake_object.FakeCinderVolume(
                           name='fake-vol',
                           status='in-use'), consts.ATTACH_TO_THIS))
    @mock.patch.object(cinder.Cinder, '_create_mountpoint')
    @mock.patch.object(mount, 'do_mount')
    def test_mount(self, mock_docker_volume, mock_create_mp, mock_do_mount):
        fd, fake_devpath = tempfile.mkstemp()
        fake_link_path = fake_devpath
        fake_mountpoint = 'fake-mount-point/'
        with mock.patch.object(FakeCinderConnector,
                               'get_device_path',
                               return_value=fake_link_path):
            with mock.patch.object(cinder.Cinder,
                                   '_get_mountpoint',
                                   return_value=fake_mountpoint):
                self.assertEqual(fake_mountpoint,
                                 self.cinderprovider.mount('fake-vol'))

    @mock.patch.object(cinder.Cinder, '_get_connector', mock_connector)
    @mock.patch.object(cinder.Cinder,
                       '_get_docker_volume',
                       return_value=(fake_object.FakeCinderVolume(status=None),
                                     None))
    def test_mount_state_not_match(self, mock_docker_volume):
        self.assertRaises(exceptions.NotMatchedState,
                          self.cinderprovider.mount, 'fake-vol')

    @mock.patch.object(cinder.Cinder, '_get_connector', mock_connector)
    @mock.patch.object(cinder.Cinder,
                       '_get_docker_volume',
                       return_value=(fake_object.FakeCinderVolume(),
                                     consts.NOT_ATTACH))
    @mock.patch.object(cinder.Cinder, '_create_mountpoint')
    @mock.patch.object(mount, 'do_mount')
    def test_mount_state_not_attach(self, mock_docker_volume, mock_create_mp,
                                    mock_do_mount):
        fd, fake_devpath = tempfile.mkstemp()
        fake_link_path = fake_devpath
        fake_mountpoint = 'fake-mount-point/'
        with mock.patch.object(FakeCinderConnector,
                               'get_device_path',
                               return_value=fake_link_path):
            with mock.patch.object(cinder.Cinder,
                                   '_get_mountpoint',
                                   return_value=fake_mountpoint):
                self.assertEqual(fake_mountpoint,
                                 self.cinderprovider.mount('fake-vol'))

    @mock.patch.object(cinder.Cinder, '_get_connector', mock_connector)
    @mock.patch.object(cinder.Cinder, '_create_mountpoint')
    @mock.patch.object(mount, 'do_mount')
    def test_mount_state_attach_to_other(self, mock_create_mp, mock_do_mount):
        fd, fake_devpath = tempfile.mkstemp()
        fake_link_path = fake_devpath
        fake_mountpoint = 'fake-mount-point/'
        with mock.patch.object(FakeCinderConnector,
                               'get_device_path',
                               return_value=fake_link_path):
            with mock.patch.object(cinder.Cinder,
                                   '_get_mountpoint',
                                   return_value=fake_mountpoint):
                fake_c_vol = fake_object.FakeCinderVolume(multiattach=True)
                with mock.patch.object(cinder.Cinder,
                                       '_get_docker_volume',
                                       return_value=(fake_c_vol,
                                                     consts.ATTACH_TO_OTHER)):
                    self.assertEqual(fake_mountpoint,
                                     self.cinderprovider.mount('fake-vol'))

                fake_c_vol = fake_object.FakeCinderVolume(multiattach=False)
                with mock.patch.object(cinder.Cinder,
                                       '_get_docker_volume',
                                       return_value=(fake_c_vol,
                                                     consts.ATTACH_TO_OTHER)):
                    self.assertRaises(exceptions.FuxiException,
                                      self.cinderprovider.mount, 'fake-vol')

    def test_unmount(self):
        self.assertIsNone(self.cinderprovider.unmount('fake-vol'))

    def test_check_exists(self):
        self.cinderprovider._get_docker_volume = mock.MagicMock()
        self.cinderprovider._get_docker_volume.return_value = (None,
                                                               consts.UNKNOWN)

        result = self.cinderprovider.check_exist('fake-vol')
        self.assertFalse(result)

        self.cinderprovider._get_docker_volume.return_value = (
            fake_object.FakeCinderVolume(), consts.NOT_ATTACH)

        result = self.cinderprovider.check_exist('fake-vol')
        self.assertTrue(result)