|
| 1 | +"""Test cleanup of workspace directories and InternalRequest CRs.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from pathlib import Path |
| 6 | +from unittest import mock |
| 7 | + |
| 8 | +import cleanup_workspace |
| 9 | +import pytest |
| 10 | + |
| 11 | + |
| 12 | +class TestCleanupDirectory: |
| 13 | + """Test directory removal logic.""" |
| 14 | + |
| 15 | + def test_removes_existing_directory(self, tmp_path: Path) -> None: |
| 16 | + """Existing subdirectory is removed.""" |
| 17 | + target = tmp_path / "subdir" |
| 18 | + target.mkdir() |
| 19 | + (target / "file.txt").write_text("content", encoding="utf-8") |
| 20 | + |
| 21 | + cleanup_workspace.cleanup_directory(str(tmp_path), "subdir") |
| 22 | + |
| 23 | + assert not target.exists() |
| 24 | + |
| 25 | + def test_noop_when_directory_does_not_exist(self, tmp_path: Path) -> None: |
| 26 | + """No error when the subdirectory does not exist.""" |
| 27 | + cleanup_workspace.cleanup_directory(str(tmp_path), "nonexistent") |
| 28 | + |
| 29 | + def test_preserves_sibling_directories(self, tmp_path: Path) -> None: |
| 30 | + """Only the target subdirectory is removed.""" |
| 31 | + target = tmp_path / "remove-me" |
| 32 | + target.mkdir() |
| 33 | + sibling = tmp_path / "keep-me" |
| 34 | + sibling.mkdir() |
| 35 | + (sibling / "important.txt").write_text("keep", encoding="utf-8") |
| 36 | + |
| 37 | + cleanup_workspace.cleanup_directory(str(tmp_path), "remove-me") |
| 38 | + |
| 39 | + assert not target.exists() |
| 40 | + assert sibling.exists() |
| 41 | + assert (sibling / "important.txt").read_text(encoding="utf-8") == "keep" |
| 42 | + |
| 43 | + |
| 44 | +class TestRun: |
| 45 | + """Test the run() orchestration.""" |
| 46 | + |
| 47 | + def test_full_flow(self, tmp_path: Path) -> None: |
| 48 | + """Directory is removed after IR cleanup and delay.""" |
| 49 | + target = tmp_path / "cleanup-dir" |
| 50 | + target.mkdir() |
| 51 | + (target / "file.txt").write_text("delete me", encoding="utf-8") |
| 52 | + |
| 53 | + with ( |
| 54 | + mock.patch("cleanup_workspace.cleanup_internal_requests.run") as mock_ir, |
| 55 | + mock.patch("cleanup_workspace.time.sleep") as mock_sleep, |
| 56 | + ): |
| 57 | + cleanup_workspace.run("cleanup-dir", 5, "uid-123", str(tmp_path)) |
| 58 | + |
| 59 | + mock_ir.assert_called_once_with("uid-123") |
| 60 | + mock_sleep.assert_called_once_with(5) |
| 61 | + assert not target.exists() |
| 62 | + |
| 63 | + def test_empty_subdirectory_skips_after_ir_cleanup(self) -> None: |
| 64 | + """Empty subdirectory string skips delay and directory removal.""" |
| 65 | + with ( |
| 66 | + mock.patch("cleanup_workspace.cleanup_internal_requests.run") as mock_ir, |
| 67 | + mock.patch("cleanup_workspace.time.sleep") as mock_sleep, |
| 68 | + ): |
| 69 | + cleanup_workspace.run("", 60, "uid-456", "/fake/path") |
| 70 | + |
| 71 | + mock_ir.assert_called_once_with("uid-456") |
| 72 | + mock_sleep.assert_not_called() |
| 73 | + |
| 74 | + def test_delay_is_applied(self, tmp_path: Path) -> None: |
| 75 | + """Delay value is passed to time.sleep.""" |
| 76 | + with ( |
| 77 | + mock.patch("cleanup_workspace.cleanup_internal_requests.run"), |
| 78 | + mock.patch("cleanup_workspace.time.sleep") as mock_sleep, |
| 79 | + ): |
| 80 | + cleanup_workspace.run("somedir", 42, "", str(tmp_path)) |
| 81 | + |
| 82 | + mock_sleep.assert_called_once_with(42) |
| 83 | + |
| 84 | + def test_ir_cleanup_called_with_uid(self, tmp_path: Path) -> None: |
| 85 | + """IR cleanup receives the pipeline run UID.""" |
| 86 | + with ( |
| 87 | + mock.patch("cleanup_workspace.cleanup_internal_requests.run") as mock_ir, |
| 88 | + mock.patch("cleanup_workspace.time.sleep"), |
| 89 | + ): |
| 90 | + cleanup_workspace.run("dir", 0, "my-uid", str(tmp_path)) |
| 91 | + |
| 92 | + mock_ir.assert_called_once_with("my-uid") |
| 93 | + |
| 94 | + def test_ir_cleanup_called_with_empty_uid(self, tmp_path: Path) -> None: |
| 95 | + """IR cleanup is called even with empty UID (it handles the no-op).""" |
| 96 | + with ( |
| 97 | + mock.patch("cleanup_workspace.cleanup_internal_requests.run") as mock_ir, |
| 98 | + mock.patch("cleanup_workspace.time.sleep"), |
| 99 | + ): |
| 100 | + cleanup_workspace.run("dir", 0, "", str(tmp_path)) |
| 101 | + |
| 102 | + mock_ir.assert_called_once_with("") |
| 103 | + |
| 104 | + def test_ir_cleanup_error_propagates(self) -> None: |
| 105 | + """RuntimeError from IR cleanup propagates to the caller.""" |
| 106 | + with mock.patch( |
| 107 | + "cleanup_workspace.cleanup_internal_requests.run", |
| 108 | + side_effect=RuntimeError("kubectl failed"), |
| 109 | + ): |
| 110 | + with pytest.raises(RuntimeError, match="kubectl failed"): |
| 111 | + cleanup_workspace.run("dir", 0, "uid", "/fake") |
| 112 | + |
| 113 | + |
| 114 | +class TestParseArgs: |
| 115 | + """Test argument parsing.""" |
| 116 | + |
| 117 | + def test_all_args(self) -> None: |
| 118 | + """All arguments are parsed correctly.""" |
| 119 | + args = cleanup_workspace._parse_args( |
| 120 | + [ |
| 121 | + "--subdirectory", |
| 122 | + "mydir", |
| 123 | + "--delay", |
| 124 | + "30", |
| 125 | + "--pipeline-run-uid", |
| 126 | + "uid-789", |
| 127 | + "--workspace-path", |
| 128 | + "/workspace", |
| 129 | + ] |
| 130 | + ) |
| 131 | + assert args.subdirectory == "mydir" |
| 132 | + assert args.delay == 30 |
| 133 | + assert args.pipeline_run_uid == "uid-789" |
| 134 | + assert args.workspace_path == "/workspace" |
| 135 | + |
| 136 | + def test_defaults(self) -> None: |
| 137 | + """Default values for optional arguments.""" |
| 138 | + args = cleanup_workspace._parse_args(["--workspace-path", "/workspace"]) |
| 139 | + assert args.subdirectory == "" |
| 140 | + assert args.delay == 60 |
| 141 | + assert args.pipeline_run_uid == "" |
| 142 | + |
| 143 | + def test_workspace_path_required(self) -> None: |
| 144 | + """Missing --workspace-path causes a SystemExit.""" |
| 145 | + with pytest.raises(SystemExit): |
| 146 | + cleanup_workspace._parse_args([]) |
| 147 | + |
| 148 | + |
| 149 | +class TestMain: |
| 150 | + """Test the CLI entry point.""" |
| 151 | + |
| 152 | + def test_success(self) -> None: |
| 153 | + """Return 0 on successful run.""" |
| 154 | + with mock.patch("cleanup_workspace.run") as mock_run: |
| 155 | + result = cleanup_workspace.main( |
| 156 | + [ |
| 157 | + "--subdirectory", |
| 158 | + "dir", |
| 159 | + "--delay", |
| 160 | + "0", |
| 161 | + "--pipeline-run-uid", |
| 162 | + "uid", |
| 163 | + "--workspace-path", |
| 164 | + "/ws", |
| 165 | + ] |
| 166 | + ) |
| 167 | + assert result == 0 |
| 168 | + mock_run.assert_called_once_with("dir", 0, "uid", "/ws") |
| 169 | + |
| 170 | + def test_default_values(self) -> None: |
| 171 | + """Default values are forwarded correctly.""" |
| 172 | + with mock.patch("cleanup_workspace.run") as mock_run: |
| 173 | + result = cleanup_workspace.main(["--workspace-path", "/ws"]) |
| 174 | + assert result == 0 |
| 175 | + mock_run.assert_called_once_with("", 60, "", "/ws") |
| 176 | + |
| 177 | + def test_runtime_error_propagates(self) -> None: |
| 178 | + """RuntimeError from run() propagates as unhandled exception.""" |
| 179 | + with mock.patch( |
| 180 | + "cleanup_workspace.run", |
| 181 | + side_effect=RuntimeError("boom"), |
| 182 | + ): |
| 183 | + with pytest.raises(RuntimeError, match="boom"): |
| 184 | + cleanup_workspace.main(["--workspace-path", "/ws"]) |
0 commit comments