| 1 |
import unittest |
| 2 |
|
| 3 |
from parameterized import parameterized |
| 4 |
|
| 5 |
from podman_compose import PodmanCompose |
| 6 |
from podman_compose import get_net_args |
| 7 |
from tests.unit.test_container_to_args import create_compose_mock |
| 8 |
|
| 9 |
PROJECT_NAME = "test_project_name" |
| 10 |
SERVICE_NAME = "service_name" |
| 11 |
CONTAINER_NAME = f"{PROJECT_NAME}_{SERVICE_NAME}_1" |
| 12 |
|
| 13 |
|
| 14 |
def get_networked_compose(num_networks: int = 1) -> PodmanCompose: |
| 15 |
compose = create_compose_mock(PROJECT_NAME) |
| 16 |
for network in range(num_networks): |
| 17 |
compose.networks[f"net{network}"] = { |
| 18 |
"driver": "bridge", |
| 19 |
"ipam": { |
| 20 |
"config": [ |
| 21 |
{"subnet": f"192.168.{network}.0/24"}, |
| 22 |
{"subnet": f"fd00:{network}::/64"}, |
| 23 |
] |
| 24 |
}, |
| 25 |
"enable_ipv6": True, |
| 26 |
} |
| 27 |
|
| 28 |
if num_networks == 1: |
| 29 |
compose.default_net = "net0" |
| 30 |
|
| 31 |
return compose |
| 32 |
|
| 33 |
|
| 34 |
def get_minimal_container() -> dict: |
| 35 |
return { |
| 36 |
"name": CONTAINER_NAME, |
| 37 |
"service_name": SERVICE_NAME, |
| 38 |
"image": "busybox", |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
class TestGetNetArgs(unittest.TestCase): |
| 43 |
def test_minimal(self) -> None: |
| 44 |
compose = get_networked_compose() |
| 45 |
container = get_minimal_container() |
| 46 |
|
| 47 |
self.assertEqual( |
| 48 |
get_net_args(compose, container), |
| 49 |
[f"--network={PROJECT_NAME}_net0:alias={SERVICE_NAME}"], |
| 50 |
) |
| 51 |
|
| 52 |
def test_default_net_is_None(self) -> None: |
| 53 |
compose = get_networked_compose() |
| 54 |
container = get_minimal_container() |
| 55 |
|
| 56 |
mac_address = "11:22:33:44:55:66" |
| 57 |
container["mac_address"] = mac_address |
| 58 |
|
| 59 |
compose.default_net = None |
| 60 |
|
| 61 |
self.assertEqual( |
| 62 |
get_net_args(compose, container), |
| 63 |
[f"--network=bridge:alias={SERVICE_NAME},mac={mac_address}"], |
| 64 |
) |
| 65 |
|
| 66 |
def test_one_net(self) -> None: |
| 67 |
compose = get_networked_compose() |
| 68 |
container = get_minimal_container() |
| 69 |
container["networks"] = {"net0": {}} |
| 70 |
|
| 71 |
self.assertEqual( |
| 72 |
get_net_args(compose, container), |
| 73 |
[f"--network={PROJECT_NAME}_net0:alias={SERVICE_NAME}"], |
| 74 |
) |
| 75 |
|
| 76 |
def test_alias(self) -> None: |
| 77 |
compose = get_networked_compose() |
| 78 |
container = get_minimal_container() |
| 79 |
container["networks"] = {"net0": {}} |
| 80 |
container["_aliases"] = ["alias1", "alias2"] |
| 81 |
|
| 82 |
self.assertEqual( |
| 83 |
get_net_args(compose, container), |
| 84 |
[f"--network={PROJECT_NAME}_net0:alias={SERVICE_NAME},alias=alias1,alias=alias2"], |
| 85 |
) |
| 86 |
|
| 87 |
def test_aliases_on_network_scope(self) -> None: |
| 88 |
compose = get_networked_compose() |
| 89 |
container = get_minimal_container() |
| 90 |
container["networks"] = {"net0": {"aliases": ["alias1"]}} |
| 91 |
|
| 92 |
self.assertEqual( |
| 93 |
get_net_args(compose, container), |
| 94 |
[f"--network={PROJECT_NAME}_net0:alias={SERVICE_NAME},alias=alias1"], |
| 95 |
) |
| 96 |
|
| 97 |
def test_one_ipv4(self) -> None: |
| 98 |
ip = "192.168.0.42" |
| 99 |
compose = get_networked_compose() |
| 100 |
container = get_minimal_container() |
| 101 |
container["networks"] = {"net0": {"ipv4_address": ip}} |
| 102 |
|
| 103 |
self.assertEqual( |
| 104 |
get_net_args(compose, container), |
| 105 |
[f"--network={PROJECT_NAME}_net0:ip={ip},alias={SERVICE_NAME}"], |
| 106 |
) |
| 107 |
|
| 108 |
def test_one_ipv6(self) -> None: |
| 109 |
ipv6_address = "fd00:0::42" |
| 110 |
compose = get_networked_compose() |
| 111 |
container = get_minimal_container() |
| 112 |
container["networks"] = {"net0": {"ipv6_address": ipv6_address}} |
| 113 |
|
| 114 |
self.assertEqual( |
| 115 |
get_net_args(compose, container), |
| 116 |
[f"--network={PROJECT_NAME}_net0:ip6={ipv6_address},alias={SERVICE_NAME}"], |
| 117 |
) |
| 118 |
|
| 119 |
def test_one_mac(self) -> None: |
| 120 |
mac = "00:11:22:33:44:55" |
| 121 |
compose = get_networked_compose() |
| 122 |
container = get_minimal_container() |
| 123 |
container["networks"] = {"net0": {}} |
| 124 |
container["mac_address"] = mac |
| 125 |
|
| 126 |
self.assertEqual( |
| 127 |
get_net_args(compose, container), |
| 128 |
[f"--network={PROJECT_NAME}_net0:mac={mac},alias={SERVICE_NAME}"], |
| 129 |
) |
| 130 |
|
| 131 |
def test_one_mac_two_nets(self) -> None: |
| 132 |
mac = "00:11:22:33:44:55" |
| 133 |
compose = get_networked_compose(num_networks=6) |
| 134 |
container = get_minimal_container() |
| 135 |
container["networks"] = {"net0": {}, "net1": {}} |
| 136 |
container["mac_address"] = mac |
| 137 |
|
| 138 |
self.assertEqual( |
| 139 |
get_net_args(compose, container), |
| 140 |
[ |
| 141 |
f"--network={PROJECT_NAME}_net0:mac={mac},alias={SERVICE_NAME}", |
| 142 |
f"--network={PROJECT_NAME}_net1:alias={SERVICE_NAME}", |
| 143 |
], |
| 144 |
) |
| 145 |
|
| 146 |
@parameterized.expand([ |
| 147 |
"mac_address", |
| 148 |
"x-podman.mac_address", |
| 149 |
]) |
| 150 |
def test_mac_on_network(self, mac_attr: str) -> None: |
| 151 |
mac = "00:11:22:33:44:55" |
| 152 |
compose = get_networked_compose() |
| 153 |
container = get_minimal_container() |
| 154 |
container["networks"] = {"net0": {mac_attr: mac}} |
| 155 |
|
| 156 |
self.assertEqual( |
| 157 |
get_net_args(compose, container), |
| 158 |
[f"--network={PROJECT_NAME}_net0:mac={mac},alias={SERVICE_NAME}"], |
| 159 |
) |
| 160 |
|
| 161 |
def test_two_nets_as_dict(self) -> None: |
| 162 |
compose = get_networked_compose(num_networks=2) |
| 163 |
container = get_minimal_container() |
| 164 |
container["networks"] = {"net0": {}, "net1": {}} |
| 165 |
|
| 166 |
self.assertEqual( |
| 167 |
get_net_args(compose, container), |
| 168 |
[ |
| 169 |
f"--network={PROJECT_NAME}_net0:alias={SERVICE_NAME}", |
| 170 |
f"--network={PROJECT_NAME}_net1:alias={SERVICE_NAME}", |
| 171 |
], |
| 172 |
) |
| 173 |
|
| 174 |
def test_two_nets_as_list(self) -> None: |
| 175 |
compose = get_networked_compose(num_networks=2) |
| 176 |
container = get_minimal_container() |
| 177 |
container["networks"] = ["net0", "net1"] |
| 178 |
|
| 179 |
self.assertEqual( |
| 180 |
get_net_args(compose, container), |
| 181 |
[ |
| 182 |
f"--network={PROJECT_NAME}_net0:alias={SERVICE_NAME}", |
| 183 |
f"--network={PROJECT_NAME}_net1:alias={SERVICE_NAME}", |
| 184 |
], |
| 185 |
) |
| 186 |
|
| 187 |
def test_two_ipv4(self) -> None: |
| 188 |
ip0 = "192.168.0.42" |
| 189 |
ip1 = "192.168.1.42" |
| 190 |
compose = get_networked_compose(num_networks=2) |
| 191 |
container = get_minimal_container() |
| 192 |
container["networks"] = {"net0": {"ipv4_address": ip0}, "net1": {"ipv4_address": ip1}} |
| 193 |
|
| 194 |
self.assertEqual( |
| 195 |
get_net_args(compose, container), |
| 196 |
[ |
| 197 |
f"--network={PROJECT_NAME}_net0:ip={ip0},alias={SERVICE_NAME}", |
| 198 |
f"--network={PROJECT_NAME}_net1:ip={ip1},alias={SERVICE_NAME}", |
| 199 |
], |
| 200 |
) |
| 201 |
|
| 202 |
def test_two_ipv6(self) -> None: |
| 203 |
ip0 = "fd00:0::42" |
| 204 |
ip1 = "fd00:1::42" |
| 205 |
compose = get_networked_compose(num_networks=2) |
| 206 |
container = get_minimal_container() |
| 207 |
container["networks"] = {"net0": {"ipv6_address": ip0}, "net1": {"ipv6_address": ip1}} |
| 208 |
|
| 209 |
self.assertEqual( |
| 210 |
get_net_args(compose, container), |
| 211 |
[ |
| 212 |
f"--network={PROJECT_NAME}_net0:ip6={ip0},alias={SERVICE_NAME}", |
| 213 |
f"--network={PROJECT_NAME}_net1:ip6={ip1},alias={SERVICE_NAME}", |
| 214 |
], |
| 215 |
) |
| 216 |
|
| 217 |
|
| 218 |
def test_two_mac(self) -> None: |
| 219 |
mac0 = "00:00:00:00:00:01" |
| 220 |
mac1 = "00:00:00:00:00:02" |
| 221 |
compose = get_networked_compose(num_networks=2) |
| 222 |
container = get_minimal_container() |
| 223 |
container["networks"] = { |
| 224 |
"net0": {"x-podman.mac_address": mac0}, |
| 225 |
"net1": {"x-podman.mac_address": mac1}, |
| 226 |
} |
| 227 |
|
| 228 |
self.assertEqual( |
| 229 |
get_net_args(compose, container), |
| 230 |
[ |
| 231 |
f"--network={PROJECT_NAME}_net0:mac={mac0},alias={SERVICE_NAME}", |
| 232 |
f"--network={PROJECT_NAME}_net1:mac={mac1},alias={SERVICE_NAME}", |
| 233 |
], |
| 234 |
) |
| 235 |
|
| 236 |
def test_mixed_mac(self) -> None: |
| 237 |
ip4_0 = "192.168.0.42" |
| 238 |
ip4_1 = "192.168.1.42" |
| 239 |
ip4_2 = "192.168.2.42" |
| 240 |
mac_0 = "00:00:00:00:00:01" |
| 241 |
mac_1 = "00:00:00:00:00:02" |
| 242 |
|
| 243 |
compose = get_networked_compose(num_networks=3) |
| 244 |
container = get_minimal_container() |
| 245 |
container["networks"] = { |
| 246 |
"net0": {"ipv4_address": ip4_0}, |
| 247 |
"net1": {"ipv4_address": ip4_1, "x-podman.mac_address": mac_0}, |
| 248 |
"net2": {"ipv4_address": ip4_2}, |
| 249 |
} |
| 250 |
container["mac_address"] = mac_1 |
| 251 |
|
| 252 |
expected_exception = ( |
| 253 |
r"specifying mac_address on both container and network level is not supported" |
| 254 |
) |
| 255 |
self.assertRaisesRegex(RuntimeError, expected_exception, get_net_args, compose, container) |
| 256 |
|
| 257 |
def test_mixed_config(self) -> None: |
| 258 |
ip4_0 = "192.168.0.42" |
| 259 |
ip4_1 = "192.168.1.42" |
| 260 |
ip6_0 = "fd00:0::42" |
| 261 |
ip6_2 = "fd00:2::42" |
| 262 |
mac = "00:11:22:33:44:55" |
| 263 |
compose = get_networked_compose(num_networks=4) |
| 264 |
container = get_minimal_container() |
| 265 |
container["networks"] = { |
| 266 |
"net0": {"ipv4_address": ip4_0, "ipv6_address": ip6_0}, |
| 267 |
"net1": {"ipv4_address": ip4_1}, |
| 268 |
"net2": {"ipv6_address": ip6_2}, |
| 269 |
"net3": {}, |
| 270 |
} |
| 271 |
container["mac_address"] = mac |
| 272 |
|
| 273 |
self.assertEqual( |
| 274 |
get_net_args(compose, container), |
| 275 |
[ |
| 276 |
f"--network={PROJECT_NAME}_net0:ip={ip4_0},ip6={ip6_0},mac={mac},alias={SERVICE_NAME}", |
| 277 |
f"--network={PROJECT_NAME}_net1:ip={ip4_1},alias={SERVICE_NAME}", |
| 278 |
f"--network={PROJECT_NAME}_net2:ip6={ip6_2},alias={SERVICE_NAME}", |
| 279 |
f"--network={PROJECT_NAME}_net3:alias={SERVICE_NAME}", |
| 280 |
], |
| 281 |
) |
| 282 |
|
| 283 |
@parameterized.expand([ |
| 284 |
("bridge", [f"--network=bridge:alias={SERVICE_NAME},mac=11:22:33:44:55:66"]), |
| 285 |
( |
| 286 |
"bridge:ip=10.88.0.3", |
| 287 |
[f"--network=bridge:ip=10.88.0.3,alias={SERVICE_NAME},mac=11:22:33:44:55:66"], |
| 288 |
), |
| 289 |
("host", ["--network=host"]), |
| 290 |
("none", ["--network=none"]), |
| 291 |
("slirp4netns", ["--network=slirp4netns"]), |
| 292 |
("slirp4netns:cidr=10.42.0.0/24", ["--network=slirp4netns:cidr=10.42.0.0/24"]), |
| 293 |
("private", ["--network=private"]), |
| 294 |
("pasta", ["--network=pasta"]), |
| 295 |
("pasta:--ipv4-only,-a,10.0.2.0", ["--network=pasta:--ipv4-only,-a,10.0.2.0"]), |
| 296 |
("ns:my_namespace", ["--network=ns:my_namespace"]), |
| 297 |
("container:my_container", ["--network=container:my_container"]), |
| 298 |
]) |
| 299 |
def test_network_modes(self, network_mode: str, expected_args: list) -> None: |
| 300 |
compose = get_networked_compose() |
| 301 |
container = get_minimal_container() |
| 302 |
container["network_mode"] = network_mode |
| 303 |
|
| 304 |
mac_address = "11:22:33:44:55:66" |
| 305 |
container["network_mode"] = network_mode |
| 306 |
container["mac_address"] = mac_address |
| 307 |
|
| 308 |
self.assertEqual(get_net_args(compose, container), expected_args) |
| 309 |
|
| 310 |
def test_network_mode_invalid(self) -> None: |
| 311 |
compose = get_networked_compose() |
| 312 |
container = get_minimal_container() |
| 313 |
container["network_mode"] = "invalid_mode" |
| 314 |
|
| 315 |
with self.assertRaises(SystemExit): |
| 316 |
get_net_args(compose, container) |
| 317 |
|
| 318 |
def test_network__mode_service(self) -> None: |
| 319 |
compose = get_networked_compose() |
| 320 |
compose.container_names_by_service = { |
| 321 |
"service_1": ["container_1"], |
| 322 |
"service_2": ["container_2"], |
| 323 |
} |
| 324 |
|
| 325 |
container = get_minimal_container() |
| 326 |
container["network_mode"] = "service:service_2" |
| 327 |
|
| 328 |
self.assertEqual(get_net_args(compose, container), ["--network=container:container_2"]) |