|
| 1 | +"""Tests for updating the processing repository (BrainCogsEphysSorters) before queuing a job. |
| 2 | +
|
| 3 | +The checkout lives on the cluster and is pulled there (fast-forward only) before each |
| 4 | +job is queued. If it can't be pulled (read-only mount, local commits, no network) the |
| 5 | +job still runs, and the reason is reported so a Slack warning can be sent. |
| 6 | +""" |
| 7 | + |
| 8 | +import os |
| 9 | +import shlex |
| 10 | +import shutil |
| 11 | +import stat |
| 12 | +import subprocess |
| 13 | + |
| 14 | +import pytest |
| 15 | + |
| 16 | +from u19_pipeline.utils.slurm_utils import ( |
| 17 | + repository_update_warning, |
| 18 | + update_repository, |
| 19 | + update_repository_command, |
| 20 | +) |
| 21 | + |
| 22 | +pytestmark = pytest.mark.skipif(shutil.which("git") is None, reason="git not found") |
| 23 | + |
| 24 | + |
| 25 | +def git(cwd, *args): |
| 26 | + return subprocess.run( |
| 27 | + ["git", "-C", str(cwd), *args], check=True, capture_output=True, text=True |
| 28 | + ).stdout.strip() |
| 29 | + |
| 30 | + |
| 31 | +def commit(repo, name): |
| 32 | + (repo / name).write_text(name) |
| 33 | + git(repo, "add", name) |
| 34 | + git(repo, "-c", "user.name=t", "-c", "user.email=t@t", "commit", "-qm", name) |
| 35 | + return git(repo, "rev-parse", "--short", "HEAD") |
| 36 | + |
| 37 | + |
| 38 | +@pytest.fixture |
| 39 | +def upstream_and_checkout(tmp_path): |
| 40 | + upstream = tmp_path / "upstream" |
| 41 | + upstream.mkdir() |
| 42 | + git(upstream, "init", "-q", "-b", "main") |
| 43 | + commit(upstream, "first") |
| 44 | + checkout = tmp_path / "checkout" |
| 45 | + subprocess.run( |
| 46 | + ["git", "clone", "-q", str(upstream), str(checkout)], |
| 47 | + check=True, |
| 48 | + capture_output=True, |
| 49 | + ) |
| 50 | + return upstream, checkout |
| 51 | + |
| 52 | + |
| 53 | +def test_fast_forwards_to_the_latest_commit(upstream_and_checkout): |
| 54 | + upstream, checkout = upstream_and_checkout |
| 55 | + latest = commit(upstream, "second") |
| 56 | + |
| 57 | + updated, message = update_repository(str(checkout), upstream.as_uri()) |
| 58 | + |
| 59 | + assert updated |
| 60 | + assert message == latest |
| 61 | + assert git(checkout, "rev-parse", "--short", "HEAD") == latest |
| 62 | + |
| 63 | + |
| 64 | +def test_up_to_date_checkout_is_updated(upstream_and_checkout): |
| 65 | + upstream, checkout = upstream_and_checkout |
| 66 | + |
| 67 | + updated, message = update_repository(str(checkout), upstream.as_uri()) |
| 68 | + |
| 69 | + assert updated |
| 70 | + assert message == git(upstream, "rev-parse", "--short", "HEAD") |
| 71 | + |
| 72 | + |
| 73 | +def test_submodule_moves_to_the_pinned_commit( |
| 74 | + upstream_and_checkout, tmp_path, monkeypatch |
| 75 | +): |
| 76 | + upstream, checkout = upstream_and_checkout |
| 77 | + # Local file:// submodules are blocked by default in recent git |
| 78 | + monkeypatch.setenv("GIT_CONFIG_COUNT", "1") |
| 79 | + monkeypatch.setenv("GIT_CONFIG_KEY_0", "protocol.file.allow") |
| 80 | + monkeypatch.setenv("GIT_CONFIG_VALUE_0", "always") |
| 81 | + sorter = tmp_path / "sorter" |
| 82 | + sorter.mkdir() |
| 83 | + git(sorter, "init", "-q", "-b", "main") |
| 84 | + commit(sorter, "v1") |
| 85 | + git(upstream, "submodule", "add", "-q", sorter.as_uri(), "sorters/sorter") |
| 86 | + git(upstream, "-c", "user.name=t", "-c", "user.email=t@t", "commit", "-qm", "add") |
| 87 | + assert update_repository(str(checkout), upstream.as_uri())[0] |
| 88 | + |
| 89 | + v2 = commit(sorter, "v2") |
| 90 | + git(upstream / "sorters" / "sorter", "pull", "-q", "origin", "main") |
| 91 | + git(upstream, "add", "sorters/sorter") |
| 92 | + git(upstream, "-c", "user.name=t", "-c", "user.email=t@t", "commit", "-qm", "bump") |
| 93 | + |
| 94 | + updated, message = update_repository(str(checkout), upstream.as_uri()) |
| 95 | + |
| 96 | + assert updated, message |
| 97 | + assert git(checkout / "sorters" / "sorter", "rev-parse", "--short", "HEAD") == v2 |
| 98 | + |
| 99 | + |
| 100 | +@pytest.mark.skipif(os.geteuid() == 0, reason="root ignores file permissions") |
| 101 | +def test_read_only_checkout_is_reported(upstream_and_checkout): |
| 102 | + upstream, checkout = upstream_and_checkout |
| 103 | + commit(upstream, "second") |
| 104 | + git_dir = checkout / ".git" |
| 105 | + modes = {p: p.stat().st_mode for p in [git_dir, *git_dir.rglob("*")] if p.is_dir()} |
| 106 | + for p in modes: |
| 107 | + p.chmod(stat.S_IRUSR | stat.S_IXUSR) |
| 108 | + try: |
| 109 | + updated, message = update_repository(str(checkout), upstream.as_uri()) |
| 110 | + finally: |
| 111 | + for p, mode in modes.items(): |
| 112 | + p.chmod(mode) |
| 113 | + |
| 114 | + assert not updated |
| 115 | + assert message |
| 116 | + |
| 117 | + |
| 118 | +def test_diverged_checkout_is_not_merged(upstream_and_checkout): |
| 119 | + upstream, checkout = upstream_and_checkout |
| 120 | + commit(upstream, "second") |
| 121 | + local = commit(checkout, "local_change") |
| 122 | + |
| 123 | + updated, message = update_repository(str(checkout), upstream.as_uri()) |
| 124 | + |
| 125 | + assert not updated |
| 126 | + assert message |
| 127 | + assert git(checkout, "rev-parse", "--short", "HEAD") == local |
| 128 | + |
| 129 | + |
| 130 | +def test_missing_checkout_is_reported(tmp_path): |
| 131 | + updated, message = update_repository(str(tmp_path / "nope"), "https://x/y.git") |
| 132 | + |
| 133 | + assert not updated |
| 134 | + assert message |
| 135 | + |
| 136 | + |
| 137 | +def test_command_through_ssh_quotes_the_paths(): |
| 138 | + command = update_repository_command( |
| 139 | + "/mnt/dir with space/Repo", "https://github.com/o/r.git", "u19prod@host" |
| 140 | + ) |
| 141 | + |
| 142 | + assert command[:4] == ["ssh", "-o", "BatchMode=yes", "u19prod@host"] |
| 143 | + assert shlex.split(command[4]) == [ |
| 144 | + "git", "-C", "/mnt/dir with space/Repo", "pull", "--ff-only", "--quiet", |
| 145 | + "https://github.com/o/r.git", "HEAD", |
| 146 | + "&&", "git", "-C", "/mnt/dir with space/Repo", "submodule", "update", "--init", |
| 147 | + "--recursive", "--quiet", |
| 148 | + "&&", "git", "-C", "/mnt/dir with space/Repo", "rev-parse", "--short", "HEAD", |
| 149 | + ] # fmt: skip |
| 150 | + |
| 151 | + |
| 152 | +def test_warning_names_repository_host_and_job(): |
| 153 | + warning = repository_update_warning( |
| 154 | + "BrainCogsEphysSorters", |
| 155 | + "/repo/dir", |
| 156 | + "u19prod@spock", |
| 157 | + 42, |
| 158 | + "Read-only file system", |
| 159 | + ) |
| 160 | + |
| 161 | + text = warning["text"] |
| 162 | + assert "BrainCogsEphysSorters" in text |
| 163 | + assert "/repo/dir" in text |
| 164 | + assert "u19prod@spock" in text |
| 165 | + assert "job 42" in text |
| 166 | + assert "Read-only file system" in text |
0 commit comments