File: test_podman_compose_network.py

package info (click to toggle)
podman-compose 1.5.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,004 kB
  • sloc: python: 10,946; sh: 107; javascript: 48; makefile: 13
file content (142 lines) | stat: -rw-r--r-- 4,409 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# SPDX-License-Identifier: GPL-2.0

"""
test_podman_compose_networks.py

Tests the podman networking parameters
"""

# pylint: disable=redefined-outer-name
import os
import unittest
from typing import Generator

from tests.integration.test_utils import RunSubprocessMixin
from tests.integration.test_utils import podman_compose_path
from tests.integration.test_utils import test_path


class TestPodmanComposeNetwork(RunSubprocessMixin, unittest.TestCase):
    @staticmethod
    def compose_file() -> str:
        """Returns the path to the compose file used for this test module"""
        return os.path.join(test_path(), "nets_test_ip", "docker-compose.yml")

    def teardown(self) -> Generator[None, None, None]:
        """
        Ensures that the services within the "profile compose file" are removed between
        each test case.
        """
        # run the test case
        yield

        down_cmd = [
            "coverage",
            "run",
            podman_compose_path(),
            "-f",
            self.compose_file(),
            "kill",
            "-a",
        ]
        self.run_subprocess(down_cmd)

    def test_networks(self) -> None:
        up_cmd = [
            "coverage",
            "run",
            podman_compose_path(),
            "-f",
            self.compose_file(),
            "up",
            "-d",
            "--force-recreate",
        ]

        self.run_subprocess_assert_returncode(up_cmd)

        check_cmd = [
            podman_compose_path(),
            "-f",
            self.compose_file(),
            "ps",
            "--format",
            '"{{.Names}}"',
        ]
        out, _ = self.run_subprocess_assert_returncode(check_cmd)
        self.assertIn(b"nets_test_ip_web1_1", out)
        self.assertIn(b"nets_test_ip_web2_1", out)

        expected_wget = {
            "172.19.1.10": "test1",
            "172.19.2.10": "test1",
            "172.19.2.11": "test2",
            "web3": "test3",
            "172.19.1.13": "test4",
        }

        for service in ("web1", "web2"):
            for ip, expect in expected_wget.items():
                wget_cmd = [
                    podman_compose_path(),
                    "-f",
                    self.compose_file(),
                    "exec",
                    service,
                    "wget",
                    "-q",
                    "-O-",
                    f"http://{ip}:8001/index.txt",
                ]
                out, _ = self.run_subprocess_assert_returncode(wget_cmd)
                self.assertEqual(f"{expect}\r\n", out.decode('utf-8'))

        expected_macip = {
            "web1": {
                "eth0": ["172.19.1.10", "02:01:01:00:01:01"],
                "eth1": ["172.19.2.10", "02:01:01:00:02:01"],
            },
            "web2": {"eth0": ["172.19.2.11", "02:01:01:00:02:02"]},
        }

        for service, interfaces in expected_macip.items():
            ip_cmd = [
                podman_compose_path(),
                "-f",
                self.compose_file(),
                "exec",
                service,
                "ip",
                "addr",
                "show",
            ]
            out, _ = self.run_subprocess_assert_returncode(ip_cmd)
            for interface, values in interfaces.items():
                ip, mac = values
                self.assertIn(f"ether {mac}", out.decode('utf-8'))
                self.assertIn(f"inet {ip}/", out.decode('utf-8'))

    def test_down_with_network(self) -> None:
        try:
            self.run_subprocess_assert_returncode([
                "coverage",
                "run",
                podman_compose_path(),
                "-f",
                os.path.join(test_path(), "network", "docker-compose.yml"),
                "up",
                "-d",
            ])
            output, _, _ = self.run_subprocess(["podman", "network", "ls"])
            self.assertIn("network_mystack", output.decode())
        finally:
            self.run_subprocess_assert_returncode([
                "coverage",
                "run",
                podman_compose_path(),
                "-f",
                os.path.join(test_path(), "network", "docker-compose.yml"),
                "down",
            ])
            output, _, _ = self.run_subprocess(["podman", "network", "ls"])
            self.assertNotIn("network_mystack", output.decode())