-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #514 from opensafely-core/kill-job-preserve-log
fix: kill_job preserves job logs.
- Loading branch information
Showing
3 changed files
with
94 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,64 @@ | ||
from unittest import mock | ||
|
||
import pytest | ||
|
||
from jobrunner.cli import kill_job | ||
from jobrunner.executors.local import volume_api | ||
from jobrunner.lib import database, docker | ||
from jobrunner.executors import local | ||
from jobrunner.lib import database | ||
from jobrunner.models import Job, State, StatusCode | ||
from tests.factories import job_factory | ||
|
||
|
||
def test_kill_job(db, monkeypatch): | ||
@pytest.mark.parametrize("cleanup", [False, True]) | ||
def test_kill_job(cleanup, tmp_work_dir, db, monkeypatch): | ||
|
||
j1 = job_factory(state=State.RUNNING, status_code=StatusCode.EXECUTING) | ||
job = job_factory(state=State.RUNNING, status_code=StatusCode.EXECUTING) | ||
|
||
mocker = mock.MagicMock(spec=docker) | ||
mockume_api = mock.MagicMock(spec=volume_api) | ||
mocker = mock.MagicMock(spec=local.docker) | ||
mockume_api = mock.MagicMock(spec=local.volume_api) | ||
|
||
def mock_get_jobs(partial_job_ids): | ||
return [j1] | ||
return [job] | ||
|
||
# persist_outputs needs this | ||
mocker.container_inspect.return_value = { | ||
"Image": "image", | ||
"State": {"ExitCode": 137}, | ||
} | ||
|
||
# set both the docker module names used to the mocker version | ||
monkeypatch.setattr(kill_job, "docker", mocker) | ||
monkeypatch.setattr(kill_job, "volume_api", mockume_api) | ||
monkeypatch.setattr(local, "docker", mocker) | ||
monkeypatch.setattr(kill_job.local, "volume_api", mockume_api) | ||
monkeypatch.setattr(kill_job, "get_jobs", mock_get_jobs) | ||
|
||
kill_job.main(j1.id) | ||
kill_job.main(job.id, cleanup=cleanup) | ||
|
||
job1 = database.find_one(Job, id=j1.id) | ||
job1 = database.find_one(Job, id=job.id) | ||
assert job1.state == State.FAILED | ||
assert job1.status_code == StatusCode.KILLED_BY_ADMIN | ||
|
||
container = local.container_name(job) | ||
assert mocker.kill.call_args[0] == (container,) | ||
assert mocker.write_logs_to_file.call_args[0][0] == container | ||
|
||
log_dir = local.get_log_dir(job) | ||
log_file = log_dir / "logs.txt" | ||
metadata_file = log_dir / "metadata.json" | ||
|
||
assert log_file.exists() | ||
assert metadata_file.exists() | ||
|
||
workspace_log_file = ( | ||
local.get_high_privacy_workspace(job.workspace) | ||
/ local.METADATA_DIR | ||
/ f"{job.action}.log" | ||
) | ||
assert not workspace_log_file.exists() | ||
|
||
if cleanup: | ||
assert mocker.delete_container.called | ||
assert mockume_api.delete_volume.called | ||
else: | ||
assert not mocker.delete_container.called | ||
assert not mockume_api.delete_volume.called |