|
| 1 | +import os |
| 2 | +import tempfile |
| 3 | +import pytest |
| 4 | + |
| 5 | +from make87.config import load_config_from_json, get_config_value |
| 6 | + |
| 7 | + |
| 8 | +class DummyAppConfig: |
| 9 | + def __init__(self, config): |
| 10 | + self.config = config |
| 11 | + |
| 12 | + |
| 13 | +def test_secret_resolution(monkeypatch): |
| 14 | + # Create a temporary secret file |
| 15 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 16 | + secret_name = "MYSECRET" |
| 17 | + secret_value = "supersecret" |
| 18 | + secret_file = os.path.join(tmpdir, f"{secret_name}.secret") |
| 19 | + with open(secret_file, "w") as f: |
| 20 | + f.write(secret_value) |
| 21 | + |
| 22 | + # Patch open to redirect /run/secrets/MYSECRET.secret to our temp file |
| 23 | + import builtins |
| 24 | + |
| 25 | + real_open = builtins.open |
| 26 | + |
| 27 | + def fake_open(path, *args, **kwargs): |
| 28 | + if path == f"/run/secrets/{secret_name}.secret": |
| 29 | + return real_open(secret_file, *args, **kwargs) |
| 30 | + return real_open(path, *args, **kwargs) |
| 31 | + |
| 32 | + monkeypatch.setattr("builtins.open", fake_open) |
| 33 | + |
| 34 | + # Provide all required fields for ApplicationConfig |
| 35 | + config_dict = { |
| 36 | + "application_info": { |
| 37 | + "application_id": "app-id", |
| 38 | + "application_name": "dummy", |
| 39 | + "deployed_application_id": "deploy-id", |
| 40 | + "deployed_application_name": "dummy-deploy", |
| 41 | + "is_release_version": False, |
| 42 | + "name": "dummy", # legacy/extra, ignored if not in model |
| 43 | + "system_id": "sys-id", |
| 44 | + "version": "1.0", |
| 45 | + }, |
| 46 | + "interfaces": {}, |
| 47 | + "peripherals": {"peripherals": []}, |
| 48 | + "config": {"password": "${secret.MYSECRET}"}, |
| 49 | + } |
| 50 | + config = load_config_from_json(config_dict) |
| 51 | + assert config.config["password"] == secret_value |
| 52 | + |
| 53 | + |
| 54 | +def test_get_config_value(): |
| 55 | + config = DummyAppConfig({"foo": 123, "bar": "baz"}) |
| 56 | + assert get_config_value(config, "foo") == 123 |
| 57 | + assert get_config_value(config, "bar") == "baz" |
| 58 | + assert get_config_value(config, "missing", default="x") == "x" |
| 59 | + with pytest.raises(KeyError): |
| 60 | + get_config_value(config, "missing2") |
0 commit comments