From e67c52f2c476b0ff75cd695600de276f46ff6c09 Mon Sep 17 00:00:00 2001 From: lemmi Date: Sat, 9 Mar 2024 11:04:53 +0100 Subject: [PATCH 1/2] test_container_to_args: handle async tests Signed-off-by: lemmi --- pytests/test_container_to_args.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pytests/test_container_to_args.py b/pytests/test_container_to_args.py index 12a916dd..3a921aad 100644 --- a/pytests/test_container_to_args.py +++ b/pytests/test_container_to_args.py @@ -25,7 +25,7 @@ def get_minimal_container(): } -class TestContainerToArgs(unittest.TestCase): +class TestContainerToArgs(unittest.IsolatedAsyncioTestCase): async def test_minimal(self): c = create_compose_mock() From a9c335bf826aaddc8aa0500448ca293cacf84502 Mon Sep 17 00:00:00 2001 From: lemmi Date: Sat, 9 Sep 2023 08:38:23 +0200 Subject: [PATCH 2/2] Handle sysctls maps - Add support to handle sysctls maps. - Directly raise an error if sysctls is neither an array nor map instead of letting podman fail with an unhelpful message. Support for sysctls arrays was added in #261. Fixes #754: sysctls only works with arrays, not maps Signed-off-by: lemmi --- podman_compose.py | 14 ++++++- pytests/test_container_to_args.py | 64 +++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 2 deletions(-) diff --git a/podman_compose.py b/podman_compose.py index e3701418..42f17c7e 100755 --- a/podman_compose.py +++ b/podman_compose.py @@ -956,8 +956,18 @@ async def container_to_args(compose, cnt, detached=True): podman_args.append("-i") if cnt.get("stop_signal", None): podman_args.extend(["--stop-signal", cnt["stop_signal"]]) - for i in cnt.get("sysctls", []): - podman_args.extend(["--sysctl", i]) + + sysctls = cnt.get("sysctls") + if sysctls is not None: + if isinstance(sysctls, dict): + for sysctl, value in sysctls.items(): + podman_args.extend(["--sysctl", "{}={}".format(sysctl, value)]) + elif isinstance(sysctls, list): + for i in sysctls: + podman_args.extend(["--sysctl", i]) + else: + raise TypeError("sysctls should be either dict or list") + if cnt.get("tty", None): podman_args.append("--tty") if cnt.get("privileged", None): diff --git a/pytests/test_container_to_args.py b/pytests/test_container_to_args.py index 3a921aad..e5026f31 100644 --- a/pytests/test_container_to_args.py +++ b/pytests/test_container_to_args.py @@ -66,3 +66,67 @@ async def test_runtime(self): "busybox", ], ) + + async def test_sysctl_list(self): + c = create_compose_mock() + + cnt = get_minimal_container() + cnt["sysctls"] = [ + "net.core.somaxconn=1024", + "net.ipv4.tcp_syncookies=0", + ] + + args = await container_to_args(c, cnt) + self.assertEqual( + args, + [ + "--name=project_name_service_name1", + "-d", + "--net", + "", + "--network-alias", + "service_name", + "--sysctl", + "net.core.somaxconn=1024", + "--sysctl", + "net.ipv4.tcp_syncookies=0", + "busybox", + ], + ) + + async def test_sysctl_map(self): + c = create_compose_mock() + + cnt = get_minimal_container() + cnt["sysctls"] = { + "net.core.somaxconn": 1024, + "net.ipv4.tcp_syncookies": 0, + } + + args = await container_to_args(c, cnt) + self.assertEqual( + args, + [ + "--name=project_name_service_name1", + "-d", + "--net", + "", + "--network-alias", + "service_name", + "--sysctl", + "net.core.somaxconn=1024", + "--sysctl", + "net.ipv4.tcp_syncookies=0", + "busybox", + ], + ) + + async def test_sysctl_wrong_type(self): + c = create_compose_mock() + cnt = get_minimal_container() + + # check whether wrong types are correctly rejected + for wrong_type in [True, 0, 0.0, "wrong", ()]: + with self.assertRaises(TypeError): + cnt["sysctls"] = wrong_type + await container_to_args(c, cnt)