tests: Print output in case subprocess returns unexpected code

Signed-off-by: Povilas Kanapickas <povilas@radix.lt>
This commit is contained in:
Povilas Kanapickas 2024-03-08 13:14:02 +02:00
parent b34f699adb
commit f4dc5f3b93
6 changed files with 75 additions and 94 deletions

View File

@ -1,19 +1,9 @@
# SPDX-License-Identifier: GPL-2.0 # SPDX-License-Identifier: GPL-2.0
from pathlib import Path from pathlib import Path
import subprocess
import os import os
import unittest import unittest
from .test_utils import RunSubprocessMixin
def run_subprocess(command):
proc = subprocess.Popen(
command,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
out, err = proc.communicate()
return out, err, proc.returncode
def base_path(): def base_path():
@ -31,7 +21,7 @@ def podman_compose_path():
return os.path.join(base_path(), "podman_compose.py") return os.path.join(base_path(), "podman_compose.py")
class TestPodmanCompose(unittest.TestCase): class TestPodmanCompose(unittest.TestCase, RunSubprocessMixin):
def test_extends_w_file_subdir(self): def test_extends_w_file_subdir(self):
""" """
Test that podman-compose can execute podman-compose -f <file> up with extended File which Test that podman-compose can execute podman-compose -f <file> up with extended File which
@ -69,18 +59,14 @@ class TestPodmanCompose(unittest.TestCase):
"docker.io/library/busybox", "docker.io/library/busybox",
] ]
out, _, returncode = run_subprocess(command_up) self.run_subprocess_assert_returncode(command_up)
self.assertEqual(returncode, 0)
# check container was created and exists # check container was created and exists
out, err, returncode = run_subprocess(command_check_container) out, _ = self.run_subprocess_assert_returncode(command_check_container)
self.assertEqual(returncode, 0)
self.assertEqual(out, b'localhost/subdir_test:me\n') self.assertEqual(out, b'localhost/subdir_test:me\n')
out, _, returncode = run_subprocess(command_down)
# cleanup test image(tags) # cleanup test image(tags)
self.assertEqual(returncode, 0) self.run_subprocess_assert_returncode(command_down)
# check container did not exists anymore # check container did not exists anymore
out, _, returncode = run_subprocess(command_check_container) out, _ = self.run_subprocess_assert_returncode(command_check_container)
self.assertEqual(returncode, 0)
self.assertEqual(out, b'') self.assertEqual(out, b'')
def test_extends_w_empty_service(self): def test_extends_w_empty_service(self):
@ -100,5 +86,4 @@ class TestPodmanCompose(unittest.TestCase):
"-d", "-d",
] ]
_, _, returncode = run_subprocess(command_up) self.run_subprocess_assert_returncode(command_up)
self.assertEqual(returncode, 0)

View File

@ -9,8 +9,8 @@ Tests the podman-compose config command which is used to return defined compose
# pylint: disable=redefined-outer-name # pylint: disable=redefined-outer-name
import os import os
from .test_podman_compose import podman_compose_path from .test_podman_compose import podman_compose_path
from .test_podman_compose import run_subprocess
from .test_podman_compose import test_path from .test_podman_compose import test_path
from .test_utils import RunSubprocessMixin
import unittest import unittest
from parameterized import parameterized from parameterized import parameterized
@ -20,7 +20,7 @@ def profile_compose_file():
return os.path.join(test_path(), "profile", "docker-compose.yml") return os.path.join(test_path(), "profile", "docker-compose.yml")
class TestComposeConfig(unittest.TestCase): class TestComposeConfig(unittest.TestCase, RunSubprocessMixin):
def test_config_no_profiles(self): def test_config_no_profiles(self):
""" """
Tests podman-compose config command without profile enablement. Tests podman-compose config command without profile enablement.
@ -34,8 +34,7 @@ class TestComposeConfig(unittest.TestCase):
"config", "config",
] ]
out, _, return_code = run_subprocess(config_cmd) out, _ = self.run_subprocess_assert_returncode(config_cmd)
self.assertEqual(return_code, 0)
string_output = out.decode("utf-8") string_output = out.decode("utf-8")
self.assertIn("default-service", string_output) self.assertIn("default-service", string_output)
@ -68,8 +67,7 @@ class TestComposeConfig(unittest.TestCase):
config_cmd = ["coverage", "run", podman_compose_path(), "-f", profile_compose_file()] config_cmd = ["coverage", "run", podman_compose_path(), "-f", profile_compose_file()]
config_cmd.extend(profiles) config_cmd.extend(profiles)
out, _, return_code = run_subprocess(config_cmd) out, _ = self.run_subprocess_assert_returncode(config_cmd)
self.assertEqual(return_code, 0)
actual_output = out.decode("utf-8") actual_output = out.decode("utf-8")

View File

@ -4,18 +4,10 @@ from pathlib import Path
import subprocess import subprocess
import unittest import unittest
from .test_utils import RunSubprocessMixin
def run_subprocess(command):
proc = subprocess.Popen(
command,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
out, err = proc.communicate()
return out, err, proc.returncode
class TestPodmanComposeInclude(unittest.TestCase): class TestPodmanComposeInclude(unittest.TestCase, RunSubprocessMixin):
def test_podman_compose_include(self): def test_podman_compose_include(self):
""" """
Test that podman-compose can execute podman-compose -f <file> up with include Test that podman-compose can execute podman-compose -f <file> up with include
@ -55,22 +47,17 @@ class TestPodmanComposeInclude(unittest.TestCase):
command_down = ["podman", "rm", "--force", "CONTAINER_ID"] command_down = ["podman", "rm", "--force", "CONTAINER_ID"]
out, _, returncode = run_subprocess(command_up) self.run_subprocess_assert_returncode(command_up)
self.assertEqual(returncode, 0) out, _ = self.run_subprocess_assert_returncode(command_check_container)
out, _, returncode = run_subprocess(command_check_container)
self.assertEqual(returncode, 0)
self.assertEqual(out, b'"docker.io/library/busybox:latest"\n') self.assertEqual(out, b'"docker.io/library/busybox:latest"\n')
# Get container ID to remove it # Get container ID to remove it
out, _, returncode = run_subprocess(command_container_id) out, _ = self.run_subprocess_assert_returncode(command_container_id)
self.assertEqual(returncode, 0)
self.assertNotEqual(out, b"") self.assertNotEqual(out, b"")
container_id = out.decode().strip().replace('"', "") container_id = out.decode().strip().replace('"', "")
command_down[3] = container_id command_down[3] = container_id
out, _, returncode = run_subprocess(command_down) out, _ = self.run_subprocess_assert_returncode(command_down)
# cleanup test image(tags) # cleanup test image(tags)
self.assertEqual(returncode, 0)
self.assertNotEqual(out, b"") self.assertNotEqual(out, b"")
# check container did not exists anymore # check container did not exists anymore
out, _, returncode = run_subprocess(command_check_container) out, _ = self.run_subprocess_assert_returncode(command_check_container)
self.assertEqual(returncode, 0)
self.assertEqual(out, b"") self.assertEqual(out, b"")

View File

@ -11,12 +11,12 @@ import os
import time import time
import unittest import unittest
from .test_podman_compose import run_subprocess
from .test_podman_compose import podman_compose_path from .test_podman_compose import podman_compose_path
from .test_podman_compose import test_path from .test_podman_compose import test_path
from .test_utils import RunSubprocessMixin
class TestPodmanCompose(unittest.TestCase): class TestPodmanCompose(unittest.TestCase, RunSubprocessMixin):
def test_exit_from(self): def test_exit_from(self):
up_cmd = [ up_cmd = [
"coverage", "coverage",
@ -27,11 +27,8 @@ class TestPodmanCompose(unittest.TestCase):
"up", "up",
] ]
out, _, return_code = run_subprocess(up_cmd + ["--exit-code-from", "sh1"]) self.run_subprocess_assert_returncode(up_cmd + ["--exit-code-from", "sh1"], 1)
self.assertEqual(return_code, 1) self.run_subprocess_assert_returncode(up_cmd + ["--exit-code-from", "sh2"], 2)
out, _, return_code = run_subprocess(up_cmd + ["--exit-code-from", "sh2"])
self.assertEqual(return_code, 2)
def test_run(self): def test_run(self):
""" """
@ -51,7 +48,7 @@ class TestPodmanCompose(unittest.TestCase):
"wget -q -O - http://web:8000/hosts", "wget -q -O - http://web:8000/hosts",
] ]
out, _, return_code = run_subprocess(run_cmd) out, _ = self.run_subprocess_assert_returncode(run_cmd)
self.assertIn(b'127.0.0.1\tlocalhost', out) self.assertIn(b'127.0.0.1\tlocalhost', out)
# Run it again to make sure we can run it twice. I saw an issue where a second run, with the container left up, # Run it again to make sure we can run it twice. I saw an issue where a second run, with the container left up,
@ -70,9 +67,8 @@ class TestPodmanCompose(unittest.TestCase):
"wget -q -O - http://web:8000/hosts", "wget -q -O - http://web:8000/hosts",
] ]
out, _, return_code = run_subprocess(run_cmd) out, _ = self.run_subprocess_assert_returncode(run_cmd)
assert b'127.0.0.1\tlocalhost' in out self.assertIn(b'127.0.0.1\tlocalhost', out)
self.assertEqual(return_code, 0)
# This leaves a container running. Not sure it's intended, but it matches docker-compose # This leaves a container running. Not sure it's intended, but it matches docker-compose
down_cmd = [ down_cmd = [
@ -84,8 +80,7 @@ class TestPodmanCompose(unittest.TestCase):
"down", "down",
] ]
out, _, return_code = run_subprocess(run_cmd) self.run_subprocess_assert_returncode(down_cmd)
self.assertEqual(return_code, 0)
def test_up_with_ports(self): def test_up_with_ports(self):
up_cmd = [ up_cmd = [
@ -110,12 +105,10 @@ class TestPodmanCompose(unittest.TestCase):
] ]
try: try:
out, _, return_code = run_subprocess(up_cmd) self.run_subprocess_assert_returncode(up_cmd)
self.assertEqual(return_code, 0)
finally: finally:
out, _, return_code = run_subprocess(down_cmd) self.run_subprocess_assert_returncode(down_cmd)
self.assertEqual(return_code, 0)
def test_down_with_vols(self): def test_down_with_vols(self):
up_cmd = [ up_cmd = [
@ -139,29 +132,25 @@ class TestPodmanCompose(unittest.TestCase):
] ]
try: try:
out, _, return_code = run_subprocess(["podman", "volume", "create", "my-app-data"]) self.run_subprocess_assert_returncode(["podman", "volume", "create", "my-app-data"])
self.assertEqual(return_code, 0) self.run_subprocess_assert_returncode([
out, _, return_code = run_subprocess([
"podman", "podman",
"volume", "volume",
"create", "create",
"actual-name-of-volume", "actual-name-of-volume",
]) ])
self.assertEqual(return_code, 0)
out, _, return_code = run_subprocess(up_cmd) self.run_subprocess_assert_returncode(up_cmd)
self.assertEqual(return_code, 0) self.run_subprocess(["podman", "inspect", "volume", ""])
run_subprocess(["podman", "inspect", "volume", ""])
finally: finally:
out, _, return_code = run_subprocess(down_cmd) out, _, return_code = self.run_subprocess(down_cmd)
run_subprocess(["podman", "volume", "rm", "my-app-data"]) self.run_subprocess(["podman", "volume", "rm", "my-app-data"])
run_subprocess(["podman", "volume", "rm", "actual-name-of-volume"]) self.run_subprocess(["podman", "volume", "rm", "actual-name-of-volume"])
self.assertEqual(return_code, 0) self.assertEqual(return_code, 0)
def test_down_with_orphans(self): def test_down_with_orphans(self):
container_id, _, return_code = run_subprocess([ container_id, _ = self.run_subprocess_assert_returncode([
"podman", "podman",
"run", "run",
"--rm", "--rm",
@ -187,14 +176,14 @@ class TestPodmanCompose(unittest.TestCase):
"--remove-orphans", "--remove-orphans",
] ]
out, _, return_code = run_subprocess(down_cmd) self.run_subprocess_assert_returncode(down_cmd)
self.assertEqual(return_code, 0)
_, _, exists = run_subprocess([ self.run_subprocess_assert_returncode(
[
"podman", "podman",
"container", "container",
"exists", "exists",
container_id.decode("utf-8"), container_id.decode("utf-8"),
]) ],
1,
self.assertEqual(exists, 1) )

View File

@ -8,9 +8,9 @@ Tests the podman compose up and down commands used to create and remove services
# pylint: disable=redefined-outer-name # pylint: disable=redefined-outer-name
import os import os
from .test_podman_compose import run_subprocess
from .test_podman_compose import podman_compose_path from .test_podman_compose import podman_compose_path
from .test_podman_compose import test_path from .test_podman_compose import test_path
from .test_utils import RunSubprocessMixin
from parameterized import parameterized from parameterized import parameterized
import unittest import unittest
@ -20,7 +20,7 @@ def profile_compose_file():
return os.path.join(test_path(), "profile", "docker-compose.yml") return os.path.join(test_path(), "profile", "docker-compose.yml")
class TestUpDown(unittest.TestCase): class TestUpDown(unittest.TestCase, RunSubprocessMixin):
def tearDown(self): def tearDown(self):
""" """
Ensures that the services within the "profile compose file" are removed between each test case. Ensures that the services within the "profile compose file" are removed between each test case.
@ -39,7 +39,7 @@ class TestUpDown(unittest.TestCase):
profile_compose_file(), profile_compose_file(),
"down", "down",
] ]
run_subprocess(down_cmd) self.run_subprocess(down_cmd)
@parameterized.expand( @parameterized.expand(
[ [
@ -67,8 +67,7 @@ class TestUpDown(unittest.TestCase):
] ]
up_cmd.extend(profiles) up_cmd.extend(profiles)
out, _, return_code = run_subprocess(up_cmd) self.run_subprocess_assert_returncode(up_cmd)
self.assertEqual(return_code, 0)
check_cmd = [ check_cmd = [
"podman", "podman",
@ -77,8 +76,7 @@ class TestUpDown(unittest.TestCase):
"--format", "--format",
'"{{.Names}}"', '"{{.Names}}"',
] ]
out, _, return_code = run_subprocess(check_cmd) out, _ = self.run_subprocess_assert_returncode(check_cmd)
self.assertEqual(return_code, 0)
self.assertEqual(len(expected_services), 3) self.assertEqual(len(expected_services), 3)
actual_output = out.decode("utf-8") actual_output = out.decode("utf-8")

24
tests/test_utils.py Normal file
View File

@ -0,0 +1,24 @@
# SPDX-License-Identifier: GPL-2.0
import subprocess
class RunSubprocessMixin:
def run_subprocess(self, args):
proc = subprocess.Popen(
args,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
out, err = proc.communicate()
return out, err, proc.returncode
def run_subprocess_assert_returncode(self, args, expected_returncode=0):
out, err, returncode = self.run_subprocess(args)
self.assertEqual(
returncode,
expected_returncode,
f"Invalid return code of process {returncode} != {expected_returncode}\n"
f"stdout: {out}\nstderr: {err}\n",
)
return out, err