|
| 1 | +# SPDX-License-Identifier: GPL-2.0 |
| 2 | + |
| 3 | +import json |
| 4 | +import os |
| 5 | +import unittest |
| 6 | + |
| 7 | +import requests |
| 8 | + |
| 9 | +from tests.integration.test_podman_compose import podman_compose_path |
| 10 | +from tests.integration.test_podman_compose import test_path |
| 11 | +from tests.integration.test_utils import RunSubprocessMixin |
| 12 | + |
| 13 | + |
| 14 | +def compose_yaml_path(): |
| 15 | + return os.path.join(os.path.join(test_path(), "nets_test2"), "docker-compose.yml") |
| 16 | + |
| 17 | + |
| 18 | +class TestComposeNetsTest2(unittest.TestCase, RunSubprocessMixin): |
| 19 | + # test if port mapping works as expected with networks top-level element |
| 20 | + def test_nets_test2(self): |
| 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_test2_web1_1", output) |
| 38 | + self.assertIn(b"nets_test2_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 | + # inspect 1st container |
| 49 | + output, _ = self.run_subprocess_assert_returncode([ |
| 50 | + "podman", |
| 51 | + "inspect", |
| 52 | + "nets_test2_web1_1", |
| 53 | + ]) |
| 54 | + container_info = json.loads(output.decode('utf-8'))[0] |
| 55 | + |
| 56 | + # check if network got specific name from networks top-level element |
| 57 | + self.assertEqual( |
| 58 | + list(container_info["NetworkSettings"]["Networks"].keys())[0], "nets_test2_mystack" |
| 59 | + ) |
| 60 | + |
| 61 | + # check if Host port is the same as prodvided by the service port |
| 62 | + self.assertEqual( |
| 63 | + container_info['NetworkSettings']["Ports"], |
| 64 | + {"8001/tcp": [{"HostIp": "", "HostPort": "8001"}]}, |
| 65 | + ) |
| 66 | + |
| 67 | + self.assertEqual(container_info["Config"]["Hostname"], "web1") |
| 68 | + |
| 69 | + # inspect 2nd container |
| 70 | + output, _ = self.run_subprocess_assert_returncode([ |
| 71 | + "podman", |
| 72 | + "inspect", |
| 73 | + "nets_test2_web2_1", |
| 74 | + ]) |
| 75 | + container_info = json.loads(output.decode('utf-8'))[0] |
| 76 | + |
| 77 | + self.assertEqual( |
| 78 | + list(container_info["NetworkSettings"]["Networks"].keys())[0], "nets_test2_mystack" |
| 79 | + ) |
| 80 | + |
| 81 | + self.assertEqual( |
| 82 | + container_info['NetworkSettings']["Ports"], |
| 83 | + {"8001/tcp": [{"HostIp": "", "HostPort": "8002"}]}, |
| 84 | + ) |
| 85 | + |
| 86 | + self.assertEqual(container_info["Config"]["Hostname"], "web2") |
| 87 | + finally: |
| 88 | + self.run_subprocess_assert_returncode([ |
| 89 | + podman_compose_path(), |
| 90 | + "-f", |
| 91 | + compose_yaml_path(), |
| 92 | + "down", |
| 93 | + "-t", |
| 94 | + "0", |
| 95 | + ]) |
0 commit comments