| 1 |
from __future__ import annotations |
| 2 |
import json |
| 3 |
import os |
| 4 |
from pathlib import Path |
| 5 |
import platform |
| 6 |
import subprocess |
| 7 |
import time |
| 8 |
import pytest |
| 9 |
from con_duct._constants import SUFFIXES |
| 10 |
|
| 11 |
SYSTEM = platform.system() |
| 12 |
TEST_SCRIPT_DIR = Path(__file__).parent.parent / "data" |
| 13 |
|
| 14 |
_DUCT_EXECUTABLES = [ |
| 15 |
exe.strip() |
| 16 |
for exe in os.environ.get("DUCT_TEST_EXECUTABLES", "duct,con-duct run").split(",") |
| 17 |
] |
| 18 |
|
| 19 |
|
| 20 |
@pytest.fixture(params=_DUCT_EXECUTABLES) |
| 21 |
def duct_cmd(request: pytest.FixtureRequest) -> str: |
| 22 |
"""Fixture that parametrizes tests to run with different duct entry points.""" |
| 23 |
return str(request.param) |
| 24 |
|
| 25 |
|
| 26 |
def test_sanity(temp_output_dir: str, duct_cmd: str) -> None: |
| 27 |
command = f"{duct_cmd} -p {temp_output_dir}log_ sleep 0.1" |
| 28 |
subprocess.check_output(command, shell=True) |
| 29 |
|
| 30 |
|
| 31 |
@pytest.mark.flaky(reruns=3) |
| 32 |
@pytest.mark.parametrize("mode", ["plain", "subshell", "nohup", "setsid"]) |
| 33 |
@pytest.mark.parametrize("num_children", [1, 2, 10]) |
| 34 |
def test_spawn_children( |
| 35 |
temp_output_dir: str, duct_cmd: str, mode: str, num_children: int |
| 36 |
) -> None: |
| 37 |
duct_prefix = f"{temp_output_dir}log_" |
| 38 |
script_path = TEST_SCRIPT_DIR / "spawn_children.sh" |
| 39 |
dur = "0.3" |
| 40 |
command = ( |
| 41 |
f"{duct_cmd} -q --s-i 0.001 --r-i 0.01 " |
| 42 |
f"-p {duct_prefix} {script_path} {mode} {num_children} {dur}" |
| 43 |
) |
| 44 |
subprocess.check_output(command, shell=True) |
| 45 |
|
| 46 |
with open(f"{duct_prefix}{SUFFIXES['usage']}") as usage_file: |
| 47 |
all_samples = [json.loads(line) for line in usage_file] |
| 48 |
|
| 49 |
|
| 50 |
all_child_pids = set( |
| 51 |
pid |
| 52 |
for sample in all_samples |
| 53 |
for pid, proc in sample["processes"].items() |
| 54 |
if "sleep" in proc["cmd"] |
| 55 |
) |
| 56 |
|
| 57 |
if mode == "setsid": |
| 58 |
assert len(all_child_pids) == 1 |
| 59 |
else: |
| 60 |
assert len(all_child_pids) == num_children + 1 |
| 61 |
|
| 62 |
|
| 63 |
@pytest.mark.parametrize("session_mode", ["new-session", "current-session"]) |
| 64 |
def test_session_modes(temp_output_dir: str, duct_cmd: str, session_mode: str) -> None: |
| 65 |
"""Test that both session modes work correctly and collect appropriate data.""" |
| 66 |
duct_prefix = f"{temp_output_dir}log_" |
| 67 |
command = f"{duct_cmd} -q --s-i 0.01 --r-i 0.05 --mode {session_mode} -p {duct_prefix} sleep 0.3" |
| 68 |
subprocess.check_output(command, shell=True) |
| 69 |
|
| 70 |
|
| 71 |
usage_file = Path(f"{duct_prefix}{SUFFIXES['usage']}") |
| 72 |
info_file = Path(f"{duct_prefix}{SUFFIXES['info']}") |
| 73 |
|
| 74 |
assert usage_file.exists(), f"Usage file not created for {session_mode} mode" |
| 75 |
assert info_file.exists(), f"Info file not created for {session_mode} mode" |
| 76 |
|
| 77 |
|
| 78 |
with open(usage_file) as f: |
| 79 |
samples = [json.loads(line) for line in f] |
| 80 |
|
| 81 |
|
| 82 |
assert len(samples) > 0, f"No samples collected for {session_mode} mode" |
| 83 |
|
| 84 |
|
| 85 |
for sample in samples: |
| 86 |
assert "timestamp" in sample |
| 87 |
assert "processes" in sample |
| 88 |
assert "totals" in sample |
| 89 |
|
| 90 |
|
| 91 |
with open(info_file) as f: |
| 92 |
info_data = json.loads(f.read()) |
| 93 |
|
| 94 |
assert "execution_summary" in info_data |
| 95 |
assert info_data["execution_summary"]["exit_code"] == 0 |
| 96 |
assert "sleep" in info_data["command"] |
| 97 |
|
| 98 |
|
| 99 |
def test_session_mode_behavior_difference(temp_output_dir: str, duct_cmd: str) -> None: |
| 100 |
"""Test that new-session and current-session modes behave differently.""" |
| 101 |
|
| 102 |
|
| 103 |
background_process = subprocess.Popen( |
| •104 |
["python3", "-c", "print('DUCT_TEST_MARKER'); import time; time.sleep(10)"], |
| 105 |
stdout=subprocess.DEVNULL, |
| 106 |
stderr=subprocess.DEVNULL, |
| 107 |
) |
| 108 |
|
| 109 |
try: |
| 110 |
|
| 111 |
time.sleep(0.1) |
| 112 |
|
| 113 |
new_session_prefix = f"{temp_output_dir}new_" |
| 114 |
current_session_prefix = f"{temp_output_dir}current_" |
| 115 |
|
| 116 |
|
| 117 |
subprocess.check_output( |
| 118 |
f"{duct_cmd} -q --s-i 0.01 --r-i 0.05 --mode new-session -p {new_session_prefix} sleep 2", |
| 119 |
shell=True, |
| 120 |
) |
| 121 |
|
| 122 |
|
| 123 |
subprocess.check_output( |
| 124 |
f"{duct_cmd} -q --s-i 0.01 --r-i 0.05 --mode current-session -p {current_session_prefix} sleep 2", |
| 125 |
shell=True, |
| 126 |
) |
| 127 |
|
| 128 |
|
| 129 |
with open(f"{new_session_prefix}{SUFFIXES['usage']}") as f: |
| 130 |
new_session_samples = [json.loads(line) for line in f] |
| 131 |
|
| 132 |
with open(f"{current_session_prefix}{SUFFIXES['usage']}") as f: |
| 133 |
current_session_samples = [json.loads(line) for line in f] |
| 134 |
|
| 135 |
|
| 136 |
new_session_has_marker = any( |
| 137 |
any( |
| 138 |
"DUCT_TEST_MARKER" in str(proc.get("cmd", "")) |
| 139 |
for proc in sample["processes"].values() |
| 140 |
) |
| 141 |
for sample in new_session_samples |
| 142 |
) |
| 143 |
|
| 144 |
current_session_has_marker = any( |
| 145 |
any( |
| 146 |
"DUCT_TEST_MARKER" in str(proc.get("cmd", "")) |
| 147 |
for proc in sample["processes"].values() |
| 148 |
) |
| 149 |
for sample in current_session_samples |
| 150 |
) |
| 151 |
|
| 152 |
|
| 153 |
assert ( |
| 154 |
not new_session_has_marker |
| 155 |
), "new-session mode should not track background process" |
| 156 |
|
| 157 |
|
| 158 |
assert ( |
| 159 |
current_session_has_marker |
| 160 |
), "current-session mode should track background process" |
| 161 |
|
| 162 |
finally: |
| 163 |
|
| 164 |
if background_process.poll() is None: |
| 165 |
background_process.terminate() |
| 166 |
try: |
| 167 |
background_process.wait(timeout=2) |
| 168 |
except subprocess.TimeoutExpired: |
| 169 |
background_process.kill() |
| 170 |
background_process.wait() |
| 171 |
|
| 172 |
|
| 173 |
def test_logging_levels(temp_output_dir: str, duct_cmd: str) -> None: |
| 174 |
"""Test that --quiet and --log-level NONE suppress logging output.""" |
| 175 |
duct_prefix = f"{temp_output_dir}log_" |
| 176 |
|
| 177 |
|
| 178 |
result = subprocess.run( |
| 179 |
f"{duct_cmd} -p {duct_prefix} sleep 0.1", |
| 180 |
shell=True, |
| 181 |
capture_output=True, |
| 182 |
text=True, |
| 183 |
) |
| 184 |
assert result.returncode == 0 |
| 185 |
assert "Summary" in result.stderr, "Normal run should log Summary" |
| 186 |
assert "Exit Code:" in result.stderr |
| 187 |
|
| 188 |
|
| 189 |
result_quiet = subprocess.run( |
| 190 |
f"{duct_cmd} --quiet --clobber -p {duct_prefix} sleep 0.1", |
| 191 |
shell=True, |
| 192 |
capture_output=True, |
| 193 |
text=True, |
| 194 |
) |
| 195 |
assert result_quiet.returncode == 0 |
| 196 |
assert "Summary" not in result_quiet.stderr, "--quiet should suppress logging" |
| 197 |
assert ( |
| 198 |
result_quiet.stderr == "" |
| 199 |
), f"Expected empty stderr, got: {result_quiet.stderr!r}" |
| 200 |
|
| 201 |
|
| 202 |
result_none = subprocess.run( |
| 203 |
f"{duct_cmd} --log-level NONE --clobber -p {duct_prefix} sleep 0.1", |
| 204 |
shell=True, |
| 205 |
capture_output=True, |
| 206 |
text=True, |
| 207 |
) |
| 208 |
assert result_none.returncode == 0 |
| 209 |
assert ( |
| 210 |
"Summary" not in result_none.stderr |
| 211 |
), "--log-level NONE should suppress logging" |
| 212 |
assert ( |
| 213 |
result_none.stderr == "" |
| 214 |
), f"Expected empty stderr, got: {result_none.stderr!r}" |