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

Add integration test for krew upgrade #242

Merged
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
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
28 changes: 28 additions & 0 deletions integration_test/testdata/konfig.yaml
Original file line number Diff line number Diff line change
@@ -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
9 changes: 4 additions & 5 deletions integration_test/testutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
ahmetb marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -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)
}
}
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
}