Skip to content
This repository has been archived by the owner on Mar 24, 2023. It is now read-only.

Commit

Permalink
A pact test
Browse files Browse the repository at this point in the history
  • Loading branch information
marccampbell authored and laverya committed Jun 18, 2019
1 parent bb7370b commit d1864ef
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 2 deletions.
20 changes: 20 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ jobs:
name: make test
command: |
mkdir -p $GOCACHE
make build-deps citest
if [ -n "$CC_TEST_REPORTER_ID" ]; then
make ci-upload-coverage
Expand All @@ -167,6 +168,15 @@ jobs:
- /tmp/go/cache
key: ship-unit-test-build-cache-{{ epoch }}

pacts:
docker:
- image: circleci/golang:1.12
working_directory: /go/src/github.com/replicatedhq/ship
steps:
- checkout
- setup_remote_docker
- run: make build-deps pacts

windows_build_test:
docker:
- image: golang:1.12
Expand Down Expand Up @@ -592,6 +602,7 @@ workflows:
- e2e_setup

- test
- pacts
- windows_build_test
- docs
- integration_base
Expand Down Expand Up @@ -627,6 +638,7 @@ workflows:
- e2e_setup
- e2e_init
- test
- pacts
- windows_build_test
- integration_base
- integration_init
Expand Down Expand Up @@ -695,6 +707,13 @@ workflows:
branches:
ignore: /.*/

- pacts:
filters:
tags:
only: /^v[0-9]+(\.[0-9]+)*(-.*)*/
branches:
ignore: /.*/

- windows_build_test:
filters:
tags:
Expand Down Expand Up @@ -765,6 +784,7 @@ workflows:
- e2e_setup
- e2e_init
- test
- pacts
- windows_build_test
- integration_base
- integration_init
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,5 @@ cmd/ship/overlays

# local circle builds
altconfig.yaml
logs/
pacts/
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ githooks:
echo 'make fmt; git add `git diff --name-only --cached`' > .git/hooks/pre-commit
chmod +x .git/hooks/pre-commit

.PHONY: pacts
pacts:
docker build -t ship-contract-tests -f contracts/Dockerfile.testing .
docker run --rm --name ship-contract-tests \
-v `pwd`:/go/src/github.com/replicatedhq/ship \
ship-contract-tests \
pwd && ls && go test -v ./contracts/...

_mockgen:
rm -rf pkg/test-mocks
mkdir -p pkg/test-mocks/ui
Expand Down
6 changes: 6 additions & 0 deletions contracts/Dockerfile.testing
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM golang:1.12

RUN cd /opt && curl -fsSL https://raw.githubusercontent.com/pact-foundation/pact-ruby-standalone/v1.66.0/install.sh | bash
ENV PATH="/opt/pact/bin:${PATH}"

WORKDIR /go/src/github.com/replicatedhq/ship
104 changes: 104 additions & 0 deletions contracts/replicatedapp/graphql_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package replicatedapp

import (
"encoding/base64"
"fmt"
"net/http"
"testing"

"github.com/pact-foundation/pact-go/dsl"
replapp "github.com/replicatedhq/ship/pkg/specs/replicatedapp"
"github.com/spf13/viper"
"github.com/stretchr/testify/require"
)

func Test_GetReleaseFromSemver(t *testing.T) {
customerID := "ship-fetch-release-customer-0"
installationID := "ship-fetch-release-installation-0"
semver := "1.0.2"
spec := `assets:
v1:
- inline:
contents: |
#!/bin/bash
echo "installing nothing"
echo "config option: {{repl ConfigOption "test_option" }}"
dest: ./scripts/install.sh
mode: 0777
- inline:
contents: |
#!/bin/bash
echo "tested nothing"
echo "customer {{repl Installation "customer_id" }}"
echo "install {{repl Installation "installation_id" }}"
dest: ./scripts/test.sh
mode: 0777
config:
v1:
- name: test_options
title: Test Options
description: testing testing 123
items:
- name: test_option
title: Test Option
default: abc123_test-option-value
type: text
lifecycle:
v1:
- render: {}`
var test = func() (err error) {
req := require.New(t)

v := viper.New()
v.Set("customer-endpoint", fmt.Sprintf("http://localhost:%d/graphql", pact.Server.Port))

gqlClient, err := replapp.NewGraphqlClient(v, http.DefaultClient)
req.NoError(err)

selector := replapp.Selector{
CustomerID: customerID,
InstallationID: installationID,
ReleaseSemver: semver,
}

_, err = gqlClient.GetRelease(&selector)
req.NoError(err)

return nil
}

pact.AddInteraction().
Given("A request to get a single app release").
UponReceiving("A request to get the amn app release from a semver and customer id").
WithRequest(dsl.Request{
Method: "POST",
Path: dsl.String("/graphql"),
Headers: dsl.MapMatcher{
"Authorization": dsl.String(fmt.Sprintf("Basic %s", base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", customerID, installationID))))),
"Content-Type": dsl.String("application/json"),
},
Body: map[string]interface{}{
"operationName": "",
"query": replapp.GetAppspecQuery,
"variables": map[string]interface{}{
"semver": semver,
},
},
}).
WillRespondWith(dsl.Response{
Status: 200,
Body: map[string]interface{}{
"data": map[string]interface{}{
"shipRelease": map[string]interface{}{
"id": dsl.Like(dsl.String("generated")),
"spec": dsl.Like(dsl.String(spec)),
},
},
},
})

if err := pact.Verify(test); err != nil {
t.Fatalf("Error on Verify: %v", err)
}
}
42 changes: 42 additions & 0 deletions contracts/replicatedapp/prem_graphql_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package replicatedapp

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: "ship",
Provider: "prem-graphql-api",
LogDir: logDir,
PactDir: pactDir,
LogLevel: "debug",
Host: "0.0.0.0",
}
}
5 changes: 3 additions & 2 deletions pkg/specs/replicatedapp/graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"github.com/spf13/viper"
)

const getAppspecQuery = `
const GetAppspecQuery = `
query($semver: String) {
shipRelease (semver: $semver) {
id
Expand Down Expand Up @@ -248,7 +248,7 @@ func NewGraphqlClient(v *viper.Viper, client *http.Client) (*GraphQLClient, erro
// GetRelease gets a payload from the graphql server
func (c *GraphQLClient) GetRelease(selector *Selector) (*state.ShipRelease, error) {
requestObj := GraphQLRequest{
Query: getAppspecQuery,
Query: GetAppspecQuery,
Variables: map[string]string{
"semver": selector.ReleaseSemver,
},
Expand Down Expand Up @@ -396,6 +396,7 @@ func (c *GraphQLClient) callGQL(ci callInfo, result interface{}) error {
if ci.upstream != "" {
gqlServer = ci.upstream
}

graphQLRequest, err := http.NewRequest(http.MethodPost, gqlServer, bodyReader)
if err != nil {
return errors.Wrap(err, "create new request")
Expand Down

0 comments on commit d1864ef

Please sign in to comment.