diff --git a/haproxy/haproxy_cmd/cmd.go b/haproxy/haproxy_cmd/cmd.go index effa545..c6af717 100644 --- a/haproxy/haproxy_cmd/cmd.go +++ b/haproxy/haproxy_cmd/cmd.go @@ -24,6 +24,10 @@ func runCommand(sd *lib.Shutdown, cmdPath string, args ...string) (*exec.Cmd, er sd.Done() return nil, errors.Wrapf(err, "error starting %s", file) } + if cmd.Process == nil { + sd.Done() + return nil, errors.Wrapf(err, "Process '%s' could not be started", file) + } exited := uint32(0) go func() { defer sd.Done() diff --git a/haproxy/haproxy_cmd/cmd_test.go b/haproxy/haproxy_cmd/cmd_test.go new file mode 100644 index 0000000..144a88f --- /dev/null +++ b/haproxy/haproxy_cmd/cmd_test.go @@ -0,0 +1,25 @@ +package haproxy_cmd + +import ( + "testing" + + "github.com/criteo/haproxy-consul-connect/lib" + "github.com/stretchr/testify/assert" +) + +func Test_runCommand_ok(t *testing.T) { + t.Parallel() + sd := lib.NewShutdown() + cmd, err := runCommand(sd, "ls", ".") + assert.NoError(t, err) + cmd.Wait() +} + +func Test_runCommand_nok_wrong_path(t *testing.T) { + t.Parallel() + sd := lib.NewShutdown() + cmd, err := runCommand(sd, "/path/to/nowhere/that/can/be/found/myExec", "--help") + assert.NotNil(t, err) + assert.Contains(t, err.Error(), "no such file or directory") + assert.Nil(t, cmd) +} diff --git a/haproxy/haproxy_cmd/run.go b/haproxy/haproxy_cmd/run.go index 02774ed..761e75e 100644 --- a/haproxy/haproxy_cmd/run.go +++ b/haproxy/haproxy_cmd/run.go @@ -4,6 +4,7 @@ import ( "fmt" "net" "net/http" + "os" "time" "github.com/criteo/haproxy-consul-connect/haproxy/dataplane" @@ -29,8 +30,11 @@ func Start(sd *lib.Shutdown, cfg Config) (*dataplane.Dataplane, error) { if err != nil { return nil, err } + if haCmd.Process == nil { + return nil, fmt.Errorf("%s was not started", cfg.HAProxyPath) + } - _, err = runCommand(sd, + cmd, err := runCommand(sd, cfg.DataplanePath, "--scheme", "unix", "--socket-path", cfg.DataplaneSock, @@ -41,9 +45,17 @@ func Start(sd *lib.Shutdown, cfg Config) (*dataplane.Dataplane, error) { "--userlist", "controller", "--transaction-dir", cfg.DataplaneTransactionDir, ) + cleanupHAProxy := func() { + haCmd.Process.Signal(os.Kill) + } if err != nil { + cleanupHAProxy() return nil, err } + if cmd.Process == nil { + cleanupHAProxy() + return nil, fmt.Errorf("%s failed to start", cfg.DataplanePath) + } dataplaneClient := dataplane.New( "http://unix-sock",