From 7cc7367a3eaff4367a5abb81d993abfe0f062c96 Mon Sep 17 00:00:00 2001 From: Nizar Malangadan Date: Wed, 27 Nov 2019 16:00:34 +0530 Subject: [PATCH] Support Pytest version 5 --- .circleci/config.yml | 9 +++++---- server/Makefile | 3 ++- server/tests-py/conftest.py | 5 ++++- server/tests-py/context.py | 4 ++++ server/tests-py/requirements.txt | 4 +++- server/tests-py/test_allowlist_queries.py | 6 ++++-- server/tests-py/test_apis_disabled.py | 11 +++++++---- server/tests-py/test_compat.py | 3 ++- server/tests-py/test_cors.py | 3 ++- server/tests-py/test_horizontal_scale.py | 5 +++-- server/tests-py/test_jwt.py | 5 +++-- server/tests-py/test_logging.py | 3 ++- server/tests-py/test_webhook_insecure.py | 3 ++- server/tests-py/test_websocket_init_cookie.py | 3 ++- server/tests-py/validate.py | 8 ++++---- 15 files changed, 49 insertions(+), 26 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index c6918afc1d8fb..dc1bf29b39f24 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -97,7 +97,8 @@ refs: - run: name: Ensure databases are present environment: - DATABASE_URL: 'postgres://gql_test:@localhost:5432/gql_test' + # sqlalchemy throws warnings with postgres:// + DATABASE_URL: 'postgresql://gql_test:@localhost:5432/gql_test' command: | psql "$DATABASE_URL" -c "SELECT 1 FROM pg_database WHERE datname = 'gql_test2'" | grep -q -F '(1 row)' || psql "$DATABASE_URL" -c 'CREATE DATABASE gql_test2;' - run: @@ -106,8 +107,8 @@ refs: # Setting default number of threads to 2 # since circleci allocates 2 cpus per test container GHCRTS: -N2 - HASURA_GRAPHQL_DATABASE_URL: 'postgres://gql_test:@localhost:5432/gql_test' - HASURA_GRAPHQL_DATABASE_URL_2: 'postgres://gql_test:@localhost:5432/gql_test2' + HASURA_GRAPHQL_DATABASE_URL: 'postgresql://gql_test:@localhost:5432/gql_test' + HASURA_GRAPHQL_DATABASE_URL_2: 'postgresql://gql_test:@localhost:5432/gql_test2' GRAPHQL_ENGINE: '/build/_server_output/graphql-engine' GRAPHQL_ENGINE_TESTS: '/build/_server_output/graphql-engine-tests' command: | @@ -432,7 +433,7 @@ jobs: docker: - image: hasura/graphql-engine-upgrade-tester:v0.5 environment: - HASURA_GRAPHQL_DATABASE_URL: postgres://gql_test:@localhost:5432/gql_test + HASURA_GRAPHQL_DATABASE_URL: postgresql://gql_test:@localhost:5432/gql_test - image: circleci/postgres:10-alpine environment: POSTGRES_USER: gql_test diff --git a/server/Makefile b/server/Makefile index 72ca21b1e33cf..55d54f4f7dd88 100644 --- a/server/Makefile +++ b/server/Makefile @@ -54,7 +54,8 @@ release-image: $(project).cabal # assumes this is built in circleci ci-binary: mkdir -p packaging/build/rootfs - stack $(STACK_FLAGS) build --test --no-run-tests --ghc-options=-Werror $(BUILD_FLAGS) + # --no-terminal for a cleaner output in circleci + stack $(STACK_FLAGS) build --no-terminal --test --no-run-tests --ghc-options=-Werror $(BUILD_FLAGS) mkdir -p $(build_output) cp $(build_dir)/$(project)/$(project) $(build_dir)/graphql-engine-tests/graphql-engine-tests $(build_output) echo "$(VERSION)" > $(build_output)/version.txt diff --git a/server/tests-py/conftest.py b/server/tests-py/conftest.py index 056a25e9de131..7b02d8d4496c8 100644 --- a/server/tests-py/conftest.py +++ b/server/tests-py/conftest.py @@ -1,6 +1,6 @@ import pytest import time -from context import HGECtx, HGECtxError, EvtsWebhookServer, HGECtxGQLServer, GQLWsClient +from context import HGECtx, HGECtxError, EvtsWebhookServer, HGECtxGQLServer, GQLWsClient, PytestConf import threading import random from datetime import datetime @@ -101,6 +101,9 @@ def pytest_cmdline_preparse(config, args): def pytest_configure(config): + # Pytest has removed the global pytest.config + # As a solution we are going to store it in PytestConf.config + PytestConf.config = config if is_master(config): if not config.getoption('--hge-urls'): print("hge-urls should be specified") diff --git a/server/tests-py/context.py b/server/tests-py/context.py index 65d700328f036..60ad026125066 100644 --- a/server/tests-py/context.py +++ b/server/tests-py/context.py @@ -25,6 +25,10 @@ import graphql_server import graphql +# pytest has removed the global pytest.config +# As a solution to this we are going to store it in PyTestConf.config +class PytestConf(): + pass class HGECtxError(Exception): pass diff --git a/server/tests-py/requirements.txt b/server/tests-py/requirements.txt index 28f78a0ad4fde..4d3b6af00596d 100644 --- a/server/tests-py/requirements.txt +++ b/server/tests-py/requirements.txt @@ -15,13 +15,15 @@ idna==2.8 importlib-metadata==0.23 jsondiff==1.2.0 more-itertools==7.2.0 +packaging==19.2 pluggy==0.13.0 promise==2.2.1 psycopg2-binary==2.8.4 py==1.8.0 pycparser==2.19 PyJWT==1.7.1 -pytest==4.5.0 +pyparsing==2.4.5 +pytest==5.3.1 pytest-forked==1.1.3 pytest-xdist==1.30.0 PyYAML==5.1.2 diff --git a/server/tests-py/test_allowlist_queries.py b/server/tests-py/test_allowlist_queries.py index 3c8fe86276f0a..6e0924f2d7c29 100644 --- a/server/tests-py/test_allowlist_queries.py +++ b/server/tests-py/test_allowlist_queries.py @@ -3,9 +3,11 @@ import pytest from validate import check_query_f from super_classes import DefaultTestSelectQueries +from context import PytestConf + +if not PytestConf.config.getoption("--test-allowlist-queries"): + pytest.skip("flag --test-allowlist-queries is not set. Cannot runt tests for allowlist queries", allow_module_level=True) -@pytest.mark.skipif(not pytest.config.getoption("--test-allowlist-queries"), - reason="flag --test-allowlist-queries is not set. Cannot runt tests for allowlist queries") @pytest.mark.parametrize("transport", ['http','websocket']) class TestAllowlistQueries(DefaultTestSelectQueries): diff --git a/server/tests-py/test_apis_disabled.py b/server/tests-py/test_apis_disabled.py index 9d12acfd6300e..10901b8c64664 100644 --- a/server/tests-py/test_apis_disabled.py +++ b/server/tests-py/test_apis_disabled.py @@ -2,6 +2,7 @@ import pytest from validate import check_query, check_query_f +from context import PytestConf def check_post_404(hge_ctx,url): return check_query(hge_ctx, { @@ -10,8 +11,10 @@ def check_post_404(hge_ctx,url): 'query': {} })[0] +metadata_api_disabled = PytestConf.config.getoption("--test-metadata-disabled") +graphql_api_disabled = PytestConf.config.getoption("--test-graphql-disabled") -@pytest.mark.skipif(not pytest.config.getoption("--test-metadata-disabled"), +@pytest.mark.skipif(not metadata_api_disabled, reason="flag --test-metadata-disabled is not set. Cannot run tests for metadata disabled") class TestMetadataDisabled: @@ -25,7 +28,7 @@ def test_metadata_api_1_disabled(self, hge_ctx): check_post_404(hge_ctx,'/api/1/table/foo/select') -@pytest.mark.skipif(not pytest.config.getoption("--test-graphql-disabled"), +@pytest.mark.skipif(not graphql_api_disabled, reason="--test-graphql-disabled is not set. Cannot run GraphQL disabled tests") class TestGraphQLDisabled: @@ -36,7 +39,7 @@ def test_graphql_explain_disabled(self, hge_ctx): check_post_404(hge_ctx, '/v1/graphql/explain') -@pytest.mark.skipif(pytest.config.getoption("--test-graphql-disabled"), +@pytest.mark.skipif(graphql_api_disabled, reason="--test-graphql-disabled is set. Cannot run GraphQL enabled tests") class TestGraphQLEnabled: @@ -44,7 +47,7 @@ def test_graphql_introspection(self, hge_ctx): check_query_f(hge_ctx, "queries/graphql_introspection/introspection_only_kind_of_queryType.yaml") -@pytest.mark.skipif(pytest.config.getoption("--test-metadata-disabled"), +@pytest.mark.skipif(metadata_api_disabled, reason="--test-metadata-disabled is set. Cannot run metadata enabled tests") class TestMetadataEnabled: diff --git a/server/tests-py/test_compat.py b/server/tests-py/test_compat.py index 44dc139acf642..c652728e84451 100644 --- a/server/tests-py/test_compat.py +++ b/server/tests-py/test_compat.py @@ -1,6 +1,7 @@ import pytest +from context import PytestConf -if not pytest.config.getoption("--hge-key"): +if not PytestConf.config.getoption("--hge-key"): pytest.skip("--hge-key flag is missing, skipping tests", allow_module_level=True) def v1qCompat(hge_ctx, q, headers = {}): diff --git a/server/tests-py/test_cors.py b/server/tests-py/test_cors.py index 83314dddde16d..821c908ba0702 100644 --- a/server/tests-py/test_cors.py +++ b/server/tests-py/test_cors.py @@ -1,6 +1,7 @@ import pytest +from context import PytestConf -if not pytest.config.getoption("--test-cors"): +if not PytestConf.config.getoption("--test-cors"): pytest.skip("--test-cors flag is missing, skipping tests", allow_module_level=True) diff --git a/server/tests-py/test_horizontal_scale.py b/server/tests-py/test_horizontal_scale.py index ad5c740ac6da1..304f96f31abd2 100644 --- a/server/tests-py/test_horizontal_scale.py +++ b/server/tests-py/test_horizontal_scale.py @@ -2,9 +2,10 @@ import ruamel.yaml as yaml import time import jsondiff +from context import PytestConf -if not pytest.config.getoption("--test-hge-scale-url"): +if not PytestConf.config.getoption("--test-hge-scale-url"): pytest.skip("--test-hge-scale-url flag is missing, skipping tests", allow_module_level=True) @@ -24,7 +25,7 @@ def test_horizontal_scale_basic(self, hge_ctx): with open(self.dir() + "/steps.yaml") as c: conf = yaml.safe_load(c) - assert isinstance(conf, list) == True, 'Not an list' + assert isinstance(conf, list) == True, 'Not a list' for _, step in enumerate(conf): # execute operation response = hge_ctx.http.post( diff --git a/server/tests-py/test_jwt.py b/server/tests-py/test_jwt.py index 71c6f5f9a6ed5..eb07e3b5c94ff 100644 --- a/server/tests-py/test_jwt.py +++ b/server/tests-py/test_jwt.py @@ -12,12 +12,13 @@ from cryptography.hazmat.primitives import serialization from validate import check_query +from context import PytestConf -if not pytest.config.getoption('--hge-jwt-key-file'): +if not PytestConf.config.getoption('--hge-jwt-key-file'): pytest.skip('--hge-jwt-key-file is missing, skipping JWT tests', allow_module_level=True) -if not pytest.config.getoption('--hge-jwt-conf'): +if not PytestConf.config.getoption('--hge-jwt-conf'): pytest.skip('--hge-jwt-key-conf is missing, skipping JWT tests', allow_module_level=True) def get_claims_fmt(raw_conf): diff --git a/server/tests-py/test_logging.py b/server/tests-py/test_logging.py index 8e73cec690b26..d74707055094e 100644 --- a/server/tests-py/test_logging.py +++ b/server/tests-py/test_logging.py @@ -5,8 +5,9 @@ import time import pytest +from context import PytestConf -if not pytest.config.getoption("--test-logging"): +if not PytestConf.config.getoption("--test-logging"): pytest.skip("--test-logging missing, skipping tests", allow_module_level=True) class TestLogging(): diff --git a/server/tests-py/test_webhook_insecure.py b/server/tests-py/test_webhook_insecure.py index 3628182607f14..256b5a9f2883a 100644 --- a/server/tests-py/test_webhook_insecure.py +++ b/server/tests-py/test_webhook_insecure.py @@ -1,8 +1,9 @@ import pytest from validate import check_query_f from super_classes import DefaultTestSelectQueries +from context import PytestConf -if not pytest.config.getoption("--test-webhook-insecure"): +if not PytestConf.config.getoption("--test-webhook-insecure"): pytest.skip("--test-webhook-https-insecure flag is missing, skipping tests", allow_module_level=True) class TestHTTPSWebhookInsecure(DefaultTestSelectQueries): diff --git a/server/tests-py/test_websocket_init_cookie.py b/server/tests-py/test_websocket_init_cookie.py index db333305c1dc7..177741028bcc2 100644 --- a/server/tests-py/test_websocket_init_cookie.py +++ b/server/tests-py/test_websocket_init_cookie.py @@ -5,8 +5,9 @@ import websocket import pytest from validate import check_query +from context import PytestConf -if not pytest.config.getoption("--test-ws-init-cookie"): +if not PytestConf.config.getoption("--test-ws-init-cookie"): pytest.skip("--test-ws-init-cookie flag is missing, skipping tests", allow_module_level=True) diff --git a/server/tests-py/validate.py b/server/tests-py/validate.py index 151be1c6f42b2..0e61dcbea5ff2 100644 --- a/server/tests-py/validate.py +++ b/server/tests-py/validate.py @@ -16,7 +16,7 @@ import time import warnings -from context import GQLWsClient +from context import GQLWsClient, PytestConf def check_keys(keys, obj): for k in keys: @@ -254,7 +254,7 @@ def assert_graphql_resp_expected(resp_orig, exp_response_orig, query): exp_response = collapse_order_not_selset(exp_response_orig, query) matched = equal_CommentedMap(resp, exp_response) - if pytest.config.getoption("--accept"): + if PytestConf.config.getoption("--accept"): print('skipping assertion since we chose to --accept new output') else: yml = yaml.YAML() @@ -309,7 +309,7 @@ def check_query_f(hge_ctx, f, transport='http', add_auth=True): if isinstance(conf, list): for ix, sconf in enumerate(conf): actual_resp, matched = check_query(hge_ctx, sconf, transport, add_auth) - if pytest.config.getoption("--accept") and not matched: + if PytestConf.config.getoption("--accept") and not matched: conf[ix]['response'] = actual_resp should_write_back = True else: @@ -318,7 +318,7 @@ def check_query_f(hge_ctx, f, transport='http', add_auth=True): actual_resp, matched = check_query(hge_ctx, conf, transport, add_auth) # If using `--accept` write the file back out with the new expected # response set to the actual response we got: - if pytest.config.getoption("--accept") and not matched: + if PytestConf.config.getoption("--accept") and not matched: conf['response'] = actual_resp should_write_back = True