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 | |
| parent | 2d7ddbeae8286373ac19787dc7dde598a7cb0598 (diff) | |
add dtail health check unit test.
Diffstat (limited to 'integrationtests')
| -rw-r--r-- | integrationtests/commons.go | 33 | ||||
| -rw-r--r-- | integrationtests/dcat_test.go | 2 | ||||
| -rw-r--r-- | integrationtests/dgrep_test.go | 4 | ||||
| -rw-r--r-- | integrationtests/dmap_test.go | 4 | ||||
| -rw-r--r-- | integrationtests/dtail_test.go | 2 | ||||
| -rw-r--r-- | integrationtests/dtailhealthcheck.expected | 1 | ||||
| -rw-r--r-- | integrationtests/dtailhealthcheck2.expected | 1 | ||||
| -rw-r--r-- | integrationtests/dtailhealthcheck3.expected | 1 | ||||
| -rw-r--r-- | integrationtests/dtailhealthcheck_test.go | 83 |
9 files changed, 115 insertions, 16 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) diff --git a/integrationtests/dcat_test.go b/integrationtests/dcat_test.go index 4394552..a164960 100644 --- a/integrationtests/dcat_test.go +++ b/integrationtests/dcat_test.go @@ -9,7 +9,7 @@ func TestDCat(t *testing.T) { testdataFile := "dcat.txt.expected" stdoutFile := "dcat.out" - if err := runCommand(t, "../dcat", []string{"-spartan", testdataFile}, stdoutFile); err != nil { + if _, err := runCommand(t, "../dcat", []string{"-spartan", testdataFile}, stdoutFile); err != nil { t.Error(err) return } diff --git a/integrationtests/dgrep_test.go b/integrationtests/dgrep_test.go index 32c0ace..6a15ebd 100644 --- a/integrationtests/dgrep_test.go +++ b/integrationtests/dgrep_test.go @@ -10,7 +10,7 @@ func TestDGrep(t *testing.T) { stdoutFile := "dgrep.stdout.tmp" expectedStdoutFile := "dgrep.txt.expected" - if err := runCommand(t, "../dgrep", []string{"-spartan", "--grep", "20211002-071947", inFile}, stdoutFile); err != nil { + if _, err := runCommand(t, "../dgrep", []string{"-spartan", "--grep", "20211002-071947", inFile}, stdoutFile); err != nil { t.Error(err) return } @@ -27,7 +27,7 @@ func TestDGrep2(t *testing.T) { stdoutFile := "dgrep2.stdout.tmp" expectedStdoutFile := "dgrep2.txt.expected" - if err := runCommand(t, "../dgrep", []string{"-spartan", "--grep", "20211002-071947", "--invert", inFile}, stdoutFile); err != nil { + if _, err := runCommand(t, "../dgrep", []string{"-spartan", "--grep", "20211002-071947", "--invert", inFile}, stdoutFile); err != nil { t.Error(err) return } diff --git a/integrationtests/dmap_test.go b/integrationtests/dmap_test.go index dc508e2..b512985 100644 --- a/integrationtests/dmap_test.go +++ b/integrationtests/dmap_test.go @@ -16,7 +16,7 @@ func TestDMap(t *testing.T) { query := fmt.Sprintf("from STATS select count($line),last($time),avg($goroutines),min(concurrentConnections),max(lifetimeConnections) group by $hostname outfile %s", csvFile) - if err := runCommand(t, "../dmap", []string{"-query", query, inFile}, stdoutFile); err != nil { + if _, err := runCommand(t, "../dmap", []string{"-query", query, inFile}, stdoutFile); err != nil { t.Error(err) return } @@ -44,7 +44,7 @@ func TestDMap2(t *testing.T) { query := fmt.Sprintf("from STATS select count($time),$time,max($goroutines),avg($goroutines),min($goroutines) group by $time order by count($time) outfile %s", csvFile) - if err := runCommand(t, "../dmap", []string{"-query", query, inFile}, stdoutFile); err != nil { + if _, err := runCommand(t, "../dmap", []string{"-query", query, inFile}, stdoutFile); err != nil { t.Error(err) return } diff --git a/integrationtests/dtail_test.go b/integrationtests/dtail_test.go index 8d73174..9971f1a 100644 --- a/integrationtests/dtail_test.go +++ b/integrationtests/dtail_test.go @@ -9,7 +9,7 @@ func TestDTailColorTable(t *testing.T) { stdoutFile := "dtailcolortable.stdout.tmp" expectedStdoutFile := "dtailcolortable.expected" - if err := runCommand(t, "../dtail", []string{"-colorTable"}, stdoutFile); err != nil { + if _, err := runCommand(t, "../dtail", []string{"-colorTable"}, stdoutFile); err != nil { t.Error(err) return } diff --git a/integrationtests/dtailhealthcheck.expected b/integrationtests/dtailhealthcheck.expected new file mode 100644 index 0000000..7bf393c --- /dev/null +++ b/integrationtests/dtailhealthcheck.expected @@ -0,0 +1 @@ +WARNING: All seems fine but the check only run in serverless mode, please specify a remote server via --server hostname:port diff --git a/integrationtests/dtailhealthcheck2.expected b/integrationtests/dtailhealthcheck2.expected new file mode 100644 index 0000000..3dd84d8 --- /dev/null +++ b/integrationtests/dtailhealthcheck2.expected @@ -0,0 +1 @@ +CRITICAL: DTail server not operating properly at example:1! diff --git a/integrationtests/dtailhealthcheck3.expected b/integrationtests/dtailhealthcheck3.expected new file mode 100644 index 0000000..8e6dd57 --- /dev/null +++ b/integrationtests/dtailhealthcheck3.expected @@ -0,0 +1 @@ +OK: All fine at localhost:4242 :-) diff --git a/integrationtests/dtailhealthcheck_test.go b/integrationtests/dtailhealthcheck_test.go new file mode 100644 index 0000000..97fa5f2 --- /dev/null +++ b/integrationtests/dtailhealthcheck_test.go @@ -0,0 +1,83 @@ +package integrationtests + +import ( + "context" + "fmt" + "os" + "testing" + "time" +) + +func TestDTailHealthCheck(t *testing.T) { + stdoutFile := "dtailhealthcheck.stdout.tmp" + expectedStdoutFile := "dtailhealthcheck.expected" + + t.Log("Serverless check, is supposed to exit with warning state.") + exitCode, err := runCommand(t, "../dtailhealthcheck", []string{}, stdoutFile) + if exitCode != 1 { + t.Error(fmt.Sprintf("Expected exit code '1' but got '%d': %v", exitCode, err)) + return + } + + if err := compareFiles(t, stdoutFile, expectedStdoutFile); err != nil { + t.Error(err) + return + } + + os.Remove(stdoutFile) +} + +func TestDTailHealthCheck2(t *testing.T) { + stdoutFile := "dtailhealthcheck2.stdout.tmp" + expectedStdoutFile := "dtailhealthcheck2.expected" + + t.Log("Negative test, is supposed to exit with a critical state.") + exitCode, err := runCommand(t, "../dtailhealthcheck", []string{"--server", "example:1"}, stdoutFile) + if exitCode != 2 { + t.Error(fmt.Sprintf("Expected exit code '2' but got '%d': %v", exitCode, err)) + return + } + + if err := compareFiles(t, stdoutFile, expectedStdoutFile); err != nil { + t.Error(err) + return + } + + os.Remove(stdoutFile) +} + +func TestDTailHealthCheck3(t *testing.T) { + stdoutFile := "dtailhealthcheck3.stdout.tmp" + serverStdoutFile := "dtailhealthcheck3.dserver.stdout.tmp" + expectedStdoutFile := "dtailhealthcheck3.expected" + + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + go func() { + serverArgs := []string{"--logger", "stdout", "--logLevel", "trace", "--port", "4242"} + runCommandContext(t, ctx, "../dserver", serverArgs, serverStdoutFile) + }() + + var err error + for i := 0; i < 30; i++ { + t.Log("Waiting for dserver to start", i) + time.Sleep(time.Second) + var exitCode int + if exitCode, err = runCommand(t, "../dtailhealthcheck", []string{"--server", "localhost:4242"}, stdoutFile); exitCode == 0 { + break + } + } + if err != nil { + t.Error(err) + return + } + + if err := compareFiles(t, stdoutFile, expectedStdoutFile); err != nil { + t.Error(err) + return + } + + os.Remove(serverStdoutFile) + os.Remove(stdoutFile) +} |
