Esempio n. 1
0
    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')
Esempio n. 2
0
 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, {})
Esempio n. 3
0
 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})
Esempio n. 4
0
    def test_monitor_cinder_volume(self):
        fake_cinder_client = fake_client.FakeCinderClient()
        fake_cinder_volume = fake_object.FakeCinderVolume(status='available')
        fake_desired_state = 'in-use'
        fake_transient_states = ('in-use', )
        fake_time_limit = 0
        fake_state_monitor = state_monitor.StateMonitor(
            fake_cinder_client, fake_cinder_volume, fake_desired_state,
            fake_transient_states, fake_time_limit)

        fake_desired_volume = fake_object.FakeCinderVolume(status='in-use')
        with mock.patch.object(fake_client.FakeCinderClient.Volumes,
                               'get',
                               return_value=fake_desired_volume):
            self.assertEqual(fake_desired_volume,
                             fake_state_monitor.monitor_cinder_volume())
Esempio n. 5
0
    def test_monitor_cinder_volume_unexpected_state(self):
        fake_cinder_client = fake_client.FakeCinderClient()
        fake_cinder_volume = fake_object.FakeCinderVolume(status='available')
        fake_desired_state = 'in-use'
        fake_transient_states = ('in-use', )
        fake_time_limit = 0

        fake_state_monitor = state_monitor.StateMonitor(
            fake_cinder_client, fake_cinder_volume, fake_desired_state,
            fake_transient_states, fake_time_limit)
        fake_desired_volume = fake_object.FakeCinderVolume(status='attaching')

        with mock.patch.object(fake_client.FakeCinderClient.Volumes,
                               'get',
                               return_value=fake_desired_volume):
            self.assertRaises(exceptions.UnexpectedStateException,
                              fake_state_monitor.monitor_cinder_volume)
Esempio n. 6
0
 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'])
Esempio n. 7
0
 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())
Esempio n. 8
0
def mock_list_with_attach_to_other(cls, search_opts={}):
    attachments = [{
        u'server_id': u'1234',
        u'attachment_id': u'123',
        u'attached_at': u'2016-05-20T09:19:57.000000',
        u'host_name': None,
        u'device': None,
        u'id': u'123'
    }]
    return [
        fake_object.FakeCinderVolume(name='fake-vol1', attachments=attachments)
    ]
Esempio n. 9
0
    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)
Esempio n. 10
0
 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)
Esempio n. 11
0
def mock_list_with_attach_to_this(cls, search_opts=None):
    if search_opts is None:
        search_opts = {}
    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'
    }]
    return [
        fake_object.FakeCinderVolume(name='fake-vol1', attachments=attachments)
    ]
Esempio n. 12
0
    def test_monitor_manila_share_unexpected_state(self):
        fake_manila_client = fake_client.FakeManilaClient()
        fake_manila_share = fake_object.FakeManilaShare(status='creating')

        fake_state_monitor = state_monitor.StateMonitor(
            fake_manila_client, fake_manila_share, 'available', ('creating', ),
            0)
        fake_desired_share = fake_object.FakeCinderVolume(status='unknown')

        with mock.patch.object(fake_client.FakeManilaClient.Shares,
                               'get',
                               return_value=fake_desired_share):
            self.assertRaises(exceptions.UnexpectedStateException,
                              fake_state_monitor.monitor_manila_share)
Esempio n. 13
0
 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)
Esempio n. 14
0
    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))
Esempio n. 15
0
 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})
Esempio n. 16
0
    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')
Esempio n. 17
0
    def test_monitor_cinder_volume_get_failed(self):
        fake_cinder_client = fake_client.FakeCinderClient()
        fake_cinder_volume = fake_object.FakeCinderVolume(status='available')

        with mock.patch(
                'fuxi.tests.unit.fake_client.FakeCinderClient.Volumes'
                '.get',
                side_effect=cinder_exception.ClientException(404)):
            fake_state_monitor = state_monitor.StateMonitor(
                fake_cinder_client, fake_cinder_volume, None, None, -1)
            self.assertRaises(exceptions.TimeoutException,
                              fake_state_monitor.monitor_cinder_volume)

        with mock.patch(
                'fuxi.tests.unit.fake_client.FakeCinderClient.Volumes'
                '.get',
                side_effect=cinder_exception.ClientException(404)):
            fake_state_monitor = state_monitor.StateMonitor(
                fake_cinder_client, fake_cinder_volume, None, None)
            self.assertRaises(cinder_exception.ClientException,
                              fake_state_monitor.monitor_cinder_volume)
Esempio n. 18
0
 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'])
Esempio n. 19
0
 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)
Esempio n. 20
0
 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)
Esempio n. 21
0
 def list(self, search_opts=None):
     if search_opts is None:
         search_opts = {}
     return [fake_object.FakeCinderVolume(name='fake-vol1')]
Esempio n. 22
0
 def create(self, *args, **kwargs):
     return fake_object.FakeCinderVolume(**kwargs)
Esempio n. 23
0
 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))
Esempio n. 24
0
 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)
Esempio n. 25
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)
Esempio n. 26
0
 def get(self, volume_id):
     return fake_object.FakeCinderVolume(id=volume_id)
Esempio n. 27
0
 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. 28
0
 def list(self, search_opts={}):
     return [fake_object.FakeCinderVolume(name='fake-vol1')]