diff --git a/.github/workflows/test_ci.yml b/.github/workflows/test_ci.yml index 42e12de1..45a09496 100644 --- a/.github/workflows/test_ci.yml +++ b/.github/workflows/test_ci.yml @@ -36,5 +36,9 @@ jobs: ${{ runner.os }}-pip- - name: Install python packages run: python -m pip install pytest fakeredis typing-extensions -r ${{ matrix.test-dir }}/requirements.txt + - name: Create users + run: | + sudo adduser --disabled-login --no-create-home fake_user + sudo adduser --disabled-login --no-create-home fake_user_2 - name: run tests run: pytest ${{ matrix.test-dir }} diff --git a/Changelog.md b/Changelog.md index 366f3570..e71c9ff4 100644 --- a/Changelog.md +++ b/Changelog.md @@ -4,6 +4,7 @@ All notable changes to this project will be documented here. ## [unreleased] - Add tidyverse as a default R tester package (#512) - For the Haskell tester, make stack resolver a test setting (#526) +- Clean up tmp directory after test runs (#528) ## [v2.4.3] - Omit skipped test cases in Python tester (#522) diff --git a/server/autotest_server/__init__.py b/server/autotest_server/__init__.py index 217421e3..a44d8d98 100644 --- a/server/autotest_server/__init__.py +++ b/server/autotest_server/__init__.py @@ -252,14 +252,19 @@ def _run_test_specs( def _clear_working_directory(tests_path: str, test_username: str) -> None: """ - Run commands that clear the tests_path working directory + Run commands that clear the tests_path working directory, as well + as clearing any files or directories owned by test_username in the /tmp directory """ if test_username != getpass.getuser(): sticky_cmd = f"sudo -u {test_username} -- bash -c 'chmod -Rf -t {tests_path}'" chmod_cmd = f"sudo -u {test_username} -- bash -c 'chmod -Rf ugo+rwX {tests_path}'" + clean_tmp_cmd = ( + f"sudo -u {test_username} -- bash -c 'find /tmp -maxdepth 1 -user {test_username} -exec rm " f"-rf {{}} +' " + ) else: sticky_cmd = f"chmod -Rf -t {tests_path}" chmod_cmd = f"chmod -Rf ugo+rwX {tests_path}" + clean_tmp_cmd = f"find /tmp -maxdepth 1 -user {test_username} -exec rm -rf {{}} +" subprocess.run(sticky_cmd, shell=True) subprocess.run(chmod_cmd, shell=True) @@ -268,6 +273,7 @@ def _clear_working_directory(tests_path: str, test_username: str) -> None: # set the group ownership with sudo (and that is only done in ../install.sh) clean_cmd = f"rm -rf {tests_path}/.[!.]* {tests_path}/*" subprocess.run(clean_cmd, shell=True) + subprocess.run(clean_tmp_cmd, shell=True) def _stop_tester_processes(test_username: str) -> None: diff --git a/server/autotest_server/tests/fixtures/test_config.yml b/server/autotest_server/tests/fixtures/test_config.yml index 3b651714..1b731323 100644 --- a/server/autotest_server/tests/fixtures/test_config.yml +++ b/server/autotest_server/tests/fixtures/test_config.yml @@ -3,7 +3,9 @@ redis_url: fake_url supervisor_url: fake_url workers: - user: fake_user - queues: + queues: &queues - high - low - batch + - user: fake_user_2 + queues: *queues diff --git a/server/autotest_server/tests/test_autotest_server.py b/server/autotest_server/tests/test_autotest_server.py index 0cf74b38..260767dd 100644 --- a/server/autotest_server/tests/test_autotest_server.py +++ b/server/autotest_server/tests/test_autotest_server.py @@ -65,3 +65,41 @@ def test_pre_remove(): autotest_server._clear_working_directory(autotest_worker_working_dir, autotest_worker) assert os.path.exists(path) is False + + +def test_tmp_remove_file(): + workers = autotest_server.config["workers"] + autotest_worker = workers[0]["user"] + autotest_worker_working_dir = f"/home/docker/.autotesting/workers/{autotest_worker}" + path = "/tmp/test.txt" + touch_cmd = f"sudo -u {autotest_worker} touch {path}" + subprocess.run(touch_cmd, shell=True) + autotest_server._clear_working_directory(autotest_worker_working_dir, autotest_worker) + assert os.path.exists(path) is False + + +def test_tmp_remove_dir(): + workers = autotest_server.config["workers"] + autotest_worker = workers[0]["user"] + autotest_worker_working_dir = f"/home/docker/.autotesting/workers/{autotest_worker}" + path = "/tmp/folder" + mkdir_cmd = f"sudo -u {autotest_worker} mkdir {path}" + subprocess.run(mkdir_cmd, shell=True) + touch_cmd = f"sudo -u {autotest_worker} touch {path}/test.txt" + subprocess.run(touch_cmd, shell=True) + autotest_server._clear_working_directory(autotest_worker_working_dir, autotest_worker) + assert os.path.exists(path) is False + + +def test_tmp_remove_other_user(): + workers = autotest_server.config["workers"] + autotest_worker_creator = workers[0]["user"] + autotest_worker_remover = workers[1]["user"] + autotest_worker_working_dir = f"/home/docker/.autotesting/workers/{autotest_worker_remover}" + path = "/tmp/folder" + mkdir_cmd = f"sudo -u {autotest_worker_creator} mkdir {path}" + subprocess.run(mkdir_cmd, shell=True) + touch_cmd = f"sudo -u {autotest_worker_creator} touch {path}/test.txt" + subprocess.run(touch_cmd, shell=True) + autotest_server._clear_working_directory(autotest_worker_working_dir, autotest_worker_remover) + assert os.path.exists(path) is True