Skip to content

Commit f8e7eba

Browse files
chore: setup gold pact for contract testing [IDE-296] (#55)
1 parent b70c900 commit f8e7eba

File tree

14 files changed

+186
-116
lines changed

14 files changed

+186
-116
lines changed

.circleci/config.yml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@ jobs:
3131
executor: default
3232
steps:
3333
- checkout
34-
- run:
35-
name: Install tools
36-
command: make tools
3734
- run:
3835
name: Run unit tests
3936
command: make test
@@ -50,13 +47,17 @@ jobs:
5047
contract_test:
5148
executor: default
5249
steps:
50+
- setup_remote_docker
5351
- checkout
5452
- run:
5553
name: Install tools
5654
command: make tools
5755
- run:
5856
name: Run contract tests
5957
command: make contract-test
58+
- run:
59+
name: Publish contract
60+
command: make publish-contract
6061
build:
6162
executor: default
6263
steps:
@@ -71,6 +72,7 @@ jobs:
7172
- prodsec/security_scans:
7273
mode: auto
7374
iac-scan: disabled
75+
7476
# Orchestrate our job run sequence
7577
workflows:
7678
version: 2
@@ -94,11 +96,13 @@ workflows:
9496
- smoke_test:
9597
name: Smoke tests
9698
context:
97-
- code-client-go-smoke-tests-token
99+
- code-client-go-smoke-tests-token # SMOKE_TEST_TOKEN
98100
requires:
99101
- Unit tests
100102
- contract_test:
101103
name: Contract tests
104+
context:
105+
- code-client-go-contract-tests # PACT_BROKER_BASE_URL, PACT_BROKER_TOKEN
102106
requires:
103107
- Unit tests
104108
- build:

CONTRIBUTING.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ make contract-test
8686
These tests use `pact` and the name of each test should start with `TestPact`. The contracts end up in [https://snyk-dev.pactflow.io/](https://snyk-dev.pactflow.io/).
8787

8888
If writing contract tests, use the test implementations in [./internal/util/testutil](./internal/util/testutil) and
89-
make sure that at the top of each file is the following line `//go:build CONTRACT`.
89+
make sure that at the top of each file is the following line `//go:build contract`.
9090

91-
These tests are used then in the CI/CD as a gate for the PR.
91+
These tests are used then in the CI/CD as a gate for the PR and for the release process in `main`.
9292

9393
### Smoke tests
9494

@@ -192,7 +192,13 @@ Do not hold onto your changes for too long. Commit and push frequently and creat
192192
We use a [GitHub Action](https://github.com/marketplace/actions/conventional-release-labels) to generate labels which are then used to generate the release notes when merging the PR.
193193
The title of the PR is what is used to generate the labels.
194194

195+
The following steps run in the CI/CD of a PR and gate the merge:
196+
![](PR pipeline.jpg)
197+
195198
## Merging PRs
196199

197200
We use a [GitHub Action](https://github.com/marketplace/actions/semver-conventional-commits) to compute the version based on conventional commit messages, push a tag with the computed version, then use
198-
the [GitHub Release CLI](https://cli.github.com/manual/gh_release_create) to generate release notes based on labels.
201+
the [GitHub Release CLI](https://cli.github.com/manual/gh_release_create) to generate release notes based on labels.
202+
203+
The following steps run in the CI/CD of the `main` branch and gate the release:
204+
![](main pipeline.png)

Makefile

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,17 @@ GOARCH = $(shell go env GOARCH)
55
TOOLS_BIN := $(shell pwd)/.bin
66

77
OVERRIDE_GOCI_LINT_V := v1.55.2
8-
SHELL:=env PATH=$(TOOLS_BIN)/go:$(PATH) $(SHELL)
8+
SHELL:=env PATH=$(TOOLS_BIN)/go:$(TOOLS_BIN)/pact/bin:$(PATH) $(SHELL)
99

1010
## tools: Install required tooling.
1111
.PHONY: tools
1212
tools: $(TOOLS_BIN)/golangci-lint $(TOOLS_BIN)/go
1313
$(TOOLS_BIN)/golangci-lint:
1414
@curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/$(OVERRIDE_GOCI_LINT_V)/install.sh | sh -s -- -b $(TOOLS_BIN)/ $(OVERRIDE_GOCI_LINT_V)
1515

16+
$(TOOLS_BIN)/pact-broker:
17+
@cd $(TOOLS_BIN); curl -fsSL https://raw.githubusercontent.com/pact-foundation/pact-ruby-standalone/master/install.sh | PACT_CLI_VERSION=v2.4.4 bash; cd ../
18+
1619
$(TOOLS_BIN)/go:
1720
mkdir -p ${TOOLS_BIN}/go
1821
@cat tools.go | grep _ | awk -F'"' '{print $$2}' | xargs -tI % sh -c 'GOBIN=${TOOLS_BIN}/go go install %'
@@ -44,24 +47,28 @@ clean:
4447
@rm -rf $(TOOLS_BIN)
4548

4649
.PHONY: test
47-
test:
50+
test:
4851
@echo "Testing..."
49-
@go test -cover ./...
52+
@go test -cover . ./...
5053

5154
.PHONY: testv
52-
testv:
55+
testv:
5356
@echo "Testing verbosely..."
54-
@go test -v
57+
@go test -v . ./...
58+
59+
.PHONY: smoke-test
60+
smoke-test:
61+
@echo "Smoke testing..."
62+
@go test -tags=smoke
5563

5664
.PHONY: contract-test
5765
contract-test: $(TOOLS_BIN)
5866
@echo "Contract testing..."
59-
@go test -tags=CONTRACT ./...
67+
@go test -tags=contract ./...
6068

61-
.PHONY: smoke-test
62-
smoke-test:
63-
@echo "Smoke testing..."
64-
@go test -tags=SMOKE
69+
.PHONY: publish-contract
70+
publish-contract: $(TOOLS_BIN)/pact-broker
71+
./scripts/publish-contract.sh
6572

6673
.PHONY: generate
6774
generate:
@@ -106,5 +113,8 @@ help:
106113
@echo "$(LOG_PREFIX) download-apis"
107114
@echo "$(LOG_PREFIX) download-workspace-api"
108115
@echo "$(LOG_PREFIX) download-orchestration-api"
116+
@echo "$(LOG_PREFIX) contract-test
117+
@echo "$(LOG_PREFIX) smoke-test
118+
@echo "$(LOG_PREFIX) publish-contract
109119
@echo "$(LOG_PREFIX) GOOS Specify Operating System to compile for (see golang GOOS, default=$(GOOS))"
110120
@echo "$(LOG_PREFIX) GOARCH Specify Architecture to compile for (see golang GOARCH, default=$(GOARCH))"

PR pipeline.jpg

699 KB
Loading

internal/deepcode/client_pact_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//go:build CONTRACT
1+
//go:build contract
22

33
/*
44
* © 2022-2024 Snyk Limited
@@ -51,7 +51,7 @@ var (
5151
pact *consumer.V2HTTPMockProvider
5252
)
5353

54-
func TestSnykCodeClientPact(t *testing.T) {
54+
func TestPact_DeepcodeClient(t *testing.T) {
5555
setupPact(t)
5656

5757
t.Run("Create bundle", func(t *testing.T) {

internal/orchestration/2024-02-16/client_pact_test.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//go:build CONTRACT
1+
//go:build contract
22

33
/*
44
* © 2022-2024 Snyk Limited
@@ -23,6 +23,7 @@ import (
2323
"fmt"
2424
"net/http"
2525
"testing"
26+
"time"
2627

2728
"github.com/pact-foundation/pact-go/v2/consumer"
2829
"github.com/pact-foundation/pact-go/v2/matchers"
@@ -42,7 +43,7 @@ import (
4243
const (
4344
consumerName = "code-client-go"
4445
pactDir = "./pacts"
45-
pactProvider = "OrchestrationApi"
46+
pactProvider = "orchestration-service"
4647

4748
orgUUID = "e7ea34c9-de0f-422c-bf2c-4654c2e2da90"
4849
workspaceId = "b6ea34c9-de0f-422c-bf2c-4654c2e2da90"
@@ -256,9 +257,13 @@ func setupPact(t *testing.T) {
256257
errorReporter := testutil.NewTestErrorReporter()
257258
httpClient = codeClientHTTP.NewHTTPClient(
258259
func() *http.Client {
259-
return http.DefaultClient
260+
client := http.Client{
261+
Timeout: time.Duration(60) * time.Second,
262+
Transport: testutil.TestAuthRoundTripper{http.DefaultTransport},
263+
}
264+
return &client
260265
},
261-
codeClientHTTP.WithRetryCount(3),
266+
codeClientHTTP.WithRetryCount(1),
262267
codeClientHTTP.WithInstrumentor(instrumentor),
263268
codeClientHTTP.WithErrorReporter(errorReporter),
264269
codeClientHTTP.WithLogger(&logger),

internal/orchestration/2024-02-16/pacts/code-client-go-orchestrationapi.json renamed to internal/orchestration/2024-02-16/pacts/code-client-go-orchestration-service.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@
164164
"providerState": "Scan ID",
165165
"request": {
166166
"method": "GET",
167-
"path": "/orgs/e7ea34c9-de0f-422c-bf2c-4654c2e2da90/scans/c4a998ce-be47-4bca-b470-6a1f88becdda",
167+
"path": "/orgs/e7ea34c9-de0f-422c-bf2c-4654c2e2da90/scans/d1216ba2-d040-443c-bf3e-9c3cf16fa809",
168168
"query": "version=2024-02-16%7eexperimental"
169169
},
170170
"response": {
@@ -197,6 +197,6 @@
197197
}
198198
},
199199
"provider": {
200-
"name": "OrchestrationApi"
200+
"name": "orchestration-service"
201201
}
202202
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package testutil
2+
3+
import (
4+
"fmt"
5+
"github.com/google/uuid"
6+
"net/http"
7+
"os"
8+
)
9+
10+
type TestAuthRoundTripper struct {
11+
http.RoundTripper
12+
}
13+
14+
func (tart TestAuthRoundTripper) RoundTrip(req *http.Request) (res *http.Response, e error) {
15+
token := os.Getenv("SMOKE_TEST_TOKEN")
16+
if token == "" {
17+
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", uuid.New().String()))
18+
} else {
19+
req.Header.Set("Authorization", fmt.Sprintf("token %s", token))
20+
}
21+
return tart.RoundTripper.RoundTrip(req)
22+
}

internal/workspace/2024-05-14/client.go

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)