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/testdata/konfig.yaml b/integration_test/testdata/konfig.yaml new file mode 100644 index 00000000..a8a33f6f --- /dev/null +++ b/integration_test/testdata/konfig.yaml @@ -0,0 +1,28 @@ +apiVersion: krew.googlecontainertools.github.com/v1alpha2 +kind: Plugin +metadata: + name: konfig +spec: + version: "v0.2.0" + shortDescription: a plugin in central index with small size + platforms: + - uri: https://github.com/corneliusweig/konfig/releases/download/v0.2.0/bundle.tar.gz + sha256: 7dc7b884ca92e3c9b39f905cc11bcf31e79b9c929741a4383d9ff61414101962 + bin: konfig-krew + files: + - from: ./konfig-krew + to: "." + selector: + matchExpressions: + - key: os + operator: In + values: ["darwin", "linux"] + - uri: https://github.com/corneliusweig/konfig/releases/download/v0.2.0/bundle.tar.gz + sha256: 7dc7b884ca92e3c9b39f905cc11bcf31e79b9c929741a4383d9ff61414101962 + bin: konfig-krew.exe + files: + - from: ./konfig-krew + to: konfig-krew.exe + selector: + matchLabels: + os: windows diff --git a/integration_test/testutil_test.go b/integration_test/testutil_test.go index 442f079e..f66d2f1b 100644 --- a/integration_test/testutil_test.go +++ b/integration_test/testutil_test.go @@ -117,21 +117,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) } } @@ -140,7 +139,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 +}