From 268a72f346f7e8ba1966def3dd84b7d081a4b65b Mon Sep 17 00:00:00 2001 From: Cornelius Weig <22861411+corneliusweig@users.noreply.github.com> Date: Fri, 12 Jul 2019 22:53:06 +0200 Subject: [PATCH] Add integration test for `krew upgrade` (#242) * When looking up executables, return their location * Add integration test for `krew upgrade` * Do not return value from assertion method, expose lookup method instead * Assert upgrade by real plugin path instead of plugin hash sum This assumes that the actual plugin path changes from one version to another. * Improve name of test helper `realLocation` -> `resolvePluginSymlink` --- docs/CONTRIBUTOR_GUIDE.md | 2 +- integration_test/testutil_test.go | 9 +++---- integration_test/upgrade_test.go | 42 ++++++++++++++++++++++++++++++- 3 files changed, 46 insertions(+), 7 deletions(-) diff --git a/docs/CONTRIBUTOR_GUIDE.md b/docs/CONTRIBUTOR_GUIDE.md index 085bc8a9..43bd5ed2 100644 --- a/docs/CONTRIBUTOR_GUIDE.md +++ b/docs/CONTRIBUTOR_GUIDE.md @@ -31,7 +31,7 @@ go get golang.org/x/tools/cmd/goimports and run: ```bash -goimports -local sigs.k8s.io/krew -w cmd pkg test +goimports -local sigs.k8s.io/krew -w cmd pkg integration_test ``` In addition, a boilerplate license header is expected in all source files. diff --git a/integration_test/testutil_test.go b/integration_test/testutil_test.go index cad4e0c7..aadf1610 100644 --- a/integration_test/testutil_test.go +++ b/integration_test/testutil_test.go @@ -116,21 +116,20 @@ func lines(in []byte) []string { return strings.Split(trimmed, "\n") } -func (it *ITest) lookupExecutable(file string) error { +func (it *ITest) LookupExecutable(file string) (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 + return exec.LookPath(file) } // 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 { + if _, err := it.LookupExecutable(file); err != nil { it.t.Fatalf("executable %s not in PATH: %+v", file, err) } } @@ -139,7 +138,7 @@ func (it *ITest) AssertExecutableInPATH(file string) { // path. func (it *ITest) AssertExecutableNotInPATH(file string) { it.t.Helper() - if err := it.lookupExecutable(file); err == nil { + if _, err := it.LookupExecutable(file); err == nil { it.t.Fatalf("executable %s still exists in PATH", file) } } diff --git a/integration_test/upgrade_test.go b/integration_test/upgrade_test.go index e88dde41..63822bda 100644 --- a/integration_test/upgrade_test.go +++ b/integration_test/upgrade_test.go @@ -14,4 +14,44 @@ package integrationtest -// TODO(ahmetb): implement upgrade tests (https://krew.dev/issues/233) +import ( + "os" + "path/filepath" + "testing" + + "sigs.k8s.io/krew/pkg/constants" +) + +func TestKrewUpgrade(t *testing.T) { + skipShort(t) + + test, cleanup := NewTest(t) + defer cleanup() + + test.WithIndex(). + Krew("install", "--manifest", filepath.Join("testdata", validPlugin+constants.ManifestExtension)). + RunOrFail() + initialLocation := resolvePluginSymlink(test, validPlugin) + + test.Krew("upgrade").RunOrFail() + eventualLocation := resolvePluginSymlink(test, validPlugin) + + if initialLocation == eventualLocation { + t.Errorf("Expecting the plugin path to change but was the same.") + } +} + +func resolvePluginSymlink(test *ITest, plugin string) string { + test.t.Helper() + linkToPlugin, err := test.LookupExecutable("kubectl-" + plugin) + if err != nil { + test.t.Fatal(err) + } + + realLocation, err := os.Readlink(linkToPlugin) + if err != nil { + test.t.Fatal(err) + } + + return realLocation +}