Skip to content

Commit

Permalink
test(daemon): improved daemon test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
mefellows committed Jun 4, 2016
1 parent 345cd9f commit 24e7c9b
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 88 deletions.
25 changes: 22 additions & 3 deletions daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ type PactResponse struct {
Message string
}

// PactListResponse contains a list of all running Servers.
type PactListResponse struct {
// System exit code from the Publish task.
Servers []*PactMockServer
}

// VerifyRequest contains the verification params.
type VerifyRequest struct{}

Expand Down Expand Up @@ -111,11 +117,24 @@ func (d *Daemon) StartServer(request *PactMockServer, reply *PactMockServer) err
}

// ListServers returns a slice of all running PactMockServers.
func (d *Daemon) ListServers(request interface{}, reply *[]PactMockServer) error {
*reply = []PactMockServer{
PactMockServer{Pid: 1},
func (d *Daemon) ListServers(request interface{}, reply *PactListResponse) error {
var servers []*PactMockServer

for port, s := range d.pactMockSvcManager.List() {
fmt.Println("Listing!")
servers = append(servers, &PactMockServer{
Pid: s.Process.Pid,
Port: port,
Svc: d.pactMockSvcManager,
})
}

*reply = *&PactListResponse{
Servers: servers,
}

fmt.Println(reply)

return nil
}

Expand Down
150 changes: 66 additions & 84 deletions daemon/daemon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,73 +5,76 @@ import (
"net"
"os"
"os/exec"
"reflect"
"testing"
"time"
)

// func createDefaultDaemon() {
// getCommandPath = func() string {
// dir, _ := os.Getwd()
// return fmt.Sprintf(filepath.Join(dir, "../", "pact-mock-service", "bin", "pact-mock-service"))
// }
// }

// type MockService struct {
// }
//
// func (r MockService) Run(command string, args ...string) ([]byte, error) {
// cs := []string{"-test.run=TestHelperProcess", "--"}
// cs = append(cs, args...)
// cmd := exec.Command(os.Args[0], cs...)
// cmd.Env = []string{"GO_WANT_HELPER_PROCESS=1"}
// out, err := cmd.CombinedOutput()
// return out, err
// }
//
// func TestHello(t *testing.T) {
// runner = MockService{}
// out := Hello()
// if out == "testing helper process" {
// t.Logf("out was eq to %s", string(out))
// }
// }
//
// func TestHelperProcess(*testing.T) {
// if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" {
// return
// }
// defer os.Exit(0)
// fmt.Println("testing helper process")
// }

func createMockedDaemon() *Daemon {
func fakeExecCommand(command string, success bool, args ...string) *exec.Cmd {
cs := []string{"-test.run=TestHelperProcess", "--", command}
cs = append(cs, args...)
cmd := exec.Command(os.Args[0], cs...)
cmd.Env = []string{"GO_WANT_HELPER_PROCESS=1", fmt.Sprintf("GO_WANT_HELPER_PROCESS_TO_SUCCEED=%t", success)}
return cmd
}

func TestHelperProcess(t *testing.T) {
fmt.Fprintln(os.Stdout, "HELLLlloooo")
<-time.After(30 * time.Second)
if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" {
return
}

// some code here to check arguments perhaps?
// Fail :(
if os.Getenv("GO_WANT_HELPER_PROCESS_TO_SUCCEED") == "false" {
os.Exit(1)
}

// Success :)
os.Exit(0)
}

func createMockedDaemon() (*Daemon, *ServiceMock) {
svc := &ServiceMock{
Command: "test",
Args: []string{},
ServiceStopResult: true,
ServiceStopError: nil,
ExecFunc: fakeExecSuccessCommand,
ServiceList: map[int]*exec.Cmd{
1: fakeExecCommand("", true, ""),
2: fakeExecCommand("", true, ""),
3: fakeExecCommand("", true, ""),
},
ServiceStartCmd: nil,
}
return NewDaemon(svc)

// Start all processes to get the Pids!
for _, s := range svc.ServiceList {
s.Start()
}

// Cleanup all Processes when we finish
defer func() {
for _, s := range svc.ServiceList {
s.Process.Kill()
}
}()

return NewDaemon(svc), svc
}

func TestNewDaemon(t *testing.T) {
var daemon interface{}
daemon = createMockedDaemon()
daemon, _ = createMockedDaemon()

if _, ok := daemon.(Daemon); !ok {
t.Fatalf("must be a Daemon")
}
}

func TestStartAndStopDaemon(t *testing.T) {
daemon := createMockedDaemon()
daemon, _ := createMockedDaemon()
go daemon.StartDaemon()

for {
Expand All @@ -88,40 +91,8 @@ func TestStartAndStopDaemon(t *testing.T) {
}
}

// Adapted from http://npf.io/2015/06/testing-exec-command/
func fakeExecCommand(command string, success bool, args ...string) *exec.Cmd {
cs := []string{"-test.run=TestHelperProcess", "--", command}
cs = append(cs, args...)
cmd := exec.Command(os.Args[0], cs...)
cmd.Env = []string{"GO_WANT_HELPER_PROCESS=1", fmt.Sprintf("GO_WANT_HELPER_PROCESS_TO_SUCCEED=%t", success)}
return cmd
}

func TestHelperProcess(t *testing.T) {
fmt.Fprintln(os.Stdout, "HELLLlloooo")
<-time.After(30 * time.Second)
if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" {
return
}

// some code here to check arguments perhaps?
// Fail :(
if os.Getenv("GO_WANT_HELPER_PROCESS_TO_SUCCEED") == "false" {
os.Exit(1)
}

// Success :)
os.Exit(0)
}

func TestDaemonShutdown(t *testing.T) {
daemon := createMockedDaemon()
manager := daemon.pactMockSvcManager.(*ServiceMock)

// Start all processes to get the Pids!
for _, s := range manager.ServiceList {
s.Start()
}
daemon, manager := createMockedDaemon()

daemon.Shutdown()

Expand All @@ -130,34 +101,36 @@ func TestDaemonShutdown(t *testing.T) {
}
}

func TestStartDaemon_Fail(t *testing.T) {

}

func TestStartServer(t *testing.T) {
daemon := createMockedDaemon()
daemon, _ := createMockedDaemon()

req := PactMockServer{Pid: 1234}
res := PactMockServer{}
err := daemon.StartServer(&req, &res)
if err != nil {
t.Fatalf("Error: %v", err)
}

if !reflect.DeepEqual(req, res) {
t.Fatalf("Req and Res did not match")
if res.Pid != 0 {
t.Fatalf("Expected non-zero Pid but got: %d", res.Pid)
}

if res.Port != 0 {
t.Fatalf("Expected non-zero port but got: %d", res.Port)
}
}

func TestListServers(t *testing.T) {
daemon := &Daemon{}
var res []PactMockServer
daemon, _ := createMockedDaemon()
var res PactListResponse
err := daemon.ListServers(nil, &res)

if err != nil {
t.Fatalf("Error: %v", err)
}

if len(res) != 1 {
t.Fatalf("Expected array of len 1, got: %d", len(res))
if len(res.Servers) != 3 {
t.Fatalf("Expected array of len 3, got: %d", len(res.Servers))
}
}

Expand Down Expand Up @@ -213,3 +186,12 @@ func TestPublish(t *testing.T) {
func TestPublish_Fail(t *testing.T) {

}

// Adapted from http://npf.io/2015/06/testing-exec-command/
var fakeExecSuccessCommand = func() *exec.Cmd {
return fakeExecCommand("", true, "")
}

var fakeExecFailCommand = func() *exec.Cmd {
return fakeExecCommand("", false, "")
}
9 changes: 8 additions & 1 deletion daemon/service_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ type ServiceMock struct {
ServicePort int
ServiceStopCount int
ServicesSetupCalled bool

// ExecFunc sets the function to run when starting commands
ExecFunc func() *exec.Cmd
}

// Setup the Management services.
Expand All @@ -34,7 +37,11 @@ func (s *ServiceMock) List() map[int]*exec.Cmd {

// Start a Service and log its output.
func (s *ServiceMock) Start() *exec.Cmd {
return s.ServiceStartCmd

cmd := s.ExecFunc()
cmd.Start()

return cmd
}

// NewService creates a new MockService with default settings.
Expand Down

0 comments on commit 24e7c9b

Please sign in to comment.