Merge ~lloydwaltersj/maas:oapi-linter into maas:master

Proposed by Jack Lloyd-Walters
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)
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

To post a comment you must log in.
Revision history for this message
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://maas-ci.internal:8080/job/maas-tester/355/consoleText
COMMIT: 94339310e45dd8f4e22296b5c0e2436ab1c66120

review: Needs Fixing
Revision history for this message
Alexsander de Souza (alexsander-souza) wrote :

use tox to handle the test environment

review: Needs Fixing
Revision history for this message
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.

review: Needs Fixing
Revision history for this message
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.

~lloydwaltersj/maas:oapi-linter updated
cd623b8... by Jack Lloyd-Walters

transition to tox

Revision history for this message
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://maas-ci.internal:8080/job/maas-tester/362/consoleText
COMMIT: cd623b889ddf406d2aef707052f647e22488f931

review: Needs Fixing
~lloydwaltersj/maas:oapi-linter updated
a90fe6e... by Jack Lloyd-Walters

change how oapi doccs are generated for linting

Revision history for this message
Jack Lloyd-Walters (lloydwaltersj) wrote (last edit ):

> STATUS: FAILED
> LOG: http://maas-ci.internal:8080/job/maas-tester/362/consoleText
> COMMIT: cd623b889ddf406d2aef707052f647e22488f931

This is working exactly as expected, a successfull failure if you will!

Revision history for this message
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://maas-ci.internal:8080/job/maas-tester/363/consoleText
COMMIT: a90fe6e49bee699aa470cd374526654e94bcab95

review: Needs Fixing
Revision history for this message
Jack Lloyd-Walters (lloydwaltersj) wrote :

jenkins: !test

Revision history for this message
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://maas-ci.internal:8080/job/maas-tester/392/consoleText
COMMIT: a90fe6e49bee699aa470cd374526654e94bcab95

review: Needs Fixing
~lloydwaltersj/maas:oapi-linter updated
39c02c3... by Jack Lloyd-Walters

bring branch up to parity

c0695f9... by Jack Lloyd-Walters

revert yaml.dump change

Revision history for this message
Adam Collard (adam-collard) :
Revision history for this message
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://maas-ci.internal:8080/job/maas-tester/405/consoleText
COMMIT: c0695f98b1e9ced2ccb37447682b5c59fd50bb1d

review: Needs Fixing
~lloydwaltersj/maas:oapi-linter updated
4eaea74... by Jack Lloyd-Walters

remove make docs from lint

Revision history for this message
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://maas-ci.internal:8080/job/maas-tester/407/consoleText
COMMIT: 4eaea745bfebe77675dc6e03ae1047f4b182122e

review: Needs Fixing
Revision history for this message
Adam Collard (adam-collard) :
~lloydwaltersj/maas:oapi-linter updated
2c01771... by Jack Lloyd-Walters

remove hardcoded openapi file location

Revision history for this message
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://maas-ci.internal:8080/job/maas-tester/438/consoleText
COMMIT: 2c01771792cbdf7cb63bed44851791f6f354df7b

review: Needs Fixing
Revision history for this message
Adam Collard (adam-collard) :
~lloydwaltersj/maas:oapi-linter updated
6247138... by Jack Lloyd-Walters

bring branch up to parity

Revision history for this message
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://maas-ci.internal:8080/job/maas-tester/455/consoleText
COMMIT: 6247138baf4652bcd8ae945d9e173e0da87ccc99

review: Needs Fixing
~lloydwaltersj/maas:oapi-linter updated
b55d865... by Jack Lloyd-Walters

bring branch up to parity

Revision history for this message
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://maas-ci.internal:8080/job/maas-tester/487/consoleText
COMMIT: b55d865e978a03383164554460790a26ffe24b55

review: Needs Fixing
~lloydwaltersj/maas:oapi-linter updated
8421bf8... by Jack Lloyd-Walters

generate spec if missing

bb8d96d... by Jack Lloyd-Walters

make linter depend on spec

Revision history for this message
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: 8421bf8f4b32f39fa8592cf821d836f6ba501b29

review: Approve
Revision history for this message
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: bb8d96d2bf145e94f54b57cb39b01216d3871108

review: Approve
Revision history for this message
Adam Collard (adam-collard) :
review: Approve
Revision history for this message
Adam Collard (adam-collard) :
review: Abstain
~lloydwaltersj/maas:oapi-linter updated
921393f... by Jack Lloyd-Walters

remove help supression

Revision history for this message
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://maas-ci.internal:8080/job/maas-tester/506/consoleText
COMMIT: 921393ffd0cc2f1eabb8a6444afc8b3a49869182

review: Needs Fixing
Revision history for this message
Jack Lloyd-Walters (lloydwaltersj) wrote :

jenkins: !test

Revision history for this message
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: 921393ffd0cc2f1eabb8a6444afc8b3a49869182

review: Approve
Revision history for this message
Alexsander de Souza (alexsander-souza) wrote :

+1

review: Approve
~lloydwaltersj/maas:oapi-linter updated
46f3269... by Jack Lloyd-Walters

remove lint-oapi from lint target

1cbbc49... by Jack Lloyd-Walters

move oapi linter to test phase

Revision history for this message
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: 1cbbc4976c25603e92dad0e557d623ebe889a17f

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/Makefile b/Makefile
2index 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 {} + | \
26diff --git a/setup.cfg b/setup.cfg
27index 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
67diff --git a/utilities/check-oapi b/utilities/check-oapi
68new file mode 100755
69index 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())

Subscribers

People subscribed via source and target branches