Skip to content

Commit 1a33f13

Browse files
committed
Attempt to check the schema of the limits.yaml file
1 parent a53da1c commit 1a33f13

File tree

2 files changed

+66
-2
lines changed

2 files changed

+66
-2
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import pytest
2+
import requests_mock
3+
import yaml
4+
5+
import update_config
6+
7+
8+
@pytest.mark.parametrize(
9+
"data",
10+
[
11+
("""
12+
VM.Standard2.1:
13+
1: 1
14+
2: 1
15+
3: 1
16+
VM.Standard2.2:
17+
1: 1
18+
2: 1
19+
3: 1
20+
"""),
21+
]
22+
)
23+
def test_get_limits(mocker, data):
24+
mocker.patch("update_config.load_yaml", return_value=yaml.safe_load(data))
25+
assert isinstance(update_config.get_limits(), dict)
26+
27+
28+
@pytest.mark.parametrize(
29+
"data,error",
30+
[
31+
("""
32+
VM.Standard2.1:
33+
1: 1
34+
2: 1
35+
3: 1
36+
VM.Standard2.2:
37+
1: 1
38+
2: 1
39+
3: 1
40+
""",
41+
SyntaxError),
42+
]
43+
)
44+
def test_get_limits_errors(mocker, data, error):
45+
mocker.patch("update_config.load_yaml", return_value=yaml.safe_load(data))
46+
with pytest.raises(error):
47+
update_config.get_limits()

roles/slurm/files/update_config.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,19 @@ def get_limits() -> Dict[str, Dict[str, str]]:
1616
Until OCI has an API to fetch service limits, we have to hard-code
1717
them in a file.
1818
"""
19-
return load_yaml("limits.yaml")
19+
limits = load_yaml("limits.yaml")
20+
for mappings in limits.values():
21+
if not isinstance(mappings, dict):
22+
raise SyntaxError
23+
for ad, count in mappings.items():
24+
if not isinstance(ad, int):
25+
raise SyntaxError
26+
if not isinstance(count, int):
27+
raise SyntaxError
28+
for shape in limits:
29+
if not re.match(r"", shape):
30+
raise ValueError
31+
return limits
2032

2133

2234
def get_shapes() -> Dict[str, Dict[str, str]]:
@@ -78,7 +90,12 @@ def get_node_configs(limits, shapes, mgmt_info):
7890

7991
slurm_conf_filename = "/mnt/shared/etc/slurm/slurm.conf"
8092

81-
node_config = "\n".join(get_node_configs(get_limits(), get_shapes(), get_mgmt_info()))
93+
try:
94+
limits = get_limits()
95+
except SyntaxError:
96+
print("ERROR: Syntax error in `limits.yaml`.")
97+
exit(1)
98+
node_config = "\n".join(get_node_configs(limits, get_shapes(), get_mgmt_info()))
8299

83100
chop = re.compile('(?<=# STARTNODES\n)(.*?)(?=\n?# ENDNODES)', re.DOTALL)
84101

0 commit comments

Comments
 (0)