diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 0b1bd9d..6245b63 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -30,8 +30,8 @@ jobs: - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6 - uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6 with: - python-version: '3.14' - architecture: 'x64' + python-version: '3.14' + architecture: 'x64' - run: pip install -r requirements.txt - name: Test pactflow-example-consumer-python against ${{ matrix.pact_provider }} run: make test @@ -48,7 +48,11 @@ jobs: needs: test steps: - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6 - - run: docker pull pactfoundation/pact-cli:latest + - uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6 + with: + python-version: '3.14' + architecture: 'x64' + - run: pip install -r requirements.txt - name: Can I deploy? run: GIT_BRANCH=${GIT_REF:11} make can_i_deploy @@ -58,7 +62,11 @@ jobs: needs: can-i-deploy steps: - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6 - - run: docker pull pactfoundation/pact-cli:latest + - uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6 + with: + python-version: '3.14' + architecture: 'x64' + - run: pip install -r requirements.txt - name: Deploy run: GIT_BRANCH=${GIT_REF:11} make deploy if: github.ref == 'refs/heads/master' diff --git a/Makefile b/Makefile index 1d8fe92..b4bf0f8 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,6 @@ # It's set as a secure environment variable in the build.yml file PACTICIPANT := "pactflow-example-consumer-python" GITHUB_WEBHOOK_UUID := "04510dc1-7f0a-4ed2-997d-114bfa86f8ad" -PACT_CLI="docker run --rm -v ${PWD}:${PWD} -e PACT_BROKER_BASE_URL -e PACT_BROKER_TOKEN -e PACT_BROKER_USERNAME -e PACT_BROKER_PASSWORD pactfoundation/pact-cli:latest" # Only deploy from master ifeq ($(GIT_BRANCH),master) @@ -30,7 +29,7 @@ fake_ci: .env make ci publish_pacts: .env - @"${PACT_CLI}" publish ${PWD}/pacts --consumer-app-version ${GIT_COMMIT} --branch ${GIT_BRANCH} + pact broker publish ${PWD}/pacts --consumer-app-version ${GIT_COMMIT} --branch ${GIT_BRANCH} ## ===================== ## Build/test tasks @@ -50,7 +49,7 @@ no_deploy: can_i_deploy: .env echo "can_i_deploy" - @"${PACT_CLI}" broker can-i-deploy \ + pact broker can-i-deploy \ --pacticipant ${PACTICIPANT} \ --version ${GIT_COMMIT} \ --to-environment production \ @@ -61,7 +60,7 @@ deploy_app: @echo "Deploying to prod" record_deployment: .env - @"${PACT_CLI}" broker record-deployment --pacticipant ${PACTICIPANT} --version ${GIT_COMMIT} --environment production + pact broker record-deployment --pacticipant ${PACTICIPANT} --version ${GIT_COMMIT} --environment production ## ===================== ## PactFlow set up tasks @@ -80,8 +79,7 @@ create_github_token_secret: # so that any PRs will get a status that shows what the status of # the pact is. create_or_update_github_webhook: - @"${PACT_CLI}" \ - broker create-or-update-webhook \ + pact broker create-or-update-webhook \ 'https://api.github.com/repos/pactflow/example-consumer/statuses/$${pactbroker.consumerVersionNumber}' \ --header 'Content-Type: application/json' 'Accept: application/vnd.github.v3+json' 'Authorization: token $${user.githubCommitStatusToken}' \ --request POST \ @@ -125,4 +123,4 @@ venv: ln -sf ${CURDIR}/.venv ~/.pyenv/versions/${PROJECT} @echo "\n$(green)Use it! (populate .python-version)$(sgr0)" - pyenv local ${PROJECT} \ No newline at end of file + pyenv local ${PROJECT} diff --git a/requirements.txt b/requirements.txt index f9dee03..b364341 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,5 @@ Flask==3.1.3 pytest==9.0.3 requests==2.33.1 -pact_python==2.3.3 \ No newline at end of file +pact-python==3.3.1 +pact-python-cli==2.6.0.0 diff --git a/tests/consumer/test_products_consumer.py b/tests/consumer/test_products_consumer.py index 2b50ae3..5a72f33 100644 --- a/tests/consumer/test_products_consumer.py +++ b/tests/consumer/test_products_consumer.py @@ -1,44 +1,25 @@ -"""pact test for product service client via ruby core""" +"""pact test for product service client""" import logging -import os +from typing import Generator import pytest -from pact import Consumer, Like, Provider, Term, Format +from pact import Pact, match from src.consumer import ProductConsumer log = logging.getLogger(__name__) logging.basicConfig(level=logging.INFO) -print(Format().__dict__) -PACT_MOCK_HOST = 'localhost' -PACT_MOCK_PORT = 1234 -PACT_DIR = os.path.dirname(os.path.realpath(__file__)) - -@pytest.fixture -def consumer(): - return ProductConsumer( - 'http://{host}:{port}' - .format(host=PACT_MOCK_HOST, port=PACT_MOCK_PORT) - ) +@pytest.fixture(scope='session') +def pact() -> Generator[Pact, None, None]: + pact = Pact('pactflow-example-consumer-python', 'pactflow-example-provider-python') + yield pact.with_specification('V4') + pact.write_file('./pacts') -@pytest.fixture(scope='session') -def pact(request): - pact = Consumer('pactflow-example-consumer-python').has_pact_with( - Provider('pactflow-example-provider-python'), host_name=PACT_MOCK_HOST, port=PACT_MOCK_PORT, - pact_dir="./pacts", log_dir="./logs") - try: - print('start service') - pact.start_service() - yield pact - finally: - print('stop service') - pact.stop_service() - -def test_get_product(pact, consumer): +def test_get_product(pact) -> None: expected = { 'id': "27", 'name': 'Margharita', @@ -46,11 +27,13 @@ def test_get_product(pact, consumer): } (pact - .given('a product with ID 10 exists') .upon_receiving('a request to get a product') - .with_request('GET', '/product/10') - .will_respond_with(200, body=Like(expected))) + .given('a product with ID 10 exists') + .with_request(method='GET', path='/product/10') + .will_respond_with(200) + .with_body(match.like(expected))) - with pact: + with pact.serve() as srv: + consumer = ProductConsumer(str(srv.url)) user = consumer.get_product('10') assert user.name == 'Margharita' diff --git a/tests/consumer/test_products_consumer_v3.py b/tests/consumer/test_products_consumer_v3.py deleted file mode 100644 index 64d3d91..0000000 --- a/tests/consumer/test_products_consumer_v3.py +++ /dev/null @@ -1,37 +0,0 @@ -"""pact test for product service client via rust core""" - -import logging -from typing import Generator - -import pytest -from pact.v3.pact import Pact -from pact.v3.match import like -from src.consumer import ProductConsumer - -log = logging.getLogger(__name__) -logging.basicConfig(level=logging.INFO) - -@pytest.fixture(scope='session') -def pact() -> Generator[Pact, None, None]: - pact = Pact("pactflow-example-consumer-python-v3", "pactflow-example-provider-python-v3") - yield pact.with_specification("V4") - pact.write_file("./pacts") - -def test_get_product(pact) -> None: - expected = { - 'id': "27", - 'name': 'Margharita', - 'type': 'Pizza' - } - - (pact - .upon_receiving('a request to get a product') - .given('a product with ID 10 exists') - .with_request(method='GET', path='/product/10') - .will_respond_with(200) - .with_body(like(expected))) - - with pact.serve() as srv: - consumer = ProductConsumer(str(srv.url)) - user = consumer.get_product('10') - assert user.name == 'Margharita'