Skip to content

Commit

Permalink
Removes need to specify E2E_GETENVOY_BINARY
Browse files Browse the repository at this point in the history
After #130, we can now easily run e2e tests in an IDE like Goland.
However, it is still tedious to have to specify `E2E_GETENVOY_BINARY`
every time. This removes this and also polishes the Makefile more.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
  • Loading branch information
Adrian Cole committed Mar 31, 2021
1 parent 7cd8354 commit e69836a
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 61 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ COVERAGE_PKG_LIST ?= $(shell go list ./pkg/... | grep -v -e github.com/tetratela
GO_COVERAGE_OPTS ?= -covermode=atomic -coverpkg=./...
GO_COVERAGE_EXTRA_OPTS ?=

E2E_PKG_LIST ?= ./test/e2e/...
E2E_PKG_LIST ?= ./test/e2e
# Set the default timeout >10m as particularly rust e2e tests are slow https://golang.org/cmd/go/#hdr-Testing_flags
# Run only one test at a time, in verbose mode, so that failures are easy to diagnose. Stop at first error.
E2E_OPTS ?= -timeout 45m -test.parallel 1 -v -test.failfast
Expand Down Expand Up @@ -105,7 +105,7 @@ test.ci: generate
.PHONY: e2e
e2e: $(call GETENVOY_OUT_PATH,$(GOOS),$(GOARCH))
docker-compose up -d
E2E_GETENVOY_BINARY=$(PWD)/$(call GETENVOY_OUT_PATH,$(GOOS),$(GOARCH)) go test github.com/tetratelabs/getenvoy/test/e2e $(E2E_OPTS) $(E2E_EXTRA_OPTS) $(E2E_PKG_LIST)
go test $(E2E_OPTS) $(E2E_EXTRA_OPTS) $(E2E_PKG_LIST)

.PHONY: bin
bin: $(foreach os,$(GOOSES), bin/$(os))
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/getenvoy_extension_build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestGetEnvoyExtensionBuild(t *testing.T) {

// "getenvoy extension build" only returns stdout because `docker run -t` redirects stderr to stdout.
// We don't verify stdout because it is low signal vs looking at files created.
cmd := GetEnvoy("extension build").Args(e2e.Env.GetBuiltinContainerOptions()...)
cmd := GetEnvoy("extension build").Args(getBuiltinContainerOptions()...)
_ = requireExecNoStderr(t, cmd)

// Verify the extension built
Expand Down
3 changes: 1 addition & 2 deletions test/e2e/getenvoy_extension_run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ func TestGetEnvoyExtensionRun(t *testing.T) {
defer requireExtensionClean(t, workDir)

// "getenvoy extension run" only returns stdout because `docker run -t` redirects stderr to stdout.
cmd := GetEnvoy("extension run --envoy-options '-l trace'").
Args(e2e.Env.GetBuiltinContainerOptions()...)
cmd := GetEnvoy("extension run --envoy-options '-l trace'").Args(getBuiltinContainerOptions()...)
_, stderr, terminate := cmd.Start(t, terminateTimeout)

// The underlying call is conditional to ensure errors that raise before we stop the server, stop it.
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/getenvoy_extension_test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestGetEnvoyExtensionTest(t *testing.T) {
requireExtensionInit(t, workDir, test.Category, test.Language, extensionName)
defer requireExtensionClean(t, workDir)

cmd := GetEnvoy("extension test").Args(e2e.Env.GetBuiltinContainerOptions()...)
cmd := GetEnvoy("extension test").Args(getBuiltinContainerOptions()...)
// "getenvoy extension test" only returns stdout because `docker run -t` redirects stderr to stdout.
stdout := requireExecNoStderr(t, cmd)

Expand Down
51 changes: 46 additions & 5 deletions test/e2e/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"os"
"path/filepath"
"regexp"
"runtime"
"testing"

"github.com/stretchr/testify/require"
Expand All @@ -38,7 +39,13 @@ var (
stripAnsiEscapeRegexp = regexp.MustCompile(`(\x9B|\x1B\[)[0-?]*[ -/]*[@-~]`)
)

// TestMain ensures state required for all tests, notably that util.E2E_GETENVOY_BINARY is set.
//nolint:golint
const (
E2E_GETENVOY_BINARY = "E2E_GETENVOY_BINARY"
E2E_BUILTIN_TOOLCHAIN_CONTAINER_OPTIONS = "E2E_BUILTIN_TOOLCHAIN_CONTAINER_OPTIONS"
)

// TestMain ensures util.GetEnvoyBinaryPath is set as all end-to-end (e2e) tests will invoke it.
//
// Note: "getenvoy extension build" and commands that imply it, can be extremely slow due to implicit responsibilities
// such as downloading modules or compilation. Commands like this use Docker, so changes to the Dockerfile or contents
Expand All @@ -48,9 +55,7 @@ var (
// CI may override this to set HOME or CARGO_HOME (rust) used by "getenvoy" and effect its execution.
func TestMain(m *testing.M) {
// As this is an e2e test, we execute all tests with a binary compiled earlier.
//
// Ex. After running "make bin", E2E_GETENVOY_BINARY=$PWD/build/bin/darwin/amd64/getenvoy
path, err := e2e.Env.GetEnvoyBinary()
path, err := getEnvoyBinary()
if err != nil {
fmt.Fprintf(os.Stderr, `failed to start e2e tests: %v`, err)
os.Exit(1)
Expand All @@ -59,6 +64,42 @@ func TestMain(m *testing.M) {
os.Exit(m.Run())
}

// getEnvoyBinary reads E2E_GETENVOY_BINARY or defaults to "$PWD/build/bin/$GOOS/$GOARCH/getenvoy"
// An error is returned if the value isn't an executable file.
func getEnvoyBinary() (string, error) {
path := os.Getenv(E2E_GETENVOY_BINARY)
if path == "" {
// Assemble the default created by "make bin"
relativePath := filepath.Join("..", "..", "build", "bin", runtime.GOOS, runtime.GOARCH, "getenvoy")
abs, err := filepath.Abs(relativePath)
if err != nil {
return "", fmt.Errorf("resolve path to %s. Correct environment variable %s", path, E2E_GETENVOY_BINARY)
}
path = abs
}
stat, err := os.Stat(path)
if err != nil && os.IsNotExist(err) {
return "", fmt.Errorf("%s doesn't exist. Correct environment variable %s", path, E2E_GETENVOY_BINARY)
}
if stat.IsDir() {
return "", fmt.Errorf("%s is not a file. Correct environment variable %s", path, E2E_GETENVOY_BINARY)
}
// While "make bin" should result in correct permissions, double-check as some tools lose them, such as
// https://github.com/actions/upload-artifact#maintaining-file-permissions-and-case-sensitive-files
if stat.Mode()&0111 == 0 {
return "", fmt.Errorf("%s is not executable. Correct environment variable %s", path, E2E_GETENVOY_BINARY)
}
return path, nil
}

func getBuiltinContainerOptions() []string {
value := os.Getenv(E2E_BUILTIN_TOOLCHAIN_CONTAINER_OPTIONS)
if value == "" {
return nil
}
return []string{"--toolchain-container-options", value}
}

// requireNewTempDir creates a new directory. The function returned cleans it up.
func requireNewTempDir(t *testing.T) (string, func()) {
d, err := ioutil.TempDir("", "")
Expand Down Expand Up @@ -154,7 +195,7 @@ func extensionWasmPath(language extension.Language) string {
// requireExtensionInit is useful for tests that depend on "getenvoy extension build" as a prerequisite.
// The result of calling this is the bytes representing the built wasm
func requireExtensionBuild(t *testing.T, language extension.Language, workDir string) []byte {
cmd := GetEnvoy("extension build").Args(e2e.Env.GetBuiltinContainerOptions()...)
cmd := GetEnvoy("extension build").Args(getBuiltinContainerOptions()...)
// stderr returned is not tested because doing so is redundant to TestGetEnvoyExtensionInit.
_ = requireExecNoStderr(t, cmd)

Expand Down
50 changes: 0 additions & 50 deletions test/e2e/util/env.go

This file was deleted.

0 comments on commit e69836a

Please sign in to comment.