Skip to content

Commit

Permalink
Add rulebook schema (#3103)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssbarnea committed Mar 3, 2023
1 parent ca569d0 commit f35633c
Show file tree
Hide file tree
Showing 13 changed files with 507 additions and 73 deletions.
2 changes: 2 additions & 0 deletions .config/dictionary.txt
Expand Up @@ -36,6 +36,7 @@ aarch64
abspath
addoption
addopts
alertmanager
ansiblelint
apidoc
apport
Expand Down Expand Up @@ -124,6 +125,7 @@ extlinks
facelessuser
facter
fakerole
fastapi
fileglob
filelock
filesspot
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tox.yml
Expand Up @@ -70,7 +70,7 @@ jobs:
WSLENV: FORCE_COLOR:PYTEST_REQPASS:TOXENV:GITHUB_STEP_SUMMARY
# Number of expected test passes, safety measure for accidental skip of
# tests. Update value if you add/remove tests.
PYTEST_REQPASS: 807
PYTEST_REQPASS: 809
steps:
- name: Activate WSL1
if: "contains(matrix.shell, 'wsl')"
Expand Down
9 changes: 5 additions & 4 deletions .pre-commit-config.yaml
Expand Up @@ -18,11 +18,12 @@ exclude: >
.vscode/extensions.json|
.vscode/settings.json|
examples/broken/encoding.j2|
examples/broken/yaml-with-tabs/invalid-due-tabs.yaml|
examples/playbooks/collections/.*|
src/ansiblelint/schemas/rulebook.json|
test/schemas/data/licenses.json|
test/schemas/package-lock.json|
test/schemas/negative_test|
examples/broken/yaml-with-tabs/invalid-due-tabs.yaml|
examples/playbooks/collections/.*
test/schemas/package-lock.json
)$
repos:
- repo: meta
Expand All @@ -48,7 +49,7 @@ repos:
test/fixtures/formatting-before/.*|
test/schemas/data/.*|
test/schemas/(negative_test|test)/.*\.md|
src/ansiblelint/schemas/(molecule|tasks|playbook).json|
src/ansiblelint/schemas/(molecule|tasks|playbook|rulebook).json|
src/ansiblelint/schemas/ansible-navigator-config.json
)$
always_run: true
Expand Down
2 changes: 2 additions & 0 deletions examples/changelogs/changelog.yaml
@@ -0,0 +1,2 @@
---
foo: bar
21 changes: 21 additions & 0 deletions examples/rulebooks/rulebook-fail.yml
@@ -0,0 +1,21 @@
---
- name: Sample rulebooks
hosts: all
that_should_not_be_here: foo
sources: # should be "sources"
- name: listen for alerts
ansible.eda.alertmanager:
host: 0.0.0.0
port: 8000
rules:
- name: restart web server
condition: event.alert.labels.job == "fastapi" and event.alert.status == "firing"
action:
run_playbook:
name: ansible.eda.start_app
copy_files: true
post_events: true
- name: debug
condition: event.alert.labels.job == "fastapi"
action:
debug: sss
Expand Up @@ -9,11 +9,7 @@
port: 9092
group_id: testing
rules:
- name:
- name: Demo rule
condition: event.i is defined
action:
debug:
- name:
condition: event.stop == true
action:
shutdown:
88 changes: 52 additions & 36 deletions src/ansiblelint/rules/schema.py
Expand Up @@ -111,107 +111,123 @@ def matchyaml(self, file: Lintable) -> list[MatchError]:
@pytest.mark.parametrize(
("file", "expected_kind", "expected"),
(
(
pytest.param(
"examples/collection/galaxy.yml",
"galaxy",
["'GPL' is not one of"],
id="galaxy",
),
(
pytest.param(
"examples/roles/invalid_requirements_schema/meta/requirements.yml",
"requirements",
["{'foo': 'bar'} is not valid under any of the given schemas"],
id="requirements",
),
(
pytest.param(
"examples/roles/invalid_meta_schema/meta/main.yml",
"meta",
["False is not of type 'string'"],
id="meta",
),
(
pytest.param(
"examples/playbooks/vars/invalid_vars_schema.yml",
"vars",
["'123' does not match any of the regexes"],
id="vars",
),
(
pytest.param(
"examples/execution-environment.yml",
"execution-environment",
[],
id="execution-environment",
),
(
pytest.param(
"examples/ee_broken/execution-environment.yml",
"execution-environment",
["Additional properties are not allowed ('foo' was unexpected)"],
id="execution-environment-broken",
),
("examples/meta/runtime.yml", "meta-runtime", []),
(
pytest.param(
"examples/broken_collection_meta_runtime/meta/runtime.yml",
"meta-runtime",
["Additional properties are not allowed ('foo' was unexpected)"],
id="meta-runtime-broken",
),
(
pytest.param(
"examples/inventory/production.yml",
"inventory",
[],
id="inventory",
),
(
pytest.param(
"examples/inventory/broken_dev_inventory.yml",
"inventory",
["Additional properties are not allowed ('foo' was unexpected)"],
id="inventory-broken",
),
(
pytest.param(
".ansible-lint",
"ansible-lint-config",
[],
id="ansible-lint-config",
),
(
pytest.param(
"examples/.config/ansible-lint.yml",
"ansible-lint-config",
[],
id="ansible-lint-config2",
),
(
pytest.param(
"examples/broken/.ansible-lint",
"ansible-lint-config",
["Additional properties are not allowed ('foo' was unexpected)"],
id="ansible-lint-config-broken",
),
(
pytest.param(
"examples/ansible-navigator.yml",
"ansible-navigator-config",
[],
id="ansible-navigator-config",
),
(
pytest.param(
"examples/broken/ansible-navigator.yml",
"ansible-navigator-config",
["Additional properties are not allowed ('ansible' was unexpected)"],
id="ansible-navigator-config-broken",
),
(
pytest.param(
"examples/roles/hello/meta/argument_specs.yml",
"arg_specs",
[],
id="arg_specs",
),
(
pytest.param(
"examples/roles/broken_argument_specs/meta/argument_specs.yml",
"arg_specs",
["Additional properties are not allowed ('foo' was unexpected)"],
id="arg_specs-broken",
),
pytest.param(
"examples/changelogs/changelog.yaml",
"changelog",
["Additional properties are not allowed ('foo' was unexpected)"],
id="changelog",
),
pytest.param(
"examples/rulebooks/rulebook-fail.yml",
"rulebook",
[
"Additional properties are not allowed ('that_should_not_be_here' was unexpected)"
],
id="rulebook",
),
pytest.param(
"examples/rulebooks/rulebook-pass.yml",
"rulebook",
[],
id="rulebook2",
),
),
ids=(
# "playbook-fail",
"galaxy",
"requirements",
"meta",
"vars",
"ee",
"ee-broken",
"meta-runtime",
"meta-runtime-broken",
"inventory",
"inventory-broken",
"lint-config",
"lint-config2",
"lint-config-broken",
"navigator",
"navigator-broken",
"argspecs",
"argspecs-broken",
),
)
def test_schema(file: str, expected_kind: str, expected: list[str]) -> None:
Expand Down
2 changes: 1 addition & 1 deletion src/ansiblelint/schemas/__main__.py
Expand Up @@ -113,7 +113,7 @@ def refresh_schemas(min_age_seconds: int = 3600 * 24) -> int:


if __name__ == "__main__":
if refresh_schemas(): # pragma: no cover
if refresh_schemas(60 * 10): # pragma: no cover
# flake8: noqa: T201
print("Schemas were updated.")
sys.exit(1)
Expand Down
4 changes: 4 additions & 0 deletions src/ansiblelint/schemas/__store__.json
Expand Up @@ -47,6 +47,10 @@
"etag": "a11edf24f416043f2da8dd329f1d61338fc9708e017bd3cbe43d8c06e4b30090",
"url": "https://raw.githubusercontent.com/ansible/ansible-lint/main/src/ansiblelint/schemas/requirements.json"
},
"rulebook": {
"etag": "64b248fdbf6cede0c0e1736b51b4235f8d06ad602346ce06bf15d913708d9b30",
"url": "https://raw.githubusercontent.com/ansible/ansible-rulebook/main/ansible_rulebook/schema/ruleset_schema.json"
},
"tasks": {
"etag": "9226a9fea0a3cfa1bbc6c4db57fb4b28e5cf8abd7f390fa4a93cf1cf38ac64cd",
"url": "https://raw.githubusercontent.com/ansible/ansible-lint/main/src/ansiblelint/schemas/tasks.json"
Expand Down

0 comments on commit f35633c

Please sign in to comment.