Merge ~lloydwaltersj/maas:oapi-linter into maas:master
- Git
- lp:~lloydwaltersj/maas
- oapi-linter
- Merge into master
Status: | Merged |
---|---|
Approved by: | Jack Lloyd-Walters |
Approved revision: | 1cbbc4976c25603e92dad0e557d623ebe889a17f |
Merge reported by: | MAAS Lander |
Merged at revision: | not available |
Proposed branch: | ~lloydwaltersj/maas:oapi-linter |
Merge into: | maas:master |
Diff against target: |
135 lines (+79/-2) 3 files modified
Makefile (+6/-1) setup.cfg (+10/-1) utilities/check-oapi (+63/-0) |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
MAAS Lander | Approve | ||
Alexsander de Souza | Approve | ||
Adam Collard (community) | Abstain | ||
Review via email: mp+428646@code.launchpad.net |
Commit message
Add a linter for the OpenAPI spec
Description of the change
add the ability to lint the MAAS Http api against OAS3
Alexsander de Souza (alexsander-souza) wrote : | # |
use tox to handle the test environment
Adam Collard (adam-collard) wrote : | # |
Don't think we should consider this a linter in the same way that we do the other linters.
It should be implemented as a test in the api package, alongside the API definition, documentation etc.
If this fails, it's not because we've used a deprecated way of writing Python, or sorted imports differently, it's a failure close to the OpenAPI feature.
Alexsander de Souza (alexsander-souza) wrote : | # |
> If this fails, it's not because we've used a deprecated way of writing Python,
> or sorted imports differently, it's a failure close to the OpenAPI feature.
I think this is real linter, because if this fails it means that we failed to write valid documentation for whatever we are doing, not that the OpenAPI feature has a bug.
- cd623b8... by Jack Lloyd-Walters
-
transition to tox
MAAS Lander (maas-lander) wrote : | # |
UNIT TESTS
-b oapi-linter lp:~lloydwaltersj/maas/+git/maas into -b master lp:~maas-committers/maas
STATUS: FAILED
LOG: http://
COMMIT: cd623b889ddf406
- a90fe6e... by Jack Lloyd-Walters
-
change how oapi doccs are generated for linting
Jack Lloyd-Walters (lloydwaltersj) wrote (last edit ): | # |
> STATUS: FAILED
> LOG: http://
> COMMIT: cd623b889ddf406
This is working exactly as expected, a successfull failure if you will!
MAAS Lander (maas-lander) wrote : | # |
UNIT TESTS
-b oapi-linter lp:~lloydwaltersj/maas/+git/maas into -b master lp:~maas-committers/maas
STATUS: FAILED
LOG: http://
COMMIT: a90fe6e49bee699
Jack Lloyd-Walters (lloydwaltersj) wrote : | # |
jenkins: !test
MAAS Lander (maas-lander) wrote : | # |
UNIT TESTS
-b oapi-linter lp:~lloydwaltersj/maas/+git/maas into -b master lp:~maas-committers/maas
STATUS: FAILED
LOG: http://
COMMIT: a90fe6e49bee699
- 39c02c3... by Jack Lloyd-Walters
-
bring branch up to parity
- c0695f9... by Jack Lloyd-Walters
-
revert yaml.dump change
Adam Collard (adam-collard) : | # |
MAAS Lander (maas-lander) wrote : | # |
UNIT TESTS
-b oapi-linter lp:~lloydwaltersj/maas/+git/maas into -b master lp:~maas-committers/maas
STATUS: FAILED
LOG: http://
COMMIT: c0695f98b1e9ced
- 4eaea74... by Jack Lloyd-Walters
-
remove make docs from lint
MAAS Lander (maas-lander) wrote : | # |
UNIT TESTS
-b oapi-linter lp:~lloydwaltersj/maas/+git/maas into -b master lp:~maas-committers/maas
STATUS: FAILED
LOG: http://
COMMIT: 4eaea745bfebe77
Adam Collard (adam-collard) : | # |
- 2c01771... by Jack Lloyd-Walters
-
remove hardcoded openapi file location
MAAS Lander (maas-lander) wrote : | # |
UNIT TESTS
-b oapi-linter lp:~lloydwaltersj/maas/+git/maas into -b master lp:~maas-committers/maas
STATUS: FAILED
LOG: http://
COMMIT: 2c01771792cbdf7
Adam Collard (adam-collard) : | # |
- 6247138... by Jack Lloyd-Walters
-
bring branch up to parity
MAAS Lander (maas-lander) wrote : | # |
UNIT TESTS
-b oapi-linter lp:~lloydwaltersj/maas/+git/maas into -b master lp:~maas-committers/maas
STATUS: FAILED
LOG: http://
COMMIT: 6247138baf4652b
- b55d865... by Jack Lloyd-Walters
-
bring branch up to parity
MAAS Lander (maas-lander) wrote : | # |
UNIT TESTS
-b oapi-linter lp:~lloydwaltersj/maas/+git/maas into -b master lp:~maas-committers/maas
STATUS: FAILED
LOG: http://
COMMIT: b55d865e978a033
- 8421bf8... by Jack Lloyd-Walters
-
generate spec if missing
- bb8d96d... by Jack Lloyd-Walters
-
make linter depend on spec
MAAS Lander (maas-lander) wrote : | # |
UNIT TESTS
-b oapi-linter lp:~lloydwaltersj/maas/+git/maas into -b master lp:~maas-committers/maas
STATUS: SUCCESS
COMMIT: 8421bf8f4b32f39
MAAS Lander (maas-lander) wrote : | # |
UNIT TESTS
-b oapi-linter lp:~lloydwaltersj/maas/+git/maas into -b master lp:~maas-committers/maas
STATUS: SUCCESS
COMMIT: bb8d96d2bf145e9
Adam Collard (adam-collard) : | # |
Adam Collard (adam-collard) : | # |
- 921393f... by Jack Lloyd-Walters
-
remove help supression
MAAS Lander (maas-lander) wrote : | # |
UNIT TESTS
-b oapi-linter lp:~lloydwaltersj/maas/+git/maas into -b master lp:~maas-committers/maas
STATUS: FAILED
LOG: http://
COMMIT: 921393ffd0cc2f1
Jack Lloyd-Walters (lloydwaltersj) wrote : | # |
jenkins: !test
MAAS Lander (maas-lander) wrote : | # |
UNIT TESTS
-b oapi-linter lp:~lloydwaltersj/maas/+git/maas into -b master lp:~maas-committers/maas
STATUS: SUCCESS
COMMIT: 921393ffd0cc2f1
Alexsander de Souza (alexsander-souza) wrote : | # |
+1
- 46f3269... by Jack Lloyd-Walters
-
remove lint-oapi from lint target
- 1cbbc49... by Jack Lloyd-Walters
-
move oapi linter to test phase
MAAS Lander (maas-lander) wrote : | # |
UNIT TESTS
-b oapi-linter lp:~lloydwaltersj/maas/+git/maas into -b master lp:~maas-committers/maas
STATUS: SUCCESS
COMMIT: 1cbbc4976c25603
Preview Diff
1 | diff --git a/Makefile b/Makefile |
2 | index 5a5c2af..903c5af 100644 |
3 | --- a/Makefile |
4 | +++ b/Makefile |
5 | @@ -121,7 +121,7 @@ go-bins: |
6 | $(MAKE) -j -C src/host-info build |
7 | .PHONY: go-bins |
8 | |
9 | -test: test-missing-migrations test-py |
10 | +test: test-missing-migrations test-py lint-oapi |
11 | .PHONY: test |
12 | |
13 | test-missing-migrations: bin/database bin/maas-region |
14 | @@ -166,6 +166,11 @@ lint-py-linefeeds: |
15 | (echo "Lint check failed; run make format to fix DOS linefeeds."; false) |
16 | .PHONY: lint-py-linefeeds |
17 | |
18 | +# Open API Spec |
19 | +lint-oapi: openapi.yaml |
20 | + @tox -e oapi |
21 | +.PHONY: lint-oapi |
22 | + |
23 | # Go fmt |
24 | lint-go: |
25 | @find src/ \( -name pkg -o -name vendor \) -prune -o -name '*.go' -exec gofmt -l {} + | \ |
26 | diff --git a/setup.cfg b/setup.cfg |
27 | index 5d02873..c4c7faa 100644 |
28 | --- a/setup.cfg |
29 | +++ b/setup.cfg |
30 | @@ -94,6 +94,7 @@ lint_files = |
31 | src/provisioningserver/refresh/50-maas-01-commissioning |
32 | utilities/bin2python.py |
33 | utilities/check-imports |
34 | + utilities/check-oapi |
35 | utilities/import-db-dump |
36 | utilities/python_standard_libs.py |
37 | utilities/snap-bind-mounts |
38 | @@ -109,6 +110,9 @@ deps_lint = |
39 | cogapp == 3.3.0 |
40 | click == 8.1.3 |
41 | |
42 | +deps_oapi = |
43 | + openapi-spec-validator == 0.4.0 |
44 | + |
45 | [flake8] |
46 | ignore = E203, E266, E501, W503, W504 |
47 | exclude = |
48 | @@ -116,7 +120,7 @@ exclude = |
49 | |
50 | [tox:tox] |
51 | skipsdist = True |
52 | -envlist = format,lint |
53 | +envlist = format,lint,oapi |
54 | |
55 | [testenv:format] |
56 | deps = {[globals]deps_lint} |
57 | @@ -132,3 +136,8 @@ commands = |
58 | black --check {[globals]lint_files} |
59 | flake8 {[globals]lint_files} |
60 | cog --check --verbosity=1 {[globals]cog_files} |
61 | + |
62 | +[testenv:oapi] |
63 | +deps = {[globals]deps_oapi} |
64 | +commands = |
65 | + python3 utilities/check-oapi openapi.yaml |
66 | \ No newline at end of file |
67 | diff --git a/utilities/check-oapi b/utilities/check-oapi |
68 | new file mode 100755 |
69 | index 0000000..d77ae23 |
70 | --- /dev/null |
71 | +++ b/utilities/check-oapi |
72 | @@ -0,0 +1,63 @@ |
73 | +#!/usr/bin/env python3 |
74 | + |
75 | +"""Script to ensure that an OpenAPI spec matches OAS3""" |
76 | + |
77 | +import argparse |
78 | +import sys |
79 | + |
80 | +from openapi_spec_validator import openapi_v3_spec_validator as validator |
81 | +import yaml |
82 | + |
83 | +failure = "\033[0;31m" |
84 | +success = "\033[0;32m" |
85 | +end = "\033[0m" |
86 | + |
87 | + |
88 | +def fetch_errors(errors): |
89 | + """Return all the errors in the oapi spec with |
90 | + some nice formatting for the terminal |
91 | + """ |
92 | + num = len(errors) |
93 | + pad = len(str(num)) |
94 | + err_ = [f"{num} problems with the spec:"] |
95 | + for i, err in enumerate(errors): |
96 | + msg, reason, loc = str(err).split("\n\n") |
97 | + i = str(i).rjust(pad) |
98 | + err_ += [ |
99 | + end |
100 | + + f"{i}: {msg}\n{failure}{reason}\n{loc}".replace( |
101 | + "\n", "\n" + " " * (2 + pad) |
102 | + ) |
103 | + ] |
104 | + return failure + "\n\n".join(err for err in err_) + end |
105 | + |
106 | + |
107 | +def validate(spec): |
108 | + """a wrapper around the spec validator for our purposes""" |
109 | + errors = list(validator.iter_errors(spec)) |
110 | + if errors: |
111 | + error = fetch_errors(errors) |
112 | + raise Exception(error) |
113 | + else: |
114 | + print(success + "No errors found in spec!" + end) |
115 | + |
116 | + |
117 | +def main(): |
118 | + parser = argparse.ArgumentParser( |
119 | + description="Check an OpenAPI yaml schema against OAS3" |
120 | + ) |
121 | + parser.add_argument( |
122 | + "api_spec", |
123 | + help="path for the file to validate against.", |
124 | + type=argparse.FileType("r"), |
125 | + ) |
126 | + args = parser.parse_args() |
127 | + with args.api_spec as f: |
128 | + spec = yaml.load(f, Loader=yaml.FullLoader) |
129 | + info = spec["info"] |
130 | + print(f"Checking {info['title']} version {info['version']}") |
131 | + validate(spec) |
132 | + |
133 | + |
134 | +if __name__ == "__main__": |
135 | + sys.exit(main()) |
UNIT TESTS
-b oapi-linter lp:~lloydwaltersj/maas/+git/maas into -b master lp:~maas-committers/maas
STATUS: FAILED maas-ci. internal: 8080/job/ maas-tester/ 355/consoleText 4e22296b5c0e243 6ab1c66120
LOG: http://
COMMIT: 94339310e45dd8f