| 1 |
|
| 2 |
|
| 3 |
""" |
| 4 |
test_podman_compose_networks.py |
| 5 |
|
| 6 |
Tests the podman networking parameters |
| 7 |
""" |
| 8 |
|
| 9 |
|
| 10 |
import os |
| 11 |
import unittest |
| 12 |
from typing import Generator |
| 13 |
|
| 14 |
from packaging import version |
| 15 |
|
| 16 |
from tests.integration.test_utils import RunSubprocessMixin |
| 17 |
from tests.integration.test_utils import get_podman_version |
| 18 |
from tests.integration.test_utils import podman_compose_path |
| 19 |
from tests.integration.test_utils import test_path |
| 20 |
|
| 21 |
|
| 22 |
class TestPodmanComposeNetwork(RunSubprocessMixin, unittest.TestCase): |
| 23 |
@staticmethod |
| 24 |
def compose_file() -> str: |
| 25 |
"""Returns the path to the compose file used for this test module""" |
| 26 |
return os.path.join(test_path(), "nets_test_ip", "docker-compose.yml") |
| 27 |
|
| 28 |
def teardown(self) -> Generator[None, None, None]: |
| 29 |
""" |
| 30 |
Ensures that the services within the "profile compose file" are removed between |
| 31 |
each test case. |
| 32 |
""" |
| 33 |
|
| 34 |
yield |
| 35 |
|
| 36 |
down_cmd = [ |
| 37 |
"coverage", |
| 38 |
"run", |
| 39 |
podman_compose_path(), |
| 40 |
"-f", |
| 41 |
self.compose_file(), |
| 42 |
"kill", |
| 43 |
"-a", |
| 44 |
] |
| 45 |
self.run_subprocess(down_cmd) |
| 46 |
|
| 47 |
@unittest.skipIf(get_podman_version() >= version.parse("5.0.0"), "Breaks as of podman-5.4.2.") |
| 48 |
def test_networks(self) -> None: |
| 49 |
up_cmd = [ |
| 50 |
"coverage", |
| 51 |
"run", |
| 52 |
podman_compose_path(), |
| 53 |
"-f", |
| 54 |
self.compose_file(), |
| 55 |
"up", |
| 56 |
"-d", |
| 57 |
"--force-recreate", |
| 58 |
] |
| 59 |
|
| 60 |
self.run_subprocess_assert_returncode(up_cmd) |
| 61 |
|
| 62 |
check_cmd = [ |
| 63 |
podman_compose_path(), |
| 64 |
"-f", |
| 65 |
self.compose_file(), |
| 66 |
"ps", |
| 67 |
"--format", |
| 68 |
'"{{.Names}}"', |
| 69 |
] |
| 70 |
out, _ = self.run_subprocess_assert_returncode(check_cmd) |
| 71 |
self.assertIn(b"nets_test_ip_web1_1", out) |
| 72 |
self.assertIn(b"nets_test_ip_web2_1", out) |
| 73 |
|
| 74 |
expected_wget = { |
| 75 |
"172.19.1.10": "test1", |
| 76 |
"172.19.2.10": "test1", |
| 77 |
"172.19.2.11": "test2", |
| 78 |
"web3": "test3", |
| 79 |
"172.19.1.13": "test4", |
| 80 |
} |
| 81 |
|
| 82 |
for service in ("web1", "web2"): |
| 83 |
for ip, expect in expected_wget.items(): |
| 84 |
wget_cmd = [ |
| 85 |
podman_compose_path(), |
| 86 |
"-f", |
| 87 |
self.compose_file(), |
| 88 |
"exec", |
| 89 |
service, |
| 90 |
"wget", |
| 91 |
"-q", |
| 92 |
"-O-", |
| 93 |
f"http://{ip}:8001/index.txt", |
| 94 |
] |
| 95 |
out, _ = self.run_subprocess_assert_returncode(wget_cmd) |
| 96 |
self.assertEqual(f"{expect}\r\n", out.decode('utf-8')) |
| 97 |
|
| 98 |
expected_macip = { |
| 99 |
"web1": { |
| 100 |
"eth0": ["172.19.1.10", "02:01:01:00:01:01"], |
| 101 |
"eth1": ["172.19.2.10", "02:01:01:00:02:01"], |
| 102 |
}, |
| 103 |
"web2": {"eth0": ["172.19.2.11", "02:01:01:00:02:02"]}, |
| 104 |
} |
| 105 |
|
| 106 |
for service, interfaces in expected_macip.items(): |
| 107 |
ip_cmd = [ |
| 108 |
podman_compose_path(), |
| 109 |
"-f", |
| 110 |
self.compose_file(), |
| 111 |
"exec", |
| 112 |
service, |
| 113 |
"ip", |
| 114 |
"addr", |
| 115 |
"show", |
| 116 |
] |
| 117 |
out, _ = self.run_subprocess_assert_returncode(ip_cmd) |
| 118 |
for interface, values in interfaces.items(): |
| 119 |
ip, mac = values |
| 120 |
self.assertIn(f"ether {mac}", out.decode('utf-8')) |
| 121 |
self.assertIn(f"inet {ip}/", out.decode('utf-8')) |
| 122 |
|
| 123 |
def test_down_with_network(self) -> None: |
| 124 |
try: |
| 125 |
self.run_subprocess_assert_returncode([ |
| 126 |
"coverage", |
| 127 |
"run", |
| 128 |
podman_compose_path(), |
| 129 |
"-f", |
| 130 |
os.path.join(test_path(), "network", "docker-compose.yml"), |
| 131 |
"up", |
| 132 |
"-d", |
| 133 |
]) |
| 134 |
output, _, _ = self.run_subprocess(["podman", "network", "ls"]) |
| 135 |
self.assertIn("network_mystack", output.decode()) |
| 136 |
finally: |
| 137 |
self.run_subprocess_assert_returncode([ |
| 138 |
"coverage", |
| 139 |
"run", |
| 140 |
podman_compose_path(), |
| 141 |
"-f", |
| 142 |
os.path.join(test_path(), "network", "docker-compose.yml"), |
| 143 |
"down", |
| 144 |
]) |
| 145 |
output, _, _ = self.run_subprocess(["podman", "network", "ls"]) |
| 146 |
self.assertNotIn("network_mystack", output.decode()) |