DEVX-153: fix: run molecule destroy on test failure to clean up containers
This commit is contained in:
@@ -246,18 +246,62 @@ def cli(pairs: tuple[str, ...], roles_root: Path | None) -> None:
|
|||||||
with contextlib.suppress(ProcessLookupError):
|
with contextlib.suppress(ProcessLookupError):
|
||||||
os.killpg(os.getpgid(process.pid), signal.SIGKILL)
|
os.killpg(os.getpgid(process.pid), signal.SIGKILL)
|
||||||
process.wait()
|
process.wait()
|
||||||
|
# Clean up containers left behind by the killed test.
|
||||||
|
click.echo(_("Cleaning up: running molecule destroy for {scenario}", scenario=scenario))
|
||||||
|
destroy_cmd = ["molecule", "destroy"]
|
||||||
|
if scenario != "default":
|
||||||
|
destroy_cmd.extend(["-s", scenario])
|
||||||
|
with contextlib.suppress(subprocess.SubprocessError, OSError):
|
||||||
|
subprocess.run( # nosec B603, B607
|
||||||
|
destroy_cmd,
|
||||||
|
cwd=str(cwd),
|
||||||
|
env=env,
|
||||||
|
check=False,
|
||||||
|
capture_output=True,
|
||||||
|
timeout=120,
|
||||||
|
)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
with contextlib.suppress(ProcessLookupError):
|
with contextlib.suppress(ProcessLookupError):
|
||||||
os.killpg(os.getpgid(process.pid), signal.SIGTERM)
|
os.killpg(os.getpgid(process.pid), signal.SIGTERM)
|
||||||
process.wait()
|
process.wait()
|
||||||
|
# Clean up containers left behind by the interrupted test.
|
||||||
|
click.echo(_("Cleaning up: running molecule destroy for {scenario}", scenario=scenario))
|
||||||
|
destroy_cmd = ["molecule", "destroy"]
|
||||||
|
if scenario != "default":
|
||||||
|
destroy_cmd.extend(["-s", scenario])
|
||||||
|
with contextlib.suppress(subprocess.SubprocessError, OSError):
|
||||||
|
subprocess.run( # nosec B603, B607
|
||||||
|
destroy_cmd,
|
||||||
|
cwd=str(cwd),
|
||||||
|
env=env,
|
||||||
|
check=False,
|
||||||
|
capture_output=True,
|
||||||
|
timeout=120,
|
||||||
|
)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
rc = process.returncode
|
rc = process.returncode
|
||||||
|
|
||||||
if rc != 0:
|
if rc != 0:
|
||||||
click.echo(_("FAILED: {pair} exited with code {code}", pair=pair, code=rc))
|
click.echo(_("FAILED: {pair} exited with code {code}", pair=pair, code=rc))
|
||||||
|
# Run molecule destroy to clean up containers left behind by the
|
||||||
|
# failed test. Without this, containers stay running and accumulate
|
||||||
|
# on the runner, consuming disk/memory and degrading CI performance.
|
||||||
|
click.echo(_("Cleaning up: running molecule destroy for {scenario}", scenario=scenario))
|
||||||
|
destroy_cmd = ["molecule", "destroy"]
|
||||||
|
if scenario != "default":
|
||||||
|
destroy_cmd.extend(["-s", scenario])
|
||||||
|
with contextlib.suppress(subprocess.SubprocessError, OSError):
|
||||||
|
subprocess.run( # nosec B603, B607
|
||||||
|
destroy_cmd,
|
||||||
|
cwd=str(cwd),
|
||||||
|
env=env,
|
||||||
|
check=False,
|
||||||
|
capture_output=True,
|
||||||
|
timeout=120,
|
||||||
|
)
|
||||||
sys.exit(rc)
|
sys.exit(rc)
|
||||||
|
|
||||||
click.echo(_("PASSED: {pair}", pair=pair))
|
click.echo(_("PASSED: {pair}", pair=pair))
|
||||||
|
|||||||
+4009
-3523
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user