From a2fee63563f2b96313f075877f2a45505bcef375 Mon Sep 17 00:00:00 2001 From: Matt Fellows Date: Tue, 27 Jul 2021 23:30:45 +1000 Subject: [PATCH] test: make tests validate local and files from a pact broker --- .github/workflows/test.yml | 2 + Makefile | 27 +- examples/basic_test.go | 4 +- examples/consumer_test.go | 10 +- examples/pacts/V2Consumer-V2Provider.json | 100 ------ .../V2ConsumerMatch-V2ProviderMatch.json | 100 ------ examples/pacts/V3Consumer-V3Provider.json | 296 ------------------ .../V3MessageConsumer-V3MessageProvider.json | 60 ---- examples/provider_test.go | 14 +- make/config.mk | 6 +- 10 files changed, 44 insertions(+), 575 deletions(-) delete mode 100644 examples/pacts/V2Consumer-V2Provider.json delete mode 100644 examples/pacts/V2ConsumerMatch-V2ProviderMatch.json delete mode 100644 examples/pacts/V3Consumer-V3Provider.json delete mode 100644 examples/pacts/V3MessageConsumer-V3MessageProvider.json diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index ddaf917e4..7a5810214 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -14,6 +14,8 @@ jobs: echo "LD_LIBRARY_PATH=/tmp" >> $GITHUB_ENV echo "PACT_GO_LIB_DOWNLOAD_PATH=/tmp" >> $GITHUB_ENV echo "LOG_LEVEL=trace" >> $GITHUB_ENV + echo "APP_BRANCH=${GITHUB_REF#refs/heads/}" >> $GITHUB_ENV + echo "APP_SHA=${GITHUB_SHA}" >> $GITHUB_ENV - name: Install Go uses: actions/setup-go@v2 with: diff --git a/Makefile b/Makefile index 5d7a7ff15..b5920e73b 100755 --- a/Makefile +++ b/Makefile @@ -1,10 +1,26 @@ include make/config.mk TEST?=./... - .DEFAULT_GOAL := ci - -ci:: docker deps clean bin test pact #goveralls +PACT_CLI="docker run --rm -v ${PWD}:${PWD} -e PACT_BROKER_BASE_URL -e PACT_BROKER_USERNAME -e PACT_BROKER_PASSWORD -e PACT_BROKER_TOKEN pactfoundation/pact-cli:latest" + +ci:: deps clean bin test pact #goveralls + +# Run the ci target from a developer machine with the environment variables +# set as if it was on Travis CI. +# Use this for quick feedback when playing around with your workflows. +fake_ci: + @CI=true \ + APP_SHA=`git rev-parse --short HEAD`+`date +%s` \ + APP_BRANCH=`git rev-parse --abbrev-ref HEAD` \ + make ci + +# same as above, but just for pact +fake_pact: + @CI=true \ + APP_SHA=`git rev-parse --short HEAD`+`date +%s` \ + APP_BRANCH=`git rev-parse --abbrev-ref HEAD` \ + make pact docker: @echo "--- 🛠 Starting docker" @@ -43,8 +59,13 @@ install: bin pact: clean install #docker @echo "--- 🔨 Running Pact examples" go test -v -tags=consumer -count=1 github.com/pact-foundation/pact-go/v2/examples/... + make publish go test -v -timeout=10s -tags=provider -count=1 github.com/pact-foundation/pact-go/v2/examples/... +publish: + @echo "-- 📃 Publishing pacts" + @"${PACT_CLI}" publish ${PWD}/examples/pacts --consumer-app-version ${APP_SHA} --tag ${APP_BRANCH} + release: echo "--- 🚀 Releasing it" "$(CURDIR)/scripts/release.sh" diff --git a/examples/basic_test.go b/examples/basic_test.go index 36d89f6d0..9ca69d4a3 100644 --- a/examples/basic_test.go +++ b/examples/basic_test.go @@ -14,8 +14,8 @@ func TestProductAPIClient(t *testing.T) { // Specify the two applications in the integration we are testing // NOTE: this can usually be extracted out of the individual test for re-use) mockProvider, err := NewV2Pact(MockHTTPProviderConfig{ - Consumer: "ProductAPIConsumer", - Provider: "ProductAPI", + Consumer: "PactGoProductAPIConsumer", + Provider: "PactGoProductAPI", }) assert.NoError(t, err) diff --git a/examples/consumer_test.go b/examples/consumer_test.go index 8df3ff7be..c32fa33a5 100644 --- a/examples/consumer_test.go +++ b/examples/consumer_test.go @@ -21,7 +21,7 @@ func TestConsumerV2(t *testing.T) { SetLogLevel("TRACE") mockProvider, err := consumer.NewV2Pact(consumer.MockHTTPProviderConfig{ - Consumer: "V2Consumer", + Consumer: "PactGoV2Consumer", Provider: "V2Provider", Host: "127.0.0.1", TLS: true, @@ -64,7 +64,7 @@ func TestConsumerV2_Match(t *testing.T) { SetLogLevel("TRACE") mockProvider, err := consumer.NewV2Pact(consumer.MockHTTPProviderConfig{ - Consumer: "V2ConsumerMatch", + Consumer: "PactGoV2ConsumerMatch", Provider: "V2ProviderMatch", Host: "127.0.0.1", TLS: true, @@ -95,7 +95,7 @@ func TestConsumerV3(t *testing.T) { SetLogLevel("TRACE") mockProvider, err := NewV3Pact(MockHTTPProviderConfig{ - Consumer: "V3Consumer", + Consumer: "PactGoV3Consumer", Provider: "V3Provider", Host: "127.0.0.1", TLS: true, @@ -152,7 +152,7 @@ func TestConsumerV2AllInOne(t *testing.T) { SetLogLevel("TRACE") mockProvider, err := consumer.NewV2Pact(consumer.MockHTTPProviderConfig{ - Consumer: "V2ConsumerAllInOne", + Consumer: "PactGoV2ConsumerAllInOne", Provider: "V2Provider", Host: "127.0.0.1", TLS: true, @@ -201,7 +201,7 @@ func TestMessagePact(t *testing.T) { SetLogLevel("TRACE") provider, err := NewMessagePactV3(MessageConfig{ - Consumer: "V3MessageConsumer", + Consumer: "PactGoV3MessageConsumer", Provider: "V3MessageProvider", // must be different to the HTTP one, can't mix both interaction styles }) assert.NoError(t, err) diff --git a/examples/pacts/V2Consumer-V2Provider.json b/examples/pacts/V2Consumer-V2Provider.json deleted file mode 100644 index 495bb0586..000000000 --- a/examples/pacts/V2Consumer-V2Provider.json +++ /dev/null @@ -1,100 +0,0 @@ -{ - "consumer": { - "name": "V2Consumer" - }, - "interactions": [ - { - "description": "A request to do a foo", - "providerState": "User foo exists", - "request": { - "body": { - "datetime": "2020-01-01'T'08:00:45", - "id": 27, - "lastName": "billy", - "name": "billy" - }, - "headers": { - "Authorization": "Bearer 1234", - "Content-Type": "application/json" - }, - "matchingRules": { - "$.body.datetime": { - "match": "type" - }, - "$.body.id": { - "match": "type" - }, - "$.body.lastName": { - "match": "type" - }, - "$.body.name": { - "match": "type" - }, - "$.header.Authorization": { - "match": "type" - }, - "$.path": { - "match": "regex", - "regex": "\\/foo.*" - }, - "$.query.baz[0]": { - "match": "regex", - "regex": "[a-z]+" - }, - "$.query.baz[1]": { - "match": "regex", - "regex": "[a-z]+" - }, - "$.query.baz[2]": { - "match": "regex", - "regex": "[a-z]+" - } - }, - "method": "POST", - "path": "/foobar", - "query": "baz=bar&baz=bat&baz=baz" - }, - "response": { - "body": { - "datetime": "2020-01-01", - "itemsMin": [ - "thereshouldbe3ofthese", - "thereshouldbe3ofthese", - "thereshouldbe3ofthese" - ], - "lastName": "Sampson", - "name": "Billy" - }, - "headers": { - "Content-Type": "application/json" - }, - "matchingRules": { - "$.body.datetime": { - "match": "regex", - "regex": "[0-9\\-]+" - }, - "$.body.itemsMin": { - "match": "type", - "min": 3 - }, - "$.header.Content-Type": { - "match": "regex", - "regex": "application\\/json" - } - }, - "status": 200 - } - } - ], - "metadata": { - "pactRust": { - "version": "0.9.5" - }, - "pactSpecification": { - "version": "2.0.0" - } - }, - "provider": { - "name": "V2Provider" - } -} \ No newline at end of file diff --git a/examples/pacts/V2ConsumerMatch-V2ProviderMatch.json b/examples/pacts/V2ConsumerMatch-V2ProviderMatch.json deleted file mode 100644 index 9fffdee36..000000000 --- a/examples/pacts/V2ConsumerMatch-V2ProviderMatch.json +++ /dev/null @@ -1,100 +0,0 @@ -{ - "consumer": { - "name": "V2ConsumerMatch" - }, - "interactions": [ - { - "description": "A request to do a foo", - "providerState": "User foo exists", - "request": { - "body": { - "datetime": "2020-01-01'T'08:00:45,format=yyyy-MM-dd'T'HH:mm:ss,generator=datetime", - "id": 27, - "lastName": "Sampson", - "name": "Billy" - }, - "headers": { - "Authorization": "Bearer 1234", - "Content-Type": "application/json" - }, - "matchingRules": { - "$.body.datetime": { - "match": "type" - }, - "$.body.id": { - "match": "type" - }, - "$.body.lastName": { - "match": "type" - }, - "$.body.name": { - "match": "type" - }, - "$.header.Authorization": { - "match": "type" - }, - "$.path": { - "match": "regex", - "regex": "\\/foo.*" - }, - "$.query.baz[0]": { - "match": "regex", - "regex": "[a-z]+" - }, - "$.query.baz[1]": { - "match": "regex", - "regex": "[a-z]+" - }, - "$.query.baz[2]": { - "match": "regex", - "regex": "[a-z]+" - } - }, - "method": "POST", - "path": "/foobar", - "query": "baz=bar&baz=bat&baz=baz" - }, - "response": { - "body": { - "datetime": "2020-01-01'T'08:00:45,format=yyyy-MM-dd'T'HH:mm:ss,generator=datetime", - "id": 27, - "lastName": "Sampson", - "name": "Billy" - }, - "headers": { - "Content-Type": "application/json" - }, - "matchingRules": { - "$.body.datetime": { - "match": "type" - }, - "$.body.id": { - "match": "type" - }, - "$.body.lastName": { - "match": "type" - }, - "$.body.name": { - "match": "type" - }, - "$.header.Content-Type": { - "match": "regex", - "regex": "application\\/json" - } - }, - "status": 200 - } - } - ], - "metadata": { - "pactRust": { - "version": "0.9.5" - }, - "pactSpecification": { - "version": "2.0.0" - } - }, - "provider": { - "name": "V2ProviderMatch" - } -} \ No newline at end of file diff --git a/examples/pacts/V3Consumer-V3Provider.json b/examples/pacts/V3Consumer-V3Provider.json deleted file mode 100644 index f4435f093..000000000 --- a/examples/pacts/V3Consumer-V3Provider.json +++ /dev/null @@ -1,296 +0,0 @@ -{ - "consumer": { - "name": "V3Consumer" - }, - "interactions": [ - { - "description": "A request to do a foo", - "providerStates": [ - { - "name": "User foo exists", - "params": { - "id": "foo" - } - } - ], - "request": { - "body": { - "datetime": "2020-01-01T08:00:45", - "id": 27, - "lastName": "billy", - "name": "billy" - }, - "generators": { - "body": { - "$.datetime": { - "format": "yyyy-MM-dd'T'HH:mm:ss", - "type": "DateTime" - }, - "$.name": { - "expression": "${name}", - "type": "ProviderState" - } - } - }, - "headers": { - "Authorization": "Bearer 1234", - "Content-Type": "application/json" - }, - "matchingRules": { - "body": { - "$.datetime": { - "combine": "AND", - "matchers": [ - { - "match": "timestamp", - "timestamp": "yyyy-MM-dd'T'HH:mm:ss" - } - ] - }, - "$.id": { - "combine": "AND", - "matchers": [ - { - "match": "type" - } - ] - }, - "$.lastName": { - "combine": "AND", - "matchers": [ - { - "match": "type" - } - ] - }, - "$.name": { - "combine": "AND", - "matchers": [ - { - "match": "type" - } - ] - } - }, - "header": { - "Authorization": { - "combine": "AND", - "matchers": [ - { - "match": "type" - } - ] - } - }, - "path": { - "combine": "AND", - "matchers": [ - { - "match": "regex", - "regex": "\\/foo.*" - } - ] - }, - "query": { - "baz[0]": { - "combine": "AND", - "matchers": [ - { - "match": "regex", - "regex": "[a-z]+" - } - ] - }, - "baz[1]": { - "combine": "AND", - "matchers": [ - { - "match": "regex", - "regex": "[a-z]+" - } - ] - }, - "baz[2]": { - "combine": "AND", - "matchers": [ - { - "match": "regex", - "regex": "[a-z]+" - } - ] - } - } - }, - "method": "POST", - "path": "/foobar", - "query": { - "baz": [ - "bar", - "bat", - "baz" - ] - } - }, - "response": { - "body": { - "accountBalance": 123.76, - "arrayContaining": [ - "string", - 1, - { - "foo": "bar" - } - ], - "datetime": "2020-01-01", - "equality": "a thing", - "id": 12, - "itemsMin": [ - "thereshouldbe3ofthese", - "thereshouldbe3ofthese", - "thereshouldbe3ofthese" - ], - "itemsMinMax": [ - 27, - 27, - 27, - 27, - 27 - ], - "lastName": "Sampson", - "name": "Billy", - "superstring": "foo" - }, - "headers": { - "Content-Type": "application/json" - }, - "matchingRules": { - "body": { - "$.accountBalance": { - "combine": "AND", - "matchers": [ - { - "match": "decimal" - } - ] - }, - "$.arrayContaining": { - "combine": "AND", - "matchers": [ - { - "match": "arrayContains", - "variants": [ - { - "index": 0, - "rules": { - "$": { - "combine": "AND", - "matchers": [ - { - "match": "type" - } - ] - } - } - }, - { - "index": 1, - "rules": { - "$": { - "combine": "AND", - "matchers": [ - { - "match": "integer" - } - ] - } - } - }, - { - "index": 2, - "rules": { - "$.foo": { - "combine": "AND", - "matchers": [ - { - "match": "type" - } - ] - } - } - } - ] - } - ] - }, - "$.datetime": { - "combine": "AND", - "matchers": [ - { - "match": "regex", - "regex": "[0-9\\-]+" - } - ] - }, - "$.equality": { - "combine": "AND", - "matchers": [ - { - "match": "equality" - } - ] - }, - "$.id": { - "combine": "AND", - "matchers": [ - { - "match": "integer" - } - ] - }, - "$.itemsMin": { - "combine": "AND", - "matchers": [ - { - "match": "type", - "min": 3 - } - ] - }, - "$.itemsMinMax": { - "combine": "AND", - "matchers": [ - { - "match": "type", - "max": 5, - "min": 3 - } - ] - }, - "$.superstring": { - "combine": "AND", - "matchers": [ - { - "match": "include", - "value": "foo" - } - ] - } - }, - "header": {} - }, - "status": 200 - } - } - ], - "metadata": { - "pactRust": { - "version": "0.9.5" - }, - "pactSpecification": { - "version": "3.0.0" - } - }, - "provider": { - "name": "V3Provider" - } -} \ No newline at end of file diff --git a/examples/pacts/V3MessageConsumer-V3MessageProvider.json b/examples/pacts/V3MessageConsumer-V3MessageProvider.json deleted file mode 100644 index 7937de382..000000000 --- a/examples/pacts/V3MessageConsumer-V3MessageProvider.json +++ /dev/null @@ -1,60 +0,0 @@ -{ - "consumer": { - "name": "V3MessageConsumer" - }, - "messages": [ - { - "contents": { - "datetime": "2020-01-01", - "id": 12, - "lastName": "Sampson", - "name": "Billy" - }, - "description": "a user event", - "matchingRules": { - "body": { - "$.datetime": { - "combine": "AND", - "matchers": [ - { - "match": "regex", - "regex": "[0-9\\-]+" - } - ] - }, - "$.id": { - "combine": "AND", - "matchers": [ - { - "match": "integer" - } - ] - } - } - }, - "metadata": { - "Content-Type": "application/json", - "contentType": "application/json" - }, - "providerStates": [ - { - "name": "User with id 127 exists", - "params": { - "id": 127 - } - } - ] - } - ], - "metadata": { - "pactRust": { - "version": "0.9.5" - }, - "pactSpecification": { - "version": "3.0.0" - } - }, - "provider": { - "name": "V3MessageProvider" - } -} \ No newline at end of file diff --git a/examples/provider_test.go b/examples/provider_test.go index 64f469993..ad66f3a28 100644 --- a/examples/provider_test.go +++ b/examples/provider_test.go @@ -46,9 +46,12 @@ func TestV3HTTPProvider(t *testing.T) { // Verify the Provider with local Pact Files err := verifier.VerifyProvider(t, VerifyRequest{ ProviderBaseURL: "http://localhost:8111", + Provider: "V3Provider", + ProviderVersion: os.Getenv("APP_SHA"), + BrokerURL: os.Getenv("PACT_BROKER_BASE_URL"), PactFiles: []string{ - filepath.ToSlash(fmt.Sprintf("%s/V3Consumer-V3Provider.json", pactDir)), - filepath.ToSlash(fmt.Sprintf("%s/V2ConsumerMatch-V2ProviderMatch.json", pactDir)), + filepath.ToSlash(fmt.Sprintf("%s/PactGoV3Consumer-V3Provider.json", pactDir)), + filepath.ToSlash(fmt.Sprintf("%s/PactGoV2ConsumerMatch-V2ProviderMatch.json", pactDir)), }, RequestFilter: f, BeforeEach: func() error { @@ -119,8 +122,11 @@ func TestV3MessageProvider(t *testing.T) { // Verify the Provider with local Pact Files verifier.Verify(t, VerifyMessageRequest{ VerifyRequest: VerifyRequest{ - PactFiles: []string{filepath.ToSlash(fmt.Sprintf("%s/V3MessageConsumer-V3MessageProvider.json", pactDir))}, - StateHandlers: stateMappings, + PactFiles: []string{filepath.ToSlash(fmt.Sprintf("%s/PactGoV3MessageConsumer-V3MessageProvider.json", pactDir))}, + StateHandlers: stateMappings, + Provider: "V3MessageProvider", + ProviderVersion: os.Getenv("APP_SHA"), + BrokerURL: os.Getenv("PACT_BROKER_BASE_URL"), }, MessageHandlers: functionMappings, }) diff --git a/make/config.mk b/make/config.mk index 30bb6fc5d..5b48ce4e2 100644 --- a/make/config.mk +++ b/make/config.mk @@ -1,6 +1,2 @@ export PATH := $(PWD)/pact/bin:$(PATH) -export PATH -export PACT_BROKER_PROTO=http -export PACT_BROKER_URL=localhost -export PACT_BROKER_USERNAME=pact_workshop -export PACT_BROKER_PASSWORD=pact_workshop \ No newline at end of file +export PATH \ No newline at end of file