diff options
| author | Paul Buetow <paul@buetow.org> | 2021-11-02 09:15:43 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2021-11-02 09:15:43 +0200 |
| commit | 479a1c1bff839f40d980fdbedcab80e2e73aeb58 (patch) | |
| tree | b3f92443b1e2641b807a5976504e9d49f8d2850e | |
| parent | efeba5233c99a88dc1a49e44673ae56709814624 (diff) | |
Refactor integration tests. Also fix the dmap1 PIPE test
| -rw-r--r-- | integrationtests/commandutils.go | 23 | ||||
| -rw-r--r-- | integrationtests/dgrep1.txt.expected (renamed from integrationtests/dgrep.txt.expected) | 0 | ||||
| -rw-r--r-- | integrationtests/dgrep_test.go | 44 | ||||
| -rw-r--r-- | integrationtests/dgrepcontext1.txt.expected (renamed from integrationtests/dgrepcontext.txt.expected) | 0 | ||||
| -rw-r--r-- | integrationtests/dmap1.csv.expected (renamed from integrationtests/dmap.csv.expected) | 0 | ||||
| -rw-r--r-- | integrationtests/dmap1.csv.query.expected (renamed from integrationtests/dserver.csv.query.expected) | 2 | ||||
| -rw-r--r-- | integrationtests/dmap_test.go | 28 | ||||
| -rw-r--r-- | integrationtests/dserver1.cfg (renamed from integrationtests/dserver.cfg) | 2 | ||||
| -rw-r--r-- | integrationtests/dserver1.csv.expected (renamed from integrationtests/dserver.csv.expected) | 0 | ||||
| -rw-r--r-- | integrationtests/dserver1.csv.query.expected (renamed from integrationtests/dmap.csv.query.expected) | 2 | ||||
| -rw-r--r-- | integrationtests/dserver_test.go | 10 | ||||
| -rw-r--r-- | integrationtests/dtail_test.go | 10 | ||||
| -rw-r--r-- | integrationtests/dtailhealth1.expected (renamed from integrationtests/dtailhealth.expected) | 0 | ||||
| -rw-r--r-- | integrationtests/dtailhealth_test.go | 32 |
14 files changed, 81 insertions, 72 deletions
diff --git a/integrationtests/commandutils.go b/integrationtests/commandutils.go index af898ab..959288a 100644 --- a/integrationtests/commandutils.go +++ b/integrationtests/commandutils.go @@ -62,12 +62,22 @@ func startCommand(ctx context.Context, t *testing.T, inPipeFile, t.Log(cmdStr, strings.Join(args, " ")) cmd := exec.CommandContext(ctx, cmdStr, args...) + var stdinPipe io.WriteCloser + if inPipeFile != "" { + var err error + stdinPipe, err = cmd.StdinPipe() + if err != nil { + return stdoutCh, stderrCh, nil, err + } + } cmdStdout, err := cmd.StdoutPipe() if err != nil { return stdoutCh, stderrCh, nil, err } - cmdStderr, err := cmd.StderrPipe() + if err != nil { + return stdoutCh, stderrCh, nil, err + } err = cmd.Start() if err != nil { return stdoutCh, stderrCh, nil, err @@ -76,16 +86,15 @@ func startCommand(ctx context.Context, t *testing.T, inPipeFile, // Read input file and send to stdin pipe? if inPipeFile != "" { t.Log(fmt.Sprintf("Piping %s to stdin pipe", inPipeFile)) - stdinPipe, err := cmd.StdinPipe() - if err != nil { - return stdoutCh, stderrCh, nil, err - } fd, err := os.Open(inPipeFile) if err != nil { return stdoutCh, stderrCh, nil, err } - defer fd.Close() - go io.Copy(stdinPipe, bufio.NewReader(fd)) + go func() { + io.Copy(stdinPipe, bufio.NewReader(fd)) + stdinPipe.Close() + fd.Close() + }() } go func() { diff --git a/integrationtests/dgrep.txt.expected b/integrationtests/dgrep1.txt.expected index d5df9c1..d5df9c1 100644 --- a/integrationtests/dgrep.txt.expected +++ b/integrationtests/dgrep1.txt.expected diff --git a/integrationtests/dgrep_test.go b/integrationtests/dgrep_test.go index 5d68ca9..ab91dd6 100644 --- a/integrationtests/dgrep_test.go +++ b/integrationtests/dgrep_test.go @@ -8,16 +8,16 @@ import ( "github.com/mimecast/dtail/internal/config" ) -func TestDGrep(t *testing.T) { +func TestDGrep1(t *testing.T) { if !config.Env("DTAIL_INTEGRATION_TEST_RUN_MODE") { t.Log("Skipping") return } inFile := "mapr_testdata.log" - stdoutFile := "dgrep.stdout.tmp" - expectedStdoutFile := "dgrep.txt.expected" + outFile := "dgrep.stdout.tmp" + expectedOutFile := "dgrep1.txt.expected" - _, err := runCommand(context.TODO(), t, stdoutFile, + _, err := runCommand(context.TODO(), t, outFile, "../dgrep", "--plain", "--cfg", "none", @@ -29,12 +29,12 @@ func TestDGrep(t *testing.T) { return } - if err := compareFiles(t, stdoutFile, expectedStdoutFile); err != nil { + if err := compareFiles(t, outFile, expectedOutFile); err != nil { t.Error(err) return } - os.Remove(stdoutFile) + os.Remove(outFile) } func TestDGrep2(t *testing.T) { @@ -43,10 +43,10 @@ func TestDGrep2(t *testing.T) { return } inFile := "mapr_testdata.log" - stdoutFile := "dgrep2.stdout.tmp" - expectedStdoutFile := "dgrep2.txt.expected" + outFile := "dgrep2.stdout.tmp" + expectedOutFile := "dgrep2.txt.expected" - _, err := runCommand(context.TODO(), t, stdoutFile, + _, err := runCommand(context.TODO(), t, outFile, "../dgrep", "--plain", "--cfg", "none", @@ -59,24 +59,24 @@ func TestDGrep2(t *testing.T) { return } - if err := compareFiles(t, stdoutFile, expectedStdoutFile); err != nil { + if err := compareFiles(t, outFile, expectedOutFile); err != nil { t.Error(err) return } - os.Remove(stdoutFile) + os.Remove(outFile) } -func TestDGrepContext(t *testing.T) { +func TestDGrepContext1(t *testing.T) { if !config.Env("DTAIL_INTEGRATION_TEST_RUN_MODE") { t.Log("Skipping") return } inFile := "mapr_testdata.log" - stdoutFile := "dgrepcontext.stdout.tmp" - expectedStdoutFile := "dgrepcontext.txt.expected" + outFile := "dgrepcontext1.stdout.tmp" + expectedOutFile := "dgrepcontext1.txt.expected" - _, err := runCommand(context.TODO(), t, stdoutFile, + _, err := runCommand(context.TODO(), t, outFile, "../dgrep", "--plain", "--cfg", "none", @@ -89,12 +89,12 @@ func TestDGrepContext(t *testing.T) { return } - if err := compareFiles(t, stdoutFile, expectedStdoutFile); err != nil { + if err := compareFiles(t, outFile, expectedOutFile); err != nil { t.Error(err) return } - os.Remove(stdoutFile) + os.Remove(outFile) } func TestDGrepContext2(t *testing.T) { @@ -103,10 +103,10 @@ func TestDGrepContext2(t *testing.T) { return } inFile := "mapr_testdata.log" - stdoutFile := "dgrepcontext2.stdout.tmp" - expectedStdoutFile := "dgrepcontext2.txt.expected" + outFile := "dgrepcontext2.stdout.tmp" + expectedOutFile := "dgrepcontext2.txt.expected" - _, err := runCommand(context.TODO(), t, stdoutFile, + _, err := runCommand(context.TODO(), t, outFile, "../dgrep", "--plain", "--cfg", "none", @@ -119,10 +119,10 @@ func TestDGrepContext2(t *testing.T) { return } - if err := compareFiles(t, stdoutFile, expectedStdoutFile); err != nil { + if err := compareFiles(t, outFile, expectedOutFile); err != nil { t.Error(err) return } - os.Remove(stdoutFile) + os.Remove(outFile) } diff --git a/integrationtests/dgrepcontext.txt.expected b/integrationtests/dgrepcontext1.txt.expected index ad0ae1f..ad0ae1f 100644 --- a/integrationtests/dgrepcontext.txt.expected +++ b/integrationtests/dgrepcontext1.txt.expected diff --git a/integrationtests/dmap.csv.expected b/integrationtests/dmap1.csv.expected index d4e6f0f..d4e6f0f 100644 --- a/integrationtests/dmap.csv.expected +++ b/integrationtests/dmap1.csv.expected diff --git a/integrationtests/dserver.csv.query.expected b/integrationtests/dmap1.csv.query.expected index b45c588..f47670a 100644 --- a/integrationtests/dserver.csv.query.expected +++ b/integrationtests/dmap1.csv.query.expected @@ -1 +1 @@ -from STATS select count($line),last($time),avg($goroutines),min(concurrentConnections),max(lifetimeConnections) group by $hostname outfile ./dserver.csv
\ No newline at end of file +from STATS select count($line),last($time),avg($goroutines),min(concurrentConnections),max(lifetimeConnections) group by $hostname outfile dmap1.csv.tmp
\ No newline at end of file diff --git a/integrationtests/dmap_test.go b/integrationtests/dmap_test.go index c60a828..5e5f4d3 100644 --- a/integrationtests/dmap_test.go +++ b/integrationtests/dmap_test.go @@ -9,30 +9,30 @@ import ( "github.com/mimecast/dtail/internal/config" ) -func TestDMap(t *testing.T) { +func TestDMap1(t *testing.T) { if !config.Env("DTAIL_INTEGRATION_TEST_RUN_MODE") { t.Log("Skipping") return } t.Log("Testing dmap with input file") - if err := testDmap(t, false); err != nil { - t.Log(err) + if err := testDmap1(t, false); err != nil { + t.Error(err) return } t.Log("Testing dmap with stdin input pipe") - if err := testDmap(t, true); err != nil { - t.Log(err) + if err := testDmap1(t, true); err != nil { + t.Error(err) return } } -func testDmap(t *testing.T, usePipe bool) error { +func testDmap1(t *testing.T, usePipe bool) error { inFile := "mapr_testdata.log" - csvFile := "dmap.csv.tmp" - expectedCsvFile := "dmap.csv.expected" + csvFile := "dmap1.csv.tmp" + expectedCsvFile := "dmap1.csv.expected" queryFile := fmt.Sprintf("%s.query", csvFile) - expectedQueryFile := "dmap.csv.query.expected" + expectedQueryFile := "dmap1.csv.query.expected" query := fmt.Sprintf("from STATS select count($line),last($time),"+ "avg($goroutines),min(concurrentConnections),max(lifetimeConnections) "+ @@ -88,7 +88,7 @@ func TestDMap2(t *testing.T) { return } inFile := "mapr_testdata.log" - stdoutFile := "dmap2.stdout.tmp" + outFile := "dmap2.stdout.tmp" csvFile := "dmap2.csv.tmp" expectedCsvFile := "dmap2.csv.expected" queryFile := fmt.Sprintf("%s.query", csvFile) @@ -98,7 +98,7 @@ func TestDMap2(t *testing.T) { "avg($goroutines),min($goroutines) group by $time order by count($time) "+ "outfile %s", csvFile) - _, err := runCommand(context.TODO(), t, stdoutFile, + _, err := runCommand(context.TODO(), t, outFile, "../dmap", "--query", query, "--cfg", "none", inFile) if err != nil { t.Error(err) @@ -114,7 +114,7 @@ func TestDMap2(t *testing.T) { return } - os.Remove(stdoutFile) + os.Remove(outFile) os.Remove(csvFile) os.Remove(queryFile) } @@ -125,7 +125,7 @@ func TestDMap3(t *testing.T) { return } inFile := "mapr_testdata.log" - stdoutFile := "dmap3.stdout.tmp" + outFile := "dmap3.stdout.tmp" csvFile := "dmap3.csv.tmp" expectedCsvFile := "dmap3.csv.expected" queryFile := fmt.Sprintf("%s.query", csvFile) @@ -171,7 +171,7 @@ func TestDMap3(t *testing.T) { return } - os.Remove(stdoutFile) + os.Remove(outFile) os.Remove(csvFile) os.Remove(queryFile) } diff --git a/integrationtests/dserver.cfg b/integrationtests/dserver1.cfg index 2092b96..60a5dd1 100644 --- a/integrationtests/dserver.cfg +++ b/integrationtests/dserver1.cfg @@ -13,7 +13,7 @@ ], "Files": "./mapr_testdata.log", "Query": "from STATS select count($line),last($time),avg($goroutines),min(concurrentConnections),max(lifetimeConnections) group by $hostname", - "Outfile": "./dserver.csv" + "Outfile": "./dserver1.csv" } ] } diff --git a/integrationtests/dserver.csv.expected b/integrationtests/dserver1.csv.expected index d4e6f0f..d4e6f0f 100644 --- a/integrationtests/dserver.csv.expected +++ b/integrationtests/dserver1.csv.expected diff --git a/integrationtests/dmap.csv.query.expected b/integrationtests/dserver1.csv.query.expected index 2bb2a52..61cb46d 100644 --- a/integrationtests/dmap.csv.query.expected +++ b/integrationtests/dserver1.csv.query.expected @@ -1 +1 @@ -from STATS select count($line),last($time),avg($goroutines),min(concurrentConnections),max(lifetimeConnections) group by $hostname outfile dmap.csv.tmp
\ No newline at end of file +from STATS select count($line),last($time),avg($goroutines),min(concurrentConnections),max(lifetimeConnections) group by $hostname outfile ./dserver1.csv
\ No newline at end of file diff --git a/integrationtests/dserver_test.go b/integrationtests/dserver_test.go index 27ce773..a2f20da 100644 --- a/integrationtests/dserver_test.go +++ b/integrationtests/dserver_test.go @@ -11,24 +11,24 @@ import ( "github.com/mimecast/dtail/internal/config" ) -func TestDServer(t *testing.T) { +func TestDServer1(t *testing.T) { if !config.Env("DTAIL_INTEGRATION_TEST_RUN_MODE") { t.Log("Skipping") return } // Testing a scheduled query. - csvFile := "dserver.csv" - expectedCsvFile := "dserver.csv.expected" + csvFile := "dserver1.csv" + expectedCsvFile := "dserver1.csv.expected" queryFile := fmt.Sprintf("%s.query", csvFile) - expectedQueryFile := "dserver.csv.query.expected" + expectedQueryFile := "dserver1.csv.query.expected" ctx, cancel := context.WithCancel(context.Background()) defer cancel() stdoutCh, stderrCh, cmdErrCh, err := startCommand(ctx, t, "", "../dserver", - "--cfg", "dserver.cfg", + "--cfg", "dserver1.cfg", "--logger", "stdout", "--logLevel", "info", "--bindAddress", "localhost", diff --git a/integrationtests/dtail_test.go b/integrationtests/dtail_test.go index 0082843..64a32f1 100644 --- a/integrationtests/dtail_test.go +++ b/integrationtests/dtail_test.go @@ -134,17 +134,17 @@ func TestDTailColorTable(t *testing.T) { t.Log("Skipping") return } - stdoutFile := "dtailcolortable.stdout.tmp" - expectedStdoutFile := "dtailcolortable.expected" + outFile := "dtailcolortable.stdout.tmp" + expectedOutFile := "dtailcolortable.expected" - _, err := runCommand(context.TODO(), t, stdoutFile, "../dtail", "--colorTable") + _, err := runCommand(context.TODO(), t, outFile, "../dtail", "--colorTable") if err != nil { t.Error(err) return } - if err := compareFiles(t, stdoutFile, expectedStdoutFile); err != nil { + if err := compareFiles(t, outFile, expectedOutFile); err != nil { t.Error(err) return } - os.Remove(stdoutFile) + os.Remove(outFile) } diff --git a/integrationtests/dtailhealth.expected b/integrationtests/dtailhealth1.expected index 7bf393c..7bf393c 100644 --- a/integrationtests/dtailhealth.expected +++ b/integrationtests/dtailhealth1.expected diff --git a/integrationtests/dtailhealth_test.go b/integrationtests/dtailhealth_test.go index 0dd15e8..49fbd38 100644 --- a/integrationtests/dtailhealth_test.go +++ b/integrationtests/dtailhealth_test.go @@ -9,38 +9,38 @@ import ( "github.com/mimecast/dtail/internal/config" ) -func TestDTailHealthCheck(t *testing.T) { +func TestDTailHealth1(t *testing.T) { if !config.Env("DTAIL_INTEGRATION_TEST_RUN_MODE") { t.Log("Skipping") return } - stdoutFile := "dtailhealth.stdout.tmp" - expectedStdoutFile := "dtailhealth.expected" + outFile := "dtailhealth1.stdout.tmp" + expectedOutFile := "dtailhealth1.expected" t.Log("Serverless check, is supposed to exit with warning state.") - exitCode, err := runCommand(context.TODO(), t, stdoutFile, "../dtailhealth") + exitCode, err := runCommand(context.TODO(), t, outFile, "../dtailhealth") 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 { + if err := compareFiles(t, outFile, expectedOutFile); err != nil { t.Error(err) return } - os.Remove(stdoutFile) + os.Remove(outFile) } -func TestDTailHealthCheck2(t *testing.T) { +func TestDTailHealth2(t *testing.T) { if !config.Env("DTAIL_INTEGRATION_TEST_RUN_MODE") { t.Log("Skipping") return } - stdoutFile := "dtailhealth2.stdout.tmp" - expectedStdoutFile := "dtailhealth2.expected" + outFile := "dtailhealth2.stdout.tmp" + expectedOutFile := "dtailhealth2.expected" t.Log("Negative test, is supposed to exit with a critical state.") - exitCode, err := runCommand(context.TODO(), t, stdoutFile, + exitCode, err := runCommand(context.TODO(), t, outFile, "../dtailhealth", "--server", "example:1") if exitCode != 2 { @@ -48,12 +48,12 @@ func TestDTailHealthCheck2(t *testing.T) { return } - if err := compareFiles(t, stdoutFile, expectedStdoutFile); err != nil { + if err := compareFiles(t, outFile, expectedOutFile); err != nil { t.Error(err) return } - os.Remove(stdoutFile) + os.Remove(outFile) } func TestDTailHealthCheck3(t *testing.T) { @@ -61,7 +61,7 @@ func TestDTailHealthCheck3(t *testing.T) { t.Log("Skipping") return } - stdoutFile := "dtailhealth3.stdout.tmp" + outFile := "dtailhealth3.stdout.tmp" port := getUniquePortNumber() bindAddress := "localhost" expectedOut := fmt.Sprintf("OK: All fine at %s:%d :-)", bindAddress, port) @@ -82,17 +82,17 @@ func TestDTailHealthCheck3(t *testing.T) { return } - _, err = runCommandRetry(ctx, t, 10, stdoutFile, + _, err = runCommandRetry(ctx, t, 10, outFile, "../dtailhealth", "--server", fmt.Sprintf("%s:%d", bindAddress, port)) if err != nil { t.Error(err) return } - if err := fileContainsStr(t, stdoutFile, expectedOut); err != nil { + if err := fileContainsStr(t, outFile, expectedOut); err != nil { t.Error(err) return } - os.Remove(stdoutFile) + os.Remove(outFile) } |
