def test_sync_files_write(self): """ write data to a file located in both sync folders check for the result, expected result: both files contains the same content """ logging.info("Launching test_sync_files_write test") self.assertTrue(self.app.is_alive(), "Test application is not running") self.app.change_folders() self.assertTrue(self.dirs_check_empty(), "Checking if remote folders are empty") self.assertTrue(self.app.is_alive(), "Test application is not running") for _ in range(0, self.nr_files): self.assertTrue(self.app.is_alive(), "Test application is not running") strlen = random.randint(10, 20) fname = get_random_str(size=strlen) fname_in = os.path.join(self.app.local_folder_in, fname) fname_out = os.path.join(self.app.local_folder_out, fname) logging.debug("Writing to both files: %s and %s" % (fname_in, fname_out)) with open(fname_in, 'a'): os.utime(fname_in, None) with open(fname_out, 'a'): os.utime(fname_out, None) #self.app.sync() for _ in range(self.nr_changes): with open(fname_in, 'a') as f_in: f_in.write(get_random_str(100)) with open(fname_out, 'a') as f_out: f_out.write(get_random_str(100)) for r in range(self.app.nr_retries): self.app.attempt = r md5_in = "INPUT FILE NOT READABLE" md5_out = "OUTPUT FILE NOT READABLE" try: md5_in = self.md5_for_file(fname_in) md5_out = self.md5_for_file(fname_out) except IOError: pass if md5_in == md5_out: break self.app.sync() logging.debug("File %s md5: %s" % (fname_in, md5_in)) logging.debug("File %s md5: %s" % (fname_out, md5_out)) self.assertEqual(md5_in, md5_out, "Files do not match")
def test_sync_files_write(self): """ write data to a file located in both sync folders check for the result, expected result: both files contains the same content """ logging.info("Launching test_sync_files_write test") self.assertTrue(self.app.is_alive(), "Test application is not running") self.app.change_folders(); self.assertTrue(self.dirs_check_empty(), "Checking if remote folders are empty") self.assertTrue(self.app.is_alive(), "Test application is not running") for _ in range(0, self.nr_files): self.assertTrue(self.app.is_alive(), "Test application is not running") strlen = random.randint(10, 20) fname = get_random_str(size=strlen) fname_in = os.path.join(self.app.local_folder_in, fname) fname_out = os.path.join(self.app.local_folder_out, fname) logging.debug("Writing to both files: %s and %s" % (fname_in, fname_out)) with open(fname_in, 'a'): os.utime(fname_in, None) with open(fname_out, 'a'): os.utime(fname_out, None) #self.app.sync() for _ in range(self.nr_changes): with open(fname_in, 'a') as f_in: f_in.write(get_random_str(100)) with open(fname_out, 'a') as f_out: f_out.write(get_random_str(100)) for r in range(self.app.nr_retries): self.app.attempt=r md5_in = "INPUT FILE NOT READABLE"; md5_out = "OUTPUT FILE NOT READABLE"; try: md5_in = self.md5_for_file(fname_in) md5_out = self.md5_for_file(fname_out) except IOError: pass; if md5_in == md5_out: break self.app.sync() logging.debug("File %s md5: %s" % (fname_in, md5_in)) logging.debug("File %s md5: %s" % (fname_out, md5_out)) self.assertEqual(md5_in, md5_out, "Files do not match")
def __init__(self, local_mount_in, local_mount_out, work_folder, delete_tmp_files=True, use_large_files=True): """ work_dir: a temporary folder to place generated files remote_folder: a remote folder to sync """ self.start_time = time.time() random.seed(time.time()) self.local_mount_in = local_mount_in self.local_mount_out = local_mount_out self.rnd_folder = get_random_str() self.local_folder_in = os.path.join(self.local_mount_in, self.rnd_folder) self.local_folder_out = os.path.join(self.local_mount_out, self.rnd_folder) self.work_folder = os.path.join(work_folder, self.rnd_folder) self.nr_retries = 200 self.delete_tmp_files = delete_tmp_files self.use_large_files = use_large_files
def test_sync_files_write(self): """ write data to a file located in both sync folders check for the result, expected result: both files contains the same content """ logging.info("Launching test_sync_files_write test") self.assertTrue(self.app.is_alive(), "Test application is not running") self.assertTrue(self.dirs_check_empty(), "Checking if remote folders are empty") self.assertTrue(self.app.is_alive(), "Test application is not running") for _ in range(0, 5): self.assertTrue(self.app.is_alive(), "Test application is not running") strlen = random.randint(10, 20) fname = get_random_str(size=strlen) fname_in = os.path.join(self.app.local_folder_in, fname) fname_out = os.path.join(self.app.local_folder_out, fname) logging.debug("Writing to both files: %s and %s" % (fname_in, fname_out)) with open(fname_in, 'a'): os.utime(fname_in, None) with open(fname_out, 'a'): os.utime(fname_out, None) self.app.sync() for _ in range(10): with open(fname_in, 'a') as f_in: f_in.write(get_random_str(100)) with open(fname_out, 'a') as f_out: f_out.write(get_random_str(100)) self.app.sync() md5_in = self.md5_for_file(fname_in) md5_out = self.md5_for_file(fname_out) logging.debug("File %s md5: %s" % (fname_in, md5_in)) logging.debug("File %s md5: %s" % (fname_out, md5_out)) self.assertEqual(md5_in, md5_out, "Files do not match")
def start_megasync(self, local_folder, app_wkd, type_str): """ fork and launch "megasimplesync" application local_folder: local folder to sync """ # launch megasimplesync base_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), '../..') # the app is either in examples/ or in the project's root if platform.system() == "Windows": app_name = "megasimplesync.exe" else: app_name = "megasimplesync" bin_path = os.path.join(base_path, "examples") tmp = os.path.join(bin_path, app_name) if not os.path.isfile(tmp) or not os.access(tmp, os.X_OK): bin_path = os.path.join(base_path, "") tmp = os.path.join(bin_path, app_name) if not os.path.isfile(tmp) or not os.access(tmp, os.X_OK): raise Exception("megasimplesync application is not found!") pargs = [ os.path.join(bin_path, app_name), local_folder, self.remote_folder ] output_fname = os.path.join( self.work_dir, "megasimplesync" + "_" + type_str + "_" + get_random_str() + ".log") output_log = open(output_fname, "w") logging.info("Launching on: \"%s\", cmd: \"%s\", log: \"%s\"" % (app_wkd, " ".join(pargs), output_fname)) try: ch = subprocess.Popen(pargs, cwd=app_wkd, universal_newlines=True, stdout=output_log, stderr=subprocess.STDOUT, bufsize=1, shell=False, env=os.environ) except OSError, e: logging.error("Failed to launch megasimplesync process: %s" % e) return None
def change_folders(self): """ cleans directories and call finish """ time.sleep(0.2) # to prevent from sync algorithm interpreting we are renaming if self.delete_tmp_files: try: shutil.rmtree(self.local_folder_in) except OSError: pass time.sleep(1.2) # to prevent from sync algorithm interpreting we are renaming self.rnd_folder = get_random_str() self.local_folder_in = os.path.join(self.local_mount_in, self.rnd_folder) self.local_folder_out = os.path.join(self.local_mount_out, self.rnd_folder) self.work_folder = os.path.join(self.work_folder, self.rnd_folder) self.prepare_folders();
def change_folders(self): """ cleans directories and call finish """ time.sleep( 0.2) # to prevent from sync algorithm interpreting we are renaming if self.delete_tmp_files: try: shutil.rmtree(self.local_folder_in) except OSError: pass time.sleep( 1.2) # to prevent from sync algorithm interpreting we are renaming self.rnd_folder = get_random_str() self.local_folder_in = os.path.join(self.local_mount_in, self.rnd_folder) self.local_folder_out = os.path.join(self.local_mount_out, self.rnd_folder) self.work_folder = os.path.join(self.work_folder, self.rnd_folder) self.prepare_folders()
def start_megasync(self, local_folder, type_str): """ fork and launch "megasimplesync" application local_folder: local folder to sync """ # launch megasimplesync base_path = os.path.join(os.path.dirname(__file__), '..') # the app is either in examples/ or in the project's root if platform.system() == "Windows": app_name = "megasimplesync.exe" else: app_name = "megasimplesync" bin_path = os.path.join(base_path, "examples") tmp = os.path.join(bin_path, app_name) if not os.path.isfile(tmp) or not os.access(tmp, os.X_OK): bin_path = os.path.join(base_path, "") tmp = os.path.join(bin_path, app_name) if not os.path.isfile(tmp) or not os.access(tmp, os.X_OK): raise Exception("megasimplesync application is not found!") pargs = [os.path.join(bin_path, app_name), local_folder, self.remote_folder] output_fname = os.path.join(self.work_dir, "megasimplesync" + "_" + type_str + "_" + get_random_str() + ".log") output_log = open(output_fname, "w") logging.info("Launching cmd: \"%s\", log: \"%s\"" % (" ".join(pargs), output_fname)) try: ch = subprocess.Popen(pargs, universal_newlines=True, stdout=output_log, stderr=subprocess.STDOUT, bufsize=1, shell=False, env=os.environ) except OSError, e: logging.error("Failed to launch megasimplesync process: %s" % e) return None