Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various fixes to integration tests #228

Merged
merged 2 commits into from
Jun 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions hack/ensure-kubectl-installed.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ install_kubectl_if_needed() {
goarch="$(go env GOARCH)"
kubectl_url="https://storage.googleapis.com/kubernetes-release/release/${kubectl_version}/bin/${goos}/${goarch}/kubectl"

echo >&2 "kubectl not detected in environment, downloading ${kubectl_url}"
mkdir -p "${bin_dir}"
curl --fail --show-error --silent --location --output "$kubectl_path" "${kubectl_url}"
chmod +x "$kubectl_path"
Expand Down
10 changes: 5 additions & 5 deletions hack/run-integration-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ set -euo pipefail

SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
BINDIR="${SCRIPTDIR}/../out/bin"
GOOS="$(go env GOOS)"
GOARCH="$(go env GOARCH)"
KREW_BINARY_DEFAULT="$BINDIR/krew-${GOOS}_${GOARCH}"
goos="$(go env GOOS)"
goarch="$(go env GOARCH)"
KREW_BINARY_DEFAULT="${BINDIR}/krew-${goos}_${goarch}"

if [[ "$#" -gt 0 && ( "$1" = '-h' || "$1" = '--help' ) ]]; then
cat <<EOF
Expand All @@ -33,9 +33,9 @@ fi

KREW_BINARY=$(readlink -f "${1:-$KREW_BINARY_DEFAULT}") # needed for `kubectl krew` in tests
if [[ ! -x "${KREW_BINARY}" ]]; then
echo "Did not find $KREW_BINARY. You need to build krew before running the integration tests"
echo "Did not find $KREW_BINARY. You need to build krew FOR ${goos}/${goarch} before running the integration tests."
exit 1
fi
export KREW_BINARY

go test -v ./...
go test -test.v sigs.k8s.io/krew/test
89 changes: 61 additions & 28 deletions test/krew/krew.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"fmt"
"os"
"os/exec"
"path/filepath"
"testing"
"time"

Expand All @@ -32,51 +33,84 @@ const krewBinaryEnv = "KREW_BINARY"

// ITest is used to set up `krew` integration tests.
type ITest struct {
t *testing.T
plugin string
args []string
env []string
tempDir *testutil.TempDir
t *testing.T
plugin string
pluginsBinDir string
args []string
env []string
tempDir *testutil.TempDir
}

// NewTest creates a fluent krew ITest.
func NewTest(t *testing.T) (*ITest, func()) {
tempDir, cleanup := testutil.NewTempDir(t)
pathEnv := setupPathEnv(t, tempDir)
binDir := setupKrewBin(t, tempDir)

return &ITest{
t: t,
env: []string{pathEnv, fmt.Sprintf("KREW_ROOT=%s", tempDir.Root())},
t: t,
pluginsBinDir: binDir,
env: []string{
fmt.Sprintf("KREW_ROOT=%s", tempDir.Root()),
fmt.Sprintf("PATH=%s", augmentPATH(t, binDir)),
},
tempDir: tempDir,
}, cleanup
}

func setupPathEnv(t *testing.T, tempDir *testutil.TempDir) string {
krewBinPath := tempDir.Path("bin")
if err := os.MkdirAll(krewBinPath, os.ModePerm); err != nil {
// setupKrewBin symlinks the $KREW_BINARY to $tempDir/bin and returns the path
// to this directory.
func setupKrewBin(t *testing.T, tempDir *testutil.TempDir) string {
krewBinary, found := os.LookupEnv(krewBinaryEnv)
if !found {
t.Fatalf("%s environment variable pointing to krew binary not set", krewBinaryEnv)
}
binPath := tempDir.Path("bin")
if err := os.MkdirAll(binPath, 0755); err != nil {
t.Fatal(err)
}

if krewBinary, found := os.LookupEnv(krewBinaryEnv); found {
if err := os.Symlink(krewBinary, tempDir.Path("bin/kubectl-krew")); err != nil {
t.Fatalf("Cannot link to krew: %s", err)
}
} else {
t.Logf("Environment variable %q was not found, using krew installation from host", krewBinaryEnv)
if err := os.Symlink(krewBinary, filepath.Join(binPath, "kubectl-krew")); err != nil {
t.Fatalf("cannot link krew binary: %s", err)
}
return binPath
}

path, found := os.LookupEnv("PATH")
// augmentPATH apprends the value to the current $PATH and returns the new
// value.
func augmentPATH(t *testing.T, v string) string {
curPath, found := os.LookupEnv("PATH")
if !found {
t.Fatalf("PATH variable is not set up")
t.Fatalf("$PATH variable is not set up, required to run tests")
}

return fmt.Sprintf("PATH=%s:%s", krewBinPath, path)
return v + string(os.PathListSeparator) + curPath
}

// Call configures the runner to call plugin with arguments args.
func (it *ITest) Call(plugin string, args ...string) *ITest {
it.plugin = plugin
it.args = args
return it
func (it *ITest) lookupExecutable(file string) error {
orig := os.Getenv("PATH")
defer func() { os.Setenv("PATH", orig) }()

binPath := filepath.Join(it.Root(), "bin")
os.Setenv("PATH", binPath)

_, err := exec.LookPath(file)
return err
}

// AssertExecutableInPATH asserts that the executable file is in bin path.
func (it *ITest) AssertExecutableInPATH(file string) {
it.t.Helper()
if err := it.lookupExecutable(file); err != nil {
it.t.Fatalf("executable %s not in PATH: %+v", file, err)
}
}

// AssertExecutableNotInPATH asserts that the executable file is not in bin
// path.
func (it *ITest) AssertExecutableNotInPATH(file string) {
it.t.Helper()
if err := it.lookupExecutable(file); err == nil {
it.t.Fatalf("executable %s still exists in PATH", file)
}
}

// Krew configures the runner to call krew with arguments args.
Expand Down Expand Up @@ -155,8 +189,7 @@ func (it *ITest) cmd(ctx context.Context) *exec.Cmd {
args = append(args, it.args...)

cmd := exec.CommandContext(ctx, "kubectl", args...)
cmd.Env = append(os.Environ(), it.env...)

cmd.Env = it.env // clear env, do not inherit from system
return cmd
}

Expand Down
6 changes: 2 additions & 4 deletions test/krew_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func TestKrewInstall(t *testing.T) {
defer cleanup()

test.WithIndex().Krew("install", validPlugin).RunOrFailOutput()
test.Call(validPlugin, "--help").RunOrFail()
test.AssertExecutableInPATH("kubectl-" + validPlugin)
}

func TestKrewUninstall(t *testing.T) {
Expand All @@ -68,9 +68,7 @@ func TestKrewUninstall(t *testing.T) {

test.WithIndex().Krew("install", validPlugin).RunOrFailOutput()
test.Krew("uninstall", validPlugin).RunOrFailOutput()
if err := test.Call(validPlugin, "--help").Run(); err == nil {
t.Errorf("Expected the plugin to be uninstalled")
}
test.AssertExecutableNotInPATH("kubectl-" + validPlugin)
}

func TestKrewSearchAll(t *testing.T) {
Expand Down