| 1 |
from __future__ import annotations |
| 2 |
import json |
| 3 |
import logging |
| 4 |
import multiprocessing |
| 5 |
import os |
| 6 |
from pathlib import Path |
| 7 |
import signal |
| 8 |
import subprocess |
| 9 |
import sys |
| 10 |
from time import sleep, time |
| 11 |
import pytest |
| 12 |
from utils import assert_files, run_duct_command |
| 13 |
from con_duct import _duct_main |
| 14 |
from con_duct._constants import SUFFIXES |
| 15 |
from con_duct._models import Outputs |
| 16 |
|
| 17 |
|
| 18 |
def test_sample_less_than_report_interval(temp_output_dir: str) -> None: |
| 19 |
run_duct_command( |
| 20 |
["echo", "test"], |
| 21 |
sample_interval=0.01, |
| 22 |
report_interval=0.1, |
| 23 |
output_prefix=temp_output_dir, |
| 24 |
) |
| 25 |
|
| 26 |
|
| 27 |
def test_sample_equal_to_report_interval(temp_output_dir: str) -> None: |
| 28 |
run_duct_command( |
| 29 |
["echo", "test"], |
| 30 |
sample_interval=0.1, |
| 31 |
report_interval=0.1, |
| 32 |
output_prefix=temp_output_dir, |
| 33 |
) |
| 34 |
|
| 35 |
|
| 36 |
def test_sample_greater_than_report_interval() -> None: |
| 37 |
with pytest.raises( |
| 38 |
ValueError, |
| 39 |
match="--report-interval must be greater than or equal to --sample-interval", |
| 40 |
): |
| 41 |
run_duct_command( |
| 42 |
["echo", "test"], |
| 43 |
sample_interval=1.0, |
| 44 |
report_interval=0.1, |
| 45 |
) |
| 46 |
|
| 47 |
|
| 48 |
TEST_SCRIPT_DIR = Path(__file__).parent.parent / "data" |
| 49 |
|
| 50 |
expected_files = [ |
| 51 |
SUFFIXES["stdout"], |
| 52 |
SUFFIXES["stderr"], |
| 53 |
SUFFIXES["info"], |
| 54 |
SUFFIXES["usage"], |
| 55 |
] |
| 56 |
|
| 57 |
|
| 58 |
def assert_expected_files(temp_output_dir: str, exists: bool = True) -> None: |
| 59 |
assert_files(temp_output_dir, expected_files, exists=exists) |
| 60 |
|
| 61 |
|
| 62 |
def test_sanity_green(temp_output_dir: str) -> None: |
| 63 |
t0 = time() |
| 64 |
expected_exit_code = 0 |
| 65 |
assert ( |
| 66 |
run_duct_command( |
| 67 |
["echo", "hello", "world"], |
| 68 |
sample_interval=4.0, |
| 69 |
report_interval=60.0, |
| 70 |
output_prefix=temp_output_dir, |
| 71 |
) |
| 72 |
== expected_exit_code |
| 73 |
) |
| 74 |
assert time() - t0 < 0.4 |
| 75 |
assert_expected_files(temp_output_dir) |
| 76 |
|
| 77 |
|
| 78 |
@pytest.mark.flaky(reruns=5) |
| 79 |
def test_execution_summary( |
| 80 |
request: pytest.FixtureRequest, temp_output_dir: str |
| 81 |
) -> None: |
| 82 |
|
| 83 |
attempt = getattr(request.node, "execution_count", 1) |
| 84 |
sleep_time = 0.1 * attempt |
| 85 |
assert ( |
| 86 |
run_duct_command( |
| 87 |
["sleep", str(sleep_time)], |
| 88 |
sample_interval=0.05, |
| 89 |
report_interval=sleep_time, |
| 90 |
output_prefix=temp_output_dir, |
| 91 |
) |
| 92 |
== 0 |
| 93 |
) |
| 94 |
with open(os.path.join(temp_output_dir, SUFFIXES["info"])) as info: |
| 95 |
info_dict = json.loads(info.read()) |
| 96 |
execution_summary = info_dict["execution_summary"] |
| 97 |
|
| 98 |
assert execution_summary["average_pmem"] < 10 |
| 99 |
assert execution_summary["peak_pmem"] < 10 |
| 100 |
assert execution_summary["average_pcpu"] < 10 |
| 101 |
assert execution_summary["peak_pcpu"] < 10 |
| 102 |
assert execution_summary["exit_code"] == 0 |
| 103 |
assert execution_summary["working_directory"] == os.getcwd() |
| 104 |
|
| 105 |
|
| 106 |
@pytest.mark.parametrize("exit_code", [1, 2, 128]) |
| 107 |
def test_sanity_red( |
| 108 |
caplog: pytest.LogCaptureFixture, exit_code: int, temp_output_dir: str |
| 109 |
) -> None: |
| 110 |
caplog.set_level("INFO") |
| 111 |
assert ( |
| 112 |
run_duct_command( |
| 113 |
["sh", "-c", f"exit {exit_code}"], |
| 114 |
output_prefix=temp_output_dir, |
| 115 |
fail_time=0, |
| 116 |
) |
| 117 |
== exit_code |
| 118 |
) |
| 119 |
assert f"Exit Code: {exit_code}" in caplog.records[-1].message |
| 120 |
|
| 121 |
|
| 122 |
assert_expected_files(temp_output_dir) |
| 123 |
|
| 124 |
|
| 125 |
def test_outputs_full(temp_output_dir: str) -> None: |
| 126 |
script_path = str(TEST_SCRIPT_DIR / "test_script.py") |
| 127 |
assert ( |
| 128 |
run_duct_command( |
| 129 |
[script_path, "--duration", "1"], |
| 130 |
|
| 131 |
capture_outputs=Outputs.ALL, |
| 132 |
outputs=Outputs.ALL, |
| 133 |
output_prefix=temp_output_dir, |
| 134 |
) |
| 135 |
== 0 |
| 136 |
) |
| 137 |
assert_expected_files(temp_output_dir) |
| 138 |
|
| 139 |
|
| 140 |
def test_outputs_passthrough(temp_output_dir: str) -> None: |
| 141 |
script_path = str(TEST_SCRIPT_DIR / "test_script.py") |
| 142 |
assert ( |
| 143 |
run_duct_command( |
| 144 |
[script_path, "--duration", "1"], |
| 145 |
capture_outputs=Outputs.NONE, |
| 146 |
outputs=Outputs.ALL, |
| 147 |
output_prefix=temp_output_dir, |
| 148 |
) |
| 149 |
== 0 |
| 150 |
) |
| 151 |
expected_files = [SUFFIXES["info"], SUFFIXES["usage"]] |
| 152 |
assert_files(temp_output_dir, expected_files, exists=True) |
| 153 |
not_expected_files = [SUFFIXES["stdout"], SUFFIXES["stderr"]] |
| 154 |
assert_files(temp_output_dir, not_expected_files, exists=False) |
| 155 |
|
| 156 |
|
| 157 |
def test_outputs_capture(temp_output_dir: str) -> None: |
| 158 |
script_path = str(TEST_SCRIPT_DIR / "test_script.py") |
| 159 |
assert ( |
| 160 |
run_duct_command( |
| 161 |
[script_path, "--duration", "1"], |
| 162 |
capture_outputs=Outputs.ALL, |
| 163 |
outputs=Outputs.NONE, |
| 164 |
output_prefix=temp_output_dir, |
| 165 |
) |
| 166 |
== 0 |
| 167 |
) |
| 168 |
|
| 169 |
|
| 170 |
assert_expected_files(temp_output_dir) |
| 171 |
|
| 172 |
|
| 173 |
def test_outputs_none(temp_output_dir: str) -> None: |
| 174 |
script_path = str(TEST_SCRIPT_DIR / "test_script.py") |
| 175 |
assert ( |
| 176 |
run_duct_command( |
| 177 |
[script_path, "--duration", "1"], |
| 178 |
capture_outputs=Outputs.NONE, |
| 179 |
outputs=Outputs.NONE, |
| 180 |
output_prefix=temp_output_dir, |
| 181 |
) |
| 182 |
== 0 |
| 183 |
) |
| 184 |
|
| 185 |
|
| 186 |
expected_files = [SUFFIXES["info"], SUFFIXES["usage"]] |
| 187 |
assert_files(temp_output_dir, expected_files, exists=True) |
| 188 |
|
| 189 |
not_expected_files = [SUFFIXES["stdout"], SUFFIXES["stderr"]] |
| 190 |
assert_files(temp_output_dir, not_expected_files, exists=False) |
| 191 |
|
| 192 |
|
| 193 |
def test_exit_before_first_sample(temp_output_dir: str) -> None: |
| 194 |
assert ( |
| 195 |
run_duct_command( |
| 196 |
["ls"], |
| 197 |
sample_interval=0.1, |
| 198 |
report_interval=0.1, |
| 199 |
output_prefix=temp_output_dir, |
| 200 |
) |
| 201 |
== 0 |
| 202 |
) |
| 203 |
assert_expected_files(temp_output_dir) |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
def test_run_less_than_report_interval(temp_output_dir: str) -> None: |
| 208 |
assert ( |
| 209 |
run_duct_command( |
| 210 |
["sleep", "0.01"], |
| 211 |
sample_interval=0.001, |
| 212 |
report_interval=0.1, |
| 213 |
output_prefix=temp_output_dir, |
| 214 |
) |
| 215 |
== 0 |
| 216 |
) |
| 217 |
|
| 218 |
assert_expected_files(temp_output_dir) |
| 219 |
|
| 220 |
|
| 221 |
def test_execute_unknown_command( |
| 222 |
temp_output_dir: str, caplog: pytest.LogCaptureFixture |
| 223 |
) -> None: |
| 224 |
cmd = "this_command_does_not_exist_123abrakadabra" |
| 225 |
with caplog.at_level(logging.ERROR): |
| 226 |
assert run_duct_command([cmd]) == 127 |
| 227 |
assert f"{cmd}: command not found" in caplog.text |
| 228 |
assert_expected_files(temp_output_dir, exists=False) |
| 229 |
|
| 230 |
|
| 231 |
def _runner_for_signal_int(temp_output_dir: str, fail_time: float | None) -> int: |
| 232 |
kws = {} |
| 233 |
if fail_time is not None: |
| 234 |
kws["fail_time"] = fail_time |
| 235 |
return run_duct_command( |
| 236 |
["sleep", "60.74016230000801"], output_prefix=temp_output_dir, **kws |
| 237 |
) |
| 238 |
|
| 239 |
|
| 240 |
@pytest.mark.flaky(reruns=5) |
| 241 |
@pytest.mark.parametrize("fail_time", [None, 0, 10, -1, -3.14]) |
| 242 |
def test_signal_int( |
| 243 |
request: pytest.FixtureRequest, temp_output_dir: str, fail_time: float | None |
| 244 |
) -> None: |
| 245 |
|
| 246 |
attempt = getattr(request.node, "execution_count", 1) |
| 247 |
wait_time = 0.2 * attempt |
| 248 |
proc = multiprocessing.Process( |
| 249 |
target=_runner_for_signal_int, args=(temp_output_dir, fail_time) |
| 250 |
) |
| 251 |
proc.start() |
| 252 |
sleep(wait_time) |
| 253 |
assert proc.pid is not None, "Process PID should not be None" |
| 254 |
os.kill(proc.pid, signal.SIGINT) |
| 255 |
proc.join() |
| 256 |
|
| 257 |
|
| 258 |
assert proc.exitcode == 0 |
| 259 |
|
| 260 |
if fail_time is None or fail_time != 0: |
| 261 |
assert_expected_files(temp_output_dir, exists=False) |
| 262 |
else: |
| 263 |
|
| 264 |
with open(os.path.join(temp_output_dir, SUFFIXES["info"])) as info: |
| 265 |
info_data = json.loads(info.read()) |
| 266 |
|
| 267 |
command_exit_code = info_data["execution_summary"]["exit_code"] |
| 268 |
|
| 269 |
assert command_exit_code == 128 + 2 |
| 270 |
|
| 271 |
|
| 272 |
def _runner_for_signal_kill(temp_output_dir: str, fail_time: float | None) -> int: |
| 273 |
script_path = str(TEST_SCRIPT_DIR / "signal_ignorer.py") |
| 274 |
kws = {} |
| 275 |
if fail_time is not None: |
| 276 |
kws["fail_time"] = fail_time |
| 277 |
return run_duct_command([script_path], output_prefix=temp_output_dir, **kws) |
| 278 |
|
| 279 |
|
| 280 |
@pytest.mark.flaky(reruns=5) |
| 281 |
@pytest.mark.parametrize("fail_time", [None, 0, 10, -1, -3.14]) |
| 282 |
def test_signal_kill( |
| 283 |
request: pytest.FixtureRequest, temp_output_dir: str, fail_time: float | None |
| 284 |
) -> None: |
| 285 |
|
| 286 |
attempt = getattr(request.node, "execution_count", 1) |
| 287 |
wait_time = 0.2 * attempt |
| 288 |
proc = multiprocessing.Process( |
| 289 |
target=_runner_for_signal_kill, args=(temp_output_dir, fail_time) |
| 290 |
) |
| 291 |
proc.start() |
| 292 |
sleep(wait_time) |
| 293 |
assert proc.pid is not None, "Process PID should not be None" |
| 294 |
os.kill(proc.pid, signal.SIGINT) |
| 295 |
sleep(wait_time) |
| 296 |
os.kill(proc.pid, signal.SIGINT) |
| 297 |
sleep(wait_time) |
| 298 |
os.kill(proc.pid, signal.SIGINT) |
| 299 |
proc.join() |
| 300 |
|
| 301 |
|
| 302 |
assert proc.exitcode == 0 |
| 303 |
|
| 304 |
if fail_time is None or fail_time != 0: |
| 305 |
assert_expected_files(temp_output_dir, exists=False) |
| 306 |
else: |
| 307 |
|
| 308 |
with open(os.path.join(temp_output_dir, SUFFIXES["info"])) as info: |
| 309 |
info_data = json.loads(info.read()) |
| 310 |
|
| 311 |
command_exit_code = info_data["execution_summary"]["exit_code"] |
| 312 |
|
| 313 |
assert command_exit_code == 128 + 9 |
| 314 |
|
| 315 |
|
| 316 |
def test_duct_as_executable(temp_output_dir: str) -> None: |
| 317 |
ps_command = [ |
| 318 |
sys.executable, |
| 319 |
_duct_main.__file__, |
| 320 |
"-p", |
| 321 |
temp_output_dir, |
| 322 |
"-q", |
| 323 |
"sleep", |
| 324 |
"0.01", |
| 325 |
] |
| 326 |
|
| 327 |
subprocess.check_output(ps_command, shell=False).decode() |
| 328 |
|
| 329 |
|
| 330 |
def test_message_in_json_output(temp_output_dir: str) -> None: |
| 331 |
"""Test that message appears in JSON output when provided.""" |
| 332 |
test_message = "Electrolytes, its what plants crave" |
| 333 |
assert ( |
| 334 |
run_duct_command( |
| 335 |
["echo", "hello"], |
| 336 |
output_prefix=temp_output_dir, |
| 337 |
message=test_message, |
| 338 |
) |
| 339 |
== 0 |
| 340 |
) |
| 341 |
|
| 342 |
|
| 343 |
with open(os.path.join(temp_output_dir, SUFFIXES["info"])) as info: |
| 344 |
info_dict = json.loads(info.read()) |
| 345 |
|
| 346 |
assert "message" in info_dict |
| 347 |
assert info_dict["message"] == test_message |
| 348 |
|
| 349 |
|
| 350 |
def test_no_message_in_json_output(temp_output_dir: str) -> None: |
| 351 |
"""Test that message field is empty string when not provided.""" |
| 352 |
assert ( |
| 353 |
run_duct_command( |
| 354 |
["echo", "hello"], |
| 355 |
output_prefix=temp_output_dir, |
| 356 |
) |
| 357 |
== 0 |
| 358 |
) |
| 359 |
|
| 360 |
|
| 361 |
with open(os.path.join(temp_output_dir, SUFFIXES["info"])) as info: |
| 362 |
info_dict = json.loads(info.read()) |
| 363 |
|
| 364 |
assert "message" in info_dict |
| 365 |
assert info_dict["message"] == "" |
| 366 |
|
| 367 |
|
| 368 |
def test_execute_existing_files_no_clobber( |
| 369 |
temp_output_dir: str, caplog: pytest.LogCaptureFixture |
| 370 |
) -> None: |
| 371 |
Path(temp_output_dir, SUFFIXES["info"]).write_text("pre-existing") |
| 372 |
with caplog.at_level(logging.ERROR): |
| 373 |
assert run_duct_command(["echo", "hi"], output_prefix=temp_output_dir) == 1 |
| 374 |
assert "Conflicting files:" in caplog.text |
| 375 |
assert_files( |
| 376 |
temp_output_dir, |
| 377 |
[SUFFIXES["stdout"], SUFFIXES["stderr"], SUFFIXES["usage"]], |
| 378 |
exists=False, |
| 379 |
) |