diff options
| author | Paul Buetow <paul@buetow.org> | 2021-10-09 16:44:28 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2021-10-10 13:36:32 +0300 |
| commit | 7a7169791a64190e1002e38bc9c04ad0d5c1ce1f (patch) | |
| tree | 2c1aa056285b3e5d4febefd114a4b95f62071386 /integrationtests/commons.go | |
| parent | 2d7ddbeae8286373ac19787dc7dde598a7cb0598 (diff) | |
add dtail health check unit test.
Diffstat (limited to 'integrationtests/commons.go')
| -rw-r--r-- | integrationtests/commons.go | 33 |
1 files changed, 23 insertions, 10 deletions
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) |
