Skip to content

Commit d84fe4c

Browse files
committed
tests: Use PodmanCommand in integration tests
Use the PodmanCommand in integration tests, and more specifically its ability to start a REST API service. As a result, we can remove the previous code that did this. Signed-off-by: Alex Pyrgiotis <[email protected]>
1 parent 4d88ea8 commit d84fe4c

File tree

2 files changed

+24
-67
lines changed

2 files changed

+24
-67
lines changed

podman/tests/errors.py

Lines changed: 0 additions & 19 deletions
This file was deleted.

podman/tests/integration/utils.py

Lines changed: 24 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
import time
2626

27-
from podman.tests import errors
27+
from podman import PodmanCommand
2828

2929
logger = logging.getLogger("podman.service")
3030

@@ -41,54 +41,27 @@ def __init__(
4141
log_level: str = "WARNING",
4242
) -> None:
4343
"""create a launcher and build podman command"""
44-
podman_exe: str = podman_path
45-
if not podman_exe:
46-
podman_exe = shutil.which('podman')
47-
if podman_exe is None:
48-
raise errors.PodmanNotInstalled()
44+
self.podman = PodmanCommand(path=podman_path, privileged=privileged)
4945

46+
self.timeout = timeout
47+
self.socket_uri: str = socket_uri
5048
self.socket_file: str = socket_uri.replace('unix://', '')
5149
self.log_level = log_level
5250

5351
self.proc: Optional[subprocess.Popen[bytes]] = None
5452
self.reference_id = hash(time.monotonic())
5553

56-
self.cmd: list[str] = []
57-
if privileged:
58-
self.cmd.append('sudo')
59-
60-
self.cmd.append(podman_exe)
61-
6254
logger.setLevel(logging.getLevelName(log_level))
6355

6456
# Map from python to go logging levels, FYI trace level breaks cirrus logging
65-
self.cmd.append(f"--log-level={log_level.lower()}")
66-
57+
self.podman.options.log_level = log_level.lower()
6758
if os.environ.get("container") == "oci":
68-
self.cmd.append("--storage-driver=vfs")
69-
70-
self.cmd.extend(
71-
[
72-
"system",
73-
"service",
74-
f"--time={timeout}",
75-
socket_uri,
76-
]
77-
)
59+
self.podman.options.storage_driver = "vfs"
7860

79-
process = subprocess.run(
80-
[podman_exe, "--version"], check=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT
81-
)
82-
self.version = str(process.stdout.decode("utf-8")).strip().split()[2]
61+
self.version = self.podman.run(["--version"]).split()[2]
8362

8463
def start(self, check_socket=True) -> None:
8564
"""start podman service"""
86-
logger.info(
87-
"Launching(%s) %s refid=%s",
88-
self.version,
89-
' '.join(self.cmd),
90-
self.reference_id,
91-
)
9265

9366
def consume_lines(pipe, consume_fn):
9467
with pipe:
@@ -98,32 +71,35 @@ def consume_lines(pipe, consume_fn):
9871
def consume(line: str):
9972
logger.debug(line.strip("\n") + f" refid={self.reference_id}")
10073

101-
self.proc = subprocess.Popen(self.cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) # pylint: disable=consider-using-with
74+
self.proc = self.podman.start_service(
75+
self.socket_uri,
76+
timeout=self.timeout,
77+
stdout=subprocess.PIPE,
78+
stderr=subprocess.STDOUT,
79+
)
80+
81+
logger.info(
82+
"Launched(%s) %s pid=%s refid=%s",
83+
self.version,
84+
' '.join(self.proc.args),
85+
self.proc.pid,
86+
self.reference_id,
87+
)
88+
10289
threading.Thread(target=consume_lines, args=[self.proc.stdout, consume]).start()
10390

10491
if not check_socket:
10592
return
10693

10794
# wait for socket to be created
108-
timeout = time.monotonic() + 30
109-
while not os.path.exists(self.socket_file):
110-
if time.monotonic() > timeout:
111-
raise subprocess.TimeoutExpired("podman service ", timeout)
112-
time.sleep(0.2)
95+
self.podman.wait_for_service(self.socket_uri, self.proc, timeout=30)
11396

11497
def stop(self) -> None:
11598
"""stop podman service"""
11699
if not self.proc:
117100
return
118101

119-
self.proc.terminate()
120-
try:
121-
return_code = self.proc.wait(timeout=15)
122-
except subprocess.TimeoutExpired:
123-
self.proc.kill()
124-
return_code = self.proc.wait()
125-
self.proc = None
126-
102+
return_code = self.podman.stop_service(self.proc, timeout=15)
127103
with suppress(FileNotFoundError):
128104
os.remove(self.socket_file)
129105

0 commit comments

Comments
 (0)