From f737fc066fe254243a185afe16d351f7fb751dda Mon Sep 17 00:00:00 2001 From: Matthew Jones Date: Tue, 18 Sep 2018 13:03:38 -0400 Subject: [PATCH 1/5] Generate schema suitable for comparing for schema changes --- Makefile | 6 ++++++ awx/main/tests/conftest.py | 6 ++++++ .../tests/docs/test_swagger_generation.py | 21 +++++++++++++------ 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index 4337c13c22..421c849028 100644 --- a/Makefile +++ b/Makefile @@ -338,6 +338,12 @@ pyflakes: reports pylint: reports @(set -o pipefail && $@ | reports/$@.report) +genschema: reports + @if [ "$(VENV_BASE)" ]; then \ + . $(VENV_BASE)/awx/bin/activate; \ + fi; \ + (set -o pipefail && py.test --genschema awx/conf/tests/functional awx/main/tests/functional/api awx/main/tests/docs --release=$(VERSION_TARGET) | tee reports/$@.report) + swagger: reports @if [ "$(VENV_BASE)" ]; then \ . $(VENV_BASE)/awx/bin/activate; \ diff --git a/awx/main/tests/conftest.py b/awx/main/tests/conftest.py index 0c3cb513ec..1435c4efa1 100644 --- a/awx/main/tests/conftest.py +++ b/awx/main/tests/conftest.py @@ -15,6 +15,12 @@ from awx.main.tests.factories import ( ) +def pytest_addoption(parser): + parser.addoption( + "--genschema", action="store_true", default=False, help="execute schema validator" + ) + + def pytest_configure(config): import sys sys._called_from_test = True diff --git a/awx/main/tests/docs/test_swagger_generation.py b/awx/main/tests/docs/test_swagger_generation.py index fc6bfa25a1..cecd1016ea 100644 --- a/awx/main/tests/docs/test_swagger_generation.py +++ b/awx/main/tests/docs/test_swagger_generation.py @@ -49,7 +49,8 @@ class TestSwaggerGeneration(): data.update(response.accepted_renderer.get_customizations() or {}) data['host'] = None - data['modified'] = datetime.datetime.utcnow().isoformat() + if not pytest.config.getoption("--genschema"): + data['modified'] = datetime.datetime.utcnow().isoformat() data['schemes'] = ['https'] data['consumes'] = ['application/json'] @@ -139,11 +140,14 @@ class TestSwaggerGeneration(): for param in node[method].get('parameters'): if param['in'] == 'body': node[method]['parameters'].remove(param) - node[method].setdefault('parameters', []).append({ - 'name': 'data', - 'in': 'body', - 'schema': {'example': request_data}, - }) + if pytest.config.getoption("--genschema"): + pytest.skip("In schema generator skipping swagger generator", allow_module_level=True) + else: + node[method].setdefault('parameters', []).append({ + 'name': 'data', + 'in': 'body', + 'schema': {'example': request_data}, + }) # Build response examples if resp: @@ -168,4 +172,9 @@ class TestSwaggerGeneration(): '2018-02-01T08:00:00.000000Z', data ) + data = re.sub( + '''(\s+"client_id": ")([a-zA-Z0-9]{40})("\,\s*)''', + '\\1xxxx\\3', + data + ) f.write(data) From d70cd113e11d92edaabfbb6b70ca9900b3f425ff Mon Sep 17 00:00:00 2001 From: Elijah DeLee Date: Tue, 6 Nov 2018 15:22:07 -0500 Subject: [PATCH 2/5] Reduce duplicated logic for genschema target --- .gitignore | 3 +++ Makefile | 8 ++------ 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index f1463667a5..0ba4ef481b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ +# Ignore generated schema +swagger.json +schema.json # Tags .tags diff --git a/Makefile b/Makefile index 421c849028..886467534d 100644 --- a/Makefile +++ b/Makefile @@ -11,7 +11,6 @@ GIT_BRANCH ?= $(shell git rev-parse --abbrev-ref HEAD) MANAGEMENT_COMMAND ?= awx-manage IMAGE_REPOSITORY_AUTH ?= IMAGE_REPOSITORY_BASE ?= https://gcr.io - VERSION := $(shell cat VERSION) # NOTE: This defaults the container image version to the branch that's active @@ -339,16 +338,13 @@ pylint: reports @(set -o pipefail && $@ | reports/$@.report) genschema: reports - @if [ "$(VENV_BASE)" ]; then \ - . $(VENV_BASE)/awx/bin/activate; \ - fi; \ - (set -o pipefail && py.test --genschema awx/conf/tests/functional awx/main/tests/functional/api awx/main/tests/docs --release=$(VERSION_TARGET) | tee reports/$@.report) + $(MAKE) swagger PYTEST_ARGS="--genschema" swagger: reports @if [ "$(VENV_BASE)" ]; then \ . $(VENV_BASE)/awx/bin/activate; \ fi; \ - (set -o pipefail && py.test awx/conf/tests/functional awx/main/tests/functional/api awx/main/tests/docs --release=$(VERSION_TARGET) | tee reports/$@.report) + (set -o pipefail && py.test $(PYTEST_ARGS) awx/conf/tests/functional awx/main/tests/functional/api awx/main/tests/docs --release=$(VERSION_TARGET) | tee reports/$@.report) check: flake8 pep8 # pyflakes pylint From a68e22b1142b8c1a8542b5f6f06517f5f1294f62 Mon Sep 17 00:00:00 2001 From: Elijah DeLee Date: Thu, 15 Nov 2018 16:25:13 -0500 Subject: [PATCH 3/5] Add tox target to detect schema changes Fetches reference schema from public bucket Still need define method for updating reference schema on merge. --- .gitignore | 1 + Makefile | 14 ++++++++++++++ tox.ini | 8 ++++++++ 3 files changed, 23 insertions(+) diff --git a/.gitignore b/.gitignore index 0ba4ef481b..4bdbb40e13 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ # Ignore generated schema swagger.json schema.json +reference-schema.json # Tags .tags diff --git a/Makefile b/Makefile index 886467534d..d4857a12af 100644 --- a/Makefile +++ b/Makefile @@ -84,6 +84,11 @@ clean-venv: clean-dist: rm -rf dist +clean-schema: + rm -rf swagger.json + rm -rf schema.json + rm -rf reference-schema.json + # Remove temporary build files, compiled Python files. clean: clean-ui clean-dist rm -rf awx/public @@ -566,6 +571,15 @@ docker-compose-runtest: docker-compose-build-swagger: cd tools && CURRENT_UID=$(shell id -u) TAG=$(COMPOSE_TAG) DEV_DOCKER_TAG_BASE=$(DEV_DOCKER_TAG_BASE) docker-compose run --rm --service-ports awx /start_tests.sh swagger +docker-compose-genschema: + cd tools && CURRENT_UID=$(shell id -u) TAG=$(COMPOSE_TAG) DEV_DOCKER_TAG_BASE=$(DEV_DOCKER_TAG_BASE) docker-compose run --rm --service-ports awx /start_tests.sh genschema + mv swagger.json schema.json + +docker-compose-validate-schema: + $(MAKE) docker-compose-genschema + curl https://s3.amazonaws.com/awx-public-ci-files/schema.json -o reference-schema.json + diff -u schema.json reference-schema.json + docker-compose-clean: cd tools && CURRENT_UID=$(shell id -u) TAG=$(COMPOSE_TAG) DEV_DOCKER_TAG_BASE=$(DEV_DOCKER_TAG_BASE) docker-compose run --rm -w /awx_devel --service-ports awx make clean cd tools && TAG=$(COMPOSE_TAG) DEV_DOCKER_TAG_BASE=$(DEV_DOCKER_TAG_BASE) docker-compose rm -sf diff --git a/tox.ini b/tox.ini index 279e76b8c7..adbf79d225 100644 --- a/tox.ini +++ b/tox.ini @@ -6,6 +6,7 @@ envlist = api, ui, swagger, + validate-schema, [testenv] ;basepython = python2.7 @@ -71,3 +72,10 @@ deps = commands = make docker-compose-build make docker-compose-build-swagger + +[testenv:validate-schema] +deps = + nodeenv +commands = + make docker-compose-build + make docker-compose-validate-schema From 949cf53b890deb776a878684648f82700a72f2ca Mon Sep 17 00:00:00 2001 From: Elijah DeLee Date: Thu, 15 Nov 2018 17:06:38 -0500 Subject: [PATCH 4/5] Use r in front of regex string to make flake8 happy This means we should not escape the \ character in the same way --- awx/main/tests/docs/test_swagger_generation.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/awx/main/tests/docs/test_swagger_generation.py b/awx/main/tests/docs/test_swagger_generation.py index cecd1016ea..e85ce45a3c 100644 --- a/awx/main/tests/docs/test_swagger_generation.py +++ b/awx/main/tests/docs/test_swagger_generation.py @@ -122,7 +122,7 @@ class TestSwaggerGeneration(): pattern = pattern.replace('{id}', '[0-9]+') pattern = pattern.replace(r'{category_slug}', r'[a-zA-Z0-9\-]+') for path, result in swagger_autogen.items(): - if re.match('^{}$'.format(pattern), path): + if re.match(r'^{}$'.format(pattern), path): for key, value in result.items(): method, status_code = key content_type, resp, request_data = value @@ -168,13 +168,13 @@ class TestSwaggerGeneration(): # replace ISO dates w/ the same value so we don't generate # needless diffs data = re.sub( - '[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]+Z', - '2018-02-01T08:00:00.000000Z', + r'[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]+Z', + r'2018-02-01T08:00:00.000000Z', data ) data = re.sub( - '''(\s+"client_id": ")([a-zA-Z0-9]{40})("\,\s*)''', - '\\1xxxx\\3', + r'''(\s+"client_id": ")([a-zA-Z0-9]{40})("\,\s*)''', + r'\1xxxx\3', data ) f.write(data) From 4ae1fdef05a9f52ba770cbdc743220bbbe9bc660 Mon Sep 17 00:00:00 2001 From: Elijah DeLee Date: Fri, 16 Nov 2018 09:47:33 -0500 Subject: [PATCH 5/5] Ignore differences in whitespace for schema validation --- Makefile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index d4857a12af..d979141ec8 100644 --- a/Makefile +++ b/Makefile @@ -578,7 +578,8 @@ docker-compose-genschema: docker-compose-validate-schema: $(MAKE) docker-compose-genschema curl https://s3.amazonaws.com/awx-public-ci-files/schema.json -o reference-schema.json - diff -u schema.json reference-schema.json + # Ignore differences in whitespace with -b + diff -u -b schema.json reference-schema.json docker-compose-clean: cd tools && CURRENT_UID=$(shell id -u) TAG=$(COMPOSE_TAG) DEV_DOCKER_TAG_BASE=$(DEV_DOCKER_TAG_BASE) docker-compose run --rm -w /awx_devel --service-ports awx make clean