Skip to content

Commit

Permalink
feat(writepact): allow client to control when pact file is written
Browse files Browse the repository at this point in the history
Create new WritePact() method in dsl package, remove 'Pact' prefix from structs, remove 'pact_'
prefix from files and update E2E tests.
  • Loading branch information
mefellows committed Jun 11, 2016
1 parent 879a3b4 commit ea2c807
Show file tree
Hide file tree
Showing 19 changed files with 250 additions and 96 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@ func TestLogin(t *testing.T) {
if err != nil {
t.Fatalf("Error on Verify: %v", err)
}
// You should now have a pact file in the file `<pact-go>/pacts/my_consumer-my_provider.json`

// Write pact to file `<pact-go>/pacts/my_consumer-my_provider.json`
pact.WritePact()
}
```

Expand Down
2 changes: 1 addition & 1 deletion command/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var daemonCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
setLogLevel(verbose, logLevel)

mock := &daemon.PactMockService{}
mock := &daemon.MockService{}
mock.Setup()
verifier := &daemon.VerificationService{}
verifier.Setup()
Expand Down
6 changes: 3 additions & 3 deletions command/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ var RootCmd = &cobra.Command{
Use: "pact-go",
Short: "Pact Go makes it easier to work with Pact with Golang projects",
Long: `Pact Go is a utility that wraps a number of external applications into
an idiomatic Golang interface and CLI, providing a mock service and DSL for
the consumer project, and interaction playback and verification for the
service provider project.`,
an idiomatic Golang interface and CLI, providing a mock service and DSL for
the consumer project, and interaction playback and verification for the
service provider project.`,
}

// Execute adds all child commands to the root command sets flags appropriately.
Expand Down
22 changes: 11 additions & 11 deletions daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ type Daemon struct {
}

// NewDaemon returns a new Daemon with all instance variables initialised.
func NewDaemon(pactMockServiceManager Service, verificationServiceManager Service) *Daemon {
pactMockServiceManager.Setup()
func NewDaemon(MockServiceManager Service, verificationServiceManager Service) *Daemon {
MockServiceManager.Setup()
verificationServiceManager.Setup()

return &Daemon{
pactMockSvcManager: pactMockServiceManager,
pactMockSvcManager: MockServiceManager,
verificationSvcManager: verificationServiceManager,
signalChan: make(chan os.Signal, 1),
}
Expand Down Expand Up @@ -94,11 +94,11 @@ func (d Daemon) Shutdown() {
}
}

// StartServer starts a mock server and returns a pointer to atypes.PactMockServer
// StartServer starts a mock server and returns a pointer to atypes.MockServer
// struct.
func (d Daemon) StartServer(request types.PactMockServer, reply *types.PactMockServer) error {
func (d Daemon) StartServer(request types.MockServer, reply *types.MockServer) error {
log.Println("[DEBUG] daemon - starting mock server")
server := &types.PactMockServer{}
server := &types.MockServer{}
port, svc := d.pactMockSvcManager.NewService(request.Args)
server.Port = port
server.Status = -1
Expand Down Expand Up @@ -139,13 +139,13 @@ func (d Daemon) VerifyProvider(request types.VerifyRequest, reply *types.Command
return nil
}

// ListServers returns a slice of all running types.PactMockServers.
func (d Daemon) ListServers(request types.PactMockServer, reply *types.PactListResponse) error {
// ListServers returns a slice of all running types.MockServers.
func (d Daemon) ListServers(request types.MockServer, reply *types.PactListResponse) error {
log.Println("[DEBUG] daemon - listing mock servers")
var servers []*types.PactMockServer
var servers []*types.MockServer

for port, s := range d.pactMockSvcManager.List() {
servers = append(servers, &types.PactMockServer{
servers = append(servers, &types.MockServer{
Pid: s.Process.Pid,
Port: port,
})
Expand All @@ -159,7 +159,7 @@ func (d Daemon) ListServers(request types.PactMockServer, reply *types.PactListR
}

// StopServer stops the given mock server.
func (d Daemon) StopServer(request types.PactMockServer, reply *types.PactMockServer) error {
func (d Daemon) StopServer(request types.MockServer, reply *types.MockServer) error {
log.Println("[DEBUG] daemon - stopping mock server")
success, err := d.pactMockSvcManager.Stop(request.Pid)
if success == true && err == nil {
Expand Down
28 changes: 14 additions & 14 deletions daemon/daemon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ func TestDaemonShutdown(t *testing.T) {
func TestStartServer(t *testing.T) {
daemon, _ := createMockedDaemon(true)

req := types.PactMockServer{Pid: 1234}
res := types.PactMockServer{}
req := types.MockServer{Pid: 1234}
res := types.MockServer{}
err := daemon.StartServer(req, &res)
if err != nil {
t.Fatalf("Error: %v", err)
Expand All @@ -164,7 +164,7 @@ func TestStartServer(t *testing.T) {
func TestListServers(t *testing.T) {
daemon, _ := createMockedDaemon(true)
var res types.PactListResponse
err := daemon.ListServers(types.PactMockServer{}, &res)
err := daemon.ListServers(types.MockServer{}, &res)

if err != nil {
t.Fatalf("Error: %v", err)
Expand All @@ -178,12 +178,12 @@ func TestListServers(t *testing.T) {
func TestStopServer(t *testing.T) {
daemon, manager := createMockedDaemon(true)
var cmd *exec.Cmd
var res types.PactMockServer
var res types.MockServer

for _, s := range manager.List() {
cmd = s
}
request := types.PactMockServer{
request := types.MockServer{
Pid: cmd.Process.Pid,
}

Expand All @@ -204,12 +204,12 @@ func TestStopServer(t *testing.T) {
func TestStopServer_Fail(t *testing.T) {
daemon, manager := createMockedDaemon(true)
var cmd *exec.Cmd
var res types.PactMockServer
var res types.MockServer

for _, s := range manager.List() {
cmd = s
}
request := types.PactMockServer{
request := types.MockServer{
Pid: cmd.Process.Pid,
}

Expand All @@ -224,12 +224,12 @@ func TestStopServer_Fail(t *testing.T) {
func TestStopServer_FailedStatus(t *testing.T) {
daemon, manager := createMockedDaemon(true)
var cmd *exec.Cmd
var res types.PactMockServer
var res types.MockServer

for _, s := range manager.List() {
cmd = s
}
request := types.PactMockServer{
request := types.MockServer{
Pid: cmd.Process.Pid,
}

Expand Down Expand Up @@ -344,7 +344,7 @@ func TestRPCClient_List(t *testing.T) {

client, err := rpc.DialHTTP("tcp", fmt.Sprintf(":%d", port))
var res types.PactListResponse
err = client.Call("Daemon.ListServers", types.PactMockServer{}, &res)
err = client.Call("Daemon.ListServers", types.MockServer{}, &res)
if err != nil {
log.Fatal("rpc error:", err)
}
Expand All @@ -362,8 +362,8 @@ func TestRPCClient_StartServer(t *testing.T) {
connectToDaemon(port, t)

client, err := rpc.DialHTTP("tcp", fmt.Sprintf(":%d", port))
var res types.PactMockServer
err = client.Call("Daemon.StartServer", types.PactMockServer{}, &res)
var res types.MockServer
err = client.Call("Daemon.StartServer", types.MockServer{}, &res)
if err != nil {
log.Fatal("rpc error:", err)
}
Expand All @@ -388,12 +388,12 @@ func TestRPCClient_StopServer(t *testing.T) {
for _, s := range manager.List() {
cmd = s
}
request := types.PactMockServer{
request := types.MockServer{
Pid: cmd.Process.Pid,
}

client, err := rpc.DialHTTP("tcp", fmt.Sprintf(":%d", port))
var res *types.PactMockServer
var res *types.MockServer
err = client.Call("Daemon.StopServer", request, &res)
if err != nil {
log.Fatal("rpc error:", err)
Expand Down
17 changes: 5 additions & 12 deletions daemon/pact_mock_service.go → daemon/mock_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,27 @@ package daemon
import (
"fmt"
"log"
"os"
"path/filepath"

"github.com/kardianos/osext"
"github.com/pact-foundation/pact-go/utils"
)

// PactMockService is a wrapper for the Pact Mock Service.
type PactMockService struct {
// MockService is a wrapper for the Pact Mock Service.
type MockService struct {
ServiceManager
}

// NewService creates a new PactMockService with default settings.
func (m *PactMockService) NewService(args []string) (int, Service) {
// NewService creates a new MockService with default settings.
func (m *MockService) NewService(args []string) (int, Service) {
port, _ := utils.GetFreePort()
version := 2
dir, _ := os.Getwd()
logDir := fmt.Sprintf(filepath.Join(dir, "logs"))
dir = fmt.Sprintf(filepath.Join(dir, "pacts"))
log.Println("[DEBUG] starting mock service on port:", port)

m.Args = []string{
fmt.Sprintf("--port %d", port),
fmt.Sprintf("--pact-specification-version %d", version),
fmt.Sprintf("--pact-dir %s", dir),
fmt.Sprintf("--log %s/pact.log", logDir),
}
m.Args = append(m.Args, args...)

m.Command = getMockServiceCommandPath()
return port, m
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package daemon

import "testing"

func TestPactMockService_NewService(t *testing.T) {
s := &PactMockService{}
func TestMockService_NewService(t *testing.T) {
s := &MockService{}
port, svc := s.NewService([]string{"--foo bar"})

if port <= 0 {
Expand All @@ -14,7 +14,7 @@ func TestPactMockService_NewService(t *testing.T) {
t.Fatalf("Expected a non-nil object but got nil")
}

if s.Args[4] != "--foo bar" {
if s.Args[1] != "--foo bar" {
t.Fatalf("Expected '--foo bar' argument to be passed")
}
}
3 changes: 3 additions & 0 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ A typical consumer-side test would look something like this:
if err != nil {
t.Fatalf("Error on Verify: %v", err)
}
// Write pact to file
pact.WritePact()
}
If this test completed successfully, a Pact file should have been written to
Expand Down
14 changes: 7 additions & 7 deletions dsl/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var (

// Client is the simplified remote interface to the Pact Daemon.
type Client interface {
StartServer() *types.PactMockServer
StartServer() *types.MockServer
}

// PactClient is the default implementation of the Client interface.
Expand Down Expand Up @@ -83,12 +83,12 @@ var waitForPort = func(port int, message string) error {
}

// StartServer starts a remote Pact Mock Server.
func (p *PactClient) StartServer() *types.PactMockServer {
func (p *PactClient) StartServer(args []string) *types.MockServer {
log.Println("[DEBUG] client: starting a server")
var res types.PactMockServer
var res types.MockServer
client, err := getHTTPClient(p.Port)
if err == nil {
err = client.Call(commandStartServer, types.PactMockServer{}, &res)
err = client.Call(commandStartServer, types.MockServer{Args: args}, &res)
if err != nil {
log.Println("[ERROR] rpc:", err.Error())
}
Expand Down Expand Up @@ -129,7 +129,7 @@ func (p *PactClient) ListServers() *types.PactListResponse {
var res types.PactListResponse
client, err := getHTTPClient(p.Port)
if err == nil {
err = client.Call(commandListServers, types.PactMockServer{}, &res)
err = client.Call(commandListServers, types.MockServer{}, &res)
if err != nil {
log.Println("[ERROR] rpc:", err.Error())
}
Expand All @@ -138,9 +138,9 @@ func (p *PactClient) ListServers() *types.PactListResponse {
}

// StopServer stops a remote Pact Mock Server.
func (p *PactClient) StopServer(server *types.PactMockServer) *types.PactMockServer {
func (p *PactClient) StopServer(server *types.MockServer) *types.MockServer {
log.Println("[DEBUG] client: stop server")
var res types.PactMockServer
var res types.MockServer
client, err := getHTTPClient(p.Port)
if err == nil {
err = client.Call(commandStopServer, server, &res)
Expand Down
20 changes: 10 additions & 10 deletions dsl/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func TestClient_List(t *testing.T) {
func TestClient_ListFail(t *testing.T) {
timeoutDuration = 50 * time.Millisecond
client := &PactClient{ /* don't supply port */ }
client.StartServer()
client.StartServer([]string{})
list := client.ListServers()

if len(list.Servers) != 0 {
Expand All @@ -146,7 +146,7 @@ func TestClient_StartServer(t *testing.T) {
defer waitForDaemonToShutdown(port, t)
client := &PactClient{Port: port}

client.StartServer()
client.StartServer([]string{})
if svc.ServiceStartCount != 1 {
t.Fatalf("Expected 1 server to have been started, got %d", svc.ServiceStartCount)
}
Expand Down Expand Up @@ -185,11 +185,11 @@ func TestClient_StartServerRPCError(t *testing.T) {
"rpc: service/method request ill-formed: failcommand": func() interface{} {
return client.StopDaemon().Error()
},
&types.PactMockServer{}: func() interface{} {
return client.StopServer(&types.PactMockServer{})
&types.MockServer{}: func() interface{} {
return client.StopServer(&types.MockServer{})
},
&types.PactMockServer{}: func() interface{} {
return client.StartServer()
&types.MockServer{}: func() interface{} {
return client.StartServer([]string{})
},
&types.PactListResponse{}: func() interface{} {
return client.ListServers()
Expand Down Expand Up @@ -300,7 +300,7 @@ func TestClient_StartServerFail(t *testing.T) {
timeoutDuration = 50 * time.Millisecond

client := &PactClient{ /* don't supply port */ }
server := client.StartServer()
server := client.StartServer([]string{})
if server.Port != 0 {
t.Fatalf("Expected server to be empty %v", server)
}
Expand All @@ -314,7 +314,7 @@ func TestClient_StopServer(t *testing.T) {
defer waitForDaemonToShutdown(port, t)
client := &PactClient{Port: port}

client.StopServer(&types.PactMockServer{})
client.StopServer(&types.MockServer{})
if svc.ServiceStopCount != 1 {
t.Fatalf("Expected 1 server to have been stopped, got %d", svc.ServiceStartCount)
}
Expand All @@ -323,8 +323,8 @@ func TestClient_StopServer(t *testing.T) {
func TestClient_StopServerFail(t *testing.T) {
timeoutDuration = 50 * time.Millisecond
client := &PactClient{ /* don't supply port */ }
res := client.StopServer(&types.PactMockServer{})
should := &types.PactMockServer{}
res := client.StopServer(&types.MockServer{})
should := &types.MockServer{}
if !reflect.DeepEqual(res, should) {
t.Fatalf("Expected nil object but got a difference: %v != %v", res, should)
}
Expand Down
11 changes: 11 additions & 0 deletions dsl/interaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ func (p *Interaction) UponReceiving(description string) *Interaction {
// Mandatory.
func (p *Interaction) WithRequest(request *Request) *Interaction {
p.Request = request

// Need to fix any weird JSON marshalling issues with the body Here
// If body is a string, not an object, we need to put it back into an object
// so that it's not double encoded
switch content := request.Body.(type) {
case string:
p.Request.Body = toObject([]byte(content))
default:
// leave alone
}

return p
}

Expand Down
Loading

0 comments on commit ea2c807

Please sign in to comment.