Merge pull request #764 from lemmi/sysctls-dict

Handle sysctls maps
This commit is contained in:
Povilas Kanapickas 2024-03-09 12:47:10 +02:00 committed by GitHub
commit 970e4ac4ab
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 77 additions and 3 deletions

View File

@ -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):

View File

@ -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()
@ -66,3 +66,67 @@ class TestContainerToArgs(unittest.TestCase):
"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)