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

Commit

Permalink
Add consumer side of WIT+Auth contract tests. (#2393)
Browse files Browse the repository at this point in the history
This PR adds consumer side of the contract tests for the [WIT service](https://github.com/fabric8-services/fabric8-wit) consuming [Auth service](https://github.com/fabric8-services/fabric8-auth) API endpoints.

CICO job: [openshiftio/openshiftio-cico-jobs#940](openshiftio/openshiftio-cico-jobs#940)

The provider side (Auth service) of the contract tests is covered by [fabric8-services/fabric8-auth#711](fabric8-services/fabric8-auth#711)

WorkItem: https://openshift.io/openshiftio/Openshift_io/plan/detail/1329
  • Loading branch information
pmacik authored and kwk committed Feb 21, 2019
1 parent cf91483 commit 54759c8
Show file tree
Hide file tree
Showing 15 changed files with 647 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .make/Makefile.lnx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ GOCOVMERGE_BIN=$(VENDOR_DIR)/github.com/wadey/gocovmerge/gocovmerge
DOCKER_BIN_NAME=docker
DOCKER_COMPOSE_BIN_NAME=docker-compose

GOLINT_DIR=$(VENDOR_DIR)/github.com/golang/lint/golint
GOLINT_DIR=$(VENDOR_DIR)/golang.org/x/lint/golint
GOLINT_BIN=$(GOLINT_DIR)/golint
GOCYCLO_DIR=$(VENDOR_DIR)/github.com/fzipp/gocyclo
GOCYCLO_BIN=$(GOCYCLO_DIR)/gocyclo
Expand Down
2 changes: 1 addition & 1 deletion .make/Makefile.win
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ GOCOVMERGE_BIN=$(VENDOR_DIR)/github.com/wadey/gocovmerge/gocovmerge.exe
DOCKER_BIN_NAME=docker.exe
DOCKER_COMPOSE_BIN_NAME=docker-compose.exe

GOLINT_DIR=$(VENDOR_DIR)/github.com/golang/lint/golint
GOLINT_DIR=$(VENDOR_DIR)/golang.org/x/lint/golint
GOLINT_BIN=$(GOLINT_DIR)/golint.exe
GOCYCLO_DIR=$(VENDOR_DIR)/github.com/fzipp/gocyclo
GOCYCLO_BIN=$(GOCYCLO_DIR)/gocyclo.exe
Expand Down
34 changes: 34 additions & 0 deletions .make/test.mk
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,40 @@ test-integration-benchmark: prebuild-check migrate-database $(SOURCES)
$(eval TEST_PACKAGES:=$(shell go list ./... | grep -v $(ALL_PKGS_EXCLUDE_PATTERN)))
F8_DEVELOPER_MODE_ENABLED=1 F8_RESOURCE_DATABASE=1 F8_RESOURCE_UNIT_TEST=0 F8_LOG_LEVEL=$(F8_LOG_LEVEL) go test -run=^$$ -bench=. -cpu 1,2,4 -test.benchmem $(GO_TEST_VERBOSITY_FLAG) $(TEST_PACKAGES) | grep -E "Bench|allocs"

.PHONY: test-contracts-consumer-no-coverage
## Runs the consumer side of contract tests WITHOUT producing coverage files for each package.
test-contracts-consumer-no-coverage:
$(call log-info,"Running test: $@")
$(eval TEST_PACKAGES:=$(shell go list ./... | grep -e 'contracts/consumer'))
$(eval PACT_DIR=$(TMP_PATH)/test/contracts/pacts)
PACT_DIR=$(PACT_DIR) \
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
publish-contract-testing-pacts-to-broker:
$(call log-info,"Publishing pact files to Broker")
$(eval PACT_DIR=$(TMP_PATH)/test/contracts/pacts)
$(eval PACT_FILES:=$(shell find $(PACT_DIR) -name '*.json'))
$(eval PACT_VERSION?=PR-commit)
$(eval PACT_TAGS?=PR-number)
PACT_DIR=$(PACT_DIR) \
go run ./test/contracts/publisher/main.go "$(PACT_FILES)" "$(PACT_VERSION)" "$(PACT_TAGS)"

CLEAN_TARGETS += clean-contract-tests
.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=$(TMP_PATH)/test/contracts/pacts)
$(eval LOGS_TO_BE_CLEANED:=$(shell find $(TMP_PATH)/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.
test-remote: prebuild-check clean-coverage-remote $(COV_PATH_REMOTE)
Expand Down
39 changes: 31 additions & 8 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@


required = [
"github.com/golang/lint/golint",
"golang.org/x/lint/golint",
"github.com/fzipp/gocyclo",
"github.com/goadesign/goa/cors",
"github.com/goadesign/goa/encoding/form",
Expand All @@ -50,6 +50,8 @@ required = [
"github.com/wadey/gocovmerge",
"github.com/pilu/fresh",
"github.com/gojuno/minimock/cmd/minimock",
"github.com/pact-foundation/pact-go",
"github.com/sirupsen/logrus"
]

ignored = [
Expand Down Expand Up @@ -157,6 +159,10 @@ ignored = [
name = "github.com/dnaeon/go-vcr"
revision= "9d71b8a6df86e00127f96bc8dabc09856ab8afdb"

[[constraint]]
name = "github.com/pact-foundation/pact-go"
revision = "v1.0.0-beta.3"

[prune]
go-tests = true
unused-packages = true
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ endif

# Build go tool to analysis the code
$(GOLINT_BIN):
cd $(VENDOR_DIR)/github.com/golang/lint/golint && go build -v
cd $(GOLINT_DIR) && go build -v
$(GOCYCLO_BIN):
cd $(VENDOR_DIR)/github.com/fzipp/gocyclo && go build -v

Expand Down
37 changes: 37 additions & 0 deletions cico_run_contract_tests_consumer.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/bin/bash

. cico_setup.sh

CICO_RUN="${CICO_RUN:-true}"
if [ "$CICO_RUN" == "true" ]; then
load_jenkins_vars;
if [ -e "jenkins-env.json" ]; then
eval "$(./env-toolkit load -f jenkins-env.json --regex 'PACT_*')"
fi
install_deps;
fi
make docker-start
make docker-build

DOCKER_CONTAINER_NAME="${BUILD_TAG:-fabric8-wit-local-build}"

#Ensure Pact CLI is installed
cmd="curl -L -s https://github.com/pact-foundation/pact-ruby-standalone/releases/download/v1.63.0/pact-1.63.0-linux-x86_64.tar.gz -o /tmp/pact-cli.tar.gz \
&& mkdir -p /tmp/pact \
&& tar -xf /tmp/pact-cli.tar.gz --directory /tmp \
&& rm -vf /tmp/pact-cli.tar.gz"
docker exec -t "$DOCKER_CONTAINER_NAME" /bin/bash -ec "$cmd"

# Run the contract tests
cmd="PATH=\$PATH:/tmp/pact/bin make test-contracts-consumer-no-coverage"
docker exec -t "$DOCKER_CONTAINER_NAME" /bin/bash -ec "$cmd"

# Publish the generated Pact files to Pact broker.
cmd="PATH=\$PATH:/tmp/pact/bin make publish-contract-testing-pacts-to-broker"
docker exec -t \
-e PACT_BROKER_URL=$PACT_BROKER_URL \
-e PACT_BROKER_USERNAME=$PACT_BROKER_USERNAME \
-e PACT_BROKER_PASSWORD=$PACT_BROKER_PASSWORD \
-e PACT_VERSION=${PACT_VERSION:-PR-commit} \
-e PACT_TAGS=${PACT_TAGS:-PR-testing} \
"$DOCKER_CONTAINER_NAME" /bin/bash -ec "$cmd"
4 changes: 4 additions & 0 deletions test/contracts/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# For API contract testing
pacts
output
**/*/logs
36 changes: 36 additions & 0 deletions test/contracts/consumer/fabric8auth/api_status_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package fabric8auth_test

import (
"log"
"testing"

"github.com/fabric8-services/fabric8-wit/test/contracts"
"github.com/fabric8-services/fabric8-wit/test/contracts/consumer"
"github.com/pact-foundation/pact-go/dsl"
)

// AuthAPIStatus defines contract of /api/status endpoint
func AuthAPIStatus(t *testing.T, pact *dsl.Pact) {

log.Printf("Invoking AuthAPIStatus now\n")

// Set up our expected interactions.
pact.
AddInteraction().
Given("Auth service is up and running.").
UponReceiving("A request to get status").
WithRequest(dsl.Request{
Method: "GET",
Path: dsl.String("/api/status"),
Headers: dsl.MapMatcher{"Content-Type": dsl.String("application/json")},
}).
WillRespondWith(dsl.Response{
Status: 200,
Headers: dsl.MapMatcher{"Content-Type": dsl.String("application/vnd.status+json")},
Body: dsl.Match(APIStatusMessage{}),
})

// Verify
err := pact.Verify(consumer_test.SimpleGetInteraction(pact, "/api/status"))
contracts_test.CheckErrorAndCleanPact(t, pact, err) //workaround for https://github.com/pact-foundation/pact-go/issues/108
}
35 changes: 35 additions & 0 deletions test/contracts/consumer/fabric8auth/api_token_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package fabric8auth_test

import (
"log"
"testing"

"github.com/fabric8-services/fabric8-wit/test/contracts"
"github.com/fabric8-services/fabric8-wit/test/contracts/consumer"
"github.com/pact-foundation/pact-go/dsl"
)

// AuthAPITokenKeys defines contract of /api/status endpoint
func AuthAPITokenKeys(t *testing.T, pact *dsl.Pact) {

log.Printf("Invoking AuthAPITokenKeys now\n")

// Set up our expected interactions.
pact.
AddInteraction().
Given("Auth service is up and running.").
UponReceiving("A request to get public keys").
WithRequest(dsl.Request{
Method: "GET",
Path: dsl.String("/api/token/keys"),
}).
WillRespondWith(dsl.Response{
Status: 200,
Headers: dsl.MapMatcher{"Content-Type": dsl.String("application/vnd.publickeys+json")},
Body: dsl.Match(TokenKeys{}),
})

// Verify
err := pact.Verify(consumer_test.SimpleGetInteraction(pact, "/api/token/keys"))
contracts_test.CheckErrorAndCleanPact(t, pact, err) //workaround for https://github.com/pact-foundation/pact-go/issues/108
}
Loading

0 comments on commit 54759c8

Please sign in to comment.