From b33041476482704598cb3c6f3e2739ec5a173085 Mon Sep 17 00:00:00 2001 From: Matt Fellows Date: Sun, 18 Mar 2018 23:11:46 +1100 Subject: [PATCH] =?UTF-8?q?wip:=20commit=20to=20continue=20work=20on=20tra?= =?UTF-8?q?in=20=F0=9F=98=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- {daemon => client}/mock_service.go | 2 +- {daemon => client}/mock_service_test.go | 2 +- {daemon => client}/service.go | 2 +- {daemon => client}/service_manager.go | 2 +- {daemon => client}/service_mock.go | 2 +- {daemon => client}/service_test.go | 37 +- {daemon => client}/verification_service.go | 2 +- .../verification_service_test.go | 2 +- daemon/daemon.go | 201 -------- daemon/daemon_test.go | 478 ------------------ dsl/client.go | 153 ++---- .../consumer/goconsumer/user_service_test.go | 2 +- types/list_response.go | 7 - 13 files changed, 86 insertions(+), 806 deletions(-) rename {daemon => client}/mock_service.go (97%) rename {daemon => client}/mock_service_test.go (95%) rename {daemon => client}/service.go (96%) rename {daemon => client}/service_manager.go (99%) rename {daemon => client}/service_mock.go (99%) rename {daemon => client}/service_test.go (79%) rename {daemon => client}/verification_service.go (98%) rename {daemon => client}/verification_service_test.go (96%) delete mode 100644 daemon/daemon.go delete mode 100644 daemon/daemon_test.go delete mode 100644 types/list_response.go diff --git a/daemon/mock_service.go b/client/mock_service.go similarity index 97% rename from daemon/mock_service.go rename to client/mock_service.go index db0fa7881..a5490aec7 100644 --- a/daemon/mock_service.go +++ b/client/mock_service.go @@ -1,4 +1,4 @@ -package daemon +package client import ( "fmt" diff --git a/daemon/mock_service_test.go b/client/mock_service_test.go similarity index 95% rename from daemon/mock_service_test.go rename to client/mock_service_test.go index 052528c40..097651c94 100644 --- a/daemon/mock_service_test.go +++ b/client/mock_service_test.go @@ -1,4 +1,4 @@ -package daemon +package client import "testing" diff --git a/daemon/service.go b/client/service.go similarity index 96% rename from daemon/service.go rename to client/service.go index 6bab933fd..8939fc460 100644 --- a/daemon/service.go +++ b/client/service.go @@ -1,4 +1,4 @@ -package daemon +package client import ( "io" diff --git a/daemon/service_manager.go b/client/service_manager.go similarity index 99% rename from daemon/service_manager.go rename to client/service_manager.go index 9f478b9fe..921cb11bc 100644 --- a/daemon/service_manager.go +++ b/client/service_manager.go @@ -1,4 +1,4 @@ -package daemon +package client import ( "bufio" diff --git a/daemon/service_mock.go b/client/service_mock.go similarity index 99% rename from daemon/service_mock.go rename to client/service_mock.go index ae14c1d46..4b0c08444 100644 --- a/daemon/service_mock.go +++ b/client/service_mock.go @@ -1,4 +1,4 @@ -package daemon +package client import ( "io" diff --git a/daemon/service_test.go b/client/service_test.go similarity index 79% rename from daemon/service_test.go rename to client/service_test.go index 371fdb126..34e4c264a 100644 --- a/daemon/service_test.go +++ b/client/service_test.go @@ -1,4 +1,4 @@ -package daemon +package client import ( "fmt" @@ -175,3 +175,38 @@ func TestServiceManager_Start(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, "") +} + +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) { + if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" { + return + } + <-time.After(250 * time.Millisecond) + + // some code here to check arguments perhaps? + // Fail :( + if os.Getenv("GO_WANT_HELPER_PROCESS_TO_SUCCEED") == "false" { + fmt.Fprintf(os.Stdout, "COMMAND: oh noes!") + os.Exit(1) + } + + // Success :) + fmt.Fprintf(os.Stdout, `{"summary_line":"1 examples, 0 failures"}`) + os.Exit(0) +} diff --git a/daemon/verification_service.go b/client/verification_service.go similarity index 98% rename from daemon/verification_service.go rename to client/verification_service.go index 76957d9fb..de52e289a 100644 --- a/daemon/verification_service.go +++ b/client/verification_service.go @@ -1,4 +1,4 @@ -package daemon +package client import ( "fmt" diff --git a/daemon/verification_service_test.go b/client/verification_service_test.go similarity index 96% rename from daemon/verification_service_test.go rename to client/verification_service_test.go index abd736b95..b346801e2 100644 --- a/daemon/verification_service_test.go +++ b/client/verification_service_test.go @@ -1,4 +1,4 @@ -package daemon +package client import ( "fmt" diff --git a/daemon/daemon.go b/daemon/daemon.go deleted file mode 100644 index 50848c406..000000000 --- a/daemon/daemon.go +++ /dev/null @@ -1,201 +0,0 @@ -/* -Package daemon implements the RPC server side interface to remotely manage -external Pact dependencies: The Pact Mock Service and Provider Verification -"binaries." - -See https://github.com/pact-foundation/pact-provider-verifier and -https://github.com/bethesque/pact-mock_service for more on the Ruby "binaries". - -NOTE: The ultimate goal here is to replace the Ruby dependencies with a shared -library (Pact Reference - (https://github.com/pact-foundation/pact-reference/). -*/ -package daemon - -// Runs the RPC daemon for remote communication - -import ( - "bytes" - "encoding/json" - "fmt" - "io/ioutil" - "log" - "net" - "net/http" - "net/rpc" - "os" - "os/signal" - - "github.com/pact-foundation/pact-go/types" -) - -// Daemon wraps the commands for the RPC server. -type Daemon struct { - pactMockSvcManager Service - verificationSvcManager Service - signalChan chan os.Signal -} - -// NewDaemon returns a new Daemon with all instance variables initialised. -func NewDaemon(MockServiceManager Service, verificationServiceManager Service) *Daemon { - MockServiceManager.Setup() - verificationServiceManager.Setup() - - return &Daemon{ - pactMockSvcManager: MockServiceManager, - verificationSvcManager: verificationServiceManager, - signalChan: make(chan os.Signal, 1), - } -} - -// StartDaemon starts the daemon RPC server. -func (d Daemon) StartDaemon(port int, network string, address string) { - log.Println("[INFO] daemon - starting daemon on network:", network, "address:", address, "port:", port) - - serv := rpc.NewServer() - serv.Register(d) - - // Workaround for multiple RPC ServeMux's - oldMux := http.DefaultServeMux - mux := http.NewServeMux() - http.DefaultServeMux = mux - - serv.HandleHTTP(rpc.DefaultRPCPath, rpc.DefaultDebugPath) - - // Workaround for multiple RPC ServeMux's - http.DefaultServeMux = oldMux - - l, err := net.Listen(network, fmt.Sprintf("%s:%d", address, port)) - if err != nil { - panic(err) - } - go http.Serve(l, mux) - - // Wait for sigterm - signal.Notify(d.signalChan, os.Interrupt, os.Kill) - s := <-d.signalChan - log.Println("[INFO] daemon - received signal:", s, ", shutting down all services") - - d.Shutdown() -} - -// StopDaemon allows clients to programmatically shuts down the running Daemon -// via RPC. -func (d Daemon) StopDaemon(request string, reply *string) error { - log.Println("[DEBUG] daemon - stop daemon") - d.signalChan <- os.Interrupt - return nil -} - -// Shutdown ensures all services are cleanly destroyed. -func (d Daemon) Shutdown() { - log.Println("[DEBUG] daemon - shutdown") - for _, s := range d.verificationSvcManager.List() { - if s != nil { - d.pactMockSvcManager.Stop(s.Process.Pid) - } - } -} - -// StartServer starts a mock server and returns a pointer to atypes.MockServer -// struct. -func (d Daemon) StartServer(request types.MockServer, reply *types.MockServer) error { - log.Println("[DEBUG] daemon - starting mock server with args:", request.Args) - server := &types.MockServer{} - svc := d.pactMockSvcManager.NewService(request.Args) - server.Status = -1 - cmd := svc.Start() - server.Pid = cmd.Process.Pid - *reply = *server - return nil -} - -// VerifyProvider runs the Pact Provider Verification Process. -func (d Daemon) VerifyProvider(request types.VerifyRequest, reply *types.ProviderVerifierResponse) error { - log.Println("[DEBUG] daemon - verifying provider") - - // Convert request into flags, and validate request - err := request.Validate() - if err != nil { - return err - } - - // Run command, splitting out stderr and stdout. The command can fail for - // several reasons: - // 1. Command is unable to run at all. - // 2. Command runs, but fails for unknown reason. - // 3. Command runs, and returns exit status 1 because the tests fail. - // - // First, attempt to decode the response of the stdout. - // If that is successful, we are at case 3. Return stdout as message, no error. - // Else, return an error, include stderr and stdout in both the error and message. - svc := d.verificationSvcManager.NewService(request.Args) - cmd := svc.Command() - - stdOutPipe, err := cmd.StdoutPipe() - if err != nil { - return err - } - stdErrPipe, err := cmd.StderrPipe() - if err != nil { - return err - } - err = cmd.Start() - if err != nil { - return err - } - stdOut, err := ioutil.ReadAll(stdOutPipe) - if err != nil { - return err - } - stdErr, err := ioutil.ReadAll(stdErrPipe) - if err != nil { - return err - } - - err = cmd.Wait() - - decoder := json.NewDecoder(bytes.NewReader(stdOut)) - dErr := decoder.Decode(&reply) - if dErr == nil { - return nil - } - - if err == nil { - err = dErr - } - - return fmt.Errorf("error verifying provider: %s\n\nSTDERR:\n%s\n\nSTDOUT:\n%s", err, stdErr, stdOut) -} - -// 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.MockServer - - for port, s := range d.pactMockSvcManager.List() { - servers = append(servers, &types.MockServer{ - Pid: s.Process.Pid, - Port: port, - }) - } - - *reply = types.PactListResponse{ - Servers: servers, - } - - return nil -} - -// StopServer stops the given mock server. -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 { - request.Status = 0 - } else { - request.Status = 1 - } - *reply = request - - return nil -} diff --git a/daemon/daemon_test.go b/daemon/daemon_test.go deleted file mode 100644 index 1c67f7bff..000000000 --- a/daemon/daemon_test.go +++ /dev/null @@ -1,478 +0,0 @@ -package daemon - -import ( - "errors" - "fmt" - "log" - "net" - "net/rpc" - "os" - "os/exec" - "strings" - "testing" - "time" - - "github.com/pact-foundation/pact-go/types" - "github.com/pact-foundation/pact-go/utils" -) - -// This guy mocks out the underlying Service provider in the Daemon, -// but executes actual Daemon code. -// -// Stubbing the exec.Cmd interface is hard, see fakeExec* functions for -// the magic. -func createMockedDaemon(success bool) (*Daemon, *ServiceMock) { - execFunc := fakeExecSuccessCommand - if !success { - execFunc = fakeExecFailCommand - } - svc := &ServiceMock{ - Cmd: "test", - Args: []string{}, - ServiceStopResult: true, - ServiceStopError: nil, - ExecFunc: execFunc, - ServiceList: map[int]*exec.Cmd{ - 1: fakeExecCommand("", success, ""), - 2: fakeExecCommand("", success, ""), - 3: fakeExecCommand("", success, ""), - }, - ServiceStartCmd: nil, - } - - // 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), svc -} - -func TestNewDaemon(t *testing.T) { - var daemon interface{} - daemon, _ = createMockedDaemon(true) - - if _, ok := daemon.(*Daemon); !ok { - t.Fatalf("must be a Daemon") - } -} - -func TestStopDaemon(t *testing.T) { - d, _ := createMockedDaemon(true) - port, _ := utils.GetFreePort() - go d.StartDaemon(port, "tcp", "") - connectToDaemon(port, t) - var res string - d.StopDaemon("", &res) - waitForDaemonToShutdown(port, d, t) -} - -func TestShutdownDaemon(t *testing.T) { - d, _ := createMockedDaemon(true) - port, _ := utils.GetFreePort() - go d.StartDaemon(port, "tcp", "") - connectToDaemon(port, t) - d.Shutdown() -} - -// Use this to wait for a daemon to be running prior -// to running tests -func connectToDaemon(port int, t *testing.T) { - for { - select { - case <-time.After(250 * time.Millisecond): - t.Fatalf("Expected server to start < 1s.") - case <-time.After(50 * time.Millisecond): - _, err := net.Dial("tcp", fmt.Sprintf(":%d", port)) - if err == nil { - return - } - } - } -} - -// Use this to wait for a daemon to stop after running a test. -func waitForDaemonToShutdown(port int, daemon *Daemon, t *testing.T) { - if daemon != nil { - daemon.signalChan <- os.Interrupt - } - t.Logf("Waiting for deamon to shutdown before next test") - timeout := time.After(250 * time.Millisecond) - for { - select { - case <-timeout: - t.Fatalf("Expected server to shutdown < 1s.") - case <-time.After(50 * time.Millisecond): - conn, err := net.Dial("tcp", fmt.Sprintf(":%d", port)) - conn.SetReadDeadline(time.Now()) - defer conn.Close() - if err != nil { - return - } - buffer := make([]byte, 8) - _, err = conn.Read(buffer) - if err != nil { - return - } - } - } -} - -func TestStartAndStopDaemon(t *testing.T) { - port, _ := utils.GetFreePort() - daemon, _ := createMockedDaemon(true) - defer waitForDaemonToShutdown(port, daemon, t) - go daemon.StartDaemon(port, "tcp", "") - connectToDaemon(port, t) -} - -func TestDaemonShutdown(t *testing.T) { - daemon, manager := createMockedDaemon(true) - daemon.Shutdown() - - if manager.ServiceStopCount != 3 { - t.Fatalf("Expected Stop() to be called 3 times but got: %d", manager.ServiceStopCount) - } -} - -func TestStartServer(t *testing.T) { - daemon, _ := createMockedDaemon(true) - - req := types.MockServer{Pid: 1234} - res := types.MockServer{} - err := daemon.StartServer(req, &res) - if err != nil { - t.Fatalf("Error: %v", err) - } - - 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, _ := createMockedDaemon(true) - var res types.PactListResponse - err := daemon.ListServers(types.MockServer{}, &res) - - if err != nil { - t.Fatalf("Error: %v", err) - } - - if len(res.Servers) != 3 { - t.Fatalf("Expected array of len 3, got: %d", len(res.Servers)) - } -} - -func TestStopServer(t *testing.T) { - daemon, manager := createMockedDaemon(true) - var cmd *exec.Cmd - var res types.MockServer - - for _, s := range manager.List() { - cmd = s - } - request := types.MockServer{ - Pid: cmd.Process.Pid, - } - - err := daemon.StopServer(request, &res) - if err != nil { - t.Fatalf("Error: %v", err) - } - - if res.Pid != cmd.Process.Pid { - t.Fatalf("Expected PID to be 0 but got: %d", res.Pid) - } - - if res.Status != 0 { - t.Fatalf("Expected exit status to be 0 but got: %d", res.Status) - } -} - -func TestStopServer_Fail(t *testing.T) { - daemon, manager := createMockedDaemon(true) - var cmd *exec.Cmd - var res types.MockServer - - for _, s := range manager.List() { - cmd = s - } - request := types.MockServer{ - Pid: cmd.Process.Pid, - } - - manager.ServiceStopError = errors.New("failed to stop server") - - err := daemon.StopServer(request, &res) - if err != nil { - t.Fatalf("Error: %v", err) - } -} - -func TestStopServer_FailedStatus(t *testing.T) { - daemon, manager := createMockedDaemon(true) - var cmd *exec.Cmd - var res types.MockServer - - for _, s := range manager.List() { - cmd = s - } - request := types.MockServer{ - Pid: cmd.Process.Pid, - } - - manager.ServiceStopResult = false - - daemon.StopServer(request, &res) - - if res.Status != 1 { - t.Fatalf("Expected exit status to be 1 but got: %d", res.Status) - } -} - -func TestVerifyProvider_MissingProviderBaseURL(t *testing.T) { - daemon, _ := createMockedDaemon(true) - - req := types.VerifyRequest{ - PactURLs: []string{"url1", "url2"}, - } - res := types.ProviderVerifierResponse{} - err := daemon.VerifyProvider(req, &res) - - if err == nil { - t.Fatal("Expected an error") - } - - if !strings.Contains(err.Error(), "Provider base URL is mandatory") { - t.Fatalf("Expected error message but got '%s'", err.Error()) - } -} - -func TestVerifyProvider_MissingPactURLs(t *testing.T) { - daemon, _ := createMockedDaemon(true) - - req := types.VerifyRequest{ - ProviderBaseURL: "http://foo.com", - } - res := types.ProviderVerifierResponse{} - err := daemon.VerifyProvider(req, &res) - - if err == nil { - t.Fatal("Expected an error") - } - - if !strings.Contains(err.Error(), "Pact URLs is mandatory") { - t.Fatalf("Expected error message but got '%s'", err.Error()) - } -} - -func TestVerifyProvider_Valid(t *testing.T) { - daemon, _ := createMockedDaemon(true) - - req := types.VerifyRequest{ - ProviderBaseURL: "http://foo.com", - PactURLs: []string{"foo.json", "bar.json"}, - } - res := types.ProviderVerifierResponse{} - err := daemon.VerifyProvider(req, &res) - if err != nil { - t.Fatalf("Error: %s", err) - } -} - -func TestVerifyProvider_FailedCommand(t *testing.T) { - daemon, _ := createMockedDaemon(false) - - req := types.VerifyRequest{ - ProviderBaseURL: "http://foo.com", - PactURLs: []string{"foo.json", "bar.json"}, - } - res := types.ProviderVerifierResponse{} - err := daemon.VerifyProvider(req, &res) - if err == nil { - t.Fatal("Expected an error") - } - - if !strings.Contains(err.Error(), "COMMAND: oh noes!") { - t.Fatalf("Expected error message but got '%s'", err.Error()) - } -} - -func TestVerifyProvider_ValidProviderStates(t *testing.T) { - daemon, _ := createMockedDaemon(true) - - req := types.VerifyRequest{ - ProviderBaseURL: "http://foo.com", - PactURLs: []string{"foo.json", "bar.json"}, - BrokerUsername: "foo", - BrokerPassword: "foo", - ProviderStatesURL: "http://foo/states", - ProviderStatesSetupURL: "http://foo/states/setup", - } - res := types.ProviderVerifierResponse{} - err := daemon.VerifyProvider(req, &res) - if err != nil { - t.Fatalf("Error: %v", err) - } -} - -func TestRPCClient_List(t *testing.T) { - daemon, _ := createMockedDaemon(true) - port, _ := utils.GetFreePort() - defer waitForDaemonToShutdown(port, daemon, t) - go daemon.StartDaemon(port, "tcp", "") - connectToDaemon(port, t) - - client, err := rpc.DialHTTP("tcp", fmt.Sprintf(":%d", port)) - var res types.PactListResponse - err = client.Call("Daemon.ListServers", types.MockServer{}, &res) - if err != nil { - log.Fatal("rpc error:", err) - } - - if len(res.Servers) != 3 { - t.Fatalf("Expected 3 servers to be listed, got: %d", len(res.Servers)) - } -} - -func TestRPCClient_StartServer(t *testing.T) { - daemon, _ := createMockedDaemon(true) - port, _ := utils.GetFreePort() - defer waitForDaemonToShutdown(port, daemon, t) - go daemon.StartDaemon(port, "tcp", "") - connectToDaemon(port, t) - - client, err := rpc.DialHTTP("tcp", fmt.Sprintf(":%d", port)) - var res types.MockServer - err = client.Call("Daemon.StartServer", types.MockServer{}, &res) - if err != nil { - log.Fatal("rpc error:", err) - } - - 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 TestRPCClient_StopServer(t *testing.T) { - daemon, manager := createMockedDaemon(true) - port, _ := utils.GetFreePort() - defer waitForDaemonToShutdown(port, daemon, t) - go daemon.StartDaemon(port, "tcp", "") - connectToDaemon(port, t) - - var cmd *exec.Cmd - for _, s := range manager.List() { - cmd = s - } - request := types.MockServer{ - Pid: cmd.Process.Pid, - } - - client, err := rpc.DialHTTP("tcp", fmt.Sprintf(":%d", port)) - var res *types.MockServer - err = client.Call("Daemon.StopServer", request, &res) - if err != nil { - log.Fatal("rpc error:", err) - } - - if res.Pid != cmd.Process.Pid { - t.Fatalf("Expected PID to match request %d but got: %d", cmd.Process.Pid, res.Pid) - } - - if res.Port != 0 { - t.Fatalf("Expected non-zero port but got: %d", res.Port) - } -} - -func TestRPCClient_StopDaemon(t *testing.T) { - daemon, _ := createMockedDaemon(true) - port, _ := utils.GetFreePort() - defer waitForDaemonToShutdown(port, daemon, t) - go daemon.StartDaemon(port, "tcp", "") - connectToDaemon(port, t) - - client, err := rpc.DialHTTP("tcp", fmt.Sprintf(":%d", port)) - var res string - err = client.Call("Daemon.StopDaemon", "", &res) - if err != nil { - log.Fatal("rpc error:", err) - } - - waitForDaemonToShutdown(port, nil, t) -} - -func TestRPCClient_Verify(t *testing.T) { - daemon, _ := createMockedDaemon(true) - port, _ := utils.GetFreePort() - defer waitForDaemonToShutdown(port, daemon, t) - go daemon.StartDaemon(port, "tcp", "") - connectToDaemon(port, t) - - client, err := rpc.DialHTTP("tcp", fmt.Sprintf(":%d", port)) - req := types.VerifyRequest{ - ProviderBaseURL: "http://foo.com", - PactURLs: []string{"foo.json", "bar.json"}, - } - res := types.ProviderVerifierResponse{} - - err = client.Call("Daemon.VerifyProvider", req, &res) - if err != nil { - log.Fatal("rpc error:", err) - } - - if got, want := res.SummaryLine, "1 examples, 0 failures"; got != want { - t.Fatalf("Expected a success message but got: '%s'", got) - } -} - -// 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, "") -} - -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) { - if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" { - return - } - <-time.After(250 * time.Millisecond) - - // some code here to check arguments perhaps? - // Fail :( - if os.Getenv("GO_WANT_HELPER_PROCESS_TO_SUCCEED") == "false" { - fmt.Fprintf(os.Stdout, "COMMAND: oh noes!") - os.Exit(1) - } - - // Success :) - fmt.Fprintf(os.Stdout, `{"summary_line":"1 examples, 0 failures"}`) - os.Exit(0) -} diff --git a/dsl/client.go b/dsl/client.go index 1bbe4e922..35116554c 100644 --- a/dsl/client.go +++ b/dsl/client.go @@ -15,29 +15,54 @@ import ( ) var ( - timeoutDuration = 10 * time.Second - commandStartServer = "Daemon.StartServer" - commandStopServer = "Daemon.StopServer" - commandVerifyProvider = "Daemon.VerifyProvider" - commandListServers = "Daemon.ListServers" - commandStopDaemon = "Daemon.StopDaemon" + timeoutDuration = 10 * time.Second ) -// Client is the simplified remote interface to the Pact Daemon. -type Client interface { - StartServer() *types.MockServer -} - // PactClient is the default implementation of the Client interface. type PactClient struct { - // Port the daemon is running on. - Port int - - // Network Daemon is listening on - Network string - // Address the Daemon is listening on Address string + + // Track mock servers + Servers []MockService + + // Track stub servers + // Stubs []StubService +} + +// StartServer starts a remote Pact Mock Server. +func (p *PactClient) StartServer(args []string, port int) *MockService { + log.Println("[DEBUG] client: starting a server") + + return nil +} + +// ListServers list all available Mock Servers +func (p *PactClient) ListServers(args []string, port int) []*types.MockServer { + log.Println("[DEBUG] client: starting a server") + + return nil +} + +// StopServer stops a remote Pact Mock Server. +func (p *PactClient) StopServer(server *types.MockServer) *types.MockServer { + log.Println("[DEBUG] client: stop server") + + return nil +} + +// RemoveAllServers stops all remote Pact Mock Servers. +func (p *PactClient) RemoveAllServers(server *types.MockServer) *[]types.MockServer { + log.Println("[DEBUG] client: stop server") + + return nil +} + +// VerifyProvider runs the verification process against a running Provider. +func (p *PactClient) VerifyProvider(request types.VerifyRequest) (types.ProviderVerifierResponse, error) { + log.Println("[DEBUG] client: verifying a provider") + + return types.ProviderVerifierResponse{}, nil } // Get a port given a URL @@ -89,49 +114,6 @@ var waitForPort = func(port int, network string, address string, message string) } } -// StartServer starts a remote Pact Mock Server. -func (p *PactClient) StartServer(args []string, port int) *types.MockServer { - log.Println("[DEBUG] client: starting a server") - var res types.MockServer - client, err := getHTTPClient(p.Port, p.getNetworkInterface(), p.Address) - if err == nil { - args = append(args, []string{"--port", strconv.Itoa(port)}...) - err = client.Call(commandStartServer, types.MockServer{Args: args}, &res) - res.Port = port - if err != nil { - log.Println("[ERROR] rpc:", err.Error()) - } - } - - if err == nil { - waitForPort(port, p.getNetworkInterface(), p.Address, fmt.Sprintf(`Timed out waiting for Mock Server to - start on port %d - are you sure it's running?`, port)) - } - - return &res -} - -// VerifyProvider runs the verification process against a running Provider. -func (p *PactClient) VerifyProvider(request types.VerifyRequest) (types.ProviderVerifierResponse, error) { - log.Println("[DEBUG] client: verifying a provider") - - port := getPort(request.ProviderBaseURL) - - waitForPort(port, p.getNetworkInterface(), p.Address, fmt.Sprintf(`Timed out waiting for Provider API to start - on port %d - are you sure it's running?`, port)) - - var res types.ProviderVerifierResponse - client, err := getHTTPClient(p.Port, p.getNetworkInterface(), p.Address) - if err == nil { - err = client.Call(commandVerifyProvider, request, &res) - if err != nil { - log.Println("[ERROR] rpc: ", err.Error()) - } - } - - return res, err -} - // sanitiseRubyResponse removes Ruby-isms from the response content // making the output much more human readable func sanitiseRubyResponse(response string) string { @@ -148,54 +130,3 @@ func sanitiseRubyResponse(response string) string { return s } - -// ListServers lists all running Pact Mock Servers. -func (p *PactClient) ListServers() *types.PactListResponse { - log.Println("[DEBUG] client: listing servers") - var res types.PactListResponse - client, err := getHTTPClient(p.Port, p.getNetworkInterface(), p.Address) - if err == nil { - err = client.Call(commandListServers, types.MockServer{}, &res) - if err != nil { - log.Println("[ERROR] rpc:", err.Error()) - } - } - return &res -} - -// StopServer stops a remote Pact Mock Server. -func (p *PactClient) StopServer(server *types.MockServer) *types.MockServer { - log.Println("[DEBUG] client: stop server") - var res types.MockServer - client, err := getHTTPClient(p.Port, p.getNetworkInterface(), p.Address) - if err == nil { - err = client.Call(commandStopServer, server, &res) - if err != nil { - log.Println("[ERROR] rpc:", err.Error()) - } - } - return &res -} - -// StopDaemon remotely shuts down the Pact Daemon. -func (p *PactClient) StopDaemon() error { - log.Println("[DEBUG] client: stop daemon") - var req, res string - client, err := getHTTPClient(p.Port, p.getNetworkInterface(), p.Address) - if err == nil { - err = client.Call(commandStopDaemon, &req, &res) - if err != nil { - log.Println("[ERROR] rpc:", err.Error()) - } - } - return err -} - -// getNetworkInterface returns a default interface to communicate to the Daemon -// if none specified -func (p *PactClient) getNetworkInterface() string { - if p.Network == "" { - return "tcp" - } - return p.Network -} diff --git a/examples/consumer/goconsumer/user_service_test.go b/examples/consumer/goconsumer/user_service_test.go index 6cedd2792..5706ce7a6 100644 --- a/examples/consumer/goconsumer/user_service_test.go +++ b/examples/consumer/goconsumer/user_service_test.go @@ -80,7 +80,7 @@ func setup() { // Login form values form = url.Values{} - form.Add("username", name) + form.Add("username") form.Add("password", "issilly") // Create a request to pass to our handler. diff --git a/types/list_response.go b/types/list_response.go deleted file mode 100644 index d4836045c..000000000 --- a/types/list_response.go +++ /dev/null @@ -1,7 +0,0 @@ -package types - -// PactListResponse contains a list of all running Servers. -type PactListResponse struct { - // System exit code from the Publish task. - Servers []*MockServer -}