Skip to content

Commit cef5317

Browse files
authored
Merge pull request #8 from obeone/codex/implémenter-des-tests
Add unit tests for config generation
2 parents ee9dbe2 + 274583e commit cef5317

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed

tests/test_functions.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import os
2+
import sys
3+
sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
4+
5+
import functions
6+
import yaml
7+
8+
9+
def test_interpolate_strings_basic():
10+
obj = {"a": "{var1} {var2}", "b": ["{var2}"]}
11+
variables = {"var1": "foo", "var2": "bar"}
12+
result = functions.interpolate_strings(obj, variables)
13+
assert result == {"a": "foo bar", "b": ["bar"]}
14+
15+
16+
def test_create_registry_config_cache():
17+
base_config = {"proxy": {}, "redis": {}}
18+
registry = {
19+
"type": "cache",
20+
"url": "https://example.com",
21+
"username": "user",
22+
"password": "pass",
23+
"ttl": "24h",
24+
"name": "example",
25+
}
26+
cfg = functions.create_registry_config(base_config, registry, 2)
27+
assert cfg["proxy"]["remoteurl"] == "https://example.com"
28+
assert cfg["proxy"]["username"] == "user"
29+
assert cfg["proxy"]["password"] == "pass"
30+
assert cfg["proxy"]["ttl"] == "24h"
31+
assert cfg["redis"]["db"] == 2
32+
33+
34+
def test_create_registry_config_registry_type_removes_proxy():
35+
base_config = {"proxy": {}, "redis": {}}
36+
registry = {"type": "registry", "name": "private"}
37+
cfg = functions.create_registry_config(base_config, registry, 1)
38+
assert "proxy" not in cfg
39+
assert cfg["redis"]["db"] == 1
40+
41+
42+
def test_create_docker_service_interpolates_fields():
43+
registry = {"name": "foo"}
44+
custom = {"image": "registry:2", "volumes": ["./{name}.yaml:/data/{name}.yaml"]}
45+
result = functions.create_docker_service(registry, custom)
46+
assert result == {"image": "registry:2", "volumes": ["./foo.yaml:/data/foo.yaml"]}
47+
48+
49+
def test_create_traefik_router_and_service(tmp_path):
50+
registry = {"name": "bar"}
51+
router_custom = {"rule": "Host(`{name}.example.com`)"}
52+
service_custom = {"servers": [{"url": "http://{name}:5000"}]}
53+
54+
router = functions.create_traefik_router(registry, router_custom)
55+
service = functions.create_traefik_service(registry, service_custom)
56+
57+
assert router["rule"] == "Host(`bar.example.com`)"
58+
assert service["servers"][0]["url"] == "http://bar:5000"
59+
60+
61+
def test_write_helpers(tmp_path):
62+
yaml_file = tmp_path / "test.yaml"
63+
text_file = tmp_path / "test.txt"
64+
65+
data = {"hello": "world"}
66+
functions.write_yaml_file(yaml_file, data)
67+
functions.write_to_file(text_file, "content")
68+
69+
with open(yaml_file) as f:
70+
loaded = yaml.safe_load(f)
71+
with open(text_file) as f:
72+
txt = f.read()
73+
74+
assert loaded == data
75+
assert txt == "content"
76+
77+
78+
def test_write_http_secret_idempotent(tmp_path, monkeypatch):
79+
compose_dir = tmp_path / "compose"
80+
compose_dir.mkdir()
81+
monkeypatch.chdir(tmp_path)
82+
83+
# First call creates the secret
84+
functions.write_http_secret()
85+
env_file = compose_dir / ".env"
86+
with open(env_file) as f:
87+
lines = f.readlines()
88+
assert len(lines) == 1
89+
assert lines[0].startswith("REGISTRY_HTTP_SECRET=")
90+
first_secret = lines[0]
91+
92+
# Second call should not append another secret
93+
functions.write_http_secret()
94+
with open(env_file) as f:
95+
lines_after = f.readlines()
96+
assert lines_after[0] == first_secret
97+
assert len(lines_after) == 1

0 commit comments

Comments
 (0)