From 71f89dc7ec7cf993d1eca98771212afe6310e9c8 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Sun, 10 Oct 2021 19:42:48 +0300 Subject: refactor --- integrationtests/commandutils.go | 112 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 integrationtests/commandutils.go (limited to 'integrationtests/commandutils.go') diff --git a/integrationtests/commandutils.go b/integrationtests/commandutils.go new file mode 100644 index 0000000..d2f567f --- /dev/null +++ b/integrationtests/commandutils.go @@ -0,0 +1,112 @@ +package integrationtests + +import ( + "bufio" + "context" + "fmt" + "os" + "os/exec" + "sync" + "syscall" + "time" +) + +// The exit code and the Go error of the command terminated. +type exitPromise func() (int, error) + +func runCommand(ctx context.Context, stdoutFile, cmdStr string, + args ...string) (int, error) { + + stdinCh, _, exit, err := startCommand(ctx, cmdStr, args...) + if err != nil { + return -1, err + } + + fd, err := os.Create(stdoutFile) + if err != nil { + return -2, err + } + + var wg sync.WaitGroup + wg.Add(1) + defer wg.Wait() + + go func() { + defer fd.Close() + defer wg.Done() + for line := range stdinCh { + fd.WriteString(line) + fd.WriteString("\n") + } + }() + + return exit() +} + +func runCommandRetry(ctx context.Context, retries int, stdoutFile, cmd string, + args ...string) (exitCode int, err error) { + + for i := 0; i < retries; i++ { + time.Sleep(time.Second) + if exitCode, err = runCommand(ctx, stdoutFile, cmd, args...); exitCode == 0 { + return + } + } + return +} + +func startCommand(ctx context.Context, cmdStr string, + args ...string) (<-chan string, <-chan string, exitPromise, error) { + + stdoutCh := make(chan string) + stderrCh := make(chan string) + + if _, err := os.Stat(cmdStr); err != nil { + return stdoutCh, stderrCh, nil, + fmt.Errorf("no such executable '%s', please compile first: %v", cmdStr, err) + } + + cmd := exec.CommandContext(ctx, cmdStr, args...) + + cmdStdout, err := cmd.StdoutPipe() + if err != nil { + return stdoutCh, stderrCh, nil, err + } + cmdStderr, err := cmd.StderrPipe() + err = cmd.Start() + if err != nil { + return stdoutCh, stderrCh, nil, err + } + + go func() { + defer close(stdoutCh) + scanner := bufio.NewScanner(cmdStdout) + scanner.Split(bufio.ScanLines) + for scanner.Scan() { + stdoutCh <- scanner.Text() + } + }() + go func() { + close(stderrCh) + scanner := bufio.NewScanner(cmdStderr) + scanner.Split(bufio.ScanLines) + for scanner.Scan() { + stderrCh <- scanner.Text() + } + }() + + return stdoutCh, stderrCh, func() (int, error) { + err := cmd.Wait() + return exitCodeFromError(err), err + }, nil +} + +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 +} -- cgit v1.2.3 From 7b873100d94ddc3c698a620cb83b61dcb2074303 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Wed, 13 Oct 2021 09:00:03 +0300 Subject: add another dcat integration test - catting 100 files at once --- integrationtests/commandutils.go | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) (limited to 'integrationtests/commandutils.go') diff --git a/integrationtests/commandutils.go b/integrationtests/commandutils.go index d2f567f..82c0017 100644 --- a/integrationtests/commandutils.go +++ b/integrationtests/commandutils.go @@ -6,18 +6,20 @@ import ( "fmt" "os" "os/exec" + "strings" "sync" "syscall" + "testing" "time" ) // The exit code and the Go error of the command terminated. type exitPromise func() (int, error) -func runCommand(ctx context.Context, stdoutFile, cmdStr string, +func runCommand(ctx context.Context, t *testing.T, stdoutFile, cmdStr string, args ...string) (int, error) { - stdinCh, _, exit, err := startCommand(ctx, cmdStr, args...) + stdinCh, _, exit, err := startCommand(ctx, t, cmdStr, args...) if err != nil { return -1, err } @@ -43,19 +45,19 @@ func runCommand(ctx context.Context, stdoutFile, cmdStr string, return exit() } -func runCommandRetry(ctx context.Context, retries int, stdoutFile, cmd string, - args ...string) (exitCode int, err error) { +func runCommandRetry(ctx context.Context, t *testing.T, retries int, stdoutFile, + cmd string, args ...string) (exitCode int, err error) { for i := 0; i < retries; i++ { time.Sleep(time.Second) - if exitCode, err = runCommand(ctx, stdoutFile, cmd, args...); exitCode == 0 { + if exitCode, err = runCommand(ctx, t, stdoutFile, cmd, args...); exitCode == 0 { return } } return } -func startCommand(ctx context.Context, cmdStr string, +func startCommand(ctx context.Context, t *testing.T, cmdStr string, args ...string) (<-chan string, <-chan string, exitPromise, error) { stdoutCh := make(chan string) @@ -66,6 +68,7 @@ func startCommand(ctx context.Context, cmdStr string, fmt.Errorf("no such executable '%s', please compile first: %v", cmdStr, err) } + t.Log(cmdStr, strings.Join(args, " ")) cmd := exec.CommandContext(ctx, cmdStr, args...) cmdStdout, err := cmd.StdoutPipe() @@ -87,7 +90,7 @@ func startCommand(ctx context.Context, cmdStr string, } }() go func() { - close(stderrCh) + defer close(stderrCh) scanner := bufio.NewScanner(cmdStderr) scanner.Split(bufio.ScanLines) for scanner.Scan() { @@ -102,11 +105,12 @@ func startCommand(ctx context.Context, cmdStr string, } func exitCodeFromError(err error) int { - if err != nil { - if exitError, ok := err.(*exec.ExitError); ok { - ws := exitError.Sys().(syscall.WaitStatus) - return ws.ExitStatus() - } + if err == nil { + return 0 + } + if exitError, ok := err.(*exec.ExitError); ok { + ws := exitError.Sys().(syscall.WaitStatus) + return ws.ExitStatus() } - return 0 + panic(fmt.Sprintf("Unable to get process exit code from error: %v", err)) } -- cgit v1.2.3 From 698fb76b98c46c677abe13fdc93afc6c4f38c39e Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Thu, 14 Oct 2021 20:55:35 +0300 Subject: refactor --- integrationtests/commandutils.go | 29 ++++++++++------------------- 1 file changed, 10 insertions(+), 19 deletions(-) (limited to 'integrationtests/commandutils.go') diff --git a/integrationtests/commandutils.go b/integrationtests/commandutils.go index 82c0017..b448153 100644 --- a/integrationtests/commandutils.go +++ b/integrationtests/commandutils.go @@ -7,7 +7,6 @@ import ( "os" "os/exec" "strings" - "sync" "syscall" "testing" "time" @@ -19,30 +18,23 @@ type exitPromise func() (int, error) func runCommand(ctx context.Context, t *testing.T, stdoutFile, cmdStr string, args ...string) (int, error) { - stdinCh, _, exit, err := startCommand(ctx, t, cmdStr, args...) - if err != nil { - return -1, err + if _, err := os.Stat(cmdStr); err != nil { + return 0, fmt.Errorf("no such executable '%s', please compile first: %v", cmdStr, err) } fd, err := os.Create(stdoutFile) if err != nil { - return -2, err + return 0, nil } + defer fd.Close() - var wg sync.WaitGroup - wg.Add(1) - defer wg.Wait() + t.Log(cmdStr, strings.Join(args, " ")) + cmd := exec.CommandContext(ctx, cmdStr, args...) + out, err := cmd.CombinedOutput() - go func() { - defer fd.Close() - defer wg.Done() - for line := range stdinCh { - fd.WriteString(line) - fd.WriteString("\n") - } - }() + fd.Write(out) - return exit() + return exitCodeFromError(err), err } func runCommandRetry(ctx context.Context, t *testing.T, retries int, stdoutFile, @@ -82,7 +74,6 @@ func startCommand(ctx context.Context, t *testing.T, cmdStr string, } go func() { - defer close(stdoutCh) scanner := bufio.NewScanner(cmdStdout) scanner.Split(bufio.ScanLines) for scanner.Scan() { @@ -90,12 +81,12 @@ func startCommand(ctx context.Context, t *testing.T, cmdStr string, } }() go func() { - defer close(stderrCh) scanner := bufio.NewScanner(cmdStderr) scanner.Split(bufio.ScanLines) for scanner.Scan() { stderrCh <- scanner.Text() } + close(stderrCh) }() return stdoutCh, stderrCh, func() (int, error) { -- cgit v1.2.3 From 10314cef906fd9b73e003be69c2f6b7b3d66570c Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Fri, 15 Oct 2021 13:20:48 +0300 Subject: Can configure DTail client not to mess with ~/.ssh/known_hosts via env var - this is useful for running unit and integration tests in jenkins --- integrationtests/commandutils.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'integrationtests/commandutils.go') diff --git a/integrationtests/commandutils.go b/integrationtests/commandutils.go index b448153..23b9c37 100644 --- a/integrationtests/commandutils.go +++ b/integrationtests/commandutils.go @@ -22,16 +22,17 @@ func runCommand(ctx context.Context, t *testing.T, stdoutFile, cmdStr string, return 0, fmt.Errorf("no such executable '%s', please compile first: %v", cmdStr, err) } + t.Log("Creating stdout file", stdoutFile) fd, err := os.Create(stdoutFile) if err != nil { return 0, nil } defer fd.Close() - t.Log(cmdStr, strings.Join(args, " ")) + t.Log("Running command", cmdStr, strings.Join(args, " ")) cmd := exec.CommandContext(ctx, cmdStr, args...) out, err := cmd.CombinedOutput() - + t.Log("Done running command!", err) fd.Write(out) return exitCodeFromError(err), err -- cgit v1.2.3