def test_script_directory(unique_script_str: str, set_to: Optional[str] = None): """ Return the directory containing the test scripts for a specific assignment. Optionally updates the location of the test script directory to the value of the set_to keyword argument (if it is not None) """ r = redis_connection() if set_to is not None: r.hset(CURRENT_TEST_SCRIPT_HASH, unique_script_str, set_to) out = r.hget(CURRENT_TEST_SCRIPT_HASH, unique_script_str) return string_management.decode_if_bytes(out)
def test_script_directory(markus_address, assignment_id, set_to=None): """ Return the directory containing the test scripts for a specific assignment. Optionally updates the location of the test script directory to the value of the set_to keyword argument (if it is not None) """ key = get_test_script_key(markus_address, assignment_id) r = redis_connection() if set_to is not None: r.hset(CURRENT_TEST_SCRIPT_HASH, key, set_to) out = r.hget(CURRENT_TEST_SCRIPT_HASH, key) return string_management.decode_if_bytes(out)
def tester_user(): """ Get the workspace for the tester user specified by the MARKUSWORKERUSER environment variable, return the user_name and path to that user's workspace. Raises an AutotestError if a tester user is not specified or if a workspace has not been setup for that user. """ user_name = os.environ.get("MARKUSWORKERUSER") if user_name is None: raise TesterUserError("No worker users available to run this job") user_workspace = os.path.join(config["workspace"], config["_workspace_contents", "_workers"], user_name) if not os.path.isdir(user_workspace): raise TesterUserError(f"No workspace directory for user: {user_name}") return user_name, decode_if_bytes(user_workspace)
def run_test_specs(cmd, test_specs, test_categories, tests_path, test_username, hooks): """ Run each test script in test_scripts in the tests_path directory using the command cmd. Return the results. """ results = [] preexec_fn = set_rlimits_before_test() with hooks.around("all"): for settings in test_specs["testers"]: tester_type = settings["tester_type"] extra_hook_kwargs = {"settings": settings} with hooks.around(tester_type, extra_kwargs=extra_hook_kwargs): env_dir = settings.get("env_loc", DEFAULT_ENV_DIR) cmd_str = create_test_script_command(env_dir, tester_type) args = cmd.format(cmd_str) for test_data in settings["test_data"]: test_category = test_data.get("category", []) if set(test_category) & set( test_categories ): # TODO: make sure test_categories is non-string collection type extra_hook_kwargs = {"test_data": test_data} with hooks.around( "each", builtin_selector=test_data, extra_kwargs=extra_hook_kwargs, ): start = time.time() out, err = "", "" timeout_expired = None timeout = test_data.get("timeout") try: env_vars = get_env_vars(test_username) proc = subprocess.Popen( args, start_new_session=True, cwd=tests_path, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE, preexec_fn=preexec_fn, env={ **os.environ, **env_vars }, ) try: settings_json = json.dumps({ **settings, "test_data": test_data }).encode("utf-8") out, err = proc.communicate( input=settings_json, timeout=timeout) except subprocess.TimeoutExpired: if test_username == current_user(): pgrp = os.getpgid(proc.pid) os.killpg(pgrp, signal.SIGKILL) else: if not kill_with_reaper(test_username): kill_without_reaper(test_username) out, err = proc.communicate() timeout_expired = timeout except Exception as e: err += "\n\n{}".format(e) finally: out = decode_if_bytes(out) err = decode_if_bytes(err) duration = int( round(time.time() - start, 3) * 1000) extra_info = test_data.get("extra_info", {}) results.append( create_test_group_result( out, err, duration, extra_info, timeout_expired)) return results, hooks.format_errors()
def _run_test_specs( cmd: str, client: ClientType, test_categories: List[str], tests_path: str, test_username: str, ) -> Tuple[List[ResultData], str]: """ Run each test script in test_scripts in the tests_path directory using the command cmd. Return the results. """ results = [] hook_errors = "" test_specs = _get_test_specs(client) for settings in test_specs["testers"]: tester_type = settings["tester_type"] env_dir = settings.get("env_loc", DEFAULT_ENV_DIR) cmd_str = _create_test_script_command(env_dir, tester_type) args = cmd.format(cmd_str) for test_data in settings["test_data"]: test_category = test_data.get("category", []) if set(test_category) & set(test_categories): start = time.time() out, err = "", "" timeout_expired = None timeout = test_data.get("timeout") try: env_vars = _get_env_vars(test_username) proc = subprocess.Popen( args, start_new_session=True, cwd=tests_path, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE, preexec_fn=set_rlimits_before_test, env={ **os.environ, **env_vars }, ) try: settings_json = json.dumps({ **settings, "test_data": test_data }).encode("utf-8") out, err = proc.communicate(input=settings_json, timeout=timeout) except subprocess.TimeoutExpired: if test_username == getpass.getuser(): pgrp = os.getpgid(proc.pid) os.killpg(pgrp, signal.SIGKILL) else: if not _kill_with_reaper(test_username): _kill_without_reaper(test_username) out, err = proc.communicate() timeout_expired = timeout hook_errors += client.after_test(test_data, tests_path) except Exception as e: err += "\n\n{}".format(e) finally: out = decode_if_bytes(out) err = decode_if_bytes(err) duration = int(round(time.time() - start, 3) * 1000) extra_info = test_data.get("extra_info", {}) results.append( _create_test_group_result(out, err, duration, extra_info, timeout_expired)) return results, hook_errors
def test_decodes_string(self, string): """ Returns original string """ assert string == sm.decode_if_bytes(string)
def test_decodes_bytes_non_utf8(self, string): """ Returns original string """ assert string == sm.decode_if_bytes(string.encode("utf-16", "ignore"), "utf-16")