Skip to content

Commit 3d43def

Browse files
author
Trong Nhan Mai
committed
chore: move report schema to a share location; add a schema step
1 parent b80bc76 commit 3d43def

File tree

6 files changed

+98
-6
lines changed

6 files changed

+98
-6
lines changed

tests/integration/README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ In case you want to debug the utility script itself, there is the verbose mode f
191191
* `"verify"`: runs the `macaron verify-policy` command.
192192
* `"compare"`: compares an output file with an expected output file.
193193
* `"shell"`: runs an arbitrary shell command.
194+
* `"schema"`: validates an output file with a schema.
194195
* `options`: Configuration options for the step. These options are specific to the step kind. See their schema below.
195196
* `env` (`dict[string, string | null]`, optional): Key value pairs of environment variables being modified during the step after inheriting the environment in which the utility is executed within. Each value can be a string if you want to set a value to the environment variable, or null if you want to "unset" the variable.
196197
* `expect_fail` (`bool`, optional, default is `false`): If `true`, assert that the step must exit with non-zero code. This should be used for cases where we expect a command to fail.
@@ -214,10 +215,17 @@ In case you want to debug the utility script itself, there is the verbose mode f
214215
215216
### Compare step options Schema
216217
217-
* `kind` (`"analysis_report_json_schema" | "analysis_report" | "policy_report" | "deps_report" | "vsa"`, required): The kind of JSON report to compare.
218+
* `kind` (`"analysis_report" | "policy_report" | "deps_report" | "vsa"`, required): The kind of JSON report to compare.
218219
* `result` (`string`, required): The output file (a relative path from test case directory).
219220
* `expected` (`string`, required): The expected output file (a relative path from test case directory).
220221
222+
### Schema step options Schema
223+
224+
* `kind` (`"json_schema"`, required): The kind of schema validation to perform. For now, only json-schema is supported.
225+
* `result` (`string`, required): The output file (a relative path from test case directory).
226+
* `schema_type` (`output_json_report`, required): The type of schema for the validation. These are the default schemas available for the integration test.
227+
* `custom_schema_path` (`string`, optional): The path to the custom schema (a relative path from test case directory). If it is provided, the validation will use this schema and ignore the schema corresponding with `schema_type`.
228+
221229
### Shell step options Schema
222230
223231
* `cmd` (`string`, required): The shell command to run.

tests/integration/cases/micronaut-projects_micronaut-test/test.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ steps:
1515
- -c
1616
- micronaut_test_config.yaml
1717
- --skip-deps
18-
- name: Compare JSON report schema
19-
kind: compare
18+
- name: Validate JSON report schema
19+
kind: schema
2020
options:
21-
kind: analysis_report_json_schema
21+
kind: json_schema
22+
schema_type: output_json_report
2223
result: output/reports/github_com/micronaut-projects/micronaut-test/micronaut-test.json
23-
expected: ./report_schema.json
2424
- name: Compare dependency report
2525
kind: compare
2626
options:

tests/integration/run.py

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,20 @@ def configure_logging(verbose: bool) -> None:
7575

7676

7777
COMPARE_SCRIPTS: dict[str, Sequence[str]] = {
78-
"analysis_report_json_schema": ["tests", "analyze_report_json_schema", "schema_validate.py"],
7978
"analysis_report": ["tests", "analyze_json_output", "compare_analyze_json_output.py"],
8079
"policy_report": ["tests", "policy_engine", "compare_policy_reports.py"],
8180
"deps_report": ["tests", "dependency_analyzer", "compare_dependencies.py"],
8281
"vsa": ["tests", "vsa", "compare_vsa.py"],
8382
}
8483

84+
VALIDATE_SCHEMA_SCRIPTS: dict[str, Sequence[str]] = {
85+
"json_schema": ["tests", "schema_validation", "json_schema_validate.py"],
86+
}
87+
88+
DEFAULT_SCHEMAS: dict[str, Sequence[str]] = {
89+
"output_json_report": ["tests", "schema_validation", "report_schema.json"],
90+
}
91+
8592

8693
def check_required_file(cwd: str) -> Callable[[str], None]:
8794
"""Check for a required file of a test case."""
@@ -209,6 +216,72 @@ def cmd(self, macaron_cmd: str) -> list[str]:
209216
return self.options["cmd"].strip().split()
210217

211218

219+
class SchemaStepOptions(TypedDict):
220+
"""The configuration options of a schema validation step."""
221+
222+
kind: str
223+
result: str
224+
schema_type: str
225+
custom_schema_path: str | None
226+
227+
228+
@dataclass
229+
class SchemaStep(Step[SchemaStepOptions]):
230+
"""A schema validation step in a test case, which allows for validating a file against a schema."""
231+
232+
@staticmethod
233+
def options_schema(cwd: str, check_expected_result_files: bool) -> cfgv.Map:
234+
"""Generate the schema of a schema validation step."""
235+
if check_expected_result_files:
236+
check_file = check_required_file(cwd)
237+
else:
238+
check_file = cfgv.check_string
239+
240+
return cfgv.Map(
241+
"schema options",
242+
None,
243+
*[
244+
cfgv.Required(
245+
key="kind",
246+
check_fn=cfgv.check_one_of(tuple(VALIDATE_SCHEMA_SCRIPTS.keys())),
247+
),
248+
cfgv.Required(
249+
key="result",
250+
check_fn=cfgv.check_string,
251+
),
252+
cfgv.Required(
253+
key="schema_type",
254+
check_fn=cfgv.check_one_of(tuple(DEFAULT_SCHEMAS.keys())),
255+
),
256+
cfgv.Optional(
257+
key="custom_schema_path",
258+
default=None,
259+
check_fn=check_file,
260+
),
261+
],
262+
)
263+
264+
def cmd(self, macaron_cmd: str) -> list[str]:
265+
kind = self.options["kind"]
266+
result_file = self.options["result"]
267+
schema_type = self.options["schema_type"]
268+
custom_schema_path = self.options["custom_schema_path"]
269+
270+
if custom_schema_path is None:
271+
return [
272+
"python",
273+
os.path.abspath(os.path.join(*VALIDATE_SCHEMA_SCRIPTS[kind])),
274+
*[result_file, os.path.abspath(os.path.join(*DEFAULT_SCHEMAS[schema_type]))],
275+
]
276+
277+
logger.info("A custom schema path at %s is given, using that instead.", custom_schema_path)
278+
return [
279+
"python",
280+
os.path.abspath(os.path.join(*VALIDATE_SCHEMA_SCRIPTS[kind])),
281+
*[result_file, custom_schema_path],
282+
]
283+
284+
212285
class CompareStepOptions(TypedDict):
213286
"""Configuration of a compare step."""
214287

@@ -474,6 +547,7 @@ def gen_step_schema(cwd: str, check_expected_result_files: bool) -> cfgv.Map:
474547
"compare",
475548
"analyze",
476549
"verify",
550+
"schema",
477551
),
478552
),
479553
),
@@ -483,6 +557,15 @@ def gen_step_schema(cwd: str, check_expected_result_files: bool) -> cfgv.Map:
483557
key="options",
484558
schema=ShellStep.options_schema(),
485559
),
560+
cfgv.ConditionalRecurse(
561+
condition_key="kind",
562+
condition_value="schema",
563+
key="options",
564+
schema=SchemaStep.options_schema(
565+
cwd=cwd,
566+
check_expected_result_files=check_expected_result_files,
567+
),
568+
),
486569
cfgv.ConditionalRecurse(
487570
condition_key="kind",
488571
condition_value="compare",
@@ -700,6 +783,7 @@ def parse_step_config(step_id: int, step_config: Mapping) -> Step:
700783
"verify": VerifyStep,
701784
"shell": ShellStep,
702785
"compare": CompareStep,
786+
"schema": SchemaStep,
703787
}[kind]
704788
return step_cls( # type: ignore # https://github.com/python/mypy/issues/3115
705789
step_id=step_id,
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)