-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #80 from replicatedhq/laverya/kots-pacts
add a basic pact test for the kots client
- Loading branch information
Showing
4 changed files
with
226 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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", | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} |