Skip to content

Commit

Permalink
Merge pull request #1049 from mokibit/automate-nets_test2
Browse files Browse the repository at this point in the history
tests/integration: Automate manual `nets_test2` test

Signed-off-by: Timon de Groot <[email protected]>
  • Loading branch information
p12tic authored and tdgroot committed Oct 14, 2024
2 parents e2ae8be + df8fa58 commit 07e6dbc
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 1 deletion.
3 changes: 2 additions & 1 deletion docs/Extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ The following extension keys are available under container configuration:
* `x-podman.gidmaps` - Run the container in a new user namespace using the supplied GID mapping.

* `x-podman.rootfs` - Run the container without requiring any image management; the rootfs of the
container is assumed to be managed externally.

* `x-podman.no_hosts` - Run the container without creating /etc/hosts file

For example, the following docker-compose.yml allows running a podman container with externally managed rootfs.
```yml
Expand Down
1 change: 1 addition & 0 deletions newsfragments/container-no-hosts.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add x-podman.no_hosts setting to pass --no-hosts to podman run
2 changes: 2 additions & 0 deletions podman_compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -1254,6 +1254,8 @@ async def container_to_args(compose, cnt, detached=True):
podman_args.extend(["--uidmap", uidmap])
for gidmap in cnt.get('x-podman.gidmaps', []):
podman_args.extend(["--gidmap", gidmap])
if cnt.get("x-podman.no_hosts", False):
podman_args.extend(["--no-hosts"])
rootfs = cnt.get('x-podman.rootfs', None)
if rootfs is not None:
rootfs_mode = True
Expand Down
95 changes: 95 additions & 0 deletions tests/integration/test_podman_compose_nets_test2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# SPDX-License-Identifier: GPL-2.0

import json
import os
import unittest

import requests

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


def compose_yaml_path():
return os.path.join(os.path.join(test_path(), "nets_test2"), "docker-compose.yml")


class TestComposeNetsTest2(unittest.TestCase, RunSubprocessMixin):
# test if port mapping works as expected with networks top-level element
def test_nets_test2(self):
try:
self.run_subprocess_assert_returncode(
[
podman_compose_path(),
"-f",
compose_yaml_path(),
"up",
"-d",
],
)
output, _ = self.run_subprocess_assert_returncode([
podman_compose_path(),
"-f",
compose_yaml_path(),
"ps",
])
self.assertIn(b"nets_test2_web1_1", output)
self.assertIn(b"nets_test2_web2_1", output)

response = requests.get('http://localhost:8001/index.txt')
self.assertTrue(response.ok)
self.assertEqual(response.text, "test1\n")

response = requests.get('http://localhost:8002/index.txt')
self.assertTrue(response.ok)
self.assertEqual(response.text, "test2\n")

# inspect 1st container
output, _ = self.run_subprocess_assert_returncode([
"podman",
"inspect",
"nets_test2_web1_1",
])
container_info = json.loads(output.decode('utf-8'))[0]

# check if network got specific name from networks top-level element
self.assertEqual(
list(container_info["NetworkSettings"]["Networks"].keys())[0], "nets_test2_mystack"
)

# check if Host port is the same as prodvided by the service port
self.assertEqual(
container_info['NetworkSettings']["Ports"],
{"8001/tcp": [{"HostIp": "", "HostPort": "8001"}]},
)

self.assertEqual(container_info["Config"]["Hostname"], "web1")

# inspect 2nd container
output, _ = self.run_subprocess_assert_returncode([
"podman",
"inspect",
"nets_test2_web2_1",
])
container_info = json.loads(output.decode('utf-8'))[0]

self.assertEqual(
list(container_info["NetworkSettings"]["Networks"].keys())[0], "nets_test2_mystack"
)

self.assertEqual(
container_info['NetworkSettings']["Ports"],
{"8001/tcp": [{"HostIp": "", "HostPort": "8002"}]},
)

self.assertEqual(container_info["Config"]["Hostname"], "web2")
finally:
self.run_subprocess_assert_returncode([
podman_compose_path(),
"-f",
compose_yaml_path(),
"down",
"-t",
"0",
])
19 changes: 19 additions & 0 deletions tests/unit/test_container_to_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,25 @@ async def test_rootfs_extension(self):
],
)

async def test_no_hosts_extension(self):
c = create_compose_mock()

cnt = get_minimal_container()
cnt["x-podman.no_hosts"] = True

args = await container_to_args(c, cnt)
self.assertEqual(
args,
[
"--name=project_name_service_name1",
"-d",
"--network=bridge",
"--network-alias=service_name",
"--no-hosts",
"busybox",
],
)

async def test_env_file_str(self):
c = create_compose_mock()

Expand Down

0 comments on commit 07e6dbc

Please sign in to comment.