Skip to content

Commit

Permalink
Add integration test for krew upgrade (#242)
Browse files Browse the repository at this point in the history
* 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`
  • Loading branch information
corneliusweig authored and k8s-ci-robot committed Jul 12, 2019
1 parent 697dd32 commit 268a72f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
2 changes: 1 addition & 1 deletion docs/CONTRIBUTOR_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 4 additions & 5 deletions integration_test/testutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand All @@ -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)
}
}
Expand Down
42 changes: 41 additions & 1 deletion integration_test/upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 268a72f

Please sign in to comment.