Skip to content

Commit c02a269

Browse files
authored
Improve schema for environments.yaml (#80)
* Disallow additional properties in environments * Improve schema formatting
1 parent c664459 commit c02a269

5 files changed

Lines changed: 47 additions & 12 deletions

File tree

stackinator/schema/compilers.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
"$schema": "http://json-schema.org/draft-07/schema#",
33
"title": "Schema for Spack Stack compilers.yaml recipe file",
44
"type": "object",
5+
"additionalProperties": false,
6+
"required": ["bootstrap", "gcc"],
57
"defs": {
68
"gcc_version_spec": {
79
"type": "string",
@@ -65,8 +67,6 @@
6567
],
6668
"default": null
6769
}
68-
},
69-
"additionalProperties": false,
70-
"required": ["bootstrap", "gcc"]
70+
}
7171
}
7272

stackinator/schema/config.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
"$schema": "http://json-schema.org/draft-07/schema#",
33
"title": "Schema for Spack Stack config.yaml recipe file",
44
"type" : "object",
5+
"additionalProperties": false,
6+
"required": ["name", "system", "spack"],
57
"properties" : {
68
"name" : {
79
"type": "string"
@@ -15,6 +17,7 @@
1517
},
1618
"spack" : {
1719
"type" : "object",
20+
"additionalProperties": false,
1821
"properties" : {
1922
"repo": {
2023
"type": "string"
@@ -26,11 +29,12 @@
2629
],
2730
"default": null
2831
}
29-
},
30-
"additionalProperties": false
32+
}
3133
},
3234
"mirror" : {
3335
"type" : "object",
36+
"additionalProperties": false,
37+
"default": {"enable": true, "key": null},
3438
"properties" : {
3539
"enable" : {
3640
"type": "boolean",
@@ -43,15 +47,11 @@
4347
],
4448
"default": null
4549
}
46-
},
47-
"additionalProperties": false,
48-
"default": {"enable": true, "key": null}
50+
}
4951
},
5052
"modules" : {
5153
"type": "boolean",
5254
"default": true
5355
}
54-
},
55-
"additionalProperties": false,
56-
"required": ["name", "system", "spack"]
56+
}
5757
}

stackinator/schema/environments.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"\\w[\\w-]*": {
88
"type": "object",
99
"required": ["compiler", "specs"],
10+
"additionalProperties": false,
1011
"properties": {
1112
"unify": {
1213
"enum": ["when_possible", true, false],
@@ -16,6 +17,7 @@
1617
"type": "array",
1718
"items": {
1819
"type": "object",
20+
"additionalProperties": false,
1921
"properties": {
2022
"toolchain": {"type": "string"},
2123
"spec": {"type": "string"}
@@ -35,6 +37,7 @@
3537
"oneOf": [
3638
{
3739
"type": "object",
40+
"additionalProperties": false,
3841
"properties": {
3942
"spec": {"type": "string"},
4043
"gpu": {"enum": ["cuda", "rocm", null, false]}

unittests/test_schema.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/python3
22

3+
import jsonschema
34
import pathlib
4-
55
import pytest
66
import yaml
77

@@ -128,6 +128,16 @@ def test_environments_yaml(yaml_path):
128128
assert env["mpi"] == {"spec": "cray-mpich", "gpu": "cuda"}
129129
assert env["variants"] == ["+mpi", "+cuda"]
130130

131+
# check that only allowed fields are accepted
132+
# from an example that was silently validated
133+
with open(yaml_path / "environments.err-providers.yaml") as fid:
134+
raw = yaml.load(fid, Loader=yaml.Loader)
135+
with pytest.raises(
136+
jsonschema.exceptions.ValidationError,
137+
match=r"Additional properties are not allowed \('providers' was unexpected",
138+
):
139+
schema.validator(schema.environments_schema).validate(raw)
140+
131141

132142
def test_recipe_environments_yaml(recipe_paths):
133143
# validate the environments.yaml in the test recipes
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
full-env:
2+
compiler:
3+
- toolchain: gcc
4+
spec: gcc@11
5+
- toolchain: gcc
6+
spec: gcc@12
7+
unify: when_possible
8+
specs:
9+
- osu-micro-benchmarks@5.9
10+
- hdf5 +mpi
11+
mpi:
12+
spec: cray-mpich
13+
gpu: cuda
14+
packages:
15+
- perl
16+
- git
17+
variants:
18+
- +mpi
19+
- +cuda
20+
# expect an error because 'providers' is not defined in schema.
21+
providers:
22+
libglx: [opengl]

0 commit comments

Comments
 (0)