Beispiel #1
0
def test_random_filename():
    fn = random_filename(10)
    assert len(fn) == 10

    fn = random_filename((10, 11))
    assert 10 <= len(fn) <= 11

    fn = random_filename((11, 11))
    assert len(fn) == 11
        def create_link(self, wekafs_alternate_path):
            def _link(path, filename, linkname):
                os.link(path + '/' + filename, path + '/' + linkname)
                return linkname

            filename = choose([k for (k, v) in self.dir_contents.items()] +
                              [random_filename()])
            new_link = self.validate_same_behavior_on_both_paths(
                _link,
                wekafs_alternate_path,
                filename=filename,
                linkname=random_filename())
            if new_link:
                # this means there was no exception
                wekafs_path = self.wekafs_alternate_path if wekafs_alternate_path else self.wekafs_path
                wekafs_inode = os.stat(wekafs_path + '/' + new_link).st_ino
                self.dir_contents[new_link] = wekafs_inode
        def access_file(self, wekafs_alternate_path):
            def _access(path, filename, flag):
                return os.access(path + '/' + filename, flag)

            filename = choose([k for (k, v) in self.dir_contents.items()] +
                              [random_filename()])
            flag = choose([os.R_OK, os.W_OK, os.X_OK])
            self.validate_same_behavior_on_both_paths(_access,
                                                      wekafs_alternate_path,
                                                      filename=filename,
                                                      flag=flag)
        def remove_dir(self, wekafs_alternate_path):
            def _rmdir(path, dirname):
                os.rmdir(path + '/' + dirname)
                return dirname

            filename = choose([k for (k, v) in self.dir_contents.items()] +
                              [random_filename()])
            del_dir = self.validate_same_behavior_on_both_paths(
                _rmdir, wekafs_alternate_path, dirname=filename)
            if del_dir:
                self.dir_contents.pop(del_dir)
        def remove_file(self, wekafs_alternate_path):
            def _rm(path, filename):
                os.remove(path + '/' + filename)
                return filename

            filename = choose([k for (k, v) in self.dir_contents.items()] +
                              [random_filename()])
            del_file = self.validate_same_behavior_on_both_paths(
                _rm, wekafs_alternate_path, filename=filename)
            if del_file:
                self.dir_contents.pop(del_file)
        def chmod_file(self, wekafs_alternate_path):
            def _chmod(path, filename, value):
                os.chmod(path + '/' + filename, value)
                return (filename, value)

            filename = choose([k for (k, v) in self.dir_contents.items()] +
                              [random_filename()])
            value = randint(0, 0o777)
            self.validate_same_behavior_on_both_paths(_chmod,
                                                      wekafs_alternate_path,
                                                      filename=filename,
                                                      value=value)
        def create_file(self, wekafs_alternate_path):
            def _mknod(path, filename):
                os.mknod(path + '/' + filename)
                return filename

            filename = choose([k for (k, v) in self.dir_contents.items()] +
                              [random_filename()])
            new_file = self.validate_same_behavior_on_both_paths(
                _mknod, wekafs_alternate_path, filename=filename)
            if new_file:
                # this means there was no exception
                wekafs_path = self.wekafs_alternate_path if wekafs_alternate_path else self.wekafs_path
                wekafs_inode = os.stat(wekafs_path + '/' + new_file).st_ino
                self.dir_contents[new_file] = wekafs_inode
        def open_read_write_close(self, wekafs_alternate_path):
            def _open(path, filename, buf):
                @contextmanager
                def opened_file(filename):
                    fd = os.open(filename, flags=os.O_RDWR)
                    yield fd
                    os.close(fd)

                with opened_file(filename) as fd:
                    os.read(fd, 100)
                    os.write(fd, buf)
                return (filename, buf)

            filename = choose([k for (k, v) in self.dir_contents.items()] +
                              [random_filename()])
            buf = random_buf(100)
            self.validate_same_behavior_on_both_paths(_open,
                                                      wekafs_alternate_path,
                                                      filename=filename,
                                                      buf=buf)
        def stat_file(self, wekafs_alternate_path):
            def _stat(path, filename):
                stats = os.stat(path + '/' + filename)
                mode = stats.st_mode
                # atime, mtime and ctime might differ by a few nanoseconds
                # inode and device will be different between the 2 filesystems
                # directory's nlink is different between wekafs and posix (see WEKAPP-76966)
                # directory's size is unspecified
                # this only leaves the 'mode' stat
                return mode

            filename = choose([k for (k, v) in self.dir_contents.items()] +
                              [random_filename()])
            ret = self.validate_same_behavior_on_both_paths(
                _stat, wekafs_alternate_path, filename=filename)
            if ret:
                # this means there was no exception
                wekafs_path = self.wekafs_alternate_path if wekafs_alternate_path else self.wekafs_path
                wekafs_inode = os.stat(wekafs_path + '/' + filename).st_ino
                assert wekafs_inode == self.dir_contents[filename]
    def __init__(self, wekafs, localfs='/mnt/localfs'):
        random_suffix = random_filename(3)
        dir_name = '/dir_leases_directory_' + random_suffix

        self.localfs_path = localfs + dir_name
        os.makedirs(self.localfs_path, exist_ok=True)

        self.wekafs_path = wekafs + dir_name
        os.makedirs(self.wekafs_path, exist_ok=True)

        alternate_wekafs_mount_point = '/mnt/wekafs'
        os.makedirs(alternate_wekafs_mount_point, exist_ok=True)
        os.system('mount -t wekafs default %s -o dentry_max_age_positive=0' %
                  alternate_wekafs_mount_point)
        self.wekafs_alternate_path = alternate_wekafs_mount_point + dir_name
        os.makedirs(self.wekafs_alternate_path, exist_ok=True)

        logger.info(
            "Mount-paths are:\n%s\n%s\n%s\n" %
            (self.localfs_path, self.wekafs_path, self.wekafs_alternate_path))

        self.dir_contents = dict()