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

Commit d1864ef

Browse files
marccampbelllaverya
authored andcommittedJun 18, 2019
A pact test
1 parent bb7370b commit d1864ef

File tree

7 files changed

+185
-2
lines changed

7 files changed

+185
-2
lines changed
 

‎.circleci/config.yml

+20
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ jobs:
158158
name: make test
159159
command: |
160160
mkdir -p $GOCACHE
161+
161162
make build-deps citest
162163
if [ -n "$CC_TEST_REPORTER_ID" ]; then
163164
make ci-upload-coverage
@@ -167,6 +168,15 @@ jobs:
167168
- /tmp/go/cache
168169
key: ship-unit-test-build-cache-{{ epoch }}
169170

171+
pacts:
172+
docker:
173+
- image: circleci/golang:1.12
174+
working_directory: /go/src/github.com/replicatedhq/ship
175+
steps:
176+
- checkout
177+
- setup_remote_docker
178+
- run: make build-deps pacts
179+
170180
windows_build_test:
171181
docker:
172182
- image: golang:1.12
@@ -592,6 +602,7 @@ workflows:
592602
- e2e_setup
593603

594604
- test
605+
- pacts
595606
- windows_build_test
596607
- docs
597608
- integration_base
@@ -627,6 +638,7 @@ workflows:
627638
- e2e_setup
628639
- e2e_init
629640
- test
641+
- pacts
630642
- windows_build_test
631643
- integration_base
632644
- integration_init
@@ -695,6 +707,13 @@ workflows:
695707
branches:
696708
ignore: /.*/
697709

710+
- pacts:
711+
filters:
712+
tags:
713+
only: /^v[0-9]+(\.[0-9]+)*(-.*)*/
714+
branches:
715+
ignore: /.*/
716+
698717
- windows_build_test:
699718
filters:
700719
tags:
@@ -765,6 +784,7 @@ workflows:
765784
- e2e_setup
766785
- e2e_init
767786
- test
787+
- pacts
768788
- windows_build_test
769789
- integration_base
770790
- integration_init

‎.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,5 @@ cmd/ship/overlays
2525

2626
# local circle builds
2727
altconfig.yaml
28+
logs/
29+
pacts/

‎Makefile

+8
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@ githooks:
7373
echo 'make fmt; git add `git diff --name-only --cached`' > .git/hooks/pre-commit
7474
chmod +x .git/hooks/pre-commit
7575

76+
.PHONY: pacts
77+
pacts:
78+
docker build -t ship-contract-tests -f contracts/Dockerfile.testing .
79+
docker run --rm --name ship-contract-tests \
80+
-v `pwd`:/go/src/github.com/replicatedhq/ship \
81+
ship-contract-tests \
82+
pwd && ls && go test -v ./contracts/...
83+
7684
_mockgen:
7785
rm -rf pkg/test-mocks
7886
mkdir -p pkg/test-mocks/ui

‎contracts/Dockerfile.testing

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
FROM golang:1.12
2+
3+
RUN cd /opt && curl -fsSL https://raw.githubusercontent.com/pact-foundation/pact-ruby-standalone/v1.66.0/install.sh | bash
4+
ENV PATH="/opt/pact/bin:${PATH}"
5+
6+
WORKDIR /go/src/github.com/replicatedhq/ship
+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package replicatedapp
2+
3+
import (
4+
"encoding/base64"
5+
"fmt"
6+
"net/http"
7+
"testing"
8+
9+
"github.com/pact-foundation/pact-go/dsl"
10+
replapp "github.com/replicatedhq/ship/pkg/specs/replicatedapp"
11+
"github.com/spf13/viper"
12+
"github.com/stretchr/testify/require"
13+
)
14+
15+
func Test_GetReleaseFromSemver(t *testing.T) {
16+
customerID := "ship-fetch-release-customer-0"
17+
installationID := "ship-fetch-release-installation-0"
18+
semver := "1.0.2"
19+
spec := `assets:
20+
v1:
21+
- inline:
22+
contents: |
23+
#!/bin/bash
24+
echo "installing nothing"
25+
echo "config option: {{repl ConfigOption "test_option" }}"
26+
dest: ./scripts/install.sh
27+
mode: 0777
28+
- inline:
29+
contents: |
30+
#!/bin/bash
31+
echo "tested nothing"
32+
echo "customer {{repl Installation "customer_id" }}"
33+
echo "install {{repl Installation "installation_id" }}"
34+
dest: ./scripts/test.sh
35+
mode: 0777
36+
config:
37+
v1:
38+
- name: test_options
39+
title: Test Options
40+
description: testing testing 123
41+
items:
42+
- name: test_option
43+
title: Test Option
44+
default: abc123_test-option-value
45+
type: text
46+
47+
lifecycle:
48+
v1:
49+
- render: {}`
50+
var test = func() (err error) {
51+
req := require.New(t)
52+
53+
v := viper.New()
54+
v.Set("customer-endpoint", fmt.Sprintf("http://localhost:%d/graphql", pact.Server.Port))
55+
56+
gqlClient, err := replapp.NewGraphqlClient(v, http.DefaultClient)
57+
req.NoError(err)
58+
59+
selector := replapp.Selector{
60+
CustomerID: customerID,
61+
InstallationID: installationID,
62+
ReleaseSemver: semver,
63+
}
64+
65+
_, err = gqlClient.GetRelease(&selector)
66+
req.NoError(err)
67+
68+
return nil
69+
}
70+
71+
pact.AddInteraction().
72+
Given("A request to get a single app release").
73+
UponReceiving("A request to get the amn app release from a semver and customer id").
74+
WithRequest(dsl.Request{
75+
Method: "POST",
76+
Path: dsl.String("/graphql"),
77+
Headers: dsl.MapMatcher{
78+
"Authorization": dsl.String(fmt.Sprintf("Basic %s", base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", customerID, installationID))))),
79+
"Content-Type": dsl.String("application/json"),
80+
},
81+
Body: map[string]interface{}{
82+
"operationName": "",
83+
"query": replapp.GetAppspecQuery,
84+
"variables": map[string]interface{}{
85+
"semver": semver,
86+
},
87+
},
88+
}).
89+
WillRespondWith(dsl.Response{
90+
Status: 200,
91+
Body: map[string]interface{}{
92+
"data": map[string]interface{}{
93+
"shipRelease": map[string]interface{}{
94+
"id": dsl.Like(dsl.String("generated")),
95+
"spec": dsl.Like(dsl.String(spec)),
96+
},
97+
},
98+
},
99+
})
100+
101+
if err := pact.Verify(test); err != nil {
102+
t.Fatalf("Error on Verify: %v", err)
103+
}
104+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package replicatedapp
2+
3+
import (
4+
"os"
5+
"path"
6+
"testing"
7+
8+
"github.com/pact-foundation/pact-go/dsl"
9+
)
10+
11+
var (
12+
pact dsl.Pact
13+
)
14+
15+
func TestMain(m *testing.M) {
16+
pact = createPact()
17+
18+
pact.Setup(true)
19+
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: "ship",
36+
Provider: "prem-graphql-api",
37+
LogDir: logDir,
38+
PactDir: pactDir,
39+
LogLevel: "debug",
40+
Host: "0.0.0.0",
41+
}
42+
}

‎pkg/specs/replicatedapp/graphql.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
"github.com/spf13/viper"
1818
)
1919

20-
const getAppspecQuery = `
20+
const GetAppspecQuery = `
2121
query($semver: String) {
2222
shipRelease (semver: $semver) {
2323
id
@@ -248,7 +248,7 @@ func NewGraphqlClient(v *viper.Viper, client *http.Client) (*GraphQLClient, erro
248248
// GetRelease gets a payload from the graphql server
249249
func (c *GraphQLClient) GetRelease(selector *Selector) (*state.ShipRelease, error) {
250250
requestObj := GraphQLRequest{
251-
Query: getAppspecQuery,
251+
Query: GetAppspecQuery,
252252
Variables: map[string]string{
253253
"semver": selector.ReleaseSemver,
254254
},
@@ -396,6 +396,7 @@ func (c *GraphQLClient) callGQL(ci callInfo, result interface{}) error {
396396
if ci.upstream != "" {
397397
gqlServer = ci.upstream
398398
}
399+
399400
graphQLRequest, err := http.NewRequest(http.MethodPost, gqlServer, bodyReader)
400401
if err != nil {
401402
return errors.Wrap(err, "create new request")

0 commit comments

Comments
 (0)
This repository has been archived.