2024-03-08 11:19:12 +01:00
|
|
|
# SPDX-License-Identifier: GPL-2.0
|
|
|
|
|
2023-12-11 08:46:20 +01:00
|
|
|
"""
|
|
|
|
test_podman_compose_up_down.py
|
|
|
|
|
|
|
|
Tests the podman compose up and down commands used to create and remove services.
|
|
|
|
"""
|
2024-03-07 17:28:06 +01:00
|
|
|
|
2023-12-11 08:46:20 +01:00
|
|
|
# pylint: disable=redefined-outer-name
|
|
|
|
import os
|
2024-03-08 10:00:34 +01:00
|
|
|
import unittest
|
|
|
|
|
|
|
|
from .test_podman_compose import podman_compose_path
|
|
|
|
from .test_podman_compose import test_path
|
2024-03-08 12:14:02 +01:00
|
|
|
from .test_utils import RunSubprocessMixin
|
2024-03-08 10:00:34 +01:00
|
|
|
|
|
|
|
|
2024-03-08 12:14:02 +01:00
|
|
|
class TestPodmanCompose(unittest.TestCase, RunSubprocessMixin):
|
2024-03-08 10:00:34 +01:00
|
|
|
def test_exit_from(self):
|
|
|
|
up_cmd = [
|
|
|
|
"coverage",
|
|
|
|
"run",
|
|
|
|
podman_compose_path(),
|
|
|
|
"-f",
|
|
|
|
os.path.join(test_path(), "exit-from", "docker-compose.yaml"),
|
|
|
|
"up",
|
|
|
|
]
|
|
|
|
|
2024-03-08 12:14:02 +01:00
|
|
|
self.run_subprocess_assert_returncode(up_cmd + ["--exit-code-from", "sh1"], 1)
|
|
|
|
self.run_subprocess_assert_returncode(up_cmd + ["--exit-code-from", "sh2"], 2)
|
2024-03-08 10:00:34 +01:00
|
|
|
|
|
|
|
def test_run(self):
|
|
|
|
"""
|
|
|
|
This will test depends_on as well
|
|
|
|
"""
|
|
|
|
run_cmd = [
|
|
|
|
"coverage",
|
|
|
|
"run",
|
|
|
|
podman_compose_path(),
|
|
|
|
"-f",
|
|
|
|
os.path.join(test_path(), "deps", "docker-compose.yaml"),
|
|
|
|
"run",
|
|
|
|
"--rm",
|
|
|
|
"sleep",
|
|
|
|
"/bin/sh",
|
|
|
|
"-c",
|
|
|
|
"wget -q -O - http://web:8000/hosts",
|
|
|
|
]
|
|
|
|
|
2024-03-08 12:14:02 +01:00
|
|
|
out, _ = self.run_subprocess_assert_returncode(run_cmd)
|
2024-03-08 10:00:34 +01:00
|
|
|
self.assertIn(b'127.0.0.1\tlocalhost', out)
|
|
|
|
|
2024-03-08 22:46:24 +01:00
|
|
|
# Run it again to make sure we can run it twice. I saw an issue where a second run, with
|
|
|
|
# the container left up, would fail
|
2024-03-08 10:00:34 +01:00
|
|
|
run_cmd = [
|
|
|
|
"coverage",
|
|
|
|
"run",
|
|
|
|
podman_compose_path(),
|
|
|
|
"-f",
|
|
|
|
os.path.join(test_path(), "deps", "docker-compose.yaml"),
|
|
|
|
"run",
|
|
|
|
"--rm",
|
|
|
|
"sleep",
|
|
|
|
"/bin/sh",
|
|
|
|
"-c",
|
|
|
|
"wget -q -O - http://web:8000/hosts",
|
|
|
|
]
|
|
|
|
|
2024-03-08 12:14:02 +01:00
|
|
|
out, _ = self.run_subprocess_assert_returncode(run_cmd)
|
|
|
|
self.assertIn(b'127.0.0.1\tlocalhost', out)
|
2024-03-08 10:00:34 +01:00
|
|
|
|
|
|
|
# This leaves a container running. Not sure it's intended, but it matches docker-compose
|
|
|
|
down_cmd = [
|
|
|
|
"coverage",
|
|
|
|
"run",
|
|
|
|
podman_compose_path(),
|
|
|
|
"-f",
|
|
|
|
os.path.join(test_path(), "deps", "docker-compose.yaml"),
|
|
|
|
"down",
|
|
|
|
]
|
|
|
|
|
2024-03-08 12:14:02 +01:00
|
|
|
self.run_subprocess_assert_returncode(down_cmd)
|
2024-03-08 10:00:34 +01:00
|
|
|
|
|
|
|
def test_up_with_ports(self):
|
|
|
|
up_cmd = [
|
|
|
|
"coverage",
|
|
|
|
"run",
|
|
|
|
podman_compose_path(),
|
|
|
|
"-f",
|
|
|
|
os.path.join(test_path(), "ports", "docker-compose.yml"),
|
|
|
|
"up",
|
|
|
|
"-d",
|
|
|
|
"--force-recreate",
|
|
|
|
]
|
|
|
|
|
|
|
|
down_cmd = [
|
|
|
|
"coverage",
|
|
|
|
"run",
|
|
|
|
podman_compose_path(),
|
|
|
|
"-f",
|
|
|
|
os.path.join(test_path(), "ports", "docker-compose.yml"),
|
|
|
|
"down",
|
|
|
|
"--volumes",
|
|
|
|
]
|
|
|
|
|
|
|
|
try:
|
2024-03-08 12:14:02 +01:00
|
|
|
self.run_subprocess_assert_returncode(up_cmd)
|
2024-03-08 10:00:34 +01:00
|
|
|
|
|
|
|
finally:
|
2024-03-08 12:14:02 +01:00
|
|
|
self.run_subprocess_assert_returncode(down_cmd)
|
2024-03-08 10:00:34 +01:00
|
|
|
|
|
|
|
def test_down_with_vols(self):
|
|
|
|
up_cmd = [
|
|
|
|
"coverage",
|
|
|
|
"run",
|
|
|
|
podman_compose_path(),
|
|
|
|
"-f",
|
|
|
|
os.path.join(test_path(), "vol", "docker-compose.yaml"),
|
|
|
|
"up",
|
|
|
|
"-d",
|
|
|
|
]
|
|
|
|
|
|
|
|
down_cmd = [
|
|
|
|
"coverage",
|
|
|
|
"run",
|
|
|
|
podman_compose_path(),
|
|
|
|
"-f",
|
|
|
|
os.path.join(test_path(), "vol", "docker-compose.yaml"),
|
|
|
|
"down",
|
|
|
|
"--volumes",
|
|
|
|
]
|
|
|
|
|
|
|
|
try:
|
2024-03-08 12:14:02 +01:00
|
|
|
self.run_subprocess_assert_returncode(["podman", "volume", "create", "my-app-data"])
|
|
|
|
self.run_subprocess_assert_returncode([
|
2024-03-08 10:00:34 +01:00
|
|
|
"podman",
|
|
|
|
"volume",
|
|
|
|
"create",
|
|
|
|
"actual-name-of-volume",
|
|
|
|
])
|
|
|
|
|
2024-03-08 12:14:02 +01:00
|
|
|
self.run_subprocess_assert_returncode(up_cmd)
|
|
|
|
self.run_subprocess(["podman", "inspect", "volume", ""])
|
2024-03-08 10:00:34 +01:00
|
|
|
|
|
|
|
finally:
|
2024-03-08 12:14:02 +01:00
|
|
|
out, _, return_code = self.run_subprocess(down_cmd)
|
|
|
|
self.run_subprocess(["podman", "volume", "rm", "my-app-data"])
|
|
|
|
self.run_subprocess(["podman", "volume", "rm", "actual-name-of-volume"])
|
2024-03-08 10:00:34 +01:00
|
|
|
self.assertEqual(return_code, 0)
|
|
|
|
|
|
|
|
def test_down_with_orphans(self):
|
2024-03-08 12:14:02 +01:00
|
|
|
container_id, _ = self.run_subprocess_assert_returncode([
|
2024-03-08 10:00:34 +01:00
|
|
|
"podman",
|
|
|
|
"run",
|
|
|
|
"--rm",
|
|
|
|
"-d",
|
2024-03-08 13:45:22 +01:00
|
|
|
"nopush/podman-compose-test",
|
|
|
|
"dumb-init",
|
2024-03-08 10:00:34 +01:00
|
|
|
"/bin/busybox",
|
|
|
|
"httpd",
|
|
|
|
"-f",
|
|
|
|
"-h",
|
|
|
|
"/etc/",
|
|
|
|
"-p",
|
|
|
|
"8000",
|
|
|
|
])
|
|
|
|
|
|
|
|
down_cmd = [
|
|
|
|
"coverage",
|
|
|
|
"run",
|
|
|
|
podman_compose_path(),
|
|
|
|
"-f",
|
|
|
|
os.path.join(test_path(), "ports", "docker-compose.yml"),
|
|
|
|
"down",
|
|
|
|
"--volumes",
|
|
|
|
"--remove-orphans",
|
|
|
|
]
|
|
|
|
|
2024-03-08 12:14:02 +01:00
|
|
|
self.run_subprocess_assert_returncode(down_cmd)
|
2024-03-08 10:00:34 +01:00
|
|
|
|
2024-03-08 12:14:02 +01:00
|
|
|
self.run_subprocess_assert_returncode(
|
|
|
|
[
|
|
|
|
"podman",
|
|
|
|
"container",
|
|
|
|
"exists",
|
|
|
|
container_id.decode("utf-8"),
|
|
|
|
],
|
|
|
|
1,
|
|
|
|
)
|