66 lines
2.7 KiB
Python
66 lines
2.7 KiB
Python
"""Regression checks for stress-container cleanup on failure and interruption."""
|
|
|
|
import contextlib
|
|
import importlib.util
|
|
import io
|
|
from pathlib import Path
|
|
import signal
|
|
import subprocess
|
|
import sys
|
|
import unittest
|
|
from unittest.mock import patch
|
|
|
|
|
|
spec = importlib.util.spec_from_file_location("http2Runner", Path(__file__).with_name("verifyHTTP2.py"))
|
|
runner = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(runner)
|
|
|
|
|
|
class CleanupTests(unittest.TestCase):
|
|
def testFailedRunsCleanUpOnlyTheirOwnContainer(self):
|
|
failures = (
|
|
subprocess.TimeoutExpired("docker", 1),
|
|
KeyboardInterrupt(),
|
|
subprocess.CompletedProcess([], 7),
|
|
)
|
|
for failure in failures:
|
|
with self.subTest(failure=type(failure).__name__):
|
|
calls = []
|
|
original_handlers = {sig: signal.getsignal(sig) for sig in (signal.SIGTERM, signal.SIGHUP)}
|
|
|
|
def execute(command, **kwargs):
|
|
calls.append(command)
|
|
if command[1] == "run":
|
|
if isinstance(failure, BaseException):
|
|
raise failure
|
|
return failure
|
|
return subprocess.CompletedProcess(command, 0, "", "")
|
|
|
|
with patch.object(sys, "argv", ["verifyHTTP2.py", "--duration", "1s"]), \
|
|
patch.object(runner.shutil, "which", return_value="/fake/docker"), \
|
|
patch.object(runner.subprocess, "run", side_effect=execute), \
|
|
contextlib.redirect_stdout(io.StringIO()):
|
|
with self.assertRaises(SystemExit) as raised:
|
|
runner.main()
|
|
self.assertNotEqual(raised.exception.code, 0)
|
|
name = calls[0][calls[0].index("--name") + 1]
|
|
self.assertEqual(calls[1:], [["/fake/docker", "rm", "--force", name]])
|
|
self.assertEqual(original_handlers, {sig: signal.getsignal(sig) for sig in original_handlers})
|
|
|
|
def testCleanupFailureIsNotReportedAsPass(self):
|
|
results = [
|
|
subprocess.CompletedProcess([], 0),
|
|
subprocess.CompletedProcess([], 1, "", "Cannot connect to the Docker daemon"),
|
|
]
|
|
with patch.object(sys, "argv", ["verifyHTTP2.py", "--duration", "1s"]), \
|
|
patch.object(runner.shutil, "which", return_value="/fake/docker"), \
|
|
patch.object(runner.subprocess, "run", side_effect=results), \
|
|
contextlib.redirect_stdout(io.StringIO()) as output:
|
|
with self.assertRaisesRegex(SystemExit, "Could not clean up"):
|
|
runner.main()
|
|
self.assertNotIn("PASS", output.getvalue())
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|