Example #1
0
    def test_quobyte_is_valid_volume(self, mock_execute, mock_access):
        mnt_base = "/mnt"
        quobyte_volume = "192.168.1.1/volume-00001"
        export_mnt_base = os.path.join(mnt_base, utils.get_hash_str(quobyte_volume))

        quobyte.validate_volume(export_mnt_base)

        mock_execute.assert_called_once_with("getfattr", "-n", "quobyte.info", export_mnt_base)
Example #2
0
    def test_quobyte_umount_volume(self, mock_execute):
        mnt_base = "/mnt"
        quobyte_volume = "192.168.1.1/volume-00001"
        export_mnt_base = os.path.join(mnt_base, utils.get_hash_str(quobyte_volume))

        quobyte.umount_volume(export_mnt_base)

        mock_execute.assert_called_once_with("umount.quobyte", export_mnt_base)
Example #3
0
    def test_quobyte_umount_volume_fails(self, mock_execute, mock_exception):
        mnt_base = "/mnt"
        quobyte_volume = "192.168.1.1/volume-00001"
        export_mnt_base = os.path.join(mnt_base, utils.get_hash_str(quobyte_volume))

        quobyte.umount_volume(export_mnt_base)

        (mock_exception.assert_called_once_with("Couldn't unmount " "the Quobyte Volume at %s", export_mnt_base))
Example #4
0
    def test_quobyte_is_valid_volume_vol_not_valid_volume(self, mock_execute):
        mnt_base = '/mnt'
        quobyte_volume = '192.168.1.1/volume-00001'
        export_mnt_base = os.path.join(mnt_base,
                                       utils.get_hash_str(quobyte_volume))

        self.assertRaises(exception.PatronException, quobyte.validate_volume,
                          export_mnt_base)
Example #5
0
    def test_quobyte_umount_volume(self, mock_execute):
        mnt_base = '/mnt'
        quobyte_volume = '192.168.1.1/volume-00001'
        export_mnt_base = os.path.join(mnt_base,
                                       utils.get_hash_str(quobyte_volume))

        quobyte.umount_volume(export_mnt_base)

        mock_execute.assert_called_once_with('umount.quobyte', export_mnt_base)
Example #6
0
    def test_quobyte_mount_volume_fails(self, mock_execute, mock_ensure_tree):
        mnt_base = '/mnt'
        quobyte_volume = '192.168.1.1/volume-00001'
        export_mnt_base = os.path.join(mnt_base,
                                       utils.get_hash_str(quobyte_volume))

        self.assertRaises(processutils.ProcessExecutionError,
                          quobyte.mount_volume, quobyte_volume,
                          export_mnt_base)
Example #7
0
    def test_quobyte_mount_volume(self, mock_execute, mock_ensure_tree):
        mnt_base = "/mnt"
        quobyte_volume = "192.168.1.1/volume-00001"
        export_mnt_base = os.path.join(mnt_base, utils.get_hash_str(quobyte_volume))

        quobyte.mount_volume(quobyte_volume, export_mnt_base)

        mock_ensure_tree.assert_called_once_with(export_mnt_base)
        expected_commands = [mock.call("mount.quobyte", quobyte_volume, export_mnt_base, check_exit_code=[0, 4])]
        mock_execute.assert_has_calls(expected_commands)
Example #8
0
 def _object_path(self, bucket, object_name):
     if self.application.bucket_depth < 1:
         return os.path.abspath(
             os.path.join(self.application.directory, bucket, object_name))
     hash = utils.get_hash_str(object_name)
     path = os.path.abspath(os.path.join(self.application.directory,
                                         bucket))
     for i in range(self.application.bucket_depth):
         path = os.path.join(path, hash[:2 * (i + 1)])
     return os.path.join(path, object_name)
Example #9
0
    def test_quobyte_is_valid_volume(self, mock_execute, mock_access):
        mnt_base = '/mnt'
        quobyte_volume = '192.168.1.1/volume-00001'
        export_mnt_base = os.path.join(mnt_base,
                                       utils.get_hash_str(quobyte_volume))

        quobyte.validate_volume(export_mnt_base)

        mock_execute.assert_called_once_with('getfattr', '-n', 'quobyte.info',
                                             export_mnt_base)
Example #10
0
 def _object_path(self, bucket, object_name):
     if self.application.bucket_depth < 1:
         return os.path.abspath(os.path.join(
             self.application.directory, bucket, object_name))
     hash = utils.get_hash_str(object_name)
     path = os.path.abspath(os.path.join(
         self.application.directory, bucket))
     for i in range(self.application.bucket_depth):
         path = os.path.join(path, hash[:2 * (i + 1)])
     return os.path.join(path, object_name)
Example #11
0
    def test_quobyte_umount_volume_fails(self, mock_execute, mock_exception):
        mnt_base = '/mnt'
        quobyte_volume = '192.168.1.1/volume-00001'
        export_mnt_base = os.path.join(mnt_base,
                                       utils.get_hash_str(quobyte_volume))

        quobyte.umount_volume(export_mnt_base)

        (mock_exception.assert_called_once_with(
            "Couldn't unmount "
            "the Quobyte Volume at %s", export_mnt_base))
Example #12
0
def get_file_extension_for_os_type(os_type, specified_fs=None):
    mkfs_command = _MKFS_COMMAND.get(os_type, _DEFAULT_MKFS_COMMAND)
    if mkfs_command:
        extension = mkfs_command
    else:
        if not specified_fs:
            specified_fs = CONF.default_ephemeral_format
            if not specified_fs:
                specified_fs = _DEFAULT_FS_BY_OSTYPE.get(os_type,
                                                         _DEFAULT_FILE_SYSTEM)
        extension = specified_fs
    return utils.get_hash_str(extension)[:7]
Example #13
0
def get_file_extension_for_os_type(os_type, specified_fs=None):
    mkfs_command = _MKFS_COMMAND.get(os_type, _DEFAULT_MKFS_COMMAND)
    if mkfs_command:
        extension = mkfs_command
    else:
        if not specified_fs:
            specified_fs = CONF.default_ephemeral_format
            if not specified_fs:
                specified_fs = _DEFAULT_FS_BY_OSTYPE.get(
                    os_type, _DEFAULT_FILE_SYSTEM)
        extension = specified_fs
    return utils.get_hash_str(extension)[:7]
Example #14
0
    def test_quobyte_umount_volume_warns(self, mock_execute, mock_debug):
        mnt_base = "/mnt"
        quobyte_volume = "192.168.1.1/volume-00001"
        export_mnt_base = os.path.join(mnt_base, utils.get_hash_str(quobyte_volume))

        def exec_side_effect(*cmd, **kwargs):
            exerror = processutils.ProcessExecutionError()
            exerror.message = "Device or resource busy"
            raise exerror

        mock_execute.side_effect = exec_side_effect

        quobyte.umount_volume(export_mnt_base)

        (mock_debug.assert_called_once_with("The Quobyte volume at %s is still in use.", export_mnt_base))
Example #15
0
    def test_quobyte_mount_volume(self, mock_execute, mock_ensure_tree):
        mnt_base = '/mnt'
        quobyte_volume = '192.168.1.1/volume-00001'
        export_mnt_base = os.path.join(mnt_base,
                                       utils.get_hash_str(quobyte_volume))

        quobyte.mount_volume(quobyte_volume, export_mnt_base)

        mock_ensure_tree.assert_called_once_with(export_mnt_base)
        expected_commands = [
            mock.call('mount.quobyte',
                      quobyte_volume,
                      export_mnt_base,
                      check_exit_code=[0, 4])
        ]
        mock_execute.assert_has_calls(expected_commands)
Example #16
0
    def test_quobyte_umount_volume_warns(self, mock_execute, mock_debug):
        mnt_base = '/mnt'
        quobyte_volume = '192.168.1.1/volume-00001'
        export_mnt_base = os.path.join(mnt_base,
                                       utils.get_hash_str(quobyte_volume))

        def exec_side_effect(*cmd, **kwargs):
            exerror = processutils.ProcessExecutionError()
            exerror.message = "Device or resource busy"
            raise exerror

        mock_execute.side_effect = exec_side_effect

        quobyte.umount_volume(export_mnt_base)

        (mock_debug.assert_called_once_with(
            "The Quobyte volume at %s is still in use.", export_mnt_base))
Example #17
0
 def put(self, bucket, object_name):
     object_name = urllib.unquote(object_name)
     bucket_dir = os.path.abspath(
         os.path.join(self.application.directory, bucket))
     if (not bucket_dir.startswith(self.application.directory)
             or not os.path.isdir(bucket_dir)):
         self.set_404()
         return
     path = self._object_path(bucket, object_name)
     if not path.startswith(bucket_dir) or os.path.isdir(path):
         self.set_status(403)
         return
     directory = os.path.dirname(path)
     fileutils.ensure_tree(directory)
     object_file = open(path, "w")
     object_file.write(self.request.body)
     object_file.close()
     self.set_header('ETag', '"%s"' % utils.get_hash_str(self.request.body))
     self.finish()
Example #18
0
 def put(self, bucket, object_name):
     object_name = urllib.unquote(object_name)
     bucket_dir = os.path.abspath(os.path.join(
         self.application.directory, bucket))
     if (not bucket_dir.startswith(self.application.directory) or
             not os.path.isdir(bucket_dir)):
         self.set_404()
         return
     path = self._object_path(bucket, object_name)
     if not path.startswith(bucket_dir) or os.path.isdir(path):
         self.set_status(403)
         return
     directory = os.path.dirname(path)
     fileutils.ensure_tree(directory)
     object_file = open(path, "w")
     object_file.write(self.request.body)
     object_file.close()
     self.set_header('ETag',
                     '"%s"' % utils.get_hash_str(self.request.body))
     self.finish()
Example #19
0
 def test_get_hash_str(self):
     base_str = "foo"
     value = hashlib.md5(base_str).hexdigest()
     self.assertEqual(
         value, utils.get_hash_str(base_str))
Example #20
0
 def test_get_hash_str(self):
     base_str = "foo"
     value = hashlib.md5(base_str).hexdigest()
     self.assertEqual(value, utils.get_hash_str(base_str))
Example #21
0
    def test_quobyte_is_valid_volume_vol_no_valid_access(self, mock_execute, mock_access):
        mnt_base = "/mnt"
        quobyte_volume = "192.168.1.1/volume-00001"
        export_mnt_base = os.path.join(mnt_base, utils.get_hash_str(quobyte_volume))

        self.assertRaises(exception.PatronException, quobyte.validate_volume, export_mnt_base)
Example #22
0
class APITestCase(test.NoDBTestCase):
    def test_can_resize_need_fs_type_specified(self):
        # NOTE(mikal): Bug 1094373 saw a regression where we failed to
        # treat a failure to mount as a failure to be able to resize the
        # filesystem
        def _fake_get_disk_size(path):
            return 10

        self.useFixture(
            fixtures.MonkeyPatch('patron.virt.disk.api.get_disk_size',
                                 _fake_get_disk_size))

        def fake_trycmd(*args, **kwargs):
            return '', 'broken'

        self.useFixture(
            fixtures.MonkeyPatch('patron.utils.trycmd', fake_trycmd))

        def fake_returns_true(*args, **kwargs):
            return True

        def fake_returns_nothing(*args, **kwargs):
            return ''

        self.useFixture(
            fixtures.MonkeyPatch('patron.virt.disk.mount.nbd.NbdMount.get_dev',
                                 fake_returns_true))
        self.useFixture(
            fixtures.MonkeyPatch('patron.virt.disk.mount.nbd.NbdMount.map_dev',
                                 fake_returns_true))
        self.useFixture(
            fixtures.MonkeyPatch(
                'patron.virt.disk.vfs.localfs.VFSLocalFS.get_image_fs',
                fake_returns_nothing))

        # Force the use of localfs, which is what was used during the failure
        # reported in the bug
        def fake_import_fails(*args, **kwargs):
            raise Exception('Failed')

        self.useFixture(
            fixtures.MonkeyPatch('oslo.utils.import_module',
                                 fake_import_fails))

        imgfile = tempfile.NamedTemporaryFile()
        self.addCleanup(imgfile.close)
        self.assertFalse(api.is_image_extendable(imgfile, use_cow=True))

    def test_resize2fs_success(self):
        imgfile = tempfile.NamedTemporaryFile()

        self.mox.StubOutWithMock(utils, 'execute')
        utils.execute('e2fsck',
                      '-fp',
                      imgfile,
                      check_exit_code=[0, 1, 2],
                      run_as_root=False)
        utils.execute('resize2fs',
                      imgfile,
                      check_exit_code=False,
                      run_as_root=False)

        self.mox.ReplayAll()
        api.resize2fs(imgfile)

    def test_resize2fs_e2fsck_fails(self):
        imgfile = tempfile.NamedTemporaryFile()

        self.mox.StubOutWithMock(utils, 'execute')
        utils.execute('e2fsck',
                      '-fp',
                      imgfile,
                      check_exit_code=[0, 1, 2],
                      run_as_root=False).AndRaise(
                          processutils.ProcessExecutionError("fs error"))
        self.mox.ReplayAll()
        api.resize2fs(imgfile)

    def test_extend_qcow_success(self):
        imgfile = tempfile.NamedTemporaryFile()
        imgsize = 10
        device = "/dev/sdh"
        use_cow = True

        self.flags(resize_fs_using_block_device=True)
        mounter = FakeMount.instance_for_format(imgfile, None, None, 'qcow2')
        mounter.device = device

        self.mox.StubOutWithMock(api, 'can_resize_image')
        self.mox.StubOutWithMock(utils, 'execute')
        self.mox.StubOutWithMock(api, 'is_image_extendable')
        self.mox.StubOutWithMock(mounter, 'get_dev')
        self.mox.StubOutWithMock(mounter, 'unget_dev')
        self.mox.StubOutWithMock(api, 'resize2fs')
        self.mox.StubOutWithMock(mount.Mount,
                                 'instance_for_format',
                                 use_mock_anything=True)

        api.can_resize_image(imgfile, imgsize).AndReturn(True)
        utils.execute('qemu-img', 'resize', imgfile, imgsize)
        api.is_image_extendable(imgfile, use_cow).AndReturn(True)
        mount.Mount.instance_for_format(imgfile, None, None,
                                        'qcow2').AndReturn(mounter)
        mounter.get_dev().AndReturn(True)
        api.resize2fs(mounter.device, run_as_root=True, check_exit_code=[0])
        mounter.unget_dev()

        self.mox.ReplayAll()
        api.extend(imgfile, imgsize, use_cow=use_cow)

    def test_extend_raw_success(self):
        imgfile = tempfile.NamedTemporaryFile()
        imgsize = 10
        use_cow = False

        self.mox.StubOutWithMock(api, 'can_resize_image')
        self.mox.StubOutWithMock(utils, 'execute')
        self.mox.StubOutWithMock(api, 'is_image_extendable')
        self.mox.StubOutWithMock(api, 'resize2fs')

        api.can_resize_image(imgfile, imgsize).AndReturn(True)
        utils.execute('qemu-img', 'resize', imgfile, imgsize)
        api.is_image_extendable(imgfile, use_cow).AndReturn(True)
        api.resize2fs(imgfile, run_as_root=False, check_exit_code=[0])

        self.mox.ReplayAll()
        api.extend(imgfile, imgsize, use_cow=use_cow)

    HASH_VFAT = utils.get_hash_str(api.FS_FORMAT_VFAT)[:7]
    HASH_EXT4 = utils.get_hash_str(api.FS_FORMAT_EXT4)[:7]
    HASH_NTFS = utils.get_hash_str(api.FS_FORMAT_NTFS)[:7]

    def test_get_file_extension_for_os_type(self):
        self.assertEqual(self.HASH_VFAT,
                         api.get_file_extension_for_os_type(None, None))
        self.assertEqual(self.HASH_EXT4,
                         api.get_file_extension_for_os_type('linux', None))
        self.assertEqual(self.HASH_NTFS,
                         api.get_file_extension_for_os_type('windows', None))

    def test_get_file_extension_for_os_type_with_overrides(self):
        with mock.patch('patron.virt.disk.api._DEFAULT_MKFS_COMMAND',
                        'custom mkfs command'):
            self.assertEqual("a74d253",
                             api.get_file_extension_for_os_type('linux', None))
            self.assertEqual(
                "a74d253", api.get_file_extension_for_os_type('windows', None))
            self.assertEqual("a74d253",
                             api.get_file_extension_for_os_type('osx', None))

        with mock.patch.dict(api._MKFS_COMMAND, {'osx': 'custom mkfs command'},
                             clear=True):
            self.assertEqual(self.HASH_VFAT,
                             api.get_file_extension_for_os_type(None, None))
            self.assertEqual(self.HASH_EXT4,
                             api.get_file_extension_for_os_type('linux', None))
            self.assertEqual(
                self.HASH_NTFS,
                api.get_file_extension_for_os_type('windows', None))
            self.assertEqual("a74d253",
                             api.get_file_extension_for_os_type('osx', None))
Example #23
0
    def test_quobyte_mount_volume_fails(self, mock_execute, mock_ensure_tree):
        mnt_base = "/mnt"
        quobyte_volume = "192.168.1.1/volume-00001"
        export_mnt_base = os.path.join(mnt_base, utils.get_hash_str(quobyte_volume))

        self.assertRaises(processutils.ProcessExecutionError, quobyte.mount_volume, quobyte_volume, export_mnt_base)