Skip to content
This repository has been archived by the owner on Mar 11, 2021. It is now read-only.

Commit

Permalink
contract-tests: Move publishing to a separate make target.
Browse files Browse the repository at this point in the history
  • Loading branch information
pmacik committed Jan 24, 2019
1 parent 39d2da7 commit 0383ca6
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 30 deletions.
31 changes: 24 additions & 7 deletions .make/test.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
26 changes: 3 additions & 23 deletions test/contracts/consumer_test/fabric8auth_test/consumer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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)
}
26 changes: 26 additions & 0 deletions test/contracts/pact.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
}
}
28 changes: 28 additions & 0 deletions test/contracts/publisher/main.go
Original file line number Diff line number Diff line change
@@ -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),
)
}

0 comments on commit 0383ca6

Please sign in to comment.