From 0383ca68bb2cd91964fad86142c134927c089d0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?PaveL=20Mac=C3=ADk?= Date: Wed, 23 Jan 2019 17:35:03 +0100 Subject: [PATCH] contract-tests: Move publishing to a separate make target. --- .make/test.mk | 31 ++++++++++++++----- .../fabric8auth_test/consumer_test.go | 26 ++-------------- test/contracts/pact.go | 26 ++++++++++++++++ test/contracts/publisher/main.go | 28 +++++++++++++++++ 4 files changed, 81 insertions(+), 30 deletions(-) create mode 100644 test/contracts/publisher/main.go diff --git a/.make/test.mk b/.make/test.mk index 93143c017c..adb7f9c0ed 100644 --- a/.make/test.mk +++ b/.make/test.mk @@ -182,18 +182,35 @@ test-integration-benchmark: prebuild-check migrate-database $(SOURCES) .PHONY: test-contracts-consumer-no-coverage ## Runs the consumer side of contract tests WITHOUT producing coverage files for each package. -## and publish generated Pact file (the contract) to the Pact broker. +test-contracts-consumer-no-coverage: + $(call log-info,"Running test: $@") + $(eval TEST_PACKAGES:=$(shell go list ./... | grep -e 'contracts/consumer')) + $(eval PACT_DIR=$(PWD)/test/contracts/pacts) + $(eval PACT_VERSION="latest") + go test $(GO_TEST_VERBOSITY_FLAG) -count=1 $(TEST_PACKAGES) + +.PHONY: publish-contract-testing-pacts-to-broker +## Publishes generated Pact file (the contracts) to the Pact broker. ## The following env variables needs to be set in environment: ## - Pact broker for storing pact files ## PACT_BROKER_URL ## PACT_BROKER_USERNAME ## PACT_BROKER_PASSWORD -test-contracts-consumer-no-coverage: - $(call log-info,"Running test: $@") - $(eval TEST_PACKAGES:=$(shell go list ./... | grep -e 'contracts/consumer')) - PACT_DIR=$(PWD)/test/contracts/pacts \ - PACT_VERSION="latest" \ - go test $(GO_TEST_VERBOSITY_FLAG) -count=1 $(TEST_PACKAGES) +publish-contract-testing-pacts-to-broker: + $(call log-info,"Publishing pact files to Broker") + $(eval PACT_DIR=$(PWD)/test/contracts/pacts) + $(eval PACT_FILES:=$(shell find $(PACT_DIR) -name '*.json')) + $(eval PACT_VERSION?=1.0.0) + $(eval PACT_TAGS?=latest) + go run ./test/contracts/publisher/main.go "$(PACT_FILES)" "$(PACT_VERSION)" "$(PACT_TAGS)" + +.PHONY: clean-contract-tests +## Cleans generated pacts and logs from contract tests +clean-contract-tests: + $(call log-info,"Cleaning generated pacts and logs from contract tests") + $(eval PACT_DIR=$(PWD)/test/contracts/pacts) + $(eval LOGS_TO_BE_CLEANED:=$(shell find test/contracts -name '*logs')) + rm -rvf $(PACT_DIR) $(LOGS_TO_BE_CLEANED) .PHONY: test-remote ## Runs the remote tests and produces coverage files for each package. diff --git a/test/contracts/consumer_test/fabric8auth_test/consumer_test.go b/test/contracts/consumer_test/fabric8auth_test/consumer_test.go index 93e7c48d77..be83b5c69a 100644 --- a/test/contracts/consumer_test/fabric8auth_test/consumer_test.go +++ b/test/contracts/consumer_test/fabric8auth_test/consumer_test.go @@ -8,7 +8,7 @@ import ( "github.com/fabric8-services/fabric8-auth/test/contracts/model" "github.com/fabric8-services/fabric8-wit/test/contracts" "github.com/pact-foundation/pact-go/dsl" - "github.com/pact-foundation/pact-go/types" + "github.com/stretchr/testify/require" ) // TestFabric8AuthConsumer runs all consumer side contract tests @@ -17,15 +17,10 @@ func TestFabric8AuthConsumer(t *testing.T) { log.SetOutput(os.Stdout) var pactDir = os.Getenv("PACT_DIR") - var pactVersion = os.Getenv("PACT_VERSION") var pactConsumer = "fabric8-wit" var pactProvider = "fabric8-auth" - var pactBrokerURL = os.Getenv("PACT_BROKER_URL") - var pactBrokerUsername = os.Getenv("PACT_BROKER_USERNAME") - var pactBrokerPassword = os.Getenv("PACT_BROKER_PASSWORD") - // Create Pact connecting to local Daemon pact := &dsl.Pact{ Consumer: pactConsumer, @@ -52,21 +47,6 @@ func TestFabric8AuthConsumer(t *testing.T) { // Write a pact file pactFile := contracts.PactFile(pactConsumer, pactProvider) log.Printf("All tests done, writing a pact file (%s).\n", pactFile) - pact.WritePact() - - log.Printf("Publishing pact to a broker (%s)...\n", pactBrokerURL) - - p := dsl.Publisher{} - err := p.Publish(types.PublishRequest{ - PactURLs: []string{pactFile}, - PactBroker: pactBrokerURL, - BrokerUsername: pactBrokerUsername, - BrokerPassword: pactBrokerPassword, - ConsumerVersion: pactVersion, - Tags: []string{"latest"}, - }) - - if err != nil { - log.Fatalf("Unable to publish pact to a broker (%s):\n%+v\n", pactBrokerURL, err) - } + err := pact.WritePact() + require.NoError(t, err) } diff --git a/test/contracts/pact.go b/test/contracts/pact.go index a3f86b2023..99b4edbcc3 100644 --- a/test/contracts/pact.go +++ b/test/contracts/pact.go @@ -2,11 +2,14 @@ package contracts import ( "fmt" + "log" "os" "strings" "testing" + "github.com/hashicorp/logutils" "github.com/pact-foundation/pact-go/dsl" + "github.com/pact-foundation/pact-go/types" "github.com/stretchr/testify/require" ) @@ -35,3 +38,26 @@ func CheckErrorAndCleanPact(t *testing.T, pact *dsl.Pact, err1 error) { } require.NoError(t, err1) } + +// PublishPactFileToBroker publishes given Pact files to a given Pact broker. +func PublishPactFileToBroker(pactFiles []string, pactBrokerURL string, pactBrokerUsername string, pactBrokerPassword string, pactVersion string, tags []string) { + log.SetOutput(&logutils.LevelFilter{ + Levels: []logutils.LogLevel{"DEBUG", "INFO", "WARN", "ERROR"}, + MinLevel: logutils.LogLevel("INFO"), + Writer: os.Stderr, + }) + log.Printf("Publishing pact files %s to a broker (%s)...\n", pactFiles, pactBrokerURL) + p := dsl.Publisher{} + err := p.Publish(types.PublishRequest{ + PactURLs: pactFiles, + PactBroker: pactBrokerURL, + BrokerUsername: pactBrokerUsername, + BrokerPassword: pactBrokerPassword, + ConsumerVersion: pactVersion, + Tags: tags, + }) + + if err != nil { + log.Fatalf("Unable to publish pact to a broker (%s):\n%+v\n", pactBrokerURL, err) + } +} diff --git a/test/contracts/publisher/main.go b/test/contracts/publisher/main.go new file mode 100644 index 0000000000..d2df7aeb86 --- /dev/null +++ b/test/contracts/publisher/main.go @@ -0,0 +1,28 @@ +package main + +import ( + "os" + "regexp" + + "github.com/fabric8-services/fabric8-wit/test/contracts" +) + +func main() { + pactFiles := os.Args[1] + pactBrokerURL := os.Getenv("PACT_BROKER_URL") + pactBrokerUsername := os.Getenv("PACT_BROKER_USERNAME") + pactBrokerPassword := os.Getenv("PACT_BROKER_PASSWORD") + pactVersion := os.Args[2] + tags := os.Args[3] + + re := regexp.MustCompile("[;\n]") + + contracts.PublishPactFileToBroker( + re.Split(pactFiles, -1), + pactBrokerURL, + pactBrokerUsername, + pactBrokerPassword, + pactVersion, + re.Split(tags, -1), + ) +}