Skip to content

Commit

Permalink
Improve schema for environments.yaml (#80)
Browse files Browse the repository at this point in the history
* Disallow additional properties in environments
* Improve schema formatting
  • Loading branch information
bcumming authored Apr 5, 2023
1 parent c664459 commit c02a269
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 12 deletions.
6 changes: 3 additions & 3 deletions stackinator/schema/compilers.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Schema for Spack Stack compilers.yaml recipe file",
"type": "object",
"additionalProperties": false,
"required": ["bootstrap", "gcc"],
"defs": {
"gcc_version_spec": {
"type": "string",
Expand Down Expand Up @@ -65,8 +67,6 @@
],
"default": null
}
},
"additionalProperties": false,
"required": ["bootstrap", "gcc"]
}
}

16 changes: 8 additions & 8 deletions stackinator/schema/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Schema for Spack Stack config.yaml recipe file",
"type" : "object",
"additionalProperties": false,
"required": ["name", "system", "spack"],
"properties" : {
"name" : {
"type": "string"
Expand All @@ -15,6 +17,7 @@
},
"spack" : {
"type" : "object",
"additionalProperties": false,
"properties" : {
"repo": {
"type": "string"
Expand All @@ -26,11 +29,12 @@
],
"default": null
}
},
"additionalProperties": false
}
},
"mirror" : {
"type" : "object",
"additionalProperties": false,
"default": {"enable": true, "key": null},
"properties" : {
"enable" : {
"type": "boolean",
Expand All @@ -43,15 +47,11 @@
],
"default": null
}
},
"additionalProperties": false,
"default": {"enable": true, "key": null}
}
},
"modules" : {
"type": "boolean",
"default": true
}
},
"additionalProperties": false,
"required": ["name", "system", "spack"]
}
}
3 changes: 3 additions & 0 deletions stackinator/schema/environments.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"\\w[\\w-]*": {
"type": "object",
"required": ["compiler", "specs"],
"additionalProperties": false,
"properties": {
"unify": {
"enum": ["when_possible", true, false],
Expand All @@ -16,6 +17,7 @@
"type": "array",
"items": {
"type": "object",
"additionalProperties": false,
"properties": {
"toolchain": {"type": "string"},
"spec": {"type": "string"}
Expand All @@ -35,6 +37,7 @@
"oneOf": [
{
"type": "object",
"additionalProperties": false,
"properties": {
"spec": {"type": "string"},
"gpu": {"enum": ["cuda", "rocm", null, false]}
Expand Down
12 changes: 11 additions & 1 deletion unittests/test_schema.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/python3

import jsonschema
import pathlib

import pytest
import yaml

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

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


def test_recipe_environments_yaml(recipe_paths):
# validate the environments.yaml in the test recipes
Expand Down
22 changes: 22 additions & 0 deletions unittests/yaml/environments.err-providers.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
full-env:
compiler:
- toolchain: gcc
spec: gcc@11
- toolchain: gcc
spec: gcc@12
unify: when_possible
specs:
- [email protected]
- hdf5 +mpi
mpi:
spec: cray-mpich
gpu: cuda
packages:
- perl
- git
variants:
- +mpi
- +cuda
# expect an error because 'providers' is not defined in schema.
providers:
libglx: [opengl]

0 comments on commit c02a269

Please sign in to comment.