Skip to content

Commit 8da3ae8

Browse files
authored
Merge pull request #71 from replicatedhq/laverya/add-vendor-api-pact-tests
add (basic) vendor-api pact tests
2 parents 48134e7 + daccc22 commit 8da3ae8

File tree

5 files changed

+288
-20
lines changed

5 files changed

+288
-20
lines changed

Makefile

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ pacts:
5353
docker run --rm --name replicated-cli-tests \
5454
-v `pwd`:/go/src/github.com/replicatedhq/replicated \
5555
replicated-cli-test \
56-
go test -v ./pkg/shipclient/...
56+
go test -v ./pkg/...
57+
5758

5859
publish-pacts:
5960
curl \
@@ -63,6 +64,13 @@ publish-pacts:
6364
-H "Content-Type: application/json" \
6465
-d@pacts/replicated-cli-vendor-graphql-api.json \
6566
https://replicated-pact-broker.herokuapp.com/pacts/provider/vendor-graphql-api/consumer/replicated-cli/version/$(ABBREV_VERSION)
67+
curl \
68+
--silent --output /dev/null --show-error --fail \
69+
--user ${PACT_BROKER_USERNAME}:${PACT_BROKER_PASSWORD} \
70+
-X PUT \
71+
-H "Content-Type: application/json" \
72+
-d@pacts/replicated-cli-vendor-api.json \
73+
https://replicated-pact-broker.herokuapp.com/pacts/provider/vendor-api/consumer/replicated-cli/version/$(ABBREV_VERSION)
6674

6775
# fetch the swagger specs from the production Vendor API
6876
get-spec-prod:
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
{
2+
"consumer": {
3+
"name": "replicated-cli"
4+
},
5+
"provider": {
6+
"name": "vendor-api"
7+
},
8+
"interactions": [
9+
{
10+
"description": "A request to delete an app for cli-delete-app-id",
11+
"providerState": "Delete an app cli-delete-app-id",
12+
"request": {
13+
"method": "DELETE",
14+
"path": "/v1/app/cli-delete-app-id",
15+
"headers": {
16+
"Authorization": "cli-delete-app-auth"
17+
}
18+
},
19+
"response": {
20+
"status": 204,
21+
"headers": {
22+
},
23+
"body": ""
24+
}
25+
},
26+
{
27+
"description": "A request to create a new release for cli-create-release-app-id",
28+
"providerState": "Create a release for cli-create-release-app-id",
29+
"request": {
30+
"method": "POST",
31+
"path": "/v1/app/cli-create-release-app-id/release",
32+
"headers": {
33+
"Authorization": "cli-create-release-auth",
34+
"Content-Type": "application/json"
35+
},
36+
"body": {
37+
"source": "latest",
38+
"sourcedata": 0
39+
}
40+
},
41+
"response": {
42+
"status": 201,
43+
"headers": {
44+
},
45+
"body": {
46+
"Config": "",
47+
"CreatedAt": "2006-01-02T15:04:05Z",
48+
"Editable": true,
49+
"EditedAt": "2006-01-02T15:04:05Z",
50+
"Sequence": 10
51+
},
52+
"matchingRules": {
53+
"$.body.Config": {
54+
"match": "type"
55+
},
56+
"$.body.CreatedAt": {
57+
"match": "type"
58+
},
59+
"$.body.EditedAt": {
60+
"match": "type"
61+
},
62+
"$.body.Sequence": {
63+
"match": "type"
64+
}
65+
}
66+
}
67+
},
68+
{
69+
"description": "A request to get an existing release for cli-create-release-app-id",
70+
"providerState": "Get a release for cli-create-release-app-id",
71+
"request": {
72+
"method": "GET",
73+
"path": "/v1/app/cli-create-release-app-id/2/properties",
74+
"headers": {
75+
"Authorization": "cli-create-release-auth"
76+
}
77+
},
78+
"response": {
79+
"status": 200,
80+
"headers": {
81+
},
82+
"body": {
83+
"Config": "there might be a config here",
84+
"CreatedAt": "2006-01-02T15:04:05Z",
85+
"Editable": true,
86+
"EditedAt": "2006-01-02T15:04:05Z",
87+
"Sequence": 2
88+
},
89+
"matchingRules": {
90+
"$.body.Config": {
91+
"match": "type"
92+
},
93+
"$.body.CreatedAt": {
94+
"match": "type"
95+
},
96+
"$.body.EditedAt": {
97+
"match": "type"
98+
}
99+
}
100+
}
101+
}
102+
],
103+
"metadata": {
104+
"pactSpecification": {
105+
"version": "2.0.0"
106+
}
107+
}
108+
}

pkg/platformclient/app_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package platformclient
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/pact-foundation/pact-go/dsl"
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
// this functions as a "does vendor-api accept empty strings" test
12+
func Test_DeleteApp(t *testing.T) {
13+
var test = func() (err error) {
14+
appId := "cli-delete-app-id"
15+
token := "cli-delete-app-auth"
16+
17+
u := fmt.Sprintf("http://localhost:%d", pact.Server.Port)
18+
client := HTTPClient{
19+
apiKey: token,
20+
apiOrigin: u,
21+
}
22+
23+
err = client.DeleteApp(appId)
24+
assert.Nil(t, err)
25+
return nil
26+
}
27+
28+
pact.AddInteraction().
29+
Given("Delete an app cli-delete-app-id").
30+
UponReceiving("A request to delete an app for cli-delete-app-id").
31+
WithRequest(dsl.Request{
32+
Method: "DELETE",
33+
Path: dsl.String("/v1/app/cli-delete-app-id"),
34+
Headers: dsl.MapMatcher{
35+
"Authorization": dsl.String("cli-delete-app-auth"),
36+
},
37+
Body: nil,
38+
}).
39+
WillRespondWith(dsl.Response{
40+
Status: 204,
41+
Body: "",
42+
})
43+
44+
if err := pact.Verify(test); err != nil {
45+
t.Fatalf("Error on Verify: %v", err)
46+
}
47+
}

pkg/platformclient/client_test.go

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,41 @@
11
package platformclient
22

33
import (
4-
"fmt"
5-
"log"
64
"os"
5+
"path"
6+
"testing"
7+
8+
"github.com/pact-foundation/pact-go/dsl"
79
)
810

9-
func ExampleNew() {
10-
token := os.Getenv("REPLICATED_API_TOKEN")
11-
appSlugOrID := os.Getenv("REPLICATED_APP")
11+
var (
12+
pact dsl.Pact
13+
)
1214

13-
api := New(token)
15+
func TestMain(m *testing.M) {
16+
pact = createPact()
1417

15-
app, err := api.GetApp(appSlugOrID)
16-
if err != nil {
17-
log.Fatal(err)
18-
}
18+
pact.Setup(true)
1919

20-
channels, err := api.ListChannels(app.Id)
21-
if err != nil {
22-
log.Fatal(err)
23-
}
24-
for _, c := range channels {
25-
if c.Name == "Stable" {
26-
fmt.Println("We have a Stable channel")
27-
}
20+
code := m.Run()
21+
22+
pact.WritePact()
23+
pact.Teardown()
24+
25+
os.Exit(code)
26+
}
27+
28+
func createPact() dsl.Pact {
29+
dir, _ := os.Getwd()
30+
31+
pactDir := path.Join(dir, "..", "..", "pacts")
32+
logDir := path.Join(dir, "..", "..", "logs")
33+
34+
return dsl.Pact{
35+
Consumer: "replicated-cli",
36+
Provider: "vendor-api",
37+
LogDir: logDir,
38+
PactDir: pactDir,
39+
LogLevel: "debug",
2840
}
29-
// Output: We have a Stable channel
3041
}

pkg/platformclient/release_test.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package platformclient
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/pact-foundation/pact-go/dsl"
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
func Test_CreateRelease(t *testing.T) {
12+
var test = func() (err error) {
13+
appId := "cli-create-release-app-id"
14+
token := "cli-create-release-auth"
15+
16+
u := fmt.Sprintf("http://localhost:%d", pact.Server.Port)
17+
client := NewHTTPClient(u, token)
18+
19+
release, err := client.CreateRelease(appId, "")
20+
assert.Nil(t, err)
21+
assert.Equal(t, true, release.Editable)
22+
return nil
23+
}
24+
25+
pact.AddInteraction().
26+
Given("Create a release for cli-create-release-app-id").
27+
UponReceiving("A request to create a new release for cli-create-release-app-id").
28+
WithRequest(dsl.Request{
29+
Method: "POST",
30+
Path: dsl.String("/v1/app/cli-create-release-app-id/release"),
31+
Headers: dsl.MapMatcher{
32+
"Authorization": dsl.String("cli-create-release-auth"),
33+
"Content-Type": dsl.String("application/json"),
34+
},
35+
Body: map[string]interface{}{
36+
"source": "latest",
37+
"sourcedata": 0,
38+
},
39+
}).
40+
WillRespondWith(dsl.Response{
41+
Status: 201,
42+
Body: map[string]interface{}{
43+
"Sequence": dsl.Like(10),
44+
"Config": dsl.Like(""),
45+
"Editable": true,
46+
"CreatedAt": dsl.Like("2006-01-02T15:04:05Z"),
47+
"EditedAt": dsl.Like("2006-01-02T15:04:05Z"),
48+
},
49+
})
50+
51+
if err := pact.Verify(test); err != nil {
52+
t.Fatalf("Error on Verify: %v", err)
53+
}
54+
}
55+
56+
func Test_GetRelease(t *testing.T) {
57+
var test = func() (err error) {
58+
appId := "cli-create-release-app-id"
59+
token := "cli-create-release-auth"
60+
61+
u := fmt.Sprintf("http://localhost:%d", pact.Server.Port)
62+
client := NewHTTPClient(u, token)
63+
64+
release, err := client.GetRelease(appId, 2)
65+
assert.Nil(t, err)
66+
assert.Equal(t, int64(2), release.Sequence)
67+
return nil
68+
}
69+
70+
pact.AddInteraction().
71+
Given("Get a release for cli-create-release-app-id").
72+
UponReceiving("A request to get an existing release for cli-create-release-app-id").
73+
WithRequest(dsl.Request{
74+
Method: "GET",
75+
Path: dsl.String("/v1/app/cli-create-release-app-id/2/properties"),
76+
Headers: dsl.MapMatcher{
77+
"Authorization": dsl.String("cli-create-release-auth"),
78+
},
79+
}).
80+
WillRespondWith(dsl.Response{
81+
Status: 200,
82+
Body: map[string]interface{}{
83+
"Sequence": 2, // mandated by requesting sequence #2
84+
"Config": dsl.Like("there might be a config here"),
85+
"Editable": true,
86+
"CreatedAt": dsl.Like("2006-01-02T15:04:05Z"),
87+
"EditedAt": dsl.Like("2006-01-02T15:04:05Z"),
88+
},
89+
})
90+
91+
if err := pact.Verify(test); err != nil {
92+
t.Fatalf("Error on Verify: %v", err)
93+
}
94+
}

0 commit comments

Comments
 (0)