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

Add consumer side of WIT+Auth contract tests. #2393

Merged
merged 31 commits into from
Feb 21, 2019
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
43cdc1a
contract-tests: Add contract tests.
pmacik Nov 9, 2018
d774c30
contract-tests: Implement consumer of fabric8auth service.
pmacik Jan 11, 2019
ef0cebe
contract-tests: Add CICO job script.
pmacik Jan 17, 2019
ece3cde
contract-tests: Update according to #2939 suggestions
pmacik Jan 21, 2019
07d2cbb
contract-tests: Update package names and workaround pact-go#108 issue.
pmacik Jan 22, 2019
189ead0
contract-tests: Move publishing to a separate make target.
pmacik Jan 23, 2019
2ad63c6
contract-tests: Update cico job to publish pact to broker.
pmacik Jan 24, 2019
05969c4
contract-tests: Rearange test files/packages.
pmacik Jan 28, 2019
bdcdf36
Update .make/test.mk
kwk Jan 29, 2019
2452c80
Update .make/test.mk
kwk Jan 29, 2019
8a5de57
Update .make/test.mk
kwk Jan 29, 2019
51c5800
Update test/contracts/.gitignore
kwk Jan 29, 2019
5f134b3
Update .make/test.mk
kwk Jan 29, 2019
7dfed09
Update .make/test.mk
kwk Jan 29, 2019
3928198
Update test/contracts/.gitignore
kwk Jan 29, 2019
9db26af
contract-tests: Move auth model closer.
pmacik Feb 1, 2019
46de846
Use GOLINT_DIR instead of hard-coded path
pmacik Feb 1, 2019
37b0d23
Make it run :)
pmacik Feb 1, 2019
660ebeb
contract-tests: Set PactDir and LogDir properly to be in tmp directory.
pmacik Feb 4, 2019
62dba45
contract-tests: Ensure Pact CLI is installed in the CICO script.
pmacik Feb 6, 2019
47c043f
contract-tests: Optionally disable cico_setup function.
pmacik Feb 13, 2019
908f3d9
Merge branch 'master' into contract-tests-poc
pmacik Feb 14, 2019
5aabcc3
contract-tests: Add pact-go in Gopkg.toml.
pmacik Feb 14, 2019
4047d95
contract-tests: Remove hashicorp/logutils 'hack' to suppress DEBUG ou…
pmacik Feb 14, 2019
08070e7
contract-tests: Add logrus dependency into Gopkg.toml.
pmacik Feb 19, 2019
cd4c62e
contract-tests: Workaround 'set -e' for pact-mock-service check.
pmacik Feb 19, 2019
4f70d36
contract-tests: Make it work on CICO job.
pmacik Feb 19, 2019
0b13e6b
contract-tests: Fix cico setup.
pmacik Feb 19, 2019
8853962
contract-tests: Fix docker container name in cico script.
pmacik Feb 20, 2019
33ced80
contract_tests: Load PACT_* env variables from Jenkins.
pmacik Feb 20, 2019
f8189b4
Merge branch 'master' into contract-tests-poc
pmacik Feb 21, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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?=1.0.0)
$(eval PACT_TAGS?=latest)
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
pmacik marked this conversation as resolved.
Show resolved Hide resolved
## 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
36 changes: 29 additions & 7 deletions Gopkg.lock

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

7 changes: 6 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,7 @@ required = [
"github.com/wadey/gocovmerge",
"github.com/pilu/fresh",
"github.com/gojuno/minimock/cmd/minimock",
"github.com/pact-foundation/pact-go",
]

ignored = [
Expand Down Expand Up @@ -157,6 +158,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
29 changes: 29 additions & 0 deletions cico_run_contract_tests_consumer.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/bash

. cico_setup.sh

CICO_RUN="${CICO_RUN:-true}"
if [ "$CICO_RUN" == "true" ]; then
cico_setup;
fi

make deps

export TMP_DIR="$(readlink -f tmp)"

# Add Pact CLI to PATH
export PATH="$TMP_DIR/pact/bin:$PATH"

# Ensure Pact CLI is installed
pact-mock-service version &> /dev/null
test_pact_exit=$?
if [ $test_pact_exit -ne 0 ]; then
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_DIR/pact-cli.tar.gz"
tar -xf "$TMP_DIR/pact-cli.tar.gz" --directory "$TMP_DIR"
fi

# Run the contract tests
make test-contracts-consumer-no-coverage

# Publish the generated Pact files to Pact broker.
make publish-contract-testing-pacts-to-broker
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
pmacik marked this conversation as resolved.
Show resolved Hide resolved
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