def write_test(file_path, fuse_os): with open(file_path, "w+") as file_handle: file_content = build_random_string(50) file_content_1 = build_random_string(50) file_handle.write(file_content) file_handle.seek(0) assert file_content == file_handle.read() stat_test(fuse_os.stat(file_handle.name), mode=0o644, node_type=S_IFREG, size=50) file_handle.seek(5) file_handle.write(file_content_1[5:15]) file_handle.seek(0) assert (file_content[0:5] + file_content_1[5:15] + file_content[15:]) == file_handle.read() stat_test(fuse_os.stat(file_handle.name), mode=0o644, node_type=S_IFREG, size=50) file_handle.seek(45) file_handle.write(file_content_1[40:]) file_handle.seek(0) assert (file_content[0:5] + file_content_1[5:15] + file_content[15:45] + file_content_1[40:]) == file_handle.read() stat_test(fuse_os.stat(file_handle.name), mode=0o644, node_type=S_IFREG, size=55)
def get_xattr_test(node_path, fuse_os, noop=False): node_stats = fuse_os.stat(node_path) key = build_random_string(10) assert None == fuse_os.get_xattr(node_path, key) value = build_random_string(10) fuse_os.set_xattr(node_path, key, value) assert node_stats == fuse_os.stat(node_path) if noop: assert None == fuse_os.get_xattr(node_path, key) else: assert value == fuse_os.get_xattr(node_path, key)
def delete_xattr_test(node_path, fuse_os, noop=False): node_stats = fuse_os.stat(node_path) key = build_random_string(10) assert None == fuse_os.get_xattr(node_path, key) value = build_random_string(10) fuse_os.set_xattr(node_path, key, value) # This may not belong here if noop: assert None == fuse_os.get_xattr(node_path, key) else: assert value == fuse_os.get_xattr(node_path, key) fuse_os.delete_xattr(node_path, key) u_node_stats = fuse_os.stat(node_path) assert node_stats == u_node_stats assert None == fuse_os.get_xattr(node_path, key)
def create_file_test(root_path): pool = Pool(8) results = [] file_paths = [ os.path.join(root_path, "create_delete_file_%s" % index) for index in range(0, 10) ] file_contents = [build_random_string(1024 * 200) for _ in range(0, 10)] root_stats = os.stat(root_path) for iteration in range(0, 10000): file_path = file_paths[random.randint(0, len(file_paths) - 1)] file_content = file_contents[random.randint(0, len(file_contents) - 1)] results.append( pool.apply_async(create_delete_file, (file_path, file_content, False))) pool.close() pool.join() u_root_stats = os.stat(root_path) retry_assertion( lambda: root_stats.st_nlink + len(file_paths) == u_root_stats.st_nlink) for result in results: assert result for file_path in file_paths: with open(file_path, 'r') as f: assert f.read() in file_contents os.unlink(file_path)
def truncate_test(file_path, fuse_os): file_content = build_random_string(50) with open(file_path, "w+") as file_handle: file_handle.write(file_content) with open(file_path, "r+") as file_handle: file_handle.truncate(70) file_handle.seek(0) assert file_content + ('\0' * 20) == file_handle.read() stat_test(fuse_os.stat(file_handle.name), mode=0o644, node_type=S_IFREG, size=70) file_handle.truncate(20) file_handle.seek(0) assert file_content[0:20] == file_handle.read() stat_test(fuse_os.stat(file_handle.name), mode=0o644, node_type=S_IFREG, size=20) file_handle.truncate(0) file_handle.seek(0) assert '' == file_handle.read() stat_test(fuse_os.stat(file_handle.name), mode=0o644, node_type=S_IFREG, size=0)
def fuse_dir(temp_dir_module, request): if request.param == "./": yield os.path.join(temp_dir_module, request.param) return dir_path = os.path.join(temp_dir_module, request.param, build_random_string(10)) os.makedirs(dir_path) yield dir_path if os.path.isdir(dir_path): shutil.rmtree(dir_path)
def create_test(dir_path, fuse_os, node_stats=None): parent_stats = node_stats file_name = TMP_FILE_PREFIX + build_random_string(10) file_path = os.path.join(dir_path, file_name) with open(file_path, 'w') as f: assert 0 <= f.fileno() stat_test(fuse_os.stat(file_path), mode=0o644) parent_stat_after_mod_test(parent_stats, fuse_os.stat(dir_path)) assert file_name in fuse_os.listdir(dir_path)
def get_xattrs_test(node_path, fuse_os, noop=False): node_stats = fuse_os.stat(node_path) key = build_random_string(10) assert node_stats == fuse_os.stat(node_path) attrs = fuse_os.get_xattrs(node_path) assert 0 <= len( attrs ) # Might get some OS specific attributes like 'com.apple.FinderInfo' value = build_random_string(10) fuse_os.set_xattr(node_path, key, value) assert node_stats == fuse_os.stat(node_path) u_attrs = fuse_os.get_xattrs(node_path) if noop: assert len(attrs) == len(u_attrs) assert None == u_attrs.get(key) else: assert len(attrs) + 1 == len(u_attrs) assert value == u_attrs.get(key)
def mkdir_test(dir_path, fuse_os, node_stats=None, mode=0o711, nlink=2): dir_stat = node_stats new_dir_name = TMP_DIR_PREFIX + build_random_string(10) new_dir_path = os.path.join(dir_path, new_dir_name) fuse_os.mkdir(new_dir_path, 0o711) stat_test(fuse_os.stat(new_dir_path), node_type=S_IFDIR, mode=mode, nlink=nlink) parent_stat_after_mod_test(dir_stat, fuse_os.stat(dir_path)) assert new_dir_name in fuse_os.listdir(dir_path)
def fuse_file_handle(fuse_dir): file_path = os.path.join(fuse_dir, build_random_string(10)) with open(file_path, 'w+') as f: yield f if os.path.isfile(file_path): os.unlink(file_path)
def fuse_file_path(fuse_dir): return os.path.join(fuse_dir, build_random_string(10))
def symlink_test(dir_path, fuse_os, target, node_stats=None): link_name = build_random_string(10) link_path = os.path.join(dir_path, link_name) fuse_os.symlink(target, link_path) raise NotImplemented