| 1 |
|
| 2 |
|
| 3 |
import json |
| 4 |
import os |
| 5 |
import unittest |
| 6 |
|
| 7 |
import requests |
| 8 |
|
| 9 |
from tests.integration.test_utils import RunSubprocessMixin |
| 10 |
from tests.integration.test_utils import podman_compose_path |
| 11 |
from tests.integration.test_utils import test_path |
| 12 |
|
| 13 |
|
| 14 |
def compose_yaml_path() -> str: |
| 15 |
return os.path.join(os.path.join(test_path(), "nets_test1"), "docker-compose.yml") |
| 16 |
|
| 17 |
|
| 18 |
class TestComposeNetsTest1(unittest.TestCase, RunSubprocessMixin): |
| 19 |
|
| 20 |
def test_nets_test1(self) -> None: |
| 21 |
try: |
| 22 |
self.run_subprocess_assert_returncode( |
| 23 |
[ |
| 24 |
podman_compose_path(), |
| 25 |
"-f", |
| 26 |
compose_yaml_path(), |
| 27 |
"up", |
| 28 |
"-d", |
| 29 |
], |
| 30 |
) |
| 31 |
output, _ = self.run_subprocess_assert_returncode([ |
| 32 |
podman_compose_path(), |
| 33 |
"-f", |
| 34 |
compose_yaml_path(), |
| 35 |
"ps", |
| 36 |
]) |
| 37 |
self.assertIn(b"nets_test1_web1_1", output) |
| 38 |
self.assertIn(b"nets_test1_web2_1", output) |
| 39 |
|
| 40 |
response = requests.get('http://localhost:8001/index.txt') |
| 41 |
self.assertTrue(response.ok) |
| 42 |
self.assertEqual(response.text, "test1\n") |
| 43 |
|
| 44 |
response = requests.get('http://localhost:8002/index.txt') |
| 45 |
self.assertTrue(response.ok) |
| 46 |
self.assertEqual(response.text, "test2\n") |
| 47 |
|
| 48 |
|
| 49 |
output, _ = self.run_subprocess_assert_returncode([ |
| 50 |
"podman", |
| 51 |
"inspect", |
| 52 |
"nets_test1_web1_1", |
| 53 |
]) |
| 54 |
container_info = json.loads(output.decode('utf-8'))[0] |
| 55 |
|
| 56 |
|
| 57 |
self.assertEqual( |
| 58 |
list(container_info["NetworkSettings"]["Networks"].keys())[0], "nets_test1_default" |
| 59 |
) |
| 60 |
|
| 61 |
|
| 62 |
self.assertIsNotNone(container_info['NetworkSettings']["Ports"].get("8001/tcp", None)) |
| 63 |
self.assertGreater(len(container_info['NetworkSettings']["Ports"]["8001/tcp"]), 0) |
| 64 |
self.assertIsNotNone( |
| 65 |
container_info['NetworkSettings']["Ports"]["8001/tcp"][0].get("HostPort", None) |
| 66 |
) |
| 67 |
self.assertEqual( |
| 68 |
container_info['NetworkSettings']["Ports"]["8001/tcp"][0]["HostPort"], "8001" |
| 69 |
) |
| 70 |
|
| 71 |
self.assertEqual(container_info["Config"]["Hostname"], "web1") |
| 72 |
|
| 73 |
|
| 74 |
output, _ = self.run_subprocess_assert_returncode([ |
| 75 |
"podman", |
| 76 |
"inspect", |
| 77 |
"nets_test1_web2_1", |
| 78 |
]) |
| 79 |
container_info = json.loads(output.decode('utf-8'))[0] |
| 80 |
self.assertEqual( |
| 81 |
list(container_info["NetworkSettings"]["Networks"].keys())[0], "nets_test1_default" |
| 82 |
) |
| 83 |
|
| 84 |
self.assertIsNotNone(container_info['NetworkSettings']["Ports"].get("8001/tcp", None)) |
| 85 |
self.assertGreater(len(container_info['NetworkSettings']["Ports"]["8001/tcp"]), 0) |
| 86 |
self.assertIsNotNone( |
| 87 |
container_info['NetworkSettings']["Ports"]["8001/tcp"][0].get("HostPort", None) |
| 88 |
) |
| 89 |
self.assertEqual( |
| 90 |
container_info['NetworkSettings']["Ports"]["8001/tcp"][0]["HostPort"], "8002" |
| 91 |
) |
| 92 |
|
| 93 |
self.assertEqual(container_info["Config"]["Hostname"], "web2") |
| 94 |
finally: |
| 95 |
self.run_subprocess_assert_returncode([ |
| 96 |
podman_compose_path(), |
| 97 |
"-f", |
| 98 |
compose_yaml_path(), |
| 99 |
"down", |
| 100 |
"-t", |
| 101 |
"0", |
| 102 |
]) |