Skip to content

Commit

Permalink
feat(windows): add Windows PoSH build script
Browse files Browse the repository at this point in the history
- Runs examples as integration tests
- Closes pact-foundation#28
- Closes pact-foundation#42
  • Loading branch information
mefellows committed Aug 14, 2017
1 parent 2201fe3 commit 79bcada
Show file tree
Hide file tree
Showing 9 changed files with 505 additions and 429 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ DSL for the consumer project, and interaction playback and verification for the
[![Coverage Status](https://coveralls.io/repos/github/pact-foundation/pact-go/badge.svg?branch=HEAD)](https://coveralls.io/github/pact-foundation/pact-go?branch=HEAD)
[![Go Report Card](https://goreportcard.com/badge/github.com/pact-foundation/pact-go)](https://goreportcard.com/report/github.com/pact-foundation/pact-go)
[![GoDoc](https://godoc.org/github.com/pact-foundation/pact-go?status.svg)](https://godoc.org/github.com/pact-foundation/pact-go)
[![Build status](https://ci.appveyor.com/api/projects/status/lg02mfcmvr3e8w5n?svg=true)](https://ci.appveyor.com/project/mefellows/pact-go)

## Introduction

Expand Down
83 changes: 42 additions & 41 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,41 +1,42 @@
version: "{build}"

# fix lineendings in Windows
# init:
# - git config --global core.autocrlf input

# Source Config
clone_folder: c:\gopath\src\github.com\pact-foundation\pact-go

# Build host

environment:
GOPATH: c:\gopath
GOVERSION: 1.8
# PACT_INTEGRATED_TESTS: 0
PACT_BROKER_HOST: "https://test.pact.dius.com.au"
PACT_BROKER_USERNAME: "dXfltyFMgNOFZAxr8io9wJ37iUpY42M"
PACT_BROKER_PASSWORD: "O5AIZWxelWbLvqMd8PkAVycBJh2Psyg1"

init:
- git config --global core.autocrlf input

# Build

install:
# Install the specific Go version.
- rmdir c:\go /s /q
- appveyor DownloadFile https://storage.googleapis.com/golang/go%GOVERSION%.windows-amd64.msi
# Download the latest pact standalone
- msiexec /i go%GOVERSION%.windows-amd64.msi /q
- choco install bzr
- set Path=c:\go\bin;c:\gopath\bin;C:\Program Files (x86)\Bazaar\;C:\Program Files\Mercurial\%Path%
- go version
- go env

build: false
deploy: false

test_script:
- go build -o /tmp/pact-go github.com/pact-foundation/pact-go
- for /f "" %%G in ('go list github.com/pact-foundation/pact-go/... ^| find /i /v "/vendor/" ^| find /i /v "examples"') do ( go test -v %%G & IF ERRORLEVEL == 1 EXIT 1)
version: "{build}"

# fix lineendings in Windows
# init:
# - git config --global core.autocrlf input

# Source Config
clone_folder: c:\gopath\src\github.com\pact-foundation\pact-go

# Build host


environment:
GOPATH: c:\gopath
GOVERSION: 1.8
PACT_INTEGRATED_TESTS: 1
PACT_BROKER_HOST: "https://test.pact.dius.com.au"
PACT_BROKER_USERNAME: "dXfltyFMgNOFZAxr8io9wJ37iUpY42M"
PACT_BROKER_PASSWORD: "O5AIZWxelWbLvqMd8PkAVycBJh2Psyg1"

init:
- git config --global core.autocrlf input

# Build

install:
# Install the specific Go version.
- rmdir c:\go /s /q
- appveyor DownloadFile https://storage.googleapis.com/golang/go%GOVERSION%.windows-amd64.msi
# Download the latest pact standalone
- msiexec /i go%GOVERSION%.windows-amd64.msi /q
- choco install bzr
- set Path=c:\go\bin;c:\gopath\bin;C:\Program Files (x86)\Bazaar\;C:\Program Files\Mercurial\%Path%
- go version
- go env

build: false
deploy: false

test_script:
- go build -o /tmp/pact-go github.com/pact-foundation/pact-go
- c:\gopath\src\github.com\pact-foundation\pact-go\scripts\pact.ps1
195 changes: 98 additions & 97 deletions examples/gin/provider/user_service_test.go
Original file line number Diff line number Diff line change
@@ -1,97 +1,98 @@
package provider

import (
"fmt"
"os"
"testing"

"github.com/gin-gonic/gin"
"github.com/pact-foundation/pact-go/dsl"
examples "github.com/pact-foundation/pact-go/examples/types"
"github.com/pact-foundation/pact-go/types"
"github.com/pact-foundation/pact-go/utils"
)

// The actual Provider test itself
func TestPact_Provider(t *testing.T) {
go startInstrumentedProvider()

pact := createPact()

// Verify the Provider with local Pact Files
err := pact.VerifyProvider(types.VerifyRequest{
ProviderBaseURL: fmt.Sprintf("http://localhost:%d", port),
PactURLs: []string{fmt.Sprintf("%s/billy-bobby.json", pactDir)},
ProviderStatesSetupURL: fmt.Sprintf("http://localhost:%d/setup", port),
})

if err != nil {
t.Fatal("Error:", err)
}
}

// Starts the provider API with hooks for provider states.
// This essentially mirrors the main.go file, with extra routes added.
func startInstrumentedProvider() {
router := gin.Default()
router.POST("/users/login", UserLogin)
router.POST("/setup", providerStateSetup)

router.Run(fmt.Sprintf(":%d", port))
}

// Set current provider state route.
func providerStateSetup(c *gin.Context) {
var state types.ProviderState
if c.BindJSON(&state) == nil {
// Setup database for different states
if state.State == "User billy exists" {
userRepository = billyExists
} else if state.State == "User billy is unauthorized" {
userRepository = billyUnauthorized
} else {
userRepository = billyDoesNotExist
}
}
}

// Configuration / Test Data
var dir, _ = os.Getwd()
var pactDir = fmt.Sprintf("%s/../../pacts", dir)
var logDir = fmt.Sprintf("%s/log", dir)
var port, _ = utils.GetFreePort()

// Provider States data sets
var billyExists = &examples.UserRepository{
Users: map[string]*examples.User{
"billy": &examples.User{
Name: "billy",
Username: "billy",
Password: "issilly",
},
},
}

var billyDoesNotExist = &examples.UserRepository{}

var billyUnauthorized = &examples.UserRepository{
Users: map[string]*examples.User{
"billy": &examples.User{
Name: "billy",
Username: "billy",
Password: "issilly1",
},
},
}

// Setup the Pact client.
func createPact() dsl.Pact {
// Create Pact connecting to local Daemon
return dsl.Pact{
Port: 6666,
Consumer: "billy",
Provider: "bobby",
LogDir: logDir,
PactDir: pactDir,
}
}
package provider

import (
"fmt"
"os"
"path/filepath"
"testing"

"github.com/gin-gonic/gin"
"github.com/pact-foundation/pact-go/dsl"
examples "github.com/pact-foundation/pact-go/examples/types"
"github.com/pact-foundation/pact-go/types"
"github.com/pact-foundation/pact-go/utils"
)

// The actual Provider test itself
func TestPact_Provider(t *testing.T) {
go startInstrumentedProvider()

pact := createPact()

// Verify the Provider with local Pact Files
err := pact.VerifyProvider(types.VerifyRequest{
ProviderBaseURL: fmt.Sprintf("http://localhost:%d", port),
PactURLs: []string{filepath.ToSlash(fmt.Sprintf("%s/billy-bobby.json", pactDir))},
ProviderStatesSetupURL: fmt.Sprintf("http://localhost:%d/setup", port),
})

if err != nil {
t.Fatal("Error:", err)
}
}

// Starts the provider API with hooks for provider states.
// This essentially mirrors the main.go file, with extra routes added.
func startInstrumentedProvider() {
router := gin.Default()
router.POST("/users/login", UserLogin)
router.POST("/setup", providerStateSetup)

router.Run(fmt.Sprintf(":%d", port))
}

// Set current provider state route.
func providerStateSetup(c *gin.Context) {
var state types.ProviderState
if c.BindJSON(&state) == nil {
// Setup database for different states
if state.State == "User billy exists" {
userRepository = billyExists
} else if state.State == "User billy is unauthorized" {
userRepository = billyUnauthorized
} else {
userRepository = billyDoesNotExist
}
}
}

// Configuration / Test Data
var dir, _ = os.Getwd()
var pactDir = fmt.Sprintf("%s/../../pacts", dir)
var logDir = fmt.Sprintf("%s/log", dir)
var port, _ = utils.GetFreePort()

// Provider States data sets
var billyExists = &examples.UserRepository{
Users: map[string]*examples.User{
"billy": &examples.User{
Name: "billy",
Username: "billy",
Password: "issilly",
},
},
}

var billyDoesNotExist = &examples.UserRepository{}

var billyUnauthorized = &examples.UserRepository{
Users: map[string]*examples.User{
"billy": &examples.User{
Name: "billy",
Username: "billy",
Password: "issilly1",
},
},
}

// Setup the Pact client.
func createPact() dsl.Pact {
// Create Pact connecting to local Daemon
return dsl.Pact{
Port: 6666,
Consumer: "billy",
Provider: "bobby",
LogDir: logDir,
PactDir: pactDir,
}
}
7 changes: 3 additions & 4 deletions examples/go-kit/provider/cmd/usersvc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

"github.com/go-kit/kit/log"

"golang.org/x/net/context"
"context"

usersvc "github.com/pact-foundation/pact-go/examples/go-kit/provider"
)
Expand All @@ -24,8 +24,7 @@ func main() {
var logger log.Logger
{
logger = log.NewLogfmtLogger(os.Stderr)
logger = log.NewContext(logger).With("ts", log.DefaultTimestampUTC)
logger = log.NewContext(logger).With("caller", log.DefaultCaller)
logger = log.With(logger, "ts", log.DefaultTimestampUTC, "caller", log.DefaultCaller)
}

var ctx context.Context
Expand All @@ -41,7 +40,7 @@ func main() {

var h http.Handler
{
h = usersvc.MakeHTTPHandler(ctx, s, log.NewContext(logger).With("component", "HTTP"))
h = usersvc.MakeHTTPHandler(ctx, s, log.With(logger, "component", "HTTP"))
}

errs := make(chan error)
Expand Down
3 changes: 2 additions & 1 deletion examples/go-kit/provider/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import (
"encoding/json"
"net/http"

"context"

"github.com/go-kit/kit/endpoint"
"golang.org/x/net/context"
)

// Endpoints is a container to hold all of our Transport routes (HTTP, RPC etc.).
Expand Down
41 changes: 23 additions & 18 deletions examples/go-kit/provider/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import (
"encoding/json"
"net/http"

"context"

"github.com/go-kit/kit/log"
httptransport "github.com/go-kit/kit/transport/http"
"github.com/gorilla/mux"
"golang.org/x/net/context"
)

// Request // Response types
Expand All @@ -31,7 +32,6 @@ func MakeHTTPHandler(ctx context.Context, s Service, logger log.Logger) http.Han
e := MakeServerEndpoints(s)

r.Methods("POST").Path("/users/login").Handler(httptransport.NewServer(
ctx,
e.LoginEndpoint,
decodeUserRequest,
encodeResponse,
Expand Down Expand Up @@ -63,21 +63,26 @@ func encodeError(_ context.Context, err error, w http.ResponseWriter) {
}

func codeFrom(err error) int {
if e, ok := err.(httptransport.Error); ok {
switch e.Err {
case ErrNotFound:
return http.StatusNotFound
case ErrUnauthorized:
return http.StatusUnauthorized
}
switch e.Domain {
case httptransport.DomainDecode:
return http.StatusBadRequest
case httptransport.DomainDo:
return http.StatusServiceUnavailable
default:
return http.StatusInternalServerError
}
switch err {
case ErrNotFound:
return http.StatusNotFound
case ErrNotFound:
return http.StatusNotFound
case ErrUnauthorized:
return http.StatusUnauthorized
default:
return http.StatusInternalServerError
}
return http.StatusInternalServerError
// if e, ok := err.(httptransport.Error); ok {
// switch e.Err {domaind
// switch e.Domain {
// case httptransport.DomainDecode:
// return http.StatusBadRequest
// case httptransport.DomainDo:
// return http.StatusServiceUnavailable
// default:
// return http.StatusInternalServerError
// }
// }
// return http.StatusInternalServerError
}
Loading

0 comments on commit 79bcada

Please sign in to comment.