diff --git a/RAMP/module_metadata.py b/RAMP/module_metadata.py index a540ee3..7f21f98 100644 --- a/RAMP/module_metadata.py +++ b/RAMP/module_metadata.py @@ -99,8 +99,26 @@ def sha256_checksum(filename, block_size=65536): "oracle9": "rhel9", } +DEVCONTAINER_PATH = "/etc/devcontainer" + + +def _read_key_value_file(path): + values = {} + with open(path) as f: + for line in f: + if "=" in line: + key, value = line.strip().split("=", 1) + values[key] = value.strip("\"'") + return values + + def get_curr_os(): global RLEC_OS_MAP + if os.path.exists(DEVCONTAINER_PATH): + devcontainer_os = _read_key_value_file(DEVCONTAINER_PATH).get("DISTRO") + if devcontainer_os: + return devcontainer_os + curr_os = '%s%s' % (distro.id(), distro.version_parts()[0]) rlec_os = RLEC_OS_MAP.get(curr_os, curr_os) return rlec_os diff --git a/test_module_metadata.py b/test_module_metadata.py new file mode 100644 index 0000000..f71bc6c --- /dev/null +++ b/test_module_metadata.py @@ -0,0 +1,41 @@ +import os +import tempfile +import unittest +from unittest import mock + +from RAMP import module_metadata + + +class GetCurrOsTest(unittest.TestCase): + def test_uses_devcontainer_distro_before_host_distro(self): + with tempfile.TemporaryDirectory() as temp_dir: + devcontainer_path = os.path.join(temp_dir, "devcontainer") + with open(devcontainer_path, "w") as f: + f.write("DISTRO=amzn2023\n") + + with mock.patch.object(module_metadata, "DEVCONTAINER_PATH", devcontainer_path): + with mock.patch.object(module_metadata.distro, "id", return_value="rocky"): + with mock.patch.object(module_metadata.distro, "version_parts", return_value=("8",)): + self.assertEqual(module_metadata.get_curr_os(), "amzn2023") + + def test_falls_back_to_distro_when_devcontainer_has_no_distro(self): + with tempfile.TemporaryDirectory() as temp_dir: + devcontainer_path = os.path.join(temp_dir, "devcontainer") + with open(devcontainer_path, "w") as f: + f.write("OTHER=value\n") + + with mock.patch.object(module_metadata, "DEVCONTAINER_PATH", devcontainer_path): + with mock.patch.object(module_metadata.distro, "id", return_value="rocky"): + with mock.patch.object(module_metadata.distro, "version_parts", return_value=("8",)): + self.assertEqual(module_metadata.get_curr_os(), "rhel8") + + def test_preserves_distro_fallback_when_devcontainer_is_absent(self): + devcontainer_path = os.path.join(tempfile.gettempdir(), "missing-devcontainer") + with mock.patch.object(module_metadata, "DEVCONTAINER_PATH", devcontainer_path): + with mock.patch.object(module_metadata.distro, "id", return_value="ubuntu"): + with mock.patch.object(module_metadata.distro, "version_parts", return_value=("22",)): + self.assertEqual(module_metadata.get_curr_os(), "ubuntu22") + + +if __name__ == "__main__": + unittest.main() diff --git a/tox.ini b/tox.ini index adb0085..7c83bcf 100644 --- a/tox.ini +++ b/tox.ini @@ -14,6 +14,7 @@ commands_pre: pip install -r dev_requirements.txt rm -rf test_module redisgraph.zip commands: + python -m unittest test_module_metadata.py wget -q https://s3.amazonaws.com/redismodules/redisgraph/redisgraph.Linux-x86_64.{[main]graph_version}.zip -O redisgraph.zip unzip redisgraph.zip -d test_module coverage run test.py