2024-01-03 17:07:14 +01:00
|
|
|
# SPDX-License-Identifier: GPL-2.0
|
|
|
|
|
|
|
|
import unittest
|
|
|
|
from unittest import mock
|
2024-03-08 22:54:48 +01:00
|
|
|
|
2024-01-03 17:07:14 +01:00
|
|
|
from podman_compose import container_to_args
|
|
|
|
|
|
|
|
|
2024-03-21 15:19:23 +01:00
|
|
|
def create_compose_mock(project_name="test_project_name"):
|
2024-01-03 17:07:14 +01:00
|
|
|
compose = mock.Mock()
|
2024-03-21 15:19:23 +01:00
|
|
|
compose.project_name = project_name
|
2024-01-03 17:07:14 +01:00
|
|
|
compose.dirname = "test_dirname"
|
|
|
|
compose.container_names_by_service.get = mock.Mock(return_value=None)
|
|
|
|
compose.prefer_volume_over_mount = False
|
|
|
|
compose.default_net = None
|
|
|
|
compose.networks = {}
|
|
|
|
return compose
|
|
|
|
|
|
|
|
|
|
|
|
def get_minimal_container():
|
|
|
|
return {
|
|
|
|
"name": "project_name_service_name1",
|
|
|
|
"service_name": "service_name",
|
|
|
|
"image": "busybox",
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-03-09 11:04:53 +01:00
|
|
|
class TestContainerToArgs(unittest.IsolatedAsyncioTestCase):
|
2024-01-03 17:07:14 +01:00
|
|
|
async def test_minimal(self):
|
|
|
|
c = create_compose_mock()
|
|
|
|
|
|
|
|
cnt = get_minimal_container()
|
|
|
|
|
|
|
|
args = await container_to_args(c, cnt)
|
|
|
|
self.assertEqual(
|
|
|
|
args,
|
|
|
|
[
|
|
|
|
"--name=project_name_service_name1",
|
|
|
|
"-d",
|
2024-03-21 16:28:15 +01:00
|
|
|
"--network=bridge",
|
|
|
|
"--network-alias=service_name",
|
2024-01-03 17:07:14 +01:00
|
|
|
"busybox",
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
|
|
|
async def test_runtime(self):
|
|
|
|
c = create_compose_mock()
|
|
|
|
|
|
|
|
cnt = get_minimal_container()
|
|
|
|
cnt["runtime"] = "runsc"
|
|
|
|
|
|
|
|
args = await container_to_args(c, cnt)
|
|
|
|
self.assertEqual(
|
|
|
|
args,
|
|
|
|
[
|
|
|
|
"--name=project_name_service_name1",
|
|
|
|
"-d",
|
2024-03-21 16:28:15 +01:00
|
|
|
"--network=bridge",
|
|
|
|
"--network-alias=service_name",
|
2024-01-03 17:07:14 +01:00
|
|
|
"--runtime",
|
|
|
|
"runsc",
|
|
|
|
"busybox",
|
|
|
|
],
|
|
|
|
)
|
2023-09-09 08:38:23 +02:00
|
|
|
|
|
|
|
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",
|
2024-03-21 16:28:15 +01:00
|
|
|
"--network=bridge",
|
|
|
|
"--network-alias=service_name",
|
2023-09-09 08:38:23 +02:00
|
|
|
"--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",
|
2024-03-21 16:28:15 +01:00
|
|
|
"--network=bridge",
|
|
|
|
"--network-alias=service_name",
|
2023-09-09 08:38:23 +02:00
|
|
|
"--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)
|
2023-09-10 00:18:43 +02:00
|
|
|
|
|
|
|
async def test_pid(self):
|
|
|
|
c = create_compose_mock()
|
|
|
|
cnt = get_minimal_container()
|
|
|
|
|
|
|
|
cnt["pid"] = "host"
|
|
|
|
|
|
|
|
args = await container_to_args(c, cnt)
|
|
|
|
self.assertEqual(
|
|
|
|
args,
|
|
|
|
[
|
|
|
|
"--name=project_name_service_name1",
|
|
|
|
"-d",
|
2024-03-21 16:28:15 +01:00
|
|
|
"--network=bridge",
|
|
|
|
"--network-alias=service_name",
|
2023-09-10 00:18:43 +02:00
|
|
|
"--pid",
|
|
|
|
"host",
|
|
|
|
"busybox",
|
|
|
|
],
|
|
|
|
)
|
2024-03-09 18:10:09 +01:00
|
|
|
|
|
|
|
async def test_http_proxy(self):
|
|
|
|
c = create_compose_mock()
|
|
|
|
|
|
|
|
cnt = get_minimal_container()
|
|
|
|
cnt["http_proxy"] = False
|
|
|
|
|
|
|
|
args = await container_to_args(c, cnt)
|
|
|
|
self.assertEqual(
|
|
|
|
args,
|
|
|
|
[
|
|
|
|
"--name=project_name_service_name1",
|
|
|
|
"-d",
|
|
|
|
"--http-proxy=false",
|
2024-03-21 16:28:15 +01:00
|
|
|
"--network=bridge",
|
|
|
|
"--network-alias=service_name",
|
2024-03-09 18:10:09 +01:00
|
|
|
"busybox",
|
|
|
|
],
|
|
|
|
)
|