diff options
| author | Dave Henderson <dhenderson@gmail.com> | 2021-04-03 13:42:10 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-04-03 13:42:10 -0400 |
| commit | 507d41ae8a2536584eb032c98abc46496a77ea86 (patch) | |
| tree | 9a4aef54e6217eaa9405d848fbc1f73f022c50dd | |
| parent | 938e76a9b01f3d0c97d89f358d284feb1428bdac (diff) | |
| parent | ac00b782c5cbecbb1eb5a23ba21b056236c27416 (diff) | |
Merge pull request #1112 from hairyhenderson/simplify-integration-more
Simplify integration tests even more
| -rw-r--r-- | Dockerfile | 6 | ||||
| -rw-r--r-- | Dockerfile.integration | 2 | ||||
| -rw-r--r-- | Makefile | 27 | ||||
| -rw-r--r-- | internal/tests/integration/integration.go | 2 | ||||
| -rw-r--r-- | internal/tests/integration/integration_compiled_test.go | 39 | ||||
| -rw-r--r-- | internal/tests/integration/integration_inprocess_test.go | 46 | ||||
| -rw-r--r-- | internal/tests/integration/integration_test.go | 75 |
7 files changed, 97 insertions, 100 deletions
@@ -1,7 +1,7 @@ # syntax=docker/dockerfile:1.2.1-labs FROM --platform=linux/amd64 hairyhenderson/upx:3.94 AS upx -FROM --platform=linux/amd64 golang:1.16.2-alpine3.13 AS build +FROM --platform=linux/amd64 golang:1.16.3-alpine AS build ARG TARGETOS ARG TARGETARCH @@ -25,7 +25,7 @@ RUN --mount=type=cache,id=go-build-${TARGETOS}-${TARGETARCH}${TARGETVARIANT},tar make build RUN mv bin/gomplate* /bin/ -FROM --platform=linux/amd64 alpine:3.13.3 AS compress +FROM --platform=linux/amd64 alpine:3.13.4 AS compress ARG TARGETOS ARG TARGETARCH @@ -61,7 +61,7 @@ COPY --from=build /bin/gomplate_${TARGETOS}-${TARGETARCH}${TARGETVARIANT} /gompl ENTRYPOINT [ "/gomplate" ] -FROM alpine:3.13.3 AS gomplate-alpine +FROM alpine:3.13.4 AS gomplate-alpine ARG VCS_REF ARG TARGETOS diff --git a/Dockerfile.integration b/Dockerfile.integration index 917d62ee..25d6bc03 100644 --- a/Dockerfile.integration +++ b/Dockerfile.integration @@ -2,7 +2,7 @@ FROM vault:1.7.0 AS vault FROM consul:1.9.4 AS consul -FROM golang:1.16.2-alpine3.13 +FROM golang:1.16.3-alpine COPY --from=vault /bin/vault /bin/vault COPY --from=consul /bin/consul /bin/consul @@ -1,6 +1,5 @@ .DEFAULT_GOAL = build extension = $(patsubst windows,.exe,$(filter windows,$(1))) -GO := go PKG_NAME := gomplate DOCKER_REPO ?= hairyhenderson/$(PKG_NAME) PREFIX := . @@ -129,35 +128,35 @@ docker-images: gomplate.iid gomplate-slim.iid $(PREFIX)/bin/$(PKG_NAME)_%v5$(call extension,$(GOOS)): $(shell find $(PREFIX) -type f -name "*.go") GOOS=$(shell echo $* | cut -f1 -d-) GOARCH=$(shell echo $* | cut -f2 -d- ) GOARM=5 CGO_ENABLED=0 \ - $(GO) build \ + go build \ -ldflags "-w -s $(COMMIT_FLAG) $(VERSION_FLAG)" \ -o $@ \ ./cmd/$(PKG_NAME) $(PREFIX)/bin/$(PKG_NAME)_%v6$(call extension,$(GOOS)): $(shell find $(PREFIX) -type f -name "*.go") GOOS=$(shell echo $* | cut -f1 -d-) GOARCH=$(shell echo $* | cut -f2 -d- ) GOARM=6 CGO_ENABLED=0 \ - $(GO) build \ + go build \ -ldflags "-w -s $(COMMIT_FLAG) $(VERSION_FLAG)" \ -o $@ \ ./cmd/$(PKG_NAME) $(PREFIX)/bin/$(PKG_NAME)_%v7$(call extension,$(GOOS)): $(shell find $(PREFIX) -type f -name "*.go") GOOS=$(shell echo $* | cut -f1 -d-) GOARCH=$(shell echo $* | cut -f2 -d- ) GOARM=7 CGO_ENABLED=0 \ - $(GO) build \ + go build \ -ldflags "-w -s $(COMMIT_FLAG) $(VERSION_FLAG)" \ -o $@ \ ./cmd/$(PKG_NAME) $(PREFIX)/bin/$(PKG_NAME)_windows-%.exe: $(shell find $(PREFIX) -type f -name "*.go") GOOS=windows GOARCH=$* GOARM= CGO_ENABLED=0 \ - $(GO) build \ + go build \ -ldflags "-w -s $(COMMIT_FLAG) $(VERSION_FLAG)" \ -o $@ \ ./cmd/$(PKG_NAME) $(PREFIX)/bin/$(PKG_NAME)_%$(TARGETVARIANT)$(call extension,$(GOOS)): $(shell find $(PREFIX) -type f -name "*.go") GOOS=$(shell echo $* | cut -f1 -d-) GOARCH=$(shell echo $* | cut -f2 -d- ) GOARM=$(GOARM) CGO_ENABLED=0 \ - $(GO) build \ + go build \ -ldflags "-w -s $(COMMIT_FLAG) $(VERSION_FLAG)" \ -o $@ \ ./cmd/$(PKG_NAME) @@ -169,15 +168,23 @@ build: $(PREFIX)/bin/$(PKG_NAME)_$(GOOS)-$(GOARCH)$(TARGETVARIANT)$(call extensi ifeq ($(OS),Windows_NT) test: - $(GO) test -coverprofile=c.out ./... + go test -coverprofile=c.out ./... else test: - $(GO) test -race -coverprofile=c.out ./... + go test -race -coverprofile=c.out ./... endif -integration: build - $(GO) test -v -tags=integration \ +ifeq ($(OS),Windows_NT) +integration: $(PREFIX)/bin/$(PKG_NAME)$(call extension,$(GOOS)) + go test -v \ + -ldflags "-X `go list ./internal/tests/integration`.GomplateBinPath=$(shell cygpath -ma .)/$<" \ + ./internal/tests/integration +else +integration: $(PREFIX)/bin/$(PKG_NAME) + go test -v \ + -ldflags "-X `go list ./internal/tests/integration`.GomplateBinPath=$(shell pwd)/$<" \ ./internal/tests/integration +endif integration.iid: Dockerfile.integration $(PREFIX)/bin/$(PKG_NAME)_linux-amd64$(call extension,$(GOOS)) docker build -f $< --iidfile $@ . diff --git a/internal/tests/integration/integration.go b/internal/tests/integration/integration.go index 4fb82de2..0367482b 100644 --- a/internal/tests/integration/integration.go +++ b/internal/tests/integration/integration.go @@ -6,5 +6,5 @@ // // or: // -// $ go test -tags=integration ./tests/integration +// $ go test ./internal/tests/integration package integration diff --git a/internal/tests/integration/integration_compiled_test.go b/internal/tests/integration/integration_compiled_test.go deleted file mode 100644 index 98cbf947..00000000 --- a/internal/tests/integration/integration_compiled_test.go +++ /dev/null @@ -1,39 +0,0 @@ -//+build integration - -package integration - -import ( - "fmt" - "go/build" - "os" - "path/filepath" - "strings" - - "gotest.tools/v3/icmd" -) - -func gomplateBin() string { - ext := "" - if isWindows { - ext = ".exe" - } - return filepath.Join(build.Default.GOPATH, "src", "github.com", - "hairyhenderson", "gomplate", "bin", "gomplate"+ext) -} - -func (c *command) run() (o, e string, err error) { - cmd := icmd.Command(gomplateBin(), c.args...) - cmd.Dir = c.dir - cmd.Stdin = strings.NewReader(c.stdin) - cmd.Env = os.Environ() - cmd.Env = append(cmd.Env, "GOMPLATE_LOG_FORMAT=simple") - for k, v := range c.env { - cmd.Env = append(cmd.Env, k+"="+v) - } - - result := icmd.RunCmd(cmd) - if result.Error != nil { - result.Error = fmt.Errorf("%w: %s", result.Error, result.Stderr()) - } - return result.Stdout(), result.Stderr(), result.Error -} diff --git a/internal/tests/integration/integration_inprocess_test.go b/internal/tests/integration/integration_inprocess_test.go deleted file mode 100644 index 734ccca6..00000000 --- a/internal/tests/integration/integration_inprocess_test.go +++ /dev/null @@ -1,46 +0,0 @@ -//+build !integration - -package integration - -import ( - "bytes" - "context" - "os" - "strings" - - gcmd "github.com/hairyhenderson/gomplate/v3/internal/cmd" -) - -func (c *command) run() (o, e string, err error) { - origEnviron := map[string]string{} - for k, v := range c.env { - origEnviron[k] = os.Getenv(k) - os.Setenv(k, v) - } - - defer func() { - for k, v := range origEnviron { - os.Setenv(k, v) - } - }() - - if c.dir != "" { - //nolint:govet - origWd, err := os.Getwd() - if err != nil { - panic(err) - } - defer func() { os.Chdir(origWd) }() - err = os.Chdir(c.dir) - if err != nil { - panic(err) - } - } - - stdin := strings.NewReader(c.stdin) - - ctx := context.Background() - stdout, stderr := &bytes.Buffer{}, &bytes.Buffer{} - err = gcmd.Main(ctx, c.args, stdin, stdout, stderr) - return stdout.String(), stderr.String(), err -} diff --git a/internal/tests/integration/integration_test.go b/internal/tests/integration/integration_test.go index 0ab370f1..d273cb3d 100644 --- a/internal/tests/integration/integration_test.go +++ b/internal/tests/integration/integration_test.go @@ -1,17 +1,24 @@ package integration import ( + "bytes" + "context" "encoding/json" + "fmt" "io" "log" "net" "net/http" + "os" "runtime" + "strings" "testing" "time" + gcmd "github.com/hairyhenderson/gomplate/v3/internal/cmd" vaultapi "github.com/hashicorp/vault/api" "github.com/stretchr/testify/assert" + "gotest.tools/v3/icmd" ) const isWindows = runtime.GOOS == "windows" @@ -142,6 +149,7 @@ type command struct { dir string stdin string env map[string]string + envK []string args []string } @@ -163,6 +171,73 @@ func (c *command) withEnv(k, v string) *command { if c.env == nil { c.env = map[string]string{} } + if c.envK == nil { + c.envK = []string{} + } c.env[k] = v + c.envK = append(c.envK, k) return c } + +// set this at 'go test' time to test with a pre-compiled binary instead of +// running all tests in-process +var GomplateBinPath = "" + +func (c *command) run() (o, e string, err error) { + if GomplateBinPath != "" { + return c.runCompiled(GomplateBinPath) + } + return c.runInProcess() +} + +func (c *command) runInProcess() (o, e string, err error) { + // iterate env vars by order of insertion + for _, k := range c.envK { + k := k + // clean up after ourselves + if orig, ok := os.LookupEnv(k); ok { + defer os.Setenv(k, orig) + } else { + defer os.Unsetenv(k) + } + os.Setenv(k, c.env[k]) + } + + if c.dir != "" { + //nolint:govet + origWd, err := os.Getwd() + if err != nil { + panic(err) + } + defer os.Chdir(origWd) + + err = os.Chdir(c.dir) + if err != nil { + panic(err) + } + } + + stdin := strings.NewReader(c.stdin) + + ctx := context.Background() + stdout, stderr := &bytes.Buffer{}, &bytes.Buffer{} + err = gcmd.Main(ctx, c.args, stdin, stdout, stderr) + return stdout.String(), stderr.String(), err +} + +func (c *command) runCompiled(bin string) (o, e string, err error) { + cmd := icmd.Command(bin, c.args...) + cmd.Dir = c.dir + cmd.Stdin = strings.NewReader(c.stdin) + cmd.Env = os.Environ() + cmd.Env = append(cmd.Env, "GOMPLATE_LOG_FORMAT=simple") + for k, v := range c.env { + cmd.Env = append(cmd.Env, k+"="+v) + } + + result := icmd.RunCmd(cmd) + if result.Error != nil { + result.Error = fmt.Errorf("%w: %s", result.Error, result.Stderr()) + } + return result.Stdout(), result.Stderr(), result.Error +} |
