Skip to content

Commit

Permalink
Bootstrapping pact tests for replicated.app (#4882)
Browse files Browse the repository at this point in the history
  • Loading branch information
divolgin authored Sep 12, 2024
1 parent 99101de commit f8692e2
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 0 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,25 @@ jobs:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GORELEASER_CURRENT_TAG: ${{ needs.generate-tag.outputs.tag }}

publish-pact-contracts:
runs-on: ubuntu-20.04
needs: [ generate-tag, goreleaser ]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: '^1.20.0'
- uses: replicatedhq/action-install-pact@main
- name: Publish pact contracts
env:
PACT_BROKER_BASE_URL: ${{ vars.PACT_BROKER_BASE_URL }}
PACT_BROKER_TOKEN: ${{ secrets.PACT_BROKER_TOKEN }}
PACT_PUBLISH_CONTRACT: true
GIT_TAG: ${{ needs.generate-tag.outputs.tag }}
run: |
set -x
make pact-consumer
generate-kurl-addon:
runs-on: ubuntu-20.04
needs: [ generate-tag, build-kurl-proxy, build-kots, build-kotsadm-bundle ]
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ bin/
vendor
.idea/
.vscode/
pact_logs/
pacts/


# goreleaser
/dist
Expand Down
11 changes: 11 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ MINIO_TAG ?= 0.20240909.165928-r0
RQLITE_TAG ?= 8.30.2-r0
DEX_TAG ?= 2.41.1-r0
LVP_TAG ?= v0.6.7
PACT_PUBLISH_CONTRACT ?= false

define sendMetrics
@if [ -z "${PROJECT_NAME}" ]; then \
Expand Down Expand Up @@ -33,6 +34,16 @@ test:
go test $(TEST_BUILDFLAGS) ./pkg/... ./cmd/... -coverprofile cover.out; \
fi

.PHONY: pact-consumer
pact-consumer:
mkdir -p pacts/consumer && ( rm -rf pacts/consumer/* || : )
go test $(TEST_BUILDFLAGS) -v ./contracts/... || true
if [ "${PACT_PUBLISH_CONTRACT}" = "true" ]; then \
pact-broker publish ./pacts/consumer \
--auto-detect-version-properties \
--consumer-app-version ${GIT_TAG} || true; \
fi

.PHONY: e2e
e2e:
${MAKE} -C e2e
Expand Down
37 changes: 37 additions & 0 deletions contracts/replicated-app/init_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package replicated_app

import (
"os"
"path"
"testing"

"github.com/pact-foundation/pact-go/dsl"
)

var (
pact dsl.Pact
)

func TestMain(m *testing.M) {
dir, _ := os.Getwd()

pactDir := path.Join(dir, "..", "..", "pacts", "consumer")
logDir := path.Join(dir, "..", "..", "pact_logs")

pact = dsl.Pact{
Consumer: "kots",
Provider: "replicated-app",
LogDir: logDir,
PactDir: pactDir,
LogLevel: "debug",
}

pact.Setup(true)

code := m.Run()

pact.WritePact()
pact.Teardown()

os.Exit(code)
}
41 changes: 41 additions & 0 deletions contracts/replicated-app/root_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package replicated_app

import (
"fmt"
"net/http"
"testing"

"github.com/pact-foundation/pact-go/dsl"
"github.com/pkg/errors"
)

func Test_CheckConnectivity(t *testing.T) {
var test = func() (err error) {
req, err := http.NewRequest("GET", fmt.Sprintf("http://localhost:%d/", pact.Server.Port), nil)
if err != nil {
return errors.Wrap(err, "create request")
}

_, err = http.DefaultClient.Do(req)
if err != nil {
return errors.Wrap(err, "execute request")
}

return nil
}

pact.AddInteraction().
Given("Empty state").
UponReceiving("Check connectivity").
WithRequest(dsl.Request{
Method: "GET",
Path: dsl.String("/"),
}).
WillRespondWith(dsl.Response{
Status: 200,
})

if err := pact.Verify(test); err != nil {
t.Fatalf("Error on Verify: %v", err)
}
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ require (
github.com/opencontainers/image-spec v1.1.0
github.com/ory/dockertest/v3 v3.10.0
github.com/otiai10/copy v1.14.0
github.com/pact-foundation/pact-go v1.10.0
github.com/phayes/freeport v0.0.0-20220201140144-74d24b5ae9f5
github.com/pkg/errors v0.9.1
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2
Expand Down Expand Up @@ -406,6 +407,7 @@ require (
github.com/go-task/slim-sprig/v3 v3.0.0 // indirect
github.com/go-viper/mapstructure/v2 v2.1.0 // indirect
github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79 // indirect
github.com/hashicorp/logutils v1.0.0 // indirect
github.com/kopia/kopia v0.10.7 // indirect
github.com/kubernetes-csi/external-snapshotter/client/v4 v4.2.0 // indirect
github.com/moby/docker-image-spec v1.3.1 // indirect
Expand Down
Loading

0 comments on commit f8692e2

Please sign in to comment.