From dd572d44fbfaf8ddd4d28f2bb3149b206fc55024 Mon Sep 17 00:00:00 2001 From: William Banfield Date: Tue, 29 Nov 2022 13:34:56 -0500 Subject: [PATCH] add command package --- test/e2e/runner/cleanup.go | 6 ++++-- test/e2e/runner/exec.go | 34 ++++++---------------------------- test/e2e/runner/test.go | 4 +++- 3 files changed, 13 insertions(+), 31 deletions(-) diff --git a/test/e2e/runner/cleanup.go b/test/e2e/runner/cleanup.go index fbe7aa0b4..74e84f7fc 100644 --- a/test/e2e/runner/cleanup.go +++ b/test/e2e/runner/cleanup.go @@ -1,6 +1,7 @@ package main import ( + "context" "errors" "fmt" "os" @@ -8,6 +9,7 @@ import ( "github.com/tendermint/tendermint/libs/log" e2e "github.com/tendermint/tendermint/test/e2e/pkg" + "github.com/tendermint/tendermint/test/e2e/pkg/exec" ) // Cleanup removes the Docker Compose containers and testnet directory. @@ -32,13 +34,13 @@ func cleanupDocker() error { // does this by default. Ugly, but works. xargsR := `$(if [[ $OSTYPE == "linux-gnu"* ]]; then echo -n "-r"; fi)` - err := exec("bash", "-c", fmt.Sprintf( + err := exec.Command(context.Background(), "bash", "-c", fmt.Sprintf( "docker container ls -qa --filter label=e2e | xargs %v docker container rm -f", xargsR)) if err != nil { return err } - err = exec("bash", "-c", fmt.Sprintf( + err = exec.Command(context.Background(), "bash", "-c", fmt.Sprintf( "docker network ls -q --filter label=e2e | xargs %v docker network rm", xargsR)) if err != nil { return err diff --git a/test/e2e/runner/exec.go b/test/e2e/runner/exec.go index e6e47ca0a..eedc44733 100644 --- a/test/e2e/runner/exec.go +++ b/test/e2e/runner/exec.go @@ -1,49 +1,27 @@ package main import ( - "fmt" - "os" - osexec "os/exec" "path/filepath" + + "github.com/tendermint/tendermint/test/e2e/pkg/exec" + "golang.org/x/net/context" ) -// execute executes a shell command. -func exec(args ...string) error { - cmd := osexec.Command(args[0], args[1:]...) //nolint:gosec - out, err := cmd.CombinedOutput() - switch err := err.(type) { - case nil: - return nil - case *osexec.ExitError: - return fmt.Errorf("failed to run %q:\n%v", args, string(out)) - default: - return err - } -} - -// execVerbose executes a shell command while displaying its output. -func execVerbose(args ...string) error { - cmd := osexec.Command(args[0], args[1:]...) //nolint:gosec - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - return cmd.Run() -} - // execCompose runs a Docker Compose command for a testnet. func execCompose(dir string, args ...string) error { - return exec(append( + return exec.Command(context.Background(), append( []string{"docker-compose", "-f", filepath.Join(dir, "docker-compose.yml")}, args...)...) } // execComposeVerbose runs a Docker Compose command for a testnet and displays its output. func execComposeVerbose(dir string, args ...string) error { - return execVerbose(append( + return exec.CommandVerbose(context.Background(), append( []string{"docker-compose", "-f", filepath.Join(dir, "docker-compose.yml")}, args...)...) } // execDocker runs a Docker command. func execDocker(args ...string) error { - return exec(append([]string{"docker"}, args...)...) + return exec.Command(context.Background(), append([]string{"docker"}, args...)...) } diff --git a/test/e2e/runner/test.go b/test/e2e/runner/test.go index 834ce6f2d..4671a81d2 100644 --- a/test/e2e/runner/test.go +++ b/test/e2e/runner/test.go @@ -1,9 +1,11 @@ package main import ( + "context" "os" e2e "github.com/tendermint/tendermint/test/e2e/pkg" + "github.com/tendermint/tendermint/test/e2e/pkg/exec" ) // Test runs test cases under tests/ @@ -15,5 +17,5 @@ func Test(testnet *e2e.Testnet) error { return err } - return execVerbose("go", "test", "-count", "1", "./tests/...") + return exec.CommandVerbose(context.Background(), "go", "test", "-count", "1", "./tests/...") }