diff --git a/plugins/lookup/container_volumes.py b/plugins/lookup/container_volumes.py index 6901a8cbfb..e67fcf0027 100644 --- a/plugins/lookup/container_volumes.py +++ b/plugins/lookup/container_volumes.py @@ -46,6 +46,37 @@ def _coerce_mode_int(value: Any) -> int: ) from exc +_SIZE_UNITS = {"": 1, "b": 1, "k": 1024, "m": 1024**2, "g": 1024**3} + + +def _coerce_size_bytes(value: Any) -> int: + """Return a tmpfs size as the byte count the long-form mount demands. + + A declaration reads ``512m`` because that is how a human writes it and how + the short form accepts it. The long form takes an integer, and compose + rejects the string outright rather than parsing it, so the suffix has to be + resolved here. + """ + if isinstance(value, bool): + raise AnsibleFilterError( + f"container_volumes: size must be an int or a size string, got bool: {value!r}" + ) + if isinstance(value, int): + return value + text = str(value).strip().lower() + if not text: + raise AnsibleFilterError("container_volumes: size is empty") + number, unit = text[:-1], text[-1] + if unit.isdigit(): + number, unit = text, "" + try: + return int(number) * _SIZE_UNITS[unit] + except (ValueError, KeyError) as exc: + raise AnsibleFilterError( + f"container_volumes: size {value!r} is not a byte count: {exc}" + ) from exc + + def _short_form_volume(source_name: str, target: str, read_only: bool) -> str: suffix = ":ro" if read_only else "" return f"{source_name}:{target}{suffix}" @@ -140,7 +171,7 @@ def container_volumes( } tmpfs_options: dict[str, Any] = {} if mount.get("size") is not None: - tmpfs_options["size"] = mount["size"] + tmpfs_options["size"] = _coerce_size_bytes(mount["size"]) if mount.get("mode") is not None: tmpfs_options["mode"] = _coerce_mode_int(mount["mode"]) if tmpfs_options: diff --git a/tests/unit/python/plugins/lookup/test_container_volumes_render.py b/tests/unit/python/plugins/lookup/test_container_volumes_render.py index 627f609fcc..74e6d38cf3 100644 --- a/tests/unit/python/plugins/lookup/test_container_volumes_render.py +++ b/tests/unit/python/plugins/lookup/test_container_volumes_render.py @@ -122,7 +122,8 @@ class TestContainerVolumesConfigSecret(unittest.TestCase): class TestContainerVolumesTmpfs(unittest.TestCase): - def test_tmpfs_renders_long_form_dict(self) -> None: + def test_tmpfs_size_renders_as_the_byte_count_the_long_form_takes(self) -> None: + """Compose rejects "64m" outright: the long form takes an integer.""" meta = { "scratch": { "type": "tmpfs", @@ -138,12 +139,48 @@ class TestContainerVolumesTmpfs(unittest.TestCase): { "type": "tmpfs", "target": "/scratch", - "tmpfs": {"size": "64m"}, + "tmpfs": {"size": 64 * 1024**2}, } ] }, ) + def test_every_unit_a_declaration_may_use_resolves(self) -> None: + for written, expected in ( + ("512m", 512 * 1024**2), + ("64k", 64 * 1024), + ("2g", 2 * 1024**3), + ("900b", 900), + ("900", 900), + (1024, 1024), + ): + with self.subTest(size=written): + meta = { + "scratch": { + "type": "tmpfs", + "mounts": [ + {"service": "app", "target": "/scratch", "size": written} + ], + } + } + data = _parse(container_volumes(_apps(meta), "my-app", "app")) + self.assertEqual(expected, data["volumes"][0]["tmpfs"]["size"]) + + def test_a_size_that_is_not_a_byte_count_is_refused(self) -> None: + """Silently passing it on is what let "512m" reach compose.""" + for written in ("", "abc", "12x"): + with self.subTest(size=written): + meta = { + "scratch": { + "type": "tmpfs", + "mounts": [ + {"service": "app", "target": "/scratch", "size": written} + ], + } + } + with self.assertRaises(Exception): # noqa: B017 - the plugin raises an Ansible error type + container_volumes(_apps(meta), "my-app", "app") + def test_tmpfs_mode_renders_as_decimal_of_the_octal_string(self) -> None: meta = { "scratch": {