Skip to content

Commit

Permalink
[MAINTENANCE] fix REST API contract tests (#9098)
Browse files Browse the repository at this point in the history
  • Loading branch information
dctalbot committed Dec 19, 2023
1 parent 7aec10d commit 94e1cd9
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 55 deletions.
17 changes: 0 additions & 17 deletions .github/workflows/ci.yml
Expand Up @@ -316,23 +316,6 @@ jobs:
- name: Run the tests
run: invoke ci-tests 'cloud' --up-services --verbose

- name: Get mercury version
run: |
curl -s ''${GX_CLOUD_BASE_URL}'/status' | \
python3 -c "import os, sys, json; api_version=json.load(sys.stdin)['api_version']; os.system(f'echo api_version={api_version}');" \
>> $GITHUB_OUTPUT
id: get_mercury_version

- name: Verify endpoint contracts
run: |
pact-verifier -r --provider-base-url=${GX_CLOUD_BASE_URL} \
--pact-url=tests/integration/cloud/rest_contracts/pacts/great_expectations-mercury.json \
--pact-broker-url=https://greatexpectations.pactflow.io/ \
--pact-broker-token=${PACT_BROKER_READ_WRITE_TOKEN} \
--provider=mercury \
--provider-version-branch=develop \
--provider-app-version=${{ steps.get_mercury_version.outputs.api_version }}

marker-tests:
needs: [unit-tests, static-analysis]
Expand Down
43 changes: 16 additions & 27 deletions tests/integration/cloud/rest_contracts/conftest.py
Expand Up @@ -103,8 +103,13 @@ def get_git_commit_hash() -> str:
return subprocess.check_output(["git", "rev-parse", "HEAD"]).decode("ascii").strip()


@pytest.fixture
@pytest.fixture(scope="package")
def pact_test(request) -> pact.Pact:
"""
pact_test can be used as a context manager and will:
1. write a new contract to the pact dir
2. verify the contract against the mock service
"""
pact_broker_base_url = "https://greatexpectations.pactflow.io"

broker_token: str
Expand All @@ -126,7 +131,7 @@ def pact_test(request) -> pact.Pact:
# in GH, and we run the release build process on the tagged commit.
version = f"{get_git_commit_hash()}_{str(uuid.uuid4())[:5]}"

pact_test: pact.Pact = pact.Consumer(
_pact: pact.Pact = pact.Consumer(
name=CONSUMER_NAME,
version=version,
tag_with_git_branch=True,
Expand All @@ -141,9 +146,9 @@ def pact_test(request) -> pact.Pact:
publish_to_broker=publish_to_broker,
)

pact_test.start_service()
yield pact_test
pact_test.stop_service()
_pact.start_service()
yield _pact
_pact.stop_service()


class ContractInteraction(pydantic.BaseModel):
Expand Down Expand Up @@ -188,7 +193,7 @@ class Config:


@pytest.fixture
def run_pact_test(
def run_rest_api_pact_test(
gx_cloud_session: Session,
pact_test: pact.Pact,
) -> Callable:
Expand Down Expand Up @@ -236,32 +241,16 @@ def _run_pact_test(
request_url = f"http://{PACT_MOCK_HOST}:{PACT_MOCK_PORT}{contract_interaction.request_path}"

with pact_test:
gx_cloud_session.request(
# act
resp = gx_cloud_session.request(
method=contract_interaction.method,
url=request_url,
json=contract_interaction.request_body,
params=contract_interaction.request_params,
)

try:
provider_base_url: Final[str] = os.environ["GX_CLOUD_BASE_URL"]
except KeyError as e:
raise OSError("GX_CLOUD_BASE_URL is not set in this environment.") from e

verifier = pact.Verifier(
provider=PROVIDER_NAME,
provider_base_url=provider_base_url,
)

pacts: tuple[str, ...] = tuple(
str(file.resolve()) for file in PACT_DIR.glob("*.json")
)

exit_code, logs = verifier.verify_pacts(
*pacts,
verbose=False,
)
if exit_code == 1:
raise AssertionError("Pact verifier reports failed interactions")
# assert
assert resp.status_code == contract_interaction.response_status
# TODO more unit test assertions would go here e.g. response body checks

return _run_pact_test
4 changes: 2 additions & 2 deletions tests/integration/cloud/rest_contracts/test_checkpoints.py
Expand Up @@ -94,6 +94,6 @@
)
def test_checkpoint(
contract_interaction: ContractInteraction,
run_pact_test: Callable[[ContractInteraction], None],
run_rest_api_pact_test: Callable[[ContractInteraction], None],
) -> None:
run_pact_test(contract_interaction)
run_rest_api_pact_test(contract_interaction)
Expand Up @@ -37,6 +37,7 @@ def test_data_context_configuration(
cloud_access_token: str,
pact_test: pact.Pact,
) -> None:
# arrange
provider_state = "the Data Context exists"
scenario = "a request for a Data Context"
method = "GET"
Expand All @@ -59,9 +60,13 @@ def test_data_context_configuration(
)

with pact_test:
_ = gx.get_context(
# act
ctx = gx.get_context(
mode="cloud",
cloud_base_url=PACT_MOCK_SERVICE_URL,
cloud_organization_id=EXISTING_ORGANIZATION_ID,
cloud_access_token=cloud_access_token,
)

# assert
assert ctx.datasources is not None
4 changes: 2 additions & 2 deletions tests/integration/cloud/rest_contracts/test_datasource.py
Expand Up @@ -83,6 +83,6 @@
)
def test_datasource(
contract_interaction: ContractInteraction,
run_pact_test: Callable[[ContractInteraction], None],
run_rest_api_pact_test: Callable[[ContractInteraction], None],
) -> None:
run_pact_test(contract_interaction)
run_rest_api_pact_test(contract_interaction)
Expand Up @@ -252,9 +252,9 @@ def test_get_expectation_suites(
)
def test_post_expectation_suite_request(
contract_interaction: ContractInteraction,
run_pact_test: Callable[[ContractInteraction], None],
run_rest_api_pact_test: Callable[[ContractInteraction], None],
) -> None:
run_pact_test(contract_interaction)
run_rest_api_pact_test(contract_interaction)


@pytest.mark.cloud
Expand Down Expand Up @@ -297,9 +297,9 @@ def test_post_expectation_suite_request(
)
def test_put_expectation_suite_request(
contract_interaction: ContractInteraction,
run_pact_test: Callable[[ContractInteraction], None],
run_rest_api_pact_test: Callable[[ContractInteraction], None],
) -> None:
run_pact_test(contract_interaction)
run_rest_api_pact_test(contract_interaction)


@pytest.mark.cloud
Expand Down Expand Up @@ -342,9 +342,9 @@ def test_put_expectation_suite_request(
)
def test_put_non_existent_expectation_suite(
contract_interaction: ContractInteraction,
run_pact_test: Callable[[ContractInteraction], None],
run_rest_api_pact_test: Callable[[ContractInteraction], None],
) -> None:
run_pact_test(contract_interaction)
run_rest_api_pact_test(contract_interaction)


@pytest.mark.cloud
Expand Down

0 comments on commit 94e1cd9

Please sign in to comment.