From c1c14d2cbdc03183f4fda25371497c16f4e70be0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sa=C3=BAl=20de=20Le=C3=B3n=20Guerrero?= Date: Thu, 25 Nov 2021 14:54:28 +0100 Subject: [PATCH 1/2] fix: makefile and states --- .../packages/tv-shows-api/pact/provider_tests.js | 4 ++++ python/Makefile | 11 +++++++---- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/javascript/packages/tv-shows-api/pact/provider_tests.js b/javascript/packages/tv-shows-api/pact/provider_tests.js index cb056c6..43dbfca 100644 --- a/javascript/packages/tv-shows-api/pact/provider_tests.js +++ b/javascript/packages/tv-shows-api/pact/provider_tests.js @@ -15,6 +15,10 @@ startMockServer().then(() => { pactBrokerUrl: 'http://localhost', publishVerificationResult: true, providerVersion: packageJson.version, + stateHandlers: { + ['a tv show exists']: () => {}, + ['provider allows tv show creation']: () => {}, + }, } new Verifier(opts).verifyProvider().then(function () { diff --git a/python/Makefile b/python/Makefile index 0328033..9d6870a 100644 --- a/python/Makefile +++ b/python/Makefile @@ -1,17 +1,20 @@ #!/usr/bin/make -f +MOVIES_API_VERSION=4.0.0 +DURATION_PROVIDER_VERSION=3.0.0 + args = `arg="$(filter-out $@,$(MAKECMDGOALS))" && echo $${arg:-${1}}` %: @: pactman-duration-provider-verify: - pactman-verifier -b http://localhost duration-provider http://localhost:9000 http://localhost:9000/_pact/provider_states + pactman-verifier -b http://localhost/ duration-provider http://localhost:9000 http://localhost:9000/_pact/provider_states -a $(DURATION_PROVIDER_VERSION) -r pactman-movies-api-verify: - pactman-verifier -b http://localhost movies-api http://localhost:9001 http://localhost:9001/_pact/provider_states + pactman-verifier -b http://localhost/ movies-api http://localhost:9001 http://localhost:9001/_pact/provider_states -a $(MOVIES_API_VERSION) -r pactman-movies-api-publish: - $ curl -v --silent --output /dev/null --show-error --fail -XPUT \-H "Content-Type: application/json" -d@pacts/pactman/movies-api-duration-provider-pact.json http://localhost/pacts/provider/duration-provider/consumer/movies-api/version/$(call args, "1.0.0") + $ curl -v --silent --output /dev/null --show-error --fail -XPUT \-H "Content-Type: application/json" -d@pacts/pactman/movies-api-duration-provider-pact.json http://localhost/pacts/provider/duration-provider/consumer/movies-api/version/$(call args, $(MOVIES_API_VERSION)) -.PHONY: pactman-movies-api-publish pactman-duration-provider-verify \ No newline at end of file +.PHONY: pactman-movies-api-publish pactman-duration-provider-verify pactman-movies-api-verify \ No newline at end of file From b258514c77dc85a84d61dcf75c2d42eef5d886d5 Mon Sep 17 00:00:00 2001 From: Valerio Cosentino Date: Thu, 25 Nov 2021 16:04:47 +0100 Subject: [PATCH 2/2] advancing --- pact-broker/ssl/nginx.conf | 28 +++++++++---------- python/Makefile | 5 +++- python/duration_provider/launch.py | 26 +++++++++++++++++ python/duration_provider/main.py | 7 +++++ .../create_pact_live.py | 0 .../create_pact_mocked.py | 0 python/movies_api/launch.py | 27 ++++++++++++++++++ python/movies_api/main.py | 12 +++++++- 8 files changed, 89 insertions(+), 16 deletions(-) create mode 100644 python/duration_provider/launch.py rename python/{pacts/pactman => movies_api}/create_pact_live.py (100%) rename python/{pacts/pactman => movies_api}/create_pact_mocked.py (100%) create mode 100644 python/movies_api/launch.py diff --git a/pact-broker/ssl/nginx.conf b/pact-broker/ssl/nginx.conf index e355a7f..fbd2494 100644 --- a/pact-broker/ssl/nginx.conf +++ b/pact-broker/ssl/nginx.conf @@ -10,17 +10,17 @@ server { } } -server { - listen 443 ssl; - server_name localhost; - ssl_certificate /etc/nginx/cert/pact_cert.pem; - ssl_certificate_key /etc/nginx/cert/pact_cert.key; - - location / { - proxy_pass http://app:80; - proxy_set_header Host $host; - proxy_set_header X-Real-IP $remote_addr; - proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; - proxy_set_header X-Forwarded-Proto $scheme; - } -} \ No newline at end of file +# server { +# listen 443 ssl; +# server_name localhost; +# ssl_certificate /etc/nginx/cert/pact_cert.pem; +# ssl_certificate_key /etc/nginx/cert/pact_cert.key; +# +# location / { +# proxy_pass http://app:80; +# proxy_set_header Host $host; +# proxy_set_header X-Real-IP $remote_addr; +# proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; +# proxy_set_header X-Forwarded-Proto $scheme; +# } +# } \ No newline at end of file diff --git a/python/Makefile b/python/Makefile index 9d6870a..490f070 100644 --- a/python/Makefile +++ b/python/Makefile @@ -14,7 +14,10 @@ pactman-duration-provider-verify: pactman-movies-api-verify: pactman-verifier -b http://localhost/ movies-api http://localhost:9001 http://localhost:9001/_pact/provider_states -a $(MOVIES_API_VERSION) -r +pactman-movies-api-verify-prg: + python ./movies_api/launch.py + pactman-movies-api-publish: $ curl -v --silent --output /dev/null --show-error --fail -XPUT \-H "Content-Type: application/json" -d@pacts/pactman/movies-api-duration-provider-pact.json http://localhost/pacts/provider/duration-provider/consumer/movies-api/version/$(call args, $(MOVIES_API_VERSION)) -.PHONY: pactman-movies-api-publish pactman-duration-provider-verify pactman-movies-api-verify \ No newline at end of file +.PHONY: pactman-movies-api-publish pactman-duration-provider-verify pactman-movies-api-verify pactman-movies-api-verify-prg \ No newline at end of file diff --git a/python/duration_provider/launch.py b/python/duration_provider/launch.py new file mode 100644 index 0000000..8a4d0f6 --- /dev/null +++ b/python/duration_provider/launch.py @@ -0,0 +1,26 @@ +import subprocess + +VERSION = '1.0.0' +PORT = 9000 + + +def main(): + p_app = subprocess.Popen(['python', './main.py']) + + try: + p_verifier = subprocess.Popen( + [ + 'pactman-verifier', + '-b', 'http://localhost/', 'duration-provider', + f'http://localhost:{PORT}', f'http://localhost:{PORT}/_pact/provider_states', '-a', + VERSION, '-r' + ] + ) + p_verifier.communicate() + except Exception: + p_app.kill() + p_app.kill() + + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/python/duration_provider/main.py b/python/duration_provider/main.py index 9683215..29d6755 100644 --- a/python/duration_provider/main.py +++ b/python/duration_provider/main.py @@ -1,7 +1,10 @@ +import uvicorn from typing import Dict, Any from fastapi import FastAPI +PORT = 9000 + app = FastAPI() @@ -13,3 +16,7 @@ async def duration(media_id: int): @app.post('/_pact/provider_states') def provider_states(item: Dict[Any, Any] = None): return {'result': item['state']} + + +if __name__ == "__main__": + uvicorn.run(app, host="0.0.0.0", port=PORT) \ No newline at end of file diff --git a/python/pacts/pactman/create_pact_live.py b/python/movies_api/create_pact_live.py similarity index 100% rename from python/pacts/pactman/create_pact_live.py rename to python/movies_api/create_pact_live.py diff --git a/python/pacts/pactman/create_pact_mocked.py b/python/movies_api/create_pact_mocked.py similarity index 100% rename from python/pacts/pactman/create_pact_mocked.py rename to python/movies_api/create_pact_mocked.py diff --git a/python/movies_api/launch.py b/python/movies_api/launch.py new file mode 100644 index 0000000..63299f5 --- /dev/null +++ b/python/movies_api/launch.py @@ -0,0 +1,27 @@ +import subprocess + +VERSION = '1.0.0' +PORT = 9001 + + +def main(): + p_app = subprocess.Popen(['python', './main.py']) + + try: + p_verifier = subprocess.Popen( + [ + 'pactman-verifier', + '-b', 'http://localhost/', 'movies-api', + f'http://localhost:{PORT}', f'http://localhost:{PORT}/_pact/provider_states', '-a', + VERSION, '-r' + ] + ) + + p_verifier.communicate() + except Exception: + p_app.kill() + p_app.kill() + + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/python/movies_api/main.py b/python/movies_api/main.py index 558116e..e85559c 100644 --- a/python/movies_api/main.py +++ b/python/movies_api/main.py @@ -1,9 +1,11 @@ +import uvicorn from typing import Dict, Any, Optional -from fastapi import FastAPI, Response +from fastapi import FastAPI from fastapi.responses import JSONResponse from pydantic import BaseModel import requests +PORT = 9001 JSON_RESULT = { "id": 42, "name": "The Silence of the Lambs", @@ -15,6 +17,7 @@ DURATION_PROVIDER_ENDPOINT = 'http://localhost:9000/duration' HEADERS = {"Content-Type": "application/json; charset=utf-8"} + class Movie(BaseModel): name: str genre: str @@ -23,6 +26,7 @@ class Movie(BaseModel): app = FastAPI() + def get_duration(movie_id): response = requests.get(f"{DURATION_PROVIDER_ENDPOINT}/{movie_id}") return response.json() @@ -37,12 +41,18 @@ async def movie(movie_id: int): return JSONResponse(content=JSON_RESULT, headers=HEADERS) + @app.post("/movies") async def create_movie(movie: Movie): content = dict(movie) content['id'] = 42 return JSONResponse(content=content, headers=HEADERS, status_code=201) + @app.post('/_pact/provider_states') def provider_states(item: Dict[Any, Any] = None): return {'result': item['state']} + + +if __name__ == "__main__": + uvicorn.run(app, host="0.0.0.0", port=PORT) \ No newline at end of file