Skip to content

Commit 9c2eab0

Browse files
committed
add spec validation for the examples
- fix extension example since it was outputting a string extension which is invalid, per the spec (also a test) - fix the typing to disallow primitive types for extension content - added scripts/check.sh for easier validation before github actions get to run on any PRs - fixes #5, #12 Signed-off-by: mimir-d <[email protected]>
1 parent b9c7e6c commit 9c2eab0

File tree

7 files changed

+63
-32
lines changed

7 files changed

+63
-32
lines changed

.github/workflows/tests.yaml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,35 @@ jobs:
3030
- name: Test with pytest
3131
run: |
3232
pytest -v
33+
34+
spec:
35+
runs-on: ubuntu-latest
36+
name: examples / spec validation
37+
steps:
38+
- uses: actions/checkout@v4
39+
- name: Install python 3.11
40+
uses: actions/setup-python@v4
41+
with:
42+
python-version: 3.11
43+
- name: Install dependencies
44+
run: |
45+
python -m pip install --upgrade pip
46+
pip install -r requirements.txt
47+
- name: pull validator
48+
run: git clone https://github.com/opencomputeproject/ocp-diag-core.git --depth=1
49+
- name: Install go
50+
uses: actions/setup-go@v2
51+
with:
52+
go-version: "1.17.6"
53+
- name: run validator against examples
54+
run: |
55+
ROOT="$(pwd)"
56+
cd ocp-diag-core/validators/spec_validator
57+
PYTHONPATH="$ROOT" python -m examples list |
58+
grep -v demo_python_logging_io |
59+
xargs -I{} bash -c "
60+
echo validating output of example {}...
61+
PYTHONPATH=\"$ROOT\" python -m examples {} |
62+
tee /dev/stderr |
63+
go run . -schema ../../json_spec/output/root.json -
64+
"

DEVELOPER_NOTES.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ In order to do so:
3232
```bash
3333
$ pytest -v
3434
```
35-
4. *[optiona]* deactivate/exit the env
35+
4. *[optional]* deactivate/exit the env
3636
```bash
3737
$ deactivate
3838
```
@@ -61,6 +61,8 @@ Steps:
6161
$ black . # will reformat all the source files
6262
$ mypy ocptv tests examples --check-untyped-defs # check the type annotations
6363
```
64+
Alternatively, use `scripts/check.sh` to run all the CI checks (aside from spec validation).
65+
6466
4. if the tests above pass and everything is ready, push and make a PR. This can be done either from the Github website or using [gh cli](https://cli.github.com/manual/gh_pr_create).
6567
5. the PR will now be reviewed. If everything is ok, a maintainer will merge it to the `dev` branch.
6668

@@ -81,5 +83,5 @@ Run it just by simply using the `act -j pytest` command in the repository root d
8183
When necessary, likely due to code/doc changes, regenerate the api reference by running:
8284

8385
```bash
84-
$ .scripts/gendoc.sh
86+
$ scripts/gendoc.sh
8587
```

examples/extension.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,6 @@ def demo_step_extension():
1313
with run.scope(dut=tv.Dut(id="dut0")):
1414
step = run.add_step("step0")
1515
with step.scope():
16-
step.add_extension(
17-
name="simple",
18-
content="extension_identifier",
19-
)
20-
2116
step.add_extension(
2217
name="complex",
2318
content={

scripts/check.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
set -eo pipefail
3+
4+
# run all the CI checks, for local evaluation
5+
6+
# linter
7+
black . --check --diff
8+
9+
# typings
10+
mypy . --check-untyped-defs
11+
12+
# tests
13+
pytest -v --cov-fail-under=100
File renamed without changes.

src/ocptv/output/objects.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -767,11 +767,12 @@ def __post_init__(self):
767767
# note: these must specify some bounds for the extension content in python, despite
768768
# the spec saying that it can be anything. This is necessary for the json serializer
769769
# to actually know how to output the data.
770-
ExtensionContentType = ty.Union[ # pragma: no cover
771-
ty.Dict[str, "ExtensionContentType"],
772-
ty.List["ExtensionContentType"],
770+
ExtensionContentTypeInner = ty.Union[ # pragma: no cover
771+
ty.Dict[str, "ExtensionContentTypeInner"],
772+
ty.List["ExtensionContentTypeInner"],
773773
ty.Union[float, int, bool, str, None],
774774
]
775+
ExtensionContentType = ty.Dict[str, ExtensionContentTypeInner] # pragma: no cover
775776
else:
776777
# the runtime checker cannot deal with recursive types, and this is meant to be any
777778
ExtensionContentType = ty.Any

tests/output/test_run.py

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,14 @@
33

44
import ocptv.output as tv
55
from ocptv.formatter import format_timestamp
6-
from ocptv.output import DiagnosisType, LogSeverity, SoftwareType, TestResult, TestStatus, ValidatorType
6+
from ocptv.output import (
7+
DiagnosisType,
8+
LogSeverity,
9+
SoftwareType,
10+
TestResult,
11+
TestStatus,
12+
ValidatorType,
13+
)
714
from ocptv.output.emit import JSON
815

916
from .checks import IgnoreAssert, LambdaAssert, RangeAssert, assert_json
@@ -678,11 +685,6 @@ def test_step_produces_extensions(writer: MockWriter):
678685
with run.scope(dut=tv.Dut(id="dut0")):
679686
step = run.add_step("step0")
680687
with step.scope():
681-
step.add_extension(
682-
name="simple",
683-
content="extension_identifier",
684-
)
685-
686688
step.add_extension(
687689
name="complex",
688690
content={
@@ -692,23 +694,9 @@ def test_step_produces_extensions(writer: MockWriter):
692694
},
693695
)
694696

695-
assert len(writer.lines) == 7
697+
assert len(writer.lines) == 6
696698
assert_json(
697699
writer.lines[3],
698-
{
699-
"testStepArtifact": {
700-
"extension": {
701-
"name": "simple",
702-
"content": "extension_identifier",
703-
},
704-
"testStepId": "0",
705-
},
706-
"sequenceNumber": 3,
707-
"timestamp": IgnoreAssert(),
708-
},
709-
)
710-
assert_json(
711-
writer.lines[4],
712700
{
713701
"testStepArtifact": {
714702
"extension": {
@@ -721,7 +709,7 @@ def test_step_produces_extensions(writer: MockWriter):
721709
},
722710
"testStepId": "0",
723711
},
724-
"sequenceNumber": 4,
712+
"sequenceNumber": 3,
725713
"timestamp": IgnoreAssert(),
726714
},
727715
)

0 commit comments

Comments
 (0)