Skip to content

Commit

Permalink
Merge pull request #71 from replicatedhq/laverya/add-vendor-api-pact-…
Browse files Browse the repository at this point in the history
…tests

add (basic) vendor-api pact tests
  • Loading branch information
laverya authored Nov 2, 2019
2 parents 48134e7 + daccc22 commit 8da3ae8
Show file tree
Hide file tree
Showing 5 changed files with 288 additions and 20 deletions.
10 changes: 9 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ pacts:
docker run --rm --name replicated-cli-tests \
-v `pwd`:/go/src/github.com/replicatedhq/replicated \
replicated-cli-test \
go test -v ./pkg/shipclient/...
go test -v ./pkg/...


publish-pacts:
curl \
Expand All @@ -63,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-vendor-api.json \
https://replicated-pact-broker.herokuapp.com/pacts/provider/vendor-api/consumer/replicated-cli/version/$(ABBREV_VERSION)

# fetch the swagger specs from the production Vendor API
get-spec-prod:
Expand Down
108 changes: 108 additions & 0 deletions pacts/replicated-cli-vendor-api.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
{
"consumer": {
"name": "replicated-cli"
},
"provider": {
"name": "vendor-api"
},
"interactions": [
{
"description": "A request to delete an app for cli-delete-app-id",
"providerState": "Delete an app cli-delete-app-id",
"request": {
"method": "DELETE",
"path": "/v1/app/cli-delete-app-id",
"headers": {
"Authorization": "cli-delete-app-auth"
}
},
"response": {
"status": 204,
"headers": {
},
"body": ""
}
},
{
"description": "A request to create a new release for cli-create-release-app-id",
"providerState": "Create a release for cli-create-release-app-id",
"request": {
"method": "POST",
"path": "/v1/app/cli-create-release-app-id/release",
"headers": {
"Authorization": "cli-create-release-auth",
"Content-Type": "application/json"
},
"body": {
"source": "latest",
"sourcedata": 0
}
},
"response": {
"status": 201,
"headers": {
},
"body": {
"Config": "",
"CreatedAt": "2006-01-02T15:04:05Z",
"Editable": true,
"EditedAt": "2006-01-02T15:04:05Z",
"Sequence": 10
},
"matchingRules": {
"$.body.Config": {
"match": "type"
},
"$.body.CreatedAt": {
"match": "type"
},
"$.body.EditedAt": {
"match": "type"
},
"$.body.Sequence": {
"match": "type"
}
}
}
},
{
"description": "A request to get an existing release for cli-create-release-app-id",
"providerState": "Get a release for cli-create-release-app-id",
"request": {
"method": "GET",
"path": "/v1/app/cli-create-release-app-id/2/properties",
"headers": {
"Authorization": "cli-create-release-auth"
}
},
"response": {
"status": 200,
"headers": {
},
"body": {
"Config": "there might be a config here",
"CreatedAt": "2006-01-02T15:04:05Z",
"Editable": true,
"EditedAt": "2006-01-02T15:04:05Z",
"Sequence": 2
},
"matchingRules": {
"$.body.Config": {
"match": "type"
},
"$.body.CreatedAt": {
"match": "type"
},
"$.body.EditedAt": {
"match": "type"
}
}
}
}
],
"metadata": {
"pactSpecification": {
"version": "2.0.0"
}
}
}
47 changes: 47 additions & 0 deletions pkg/platformclient/app_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package platformclient

import (
"fmt"
"testing"

"github.com/pact-foundation/pact-go/dsl"
"github.com/stretchr/testify/assert"
)

// this functions as a "does vendor-api accept empty strings" test
func Test_DeleteApp(t *testing.T) {
var test = func() (err error) {
appId := "cli-delete-app-id"
token := "cli-delete-app-auth"

u := fmt.Sprintf("http://localhost:%d", pact.Server.Port)
client := HTTPClient{
apiKey: token,
apiOrigin: u,
}

err = client.DeleteApp(appId)
assert.Nil(t, err)
return nil
}

pact.AddInteraction().
Given("Delete an app cli-delete-app-id").
UponReceiving("A request to delete an app for cli-delete-app-id").
WithRequest(dsl.Request{
Method: "DELETE",
Path: dsl.String("/v1/app/cli-delete-app-id"),
Headers: dsl.MapMatcher{
"Authorization": dsl.String("cli-delete-app-auth"),
},
Body: nil,
}).
WillRespondWith(dsl.Response{
Status: 204,
Body: "",
})

if err := pact.Verify(test); err != nil {
t.Fatalf("Error on Verify: %v", err)
}
}
49 changes: 30 additions & 19 deletions pkg/platformclient/client_test.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,41 @@
package platformclient

import (
"fmt"
"log"
"os"
"path"
"testing"

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

func ExampleNew() {
token := os.Getenv("REPLICATED_API_TOKEN")
appSlugOrID := os.Getenv("REPLICATED_APP")
var (
pact dsl.Pact
)

api := New(token)
func TestMain(m *testing.M) {
pact = createPact()

app, err := api.GetApp(appSlugOrID)
if err != nil {
log.Fatal(err)
}
pact.Setup(true)

channels, err := api.ListChannels(app.Id)
if err != nil {
log.Fatal(err)
}
for _, c := range channels {
if c.Name == "Stable" {
fmt.Println("We have a Stable channel")
}
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",
Provider: "vendor-api",
LogDir: logDir,
PactDir: pactDir,
LogLevel: "debug",
}
// Output: We have a Stable channel
}
94 changes: 94 additions & 0 deletions pkg/platformclient/release_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package platformclient

import (
"fmt"
"testing"

"github.com/pact-foundation/pact-go/dsl"
"github.com/stretchr/testify/assert"
)

func Test_CreateRelease(t *testing.T) {
var test = func() (err error) {
appId := "cli-create-release-app-id"
token := "cli-create-release-auth"

u := fmt.Sprintf("http://localhost:%d", pact.Server.Port)
client := NewHTTPClient(u, token)

release, err := client.CreateRelease(appId, "")
assert.Nil(t, err)
assert.Equal(t, true, release.Editable)
return nil
}

pact.AddInteraction().
Given("Create a release for cli-create-release-app-id").
UponReceiving("A request to create a new release for cli-create-release-app-id").
WithRequest(dsl.Request{
Method: "POST",
Path: dsl.String("/v1/app/cli-create-release-app-id/release"),
Headers: dsl.MapMatcher{
"Authorization": dsl.String("cli-create-release-auth"),
"Content-Type": dsl.String("application/json"),
},
Body: map[string]interface{}{
"source": "latest",
"sourcedata": 0,
},
}).
WillRespondWith(dsl.Response{
Status: 201,
Body: map[string]interface{}{
"Sequence": dsl.Like(10),
"Config": dsl.Like(""),
"Editable": true,
"CreatedAt": dsl.Like("2006-01-02T15:04:05Z"),
"EditedAt": dsl.Like("2006-01-02T15:04:05Z"),
},
})

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

func Test_GetRelease(t *testing.T) {
var test = func() (err error) {
appId := "cli-create-release-app-id"
token := "cli-create-release-auth"

u := fmt.Sprintf("http://localhost:%d", pact.Server.Port)
client := NewHTTPClient(u, token)

release, err := client.GetRelease(appId, 2)
assert.Nil(t, err)
assert.Equal(t, int64(2), release.Sequence)
return nil
}

pact.AddInteraction().
Given("Get a release for cli-create-release-app-id").
UponReceiving("A request to get an existing release for cli-create-release-app-id").
WithRequest(dsl.Request{
Method: "GET",
Path: dsl.String("/v1/app/cli-create-release-app-id/2/properties"),
Headers: dsl.MapMatcher{
"Authorization": dsl.String("cli-create-release-auth"),
},
}).
WillRespondWith(dsl.Response{
Status: 200,
Body: map[string]interface{}{
"Sequence": 2, // mandated by requesting sequence #2
"Config": dsl.Like("there might be a config here"),
"Editable": true,
"CreatedAt": dsl.Like("2006-01-02T15:04:05Z"),
"EditedAt": dsl.Like("2006-01-02T15:04:05Z"),
},
})

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

0 comments on commit 8da3ae8

Please sign in to comment.