diff options
| -rw-r--r-- | integrationtests/dcat_test.go | 3 | ||||
| -rw-r--r-- | integrationtests/dtail_test.go | 10 | ||||
| -rw-r--r-- | integrationtests/dtailhealth3.expected | 1 | ||||
| -rw-r--r-- | integrationtests/dtailhealth_test.go | 13 | ||||
| -rw-r--r-- | integrationtests/fileutils.go | 59 | ||||
| -rw-r--r-- | integrationtests/portnumber.go | 15 |
6 files changed, 68 insertions, 33 deletions
diff --git a/integrationtests/dcat_test.go b/integrationtests/dcat_test.go index 76b7c3b..777e835 100644 --- a/integrationtests/dcat_test.go +++ b/integrationtests/dcat_test.go @@ -47,7 +47,8 @@ func TestDCat2(t *testing.T) { args = append(args, testdataFile) } - if _, err := runCommand(context.TODO(), t, stdoutFile, "../dcat", args...); err != nil { + _, err := runCommand(context.TODO(), t, stdoutFile, "../dcat", args...) + if err != nil { t.Error(err) return } diff --git a/integrationtests/dtail_test.go b/integrationtests/dtail_test.go index 6814c42..e9cf257 100644 --- a/integrationtests/dtail_test.go +++ b/integrationtests/dtail_test.go @@ -17,7 +17,9 @@ func TestDTailWithServer(t *testing.T) { return } followFile := "dtail.follow.tmp" - greetings := []string{"world!", "sol-system!", "milky-way!", "universe!", "multiverse!"} + port := getUniquePortNumber() + bindAddress := "localhost" + greetings := []string{"World!", "Sol-System!", "Milky-Way!", "Universe!", "Multiverse!"} ctx, cancel := context.WithCancel(context.Background()) defer cancel() @@ -37,8 +39,8 @@ func TestDTailWithServer(t *testing.T) { "--cfg", "none", "--logger", "stdout", "--logLevel", "info", - "--bindAddress", "localhost", - "--port", "4243", + "--bindAddress", bindAddress, + "--port", fmt.Sprintf("%d", port), "--relaxedAuth", ) if err != nil { @@ -52,7 +54,7 @@ func TestDTailWithServer(t *testing.T) { "--cfg", "none", "--logger", "stdout", "--logLevel", "info", - "--servers", "localhost:4243", + "--servers", fmt.Sprintf("%s:%d", bindAddress, port), "--files", followFile, "--grep", "Hello", "--trustAllHosts", diff --git a/integrationtests/dtailhealth3.expected b/integrationtests/dtailhealth3.expected deleted file mode 100644 index 8e6dd57..0000000 --- a/integrationtests/dtailhealth3.expected +++ /dev/null @@ -1 +0,0 @@ -OK: All fine at localhost:4242 :-) diff --git a/integrationtests/dtailhealth_test.go b/integrationtests/dtailhealth_test.go index 0dc177c..271f11d 100644 --- a/integrationtests/dtailhealth_test.go +++ b/integrationtests/dtailhealth_test.go @@ -62,7 +62,9 @@ func TestDTailHealthCheck3(t *testing.T) { return } stdoutFile := "dtailhealth3.stdout.tmp" - expectedStdoutFile := "dtailhealth3.expected" + port := getUniquePortNumber() + bindAddress := "localhost" + expectedOut := fmt.Sprintf("OK: All fine at %s:%d :-)", bindAddress, port) ctx, cancel := context.WithCancel(context.Background()) defer cancel() @@ -72,23 +74,22 @@ func TestDTailHealthCheck3(t *testing.T) { "--cfg", "none", "--logger", "stdout", "--logLevel", "trace", - "--bindAddress", "localhost", - "--port", "4242", + "--bindAddress", bindAddress, + "--port", fmt.Sprintf("%d", port), ) if err != nil { t.Error(err) return } - // TODO: Get unique test port number. _, err = runCommandRetry(ctx, t, 10, stdoutFile, - "../dtailhealth", "--server", "localhost:4242") + "../dtailhealth", "--server", fmt.Sprintf("%s:%d", bindAddress, port)) if err != nil { t.Error(err) return } - if err := compareFiles(t, stdoutFile, expectedStdoutFile); err != nil { + if err := fileContainsStr(t, stdoutFile, expectedOut); err != nil { t.Error(err) return } diff --git a/integrationtests/fileutils.go b/integrationtests/fileutils.go index 1a55732..d13617d 100644 --- a/integrationtests/fileutils.go +++ b/integrationtests/fileutils.go @@ -12,28 +12,28 @@ import ( "testing" ) -// Checks whether both files have the same lines (order doesn't matter) -func compareFilesContents(t *testing.T, fileA, fileB string) error { - mapFile := func(file string) (map[string]int, error) { - t.Log("Reading", file) - contents := make(map[string]int) - fd, err := os.Open(file) - if err != nil { - return contents, err - } - defer fd.Close() - - scanner := bufio.NewScanner(fd) - scanner.Split(bufio.ScanLines) - for scanner.Scan() { - line := scanner.Text() - count, _ := contents[line] - contents[line] = count + 1 - } +func mapFile(t *testing.T, file string) (map[string]int, error) { + t.Log("Mapping", file) + contents := make(map[string]int) + fd, err := os.Open(file) + if err != nil { + return contents, err + } + defer fd.Close() - return contents, nil + scanner := bufio.NewScanner(fd) + scanner.Split(bufio.ScanLines) + for scanner.Scan() { + line := scanner.Text() + count, _ := contents[line] + contents[line] = count + 1 } + return contents, nil +} + +// Checks whether both files have the same lines (order doesn't matter) +func compareFilesContents(t *testing.T, fileA, fileB string) error { compareMaps := func(a, b map[string]int) error { for line, countA := range a { countB, ok := b[line] @@ -49,11 +49,11 @@ func compareFilesContents(t *testing.T, fileA, fileB string) error { } // Read files into maps. - a, err := mapFile(fileA) + a, err := mapFile(t, fileA) if err != nil { return err } - b, err := mapFile(fileB) + b, err := mapFile(t, fileB) if err != nil { return err } @@ -90,6 +90,23 @@ func compareFiles(t *testing.T, fileA, fileB string) error { return nil } +func fileContainsStr(t *testing.T, file, str string) error { + t.Log("Checking if file contains string", file, str) + m, err := mapFile(t, file) + if err != nil { + return err + } + + for line := range m { + if strings.Contains(line, str) { + t.Log(line) + return nil + } + } + + return fmt.Errorf("File %s does not contain string %s", file, str) +} + func shaOfFile(t *testing.T, file string) string { bytes, err := ioutil.ReadFile(file) if err != nil { diff --git a/integrationtests/portnumber.go b/integrationtests/portnumber.go new file mode 100644 index 0000000..94c2a11 --- /dev/null +++ b/integrationtests/portnumber.go @@ -0,0 +1,15 @@ +package integrationtests + +import "sync" + +var portNumberMutex sync.Mutex +var currentPortNumber int = 4241 + +// Go tests can run concurrently, so we need unique TCP port numbers for +// each test. +func getUniquePortNumber() int { + portNumberMutex.Lock() + defer portNumberMutex.Unlock() + currentPortNumber++ + return currentPortNumber +} |
