Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clear files created in tmp after test runs #528

Merged
merged 8 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/test_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 7 additions & 1 deletion server/autotest_server/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is good, just make sure to update the function docstring to note this additional action.

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)
Expand All @@ -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:
Expand Down
4 changes: 3 additions & 1 deletion server/autotest_server/tests/fixtures/test_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
38 changes: 38 additions & 0 deletions server/autotest_server/tests/test_autotest_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading