Skip to content

Commit

Permalink
Attempt to check the schema of the limits.yaml file
Browse files Browse the repository at this point in the history
  • Loading branch information
milliams committed Oct 11, 2019
1 parent a53da1c commit 1a33f13
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 2 deletions.
47 changes: 47 additions & 0 deletions roles/slurm/files/test_update_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import pytest
import requests_mock
import yaml

import update_config


@pytest.mark.parametrize(
"data",
[
("""
VM.Standard2.1:
1: 1
2: 1
3: 1
VM.Standard2.2:
1: 1
2: 1
3: 1
"""),
]
)
def test_get_limits(mocker, data):
mocker.patch("update_config.load_yaml", return_value=yaml.safe_load(data))
assert isinstance(update_config.get_limits(), dict)


@pytest.mark.parametrize(
"data,error",
[
("""
VM.Standard2.1:
1: 1
2: 1
3: 1
VM.Standard2.2:
1: 1
2: 1
3: 1
""",
SyntaxError),
]
)
def test_get_limits_errors(mocker, data, error):
mocker.patch("update_config.load_yaml", return_value=yaml.safe_load(data))
with pytest.raises(error):
update_config.get_limits()
21 changes: 19 additions & 2 deletions roles/slurm/files/update_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,19 @@ def get_limits() -> Dict[str, Dict[str, str]]:
Until OCI has an API to fetch service limits, we have to hard-code
them in a file.
"""
return load_yaml("limits.yaml")
limits = load_yaml("limits.yaml")
for mappings in limits.values():
if not isinstance(mappings, dict):
raise SyntaxError
for ad, count in mappings.items():
if not isinstance(ad, int):
raise SyntaxError
if not isinstance(count, int):
raise SyntaxError
for shape in limits:
if not re.match(r"", shape):
raise ValueError
return limits


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

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

node_config = "\n".join(get_node_configs(get_limits(), get_shapes(), get_mgmt_info()))
try:
limits = get_limits()
except SyntaxError:
print("ERROR: Syntax error in `limits.yaml`.")
exit(1)
node_config = "\n".join(get_node_configs(limits, get_shapes(), get_mgmt_info()))

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

Expand Down

0 comments on commit 1a33f13

Please sign in to comment.