From 6e4a93eee06125a97002586473f049f3550124d0 Mon Sep 17 00:00:00 2001 From: Jon Donovan Date: Wed, 5 Aug 2020 12:16:28 -0700 Subject: [PATCH] Add integration tests for various go mod corner cases. (#179) * Add manual integration tests for various go mod corner cases. * Move integration test back and actually test the outputs. I realize now this is run in a travis CI job :) So I'll make it actually work. * Add _, gofmt * Add tools build constraint. * Stop redirecting stderr * Use local mode to support CI. --- go.mod | 1 + go.sum | 2 + hack/tools.go | 7 ++ integration_test.sh | 103 +++++++++++++++++- .../go-training/helloworld/.drone.yml | 16 +++ .../go-training/helloworld/.gitignore | 14 +++ .../github.com/go-training/helloworld/LICENSE | 21 ++++ .../go-training/helloworld/README.md | 31 ++++++ .../github.com/go-training/helloworld/main.go | 11 ++ vendor/modules.txt | 2 + 10 files changed, 207 insertions(+), 1 deletion(-) create mode 100644 hack/tools.go mode change 100644 => 100755 integration_test.sh create mode 100644 vendor/github.com/go-training/helloworld/.drone.yml create mode 100644 vendor/github.com/go-training/helloworld/.gitignore create mode 100644 vendor/github.com/go-training/helloworld/LICENSE create mode 100644 vendor/github.com/go-training/helloworld/README.md create mode 100644 vendor/github.com/go-training/helloworld/main.go diff --git a/go.mod b/go.mod index ecdaaa18..e58b8127 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,7 @@ require ( github.com/docker/docker v1.4.2-0.20190924003213-a8608b5b67c7 github.com/dprotaso/go-yit v0.0.0-20191028211022-135eb7262960 github.com/fsnotify/fsnotify v1.4.7 + github.com/go-training/helloworld v0.0.0-20200225145412-ba5f4379d78b github.com/google/go-cmp v0.3.0 github.com/google/go-containerregistry v0.0.0-20200310013544-4fe717a9b4cb github.com/gregjones/httpcache v0.0.0-20190212212710-3befbb6ad0cc // indirect diff --git a/go.sum b/go.sum index 2c8e054b..9573d88a 100644 --- a/go.sum +++ b/go.sum @@ -114,6 +114,8 @@ github.com/go-openapi/swag v0.19.2/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh github.com/go-openapi/swag v0.19.5 h1:lTz6Ys4CmqqCQmZPBlbQENR1/GucA2bzYTE12Pw4tFY= github.com/go-openapi/swag v0.19.5/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh66Z9tfKk= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= +github.com/go-training/helloworld v0.0.0-20200225145412-ba5f4379d78b h1:0pOrjn0UzTcHdhDVdxrH8LwM7QLnAp8qiUtwXM04JEE= +github.com/go-training/helloworld v0.0.0-20200225145412-ba5f4379d78b/go.mod h1:hGGmX3bRUkYkc9aKA6mkUxi6d+f1GmZF1je0FlVTgwU= github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4= github.com/gogo/protobuf v1.2.2-0.20190723190241-65acae22fc9d h1:3PaI8p3seN09VjbTYC/QWlUZdZ1qS1zGjy7LH2Wt07I= diff --git a/hack/tools.go b/hack/tools.go new file mode 100644 index 00000000..39bb6555 --- /dev/null +++ b/hack/tools.go @@ -0,0 +1,7 @@ +// +build tools + +package hack + +import ( + _ "github.com/go-training/helloworld" +) diff --git a/integration_test.sh b/integration_test.sh old mode 100644 new mode 100755 index e49496c9..8b839f7d --- a/integration_test.sh +++ b/integration_test.sh @@ -1,2 +1,103 @@ +#!/bin/bash +set -o errexit +set -o nounset +set -o pipefail + +ROOT_DIR=$(dirname $0) + +pushd "$ROOT_DIR" + +ROOT_DIR="$(pwd)" + +echo "Running smoke test." go install ./cmd/ko -ko apply -f ./cmd/ko/test -L \ No newline at end of file +ko apply -f ./cmd/ko/test -L + +echo "Moving GOPATH into /tmp/ to test modules behavior." +export ORIGINAL_GOPATH="$GOPATH" +export GOPATH="$(mktemp -d)" + +pushd "$GOPATH" || exit 1 + +echo "Copying ko to temp gopath." +mkdir -p "$GOPATH/src/github.com/google/ko" +cp -r "$ROOT_DIR/"* "$GOPATH/src/github.com/google/ko/" + +echo "Downloading github.com/go-training/helloworld" +go get -d github.com/go-training/helloworld + +pushd "$GOPATH/src/github.com/google/ko" || exit 1 + +echo "Replacing hello world in vendor with TEST." +sed -i 's/Hello World/TEST/g' ./vendor/github.com/go-training/helloworld/main.go + +echo "Building ko" + +RESULT="$(GO111MODULE="on" GOFLAGS="-mod=vendor" go build ./cmd/ko)" + +echo "Beginning scenarios." + +FILTER="[^ ]local[^ ]*" + +echo "1. GOPATH mode should always create an image that outputs 'Hello World'" +RESULT="$(GO111MODULE=off ./ko publish --local github.com/go-training/helloworld | grep "$FILTER" | xargs -I% docker run %)" +if [[ "$RESULT" != *"Hello World"** ]]; then + echo "Test FAILED. Saw $RESULT" && exit 1 +else + echo "Test PASSED" +fi + +echo "2. Go module auto mode should create an image that outputs 'Hello World' when run outside the module." + +pushd .. || exit 1 +RESULT="$(GO111MODULE=auto GOFLAGS="-mod=vendor" ./ko/ko publish --local github.com/go-training/helloworld | grep "$FILTER" | xargs -I% docker run %)" +if [[ "$RESULT" != *"Hello World"* ]]; then + echo "Test FAILED. Saw $RESULT" && exit 1 +else + echo "Test PASSED" +fi + +popd || exit 1 + +echo "3. Auto inside the module with vendoring should output TEST" + +RESULT="$(GO111MODULE=auto GOFLAGS="-mod=vendor" ./ko publish --local github.com/go-training/helloworld | grep "$FILTER" | xargs -I% docker run %)" +if [[ "$RESULT" != *"TEST"* ]]; then + echo "Test FAILED. Saw $RESULT" && exit 1 +else + echo "Test PASSED" +fi + +echo "4. Auto inside the module without vendoring should output Hello World" +RESULT="$(GO111MODULE=auto GOFLAGS="" ./ko publish --local github.com/go-training/helloworld | grep "$FILTER" | xargs -I% docker run %)" +if [[ "$RESULT" != *"Hello World"* ]]; then + echo "Test FAILED. Saw $RESULT" && exit 1 +else + echo "Test PASSED" +fi + +echo "5. On inside the module with vendor should output TEST." +RESULT="$(GO111MODULE=on GOFLAGS="-mod=vendor" ./ko publish --local github.com/go-training/helloworld | grep "$FILTER" | xargs -I% docker run %)" +if [[ "$RESULT" != *"TEST"* ]]; then + echo "Test FAILED. Saw $RESULT" && exit 1 +else + echo "Test PASSED" +fi + +echo "6. On inside the module without vendor should output Hello World" +RESULT="$(GO111MODULE=on GOFLAGS="" ./ko publish --local github.com/go-training/helloworld | grep "$FILTER" | xargs -I% docker run %)" +if [[ "$RESULT" != *"Hello World"* ]]; then + echo "Test FAILED. Saw $RESULT" && exit 1 +else + echo "Test PASSED" +fi + +echo "7. On outside the module should fail." +pushd .. || exit 1 +GO111MODULE=on ./ko/ko publish --local github.com/go-training/helloworld && exit 1 + +popd || exit 1 +popd || exit 1 +popd || exit 1 + +export GOPATH="$ORIGINAL_GOPATH" diff --git a/vendor/github.com/go-training/helloworld/.drone.yml b/vendor/github.com/go-training/helloworld/.drone.yml new file mode 100644 index 00000000..bf58cdbd --- /dev/null +++ b/vendor/github.com/go-training/helloworld/.drone.yml @@ -0,0 +1,16 @@ +--- +kind: pipeline +name: default + +steps: +- name: backend + image: golang:1.13-alpine + environment: + USERNAME: + from_secret: username + commands: + - go build + - go test + - echo $USERNAME + - echo $USERNAME > a.txt + - cat a.txt diff --git a/vendor/github.com/go-training/helloworld/.gitignore b/vendor/github.com/go-training/helloworld/.gitignore new file mode 100644 index 00000000..a1338d68 --- /dev/null +++ b/vendor/github.com/go-training/helloworld/.gitignore @@ -0,0 +1,14 @@ +# Binaries for programs and plugins +*.exe +*.dll +*.so +*.dylib + +# Test binary, build with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736 +.glide/ diff --git a/vendor/github.com/go-training/helloworld/LICENSE b/vendor/github.com/go-training/helloworld/LICENSE new file mode 100644 index 00000000..8b38dfaf --- /dev/null +++ b/vendor/github.com/go-training/helloworld/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2017 golang workshop + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/vendor/github.com/go-training/helloworld/README.md b/vendor/github.com/go-training/helloworld/README.md new file mode 100644 index 00000000..e599d4a8 --- /dev/null +++ b/vendor/github.com/go-training/helloworld/README.md @@ -0,0 +1,31 @@ +# helloworld + +[![Build Status](https://cloud.drone.io/api/badges/go-training/helloworld/status.svg)](https://cloud.drone.io/go-training/helloworld) + +Hello World for Golang + +## Simple Command + +Run golang program + +```bash +go run main.go +``` + +Testing + +```bash +go test +``` + +Build binary + +```bash +go build +``` + +Install binary + +```bash +go install +``` diff --git a/vendor/github.com/go-training/helloworld/main.go b/vendor/github.com/go-training/helloworld/main.go new file mode 100644 index 00000000..d78aad9a --- /dev/null +++ b/vendor/github.com/go-training/helloworld/main.go @@ -0,0 +1,11 @@ +package main + +import "fmt" + +func helloworld() string { + return "Hello World!!" +} + +func main() { + fmt.Println(helloworld()) +} diff --git a/vendor/modules.txt b/vendor/modules.txt index 43ecd0a7..d76ba1e5 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -66,6 +66,8 @@ github.com/go-openapi/jsonreference github.com/go-openapi/spec # github.com/go-openapi/swag v0.19.5 github.com/go-openapi/swag +# github.com/go-training/helloworld v0.0.0-20200225145412-ba5f4379d78b +github.com/go-training/helloworld # github.com/gogo/protobuf v1.2.2-0.20190723190241-65acae22fc9d github.com/gogo/protobuf/proto github.com/gogo/protobuf/sortkeys