-
Notifications
You must be signed in to change notification settings - Fork 24
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 #71 from replicatedhq/laverya/add-vendor-api-pact-…
…tests add (basic) vendor-api pact tests
- Loading branch information
Showing
5 changed files
with
288 additions
and
20 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,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" | ||
} | ||
} | ||
} |
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,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) | ||
} | ||
} |
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 |
---|---|---|
@@ -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 | ||
} |
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 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) | ||
} | ||
} |