def test_find_file_with_ext(self): """ Test find_file_with_ext method """ # try to find not existing file self.assertRaises( RuntimeError, find_file_with_ext, self.test_file_6, ['.avi']) # search recursively self.assertTrue(find_file_with_ext(self.test_dir1, ['.txt2']).endswith(".txt2")) # simple search self.assertTrue(find_file_with_ext(self.test_dir2, ['.txt']).endswith(".txt")) # search with multiple patterns file_ = find_file_with_ext(self.test_dir1, ['.txt', '.jpg']) self.assertTrue(file_.endswith(".txt") or file_.endswith(".jpg")) # search with multiple patterns (one is incorrect) self.assertTrue(find_file_with_ext(self.test_dir1, ['.txt', '.incorrect']).endswith(".txt"))
def merge_flm_files(self, new_flm, task, output): computer = LocalComputer(task, self.root_path, self.__verify_flm_ready, self.__verify_flm_failure, lambda: self.query_extra_data_for_advanced_verification(new_flm), use_task_resources=False, additional_resources=[self.test_flm, new_flm]) computer.run() if computer.tt is not None: computer.tt.join() else: return False if self.verification_error: return False commonprefix = common_dir(computer.tt.result['data']) flm = find_file_with_ext(commonprefix, [".flm"]) stderr = filter(lambda x: os.path.basename(x) == "stderr.log", computer.tt.result['data']) if flm is None or len(stderr) == 0: return False else: try: with open(stderr[0]) as f: stderr_in = f.read() if "ERROR" in stderr_in: return False except (IOError, OSError): return False shutil.copy(flm, os.path.join(self.tmp_dir, "test_result.flm")) return True
def __final_flm_ready(self, results, time_spent): commonprefix = common_dir(results['data']) flm = find_file_with_ext(commonprefix, [".flm"]) if flm is None: self.__final_flm_failure("No flm file created") return shutil.copy(flm, os.path.dirname(self.output_file)) new_flm = os.path.join(os.path.dirname(self.output_file), os.path.basename(flm)) self.__generate_final_file(new_flm)
def test_dummytask_TaskTester_should_pass(self): task = self._get_test_task() computer = TaskTester(task, self.tempdir, Mock(), Mock()) computer.run() computer.tt.join(60.0) dirname = os.path.dirname(computer.tt.result[0]['data'][0]) result = find_file_with_ext(dirname, [".result"]) assert path.isfile(result)
def __final_img_ready(self, results, time_spent): commonprefix = common_dir(results['data']) img = find_file_with_ext(commonprefix, ["." + self.output_format]) if img is None: # TODO Maybe we should try again? logger.error("No final file generated...") else: try: shutil.copy(img, self.output_file + "." + self.output_format) except (IOError, OSError) as err: logger.warning("Couldn't rename and copy img file. %s", err) self.notify_update_task()
def _extract_results(self, computer: LocalComputer, subtask_id: str) \ -> Path: """ Since the local computer uses temp dir, you should copy files out of there before you use local computer again. Otherwise the files would get overwritten (during the verification process). This is a problem only in test suite. In real life provider and requestor are separate machines """ assert isinstance(computer.tt, DockerTaskThread) dirname = path.dirname(computer.tt.result['data'][0]) result = Path(find_file_with_ext(dirname, [".result"])) self.assertTrue(result.is_file()) new_file_dir = result.parent / subtask_id new_result = self._copy_file(result, new_file_dir / "new.result") return new_result
def _run_task(self, extra_data, task): computer = LocalComputer( task, self.root_path, self.__box_rendered, self.__box_render_error, lambda: self.query_extra_data_for_advanced_verification(extra_data ), additional_resources=[]) computer.run() results = None if computer.tt: computer.tt.join() results = computer.tt.result.get("data") if results: commonprefix = os.path.commonprefix(results) img = find_file_with_ext(commonprefix, ["." + extra_data['output_format']]) if img is None: logger.error("No image file created") return img
def after_test(self, results, tmp_dir): NO_ADV_VER_MSG = "Advance verification will be impossible: " COULDNT_COPY_MSG = "Couldn't rename and copy .flm file." COULDNT_FING_MSG = "Couldn't find flm file." # Search for flm - the result of testing a lux task # It's needed for verification of received results return_data = dict() flm = find_file_with_ext(tmp_dir, [".flm"]) if flm is not None: try: shutil.copy(flm, self.__get_test_flm()) except (OSError, IOError) as err: return_data["warnings"] = NO_ADV_VER_MSG + COULDNT_COPY_MSG return_data["warnings"] += "{}".format(err) logger.warning(return_data["warnings"]) else: return_data["warnings"] = NO_ADV_VER_MSG + COULDNT_FING_MSG logger.warning(return_data["warnings"]) make_scene_analysis(self.scene_file_src, return_data) return return_data