Skip to content

Commit

Permalink
Merge pull request #80 from replicatedhq/laverya/kots-pacts
Browse files Browse the repository at this point in the history
add a basic pact test for the kots client
  • Loading branch information
laverya committed Dec 3, 2019
2 parents 54aba0d + fbc6fe5 commit 57dc97f
Show file tree
Hide file tree
Showing 4 changed files with 226 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Makefile
Expand Up @@ -64,6 +64,13 @@ publish-pacts:
-H "Content-Type: application/json" \
-d@pacts/replicated-cli-vendor-graphql-api.json \
https://replicated-pact-broker.herokuapp.com/pacts/provider/vendor-graphql-api/consumer/replicated-cli/version/$(ABBREV_VERSION)
curl \
--silent --output /dev/null --show-error --fail \
--user ${PACT_BROKER_USERNAME}:${PACT_BROKER_PASSWORD} \
-X PUT \
-H "Content-Type: application/json" \
-d@pacts/replicated-cli-kots-vendor-graphql-api.json \
https://replicated-pact-broker.herokuapp.com/pacts/provider/vendor-graphql-api/consumer/replicated-cli-kots/version/$(ABBREV_VERSION)
curl \
--silent --output /dev/null --show-error --fail \
--user ${PACT_BROKER_USERNAME}:${PACT_BROKER_PASSWORD} \
Expand Down
84 changes: 84 additions & 0 deletions pacts/replicated-cli-kots-vendor-graphql-api.json
@@ -0,0 +1,84 @@
{
"consumer": {
"name": "replicated-cli-kots"
},
"provider": {
"name": "vendor-graphql-api"
},
"interactions": [
{
"description": "A real request to list releases for all-kots-releases",
"providerState": "list releases for all-kots-releases",
"request": {
"method": "POST",
"path": "/graphql",
"headers": {
"Authorization": "all-kots-releases-read-write-token",
"Content-Type": "application/json"
},
"body": {
"operationName": "",
"query": "\n query allKotsReleases($appId: ID!, $pageSize: Int, $currentPage: Int) {\n allKotsReleases(appId: $appId, pageSize: $pageSize, currentPage: $currentPage) {\n sequence\n channelSequence\n created\n updated\n releasedAt\n releaseNotes\n channels {\n id\n name\n currentVersion\n numReleases\n }\n isReleaseNotEditable\n }\n }\n",
"variables": {
"appId": "all-kots-releases"
}
}
},
"response": {
"status": 200,
"headers": {
},
"body": {
"data": {
"allKotsReleases": [
{
"channels": [
{
"currentVersion": "1.0.1",
"id": "all-kots-releases-beta",
"name": "Beta",
"numReleases": 1
},
{
"currentVersion": "1.0.1",
"id": "all-kots-releases-nightly",
"name": "Nightly",
"numReleases": 2
}
],
"created": "Tue Nov 10 2009 23:00:00 UTC",
"sequence": 2
},
{
"channels": [
{
"currentVersion": "1.0.0",
"id": "all-kots-releases-test",
"name": "Test",
"numReleases": 1
}
],
"created": "Tue Nov 10 2009 23:00:00 UTC",
"sequence": 1
}
]
}
},
"matchingRules": {
"$.body.data.allKotsReleases[0].created": {
"match": "type"
},
"$.body.data.allKotsReleases[1].created": {
"match": "type"
}
}
},
"metadata": null
}
],
"metadata": {
"pactSpecification": {
"version": "2.0.0"
}
}
}
41 changes: 41 additions & 0 deletions pkg/kotsclient/kotsclient_test.go
@@ -0,0 +1,41 @@
package kotsclient

import (
"os"
"path"
"testing"

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

var (
pact dsl.Pact
)

func TestMain(m *testing.M) {
pact = createPact()

pact.Setup(true)

code := m.Run()

pact.WritePact()
pact.Teardown()

os.Exit(code)
}

func createPact() dsl.Pact {
dir, _ := os.Getwd()

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

return dsl.Pact{
Consumer: "replicated-cli-kots",
Provider: "vendor-graphql-api",
LogDir: logDir,
PactDir: pactDir,
LogLevel: "debug",
}
}
94 changes: 94 additions & 0 deletions pkg/kotsclient/release_test.go
@@ -0,0 +1,94 @@
package kotsclient

import (
"fmt"
"net/url"
"testing"

"github.com/pact-foundation/pact-go/dsl"
"github.com/replicatedhq/replicated/pkg/graphql"
"github.com/stretchr/testify/assert"
)

func Test_ListKotsReleasesActual(t *testing.T) {
var test = func() (err error) {
u := fmt.Sprintf("http://localhost:%d/graphql", pact.Server.Port)

uri, err := url.Parse(u)
assert.Nil(t, err)
d := &graphql.Client{
GQLServer: uri,
Token: "all-kots-releases-read-write-token",
}

c := &GraphQLClient{GraphQLClient: d}

releases, err := c.ListReleases("all-kots-releases")
assert.Nil(t, err)
assert.Len(t, releases, 2)

return nil
}

pact.AddInteraction().
Given("list releases for all-kots-releases").
UponReceiving("A real request to list releases for all-kots-releases").
WithRequest(dsl.Request{
Method: "POST",
Path: dsl.String("/graphql"),
Headers: dsl.MapMatcher{
"Authorization": dsl.String("all-kots-releases-read-write-token"),
"Content-Type": dsl.String("application/json"),
},
Body: map[string]interface{}{
"operationName": "",
"query": allKotsReleases,
"variables": map[string]interface{}{
"appId": "all-kots-releases",
},
},
}).
WillRespondWith(dsl.Response{
Status: 200,
Body: map[string]interface{}{
"data": map[string]interface{}{
"allKotsReleases": []map[string]interface{}{
{
"sequence": 2,
"created": dsl.Like(dsl.String("Tue Nov 10 2009 23:00:00 UTC")),
"channels": []map[string]interface{}{
{
"id": "all-kots-releases-beta",
"name": "Beta",
"currentVersion": "1.0.1",
"numReleases": 1,
},
{
"id": "all-kots-releases-nightly",
"name": "Nightly",
"currentVersion": "1.0.1",
"numReleases": 2,
},
},
},
{
"sequence": 1,
"created": dsl.Like(dsl.String("Tue Nov 10 2009 23:00:00 UTC")),
"channels": []map[string]interface{}{
{
"id": "all-kots-releases-test",
"name": "Test",
"currentVersion": "1.0.0",
"numReleases": 1,
},
},
},
},
},
},
})

if err := pact.Verify(test); err != nil {
t.Fatalf("Error on Verify: %v", err)
}
}

0 comments on commit 57dc97f

Please sign in to comment.