Merge pull request #1232 from p12tic/1.4-backport-1231

[1.4 backports] Fix relative host path resolution for volume bind mount source
This commit is contained in:
Povilas Kanapickas 2025-06-05 17:08:16 +03:00 committed by GitHub
commit 189c086d5b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 82 additions and 8 deletions

View File

@ -0,0 +1 @@
Fixed relative host path resolution for volume bind mount source

View File

@ -395,6 +395,7 @@ async def assert_volume(compose, mount_dict):
os.makedirs(mount_src, exist_ok=True) os.makedirs(mount_src, exist_ok=True)
except OSError: except OSError:
pass pass
mount_dict["source"] = mount_src
return return
if mount_dict["type"] != "volume" or not vol or not vol.get("name"): if mount_dict["type"] != "volume" or not vol or not vol.get("name"):
return return

View File

@ -36,8 +36,9 @@ class TestPodmanCompose(unittest.TestCase, RunSubprocessMixin):
"selinux_container1_1", "selinux_container1_1",
]) ])
inspect_out = json.loads(out) inspect_out = json.loads(out)
create_command_list = inspect_out[0].get("Config", []).get("CreateCommand", {}) create_command_list = inspect_out[0].get("Config", []).get("CreateCommand", [])
self.assertIn('./host_test_text.txt:/test_text.txt:z', create_command_list) host_path = os.path.join(test_path(), "selinux", "host_test_text.txt")
self.assertIn(f'{host_path}:/test_text.txt:z', create_command_list)
out, _ = self.run_subprocess_assert_returncode([ out, _ = self.run_subprocess_assert_returncode([
"podman", "podman",
@ -45,8 +46,9 @@ class TestPodmanCompose(unittest.TestCase, RunSubprocessMixin):
"selinux_container2_1", "selinux_container2_1",
]) ])
inspect_out = json.loads(out) inspect_out = json.loads(out)
create_command_list = inspect_out[0].get("Config", []).get("CreateCommand", {}) create_command_list = inspect_out[0].get("Config", []).get("CreateCommand", [])
self.assertIn('./host_test_text.txt:/test_text.txt', create_command_list) host_path = os.path.join(test_path(), "selinux", "host_test_text.txt")
self.assertIn(f'{host_path}:/test_text.txt', create_command_list)
finally: finally:
out, _ = self.run_subprocess_assert_returncode([ out, _ = self.run_subprocess_assert_returncode([
podman_compose_path(), podman_compose_path(),

View File

@ -529,10 +529,24 @@ class TestContainerToArgs(unittest.IsolatedAsyncioTestCase):
) )
@parameterized.expand([ @parameterized.expand([
(False, "z", ["--mount", "type=bind,source=./foo,destination=/mnt,z"]), (
(False, "Z", ["--mount", "type=bind,source=./foo,destination=/mnt,Z"]), False,
(True, "z", ["-v", "./foo:/mnt:z"]), "z",
(True, "Z", ["-v", "./foo:/mnt:Z"]), [
"--mount",
f"type=bind,source={get_test_file_path('test_dirname/foo')},destination=/mnt,z",
],
),
(
False,
"Z",
[
"--mount",
f"type=bind,source={get_test_file_path('test_dirname/foo')},destination=/mnt,Z",
],
),
(True, "z", ["-v", f"{get_test_file_path('test_dirname/foo')}:/mnt:z"]),
(True, "Z", ["-v", f"{get_test_file_path('test_dirname/foo')}:/mnt:Z"]),
]) ])
async def test_selinux_volume(self, prefer_volume, selinux_type, expected_additional_args): async def test_selinux_volume(self, prefer_volume, selinux_type, expected_additional_args):
c = create_compose_mock() c = create_compose_mock()
@ -567,6 +581,62 @@ class TestContainerToArgs(unittest.IsolatedAsyncioTestCase):
], ],
) )
@parameterized.expand([
(
"absolute_path",
get_test_file_path('test_dirname/foo'),
[
"--mount",
f"type=bind,source={get_test_file_path('test_dirname/foo')},destination=/mnt",
],
),
(
"relative_path",
'./foo',
[
"--mount",
f"type=bind,source={get_test_file_path('test_dirname/foo')},destination=/mnt",
],
),
(
"home_dir",
'~/test_dirname/foo',
[
"--mount",
f"type=bind,source={os.path.expanduser('~/test_dirname/foo')},destination=/mnt",
],
),
])
async def test_volumes_bind_mount_source(
self, test_name: str, mount_source: str, expected_additional_args: list
) -> None:
c = create_compose_mock()
cnt = get_minimal_container()
# This is supposed to happen during `_parse_compose_file`
# but that is probably getting skipped during testing
cnt["_service"] = cnt["service_name"]
cnt["volumes"] = [
{
"type": "bind",
"source": f"{mount_source}",
"target": "/mnt",
}
]
args = await container_to_args(c, cnt)
self.assertEqual(
args,
[
"--name=project_name_service_name1",
"-d",
*expected_additional_args,
"--network=bridge:alias=service_name",
"busybox",
],
)
@parameterized.expand([ @parameterized.expand([
("not_compat", False, "test_project_name", "test_project_name_network1"), ("not_compat", False, "test_project_name", "test_project_name_network1"),
("compat_no_dash", True, "test_project_name", "test_project_name_network1"), ("compat_no_dash", True, "test_project_name", "test_project_name_network1"),