From 24e7c9bc91ea1bf754ca79d16b76743f8cfdab1c Mon Sep 17 00:00:00 2001 From: Matt Fellows Date: Tue, 31 May 2016 07:30:17 +1000 Subject: [PATCH] test(daemon): improved daemon test coverage --- daemon/daemon.go | 25 ++++++- daemon/daemon_test.go | 150 ++++++++++++++++++----------------------- daemon/service_mock.go | 9 ++- 3 files changed, 96 insertions(+), 88 deletions(-) diff --git a/daemon/daemon.go b/daemon/daemon.go index 8d8fc5b40..98e9d5ead 100644 --- a/daemon/daemon.go +++ b/daemon/daemon.go @@ -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{} @@ -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 } diff --git a/daemon/daemon_test.go b/daemon/daemon_test.go index e21afe506..8513a3fc0 100644 --- a/daemon/daemon_test.go +++ b/daemon/daemon_test.go @@ -5,52 +5,42 @@ 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, ""), @@ -58,12 +48,25 @@ func createMockedDaemon() *Daemon { }, 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") @@ -71,7 +74,7 @@ func TestNewDaemon(t *testing.T) { } func TestStartAndStopDaemon(t *testing.T) { - daemon := createMockedDaemon() + daemon, _ := createMockedDaemon() go daemon.StartDaemon() for { @@ -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() @@ -130,12 +101,9 @@ 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) @@ -143,21 +111,26 @@ func TestStartServer(t *testing.T) { 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)) } } @@ -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, "") +} diff --git a/daemon/service_mock.go b/daemon/service_mock.go index cb22996d9..6ebbaa563 100644 --- a/daemon/service_mock.go +++ b/daemon/service_mock.go @@ -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. @@ -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.