Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions RAMP/module_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
41 changes: 41 additions & 0 deletions test_module_metadata.py
Original file line number Diff line number Diff line change
@@ -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()
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading