From 7a7169791a64190e1002e38bc9c04ad0d5c1ce1f Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Sat, 9 Oct 2021 16:44:28 +0300 Subject: add dtail health check unit test. --- integrationtests/commons.go | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) (limited to 'integrationtests/commons.go') diff --git a/integrationtests/commons.go b/integrationtests/commons.go index f789322..f96b532 100644 --- a/integrationtests/commons.go +++ b/integrationtests/commons.go @@ -2,6 +2,7 @@ package integrationtests import ( "bufio" + "context" "crypto/sha256" "encoding/base64" "fmt" @@ -9,29 +10,41 @@ import ( "os" "os/exec" "strings" + "syscall" "testing" ) -func runCommand(t *testing.T, cmd string, args []string, stdoutFile string) error { +func runCommand(t *testing.T, cmd string, args []string, stdoutFile string) (int, error) { + return runCommandContext(t, context.TODO(), cmd, args, stdoutFile) +} + +func runCommandContext(t *testing.T, ctx context.Context, cmd string, args []string, stdoutFile string) (int, error) { if _, err := os.Stat(cmd); err != nil { - return fmt.Errorf("No such binary %s, please compile first (%v)", cmd, err) + return -1, fmt.Errorf("No such binary %s, please compile first (%v)", cmd, err) } - t.Log("Executing command:", cmd, strings.Join(args, " ")) - bytes, err := exec.Command(cmd, args...).Output() - if err != nil { - return err - } + t.Log("Running command:", cmd, strings.Join(args, " ")) + bytes, cmdErr := exec.CommandContext(ctx, cmd, args...).Output() t.Log("Writing stdout to file", stdoutFile) fd, err := os.Create(stdoutFile) if err != nil { - return err + return -1, err } + defer fd.Close() fd.Write(bytes) - fd.Close() - return nil + return exitCodeFromError(cmdErr), err +} + +func exitCodeFromError(err error) int { + if err != nil { + if exitError, ok := err.(*exec.ExitError); ok { + ws := exitError.Sys().(syscall.WaitStatus) + return ws.ExitStatus() + } + } + return 0 } // Checks whether both files have the same lines (order doesn't matter) -- cgit v1.2.3