Skip to content
Permalink
Browse files

More integration tests (#208)

* Add further integration tests

Missing subcommand: upgrade

* Add test for `krew list`

* Be consistent with the number of lines

0 plugins -> empty string (instead of 2 lines)
1 plugin -> 1 line (instead of 2 lines)
2 plugins -> 2 lines (instead of 3 lines)

* Determine the correct krew binary for integration tests

* Remove instructions where integration tests are run with a non-standard build

* Check for existence of the krew binary when running integration tests

* Trim krew output in integration tests

* Test improvements

- clarify logic for `krew list` test
- check error case for `krew info`
- make `krew version` test more resilient against formatting changes

* Split good and bad test cases for `krew info`
  • Loading branch information...
corneliusweig authored and k8s-ci-robot committed Jun 11, 2019
1 parent 04288cb commit 95a9ad7fcec0dd195271a70baf91c97467303c01
Showing with 159 additions and 14 deletions.
  1. +0 −10 docs/CONTRIBUTOR_GUIDE.md
  2. +9 −2 hack/run-integration-tests.sh
  3. +150 −2 test/krew_test.go
@@ -59,16 +59,6 @@ hack/make-binaries.sh
hack/run-integration-tests.sh
```

The above builds binaries for all supported platforms.
Building only for your platform produces a slightly different image but will
work in most circumstances:

```bash
go build ./cmd/krew
# you need to specify your local krew binary when using this method:
hack/run-integration-tests.sh ./krew
```

## Testing `krew` in a sandbox

After making changes to krew, you should also check that it behaves as expected.
@@ -18,7 +18,9 @@ set -euo pipefail

SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
BINDIR="${SCRIPTDIR}/../out/bin"
KREW_BINARY_DEFAULT="$BINDIR/krew-linux_amd64"
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
@@ -42,8 +44,13 @@ install_kubectl_if_needed() {
fi
}

install_kubectl_if_needed
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"
exit 1
fi
export KREW_BINARY

install_kubectl_if_needed

go test -v ./...
@@ -16,6 +16,10 @@
package test

import (
"bytes"
"fmt"
"regexp"
"strings"
"testing"

"sigs.k8s.io/krew/test/krew"
@@ -26,6 +30,26 @@ const (
validPlugin = "konfig"
)

func TestKrewHelp(t *testing.T) {
skipShort(t)

test, cleanup := krew.NewTest(t)
defer cleanup()

test.Krew("help").RunOrFail()
}

func TestUnknownCommand(t *testing.T) {
skipShort(t)

test, cleanup := krew.NewTest(t)
defer cleanup()

if err := test.Krew("foobar").Run(); err == nil {
t.Errorf("Expected `krew foobar` to fail")
}
}

func TestKrewInstall(t *testing.T) {
skipShort(t)

@@ -36,13 +60,129 @@ func TestKrewInstall(t *testing.T) {
test.Call(validPlugin, "--help").RunOrFail()
}

func TestKrewHelp(t *testing.T) {
func TestKrewUninstall(t *testing.T) {
skipShort(t)

test, cleanup := krew.NewTest(t)
defer cleanup()

test.Krew("help").RunOrFail()
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")
}
}

func TestKrewSearchAll(t *testing.T) {
skipShort(t)

test, cleanup := krew.NewTest(t)
defer cleanup()

output := test.WithIndex().Krew("search").RunOrFailOutput()
if plugins := lines(output); len(plugins) < 10 {
// the first line is the header
t.Errorf("Expected at least %d plugins", len(plugins)-1)
}
}

func TestKrewSearchOne(t *testing.T) {
skipShort(t)

test, cleanup := krew.NewTest(t)
defer cleanup()

plugins := lines(test.WithIndex().Krew("search", "krew").RunOrFailOutput())
if len(plugins) < 2 {
t.Errorf("Expected krew to be a valid plugin")
}
if !strings.HasPrefix(plugins[1], "krew ") {
t.Errorf("The first match should be krew")
}
}

func TestKrewInfo(t *testing.T) {
skipShort(t)

test, cleanup := krew.NewTest(t)
defer cleanup()

test.WithIndex().Krew("info", validPlugin).RunOrFail()
}

func TestKrewInfoInvalidPlugin(t *testing.T) {
skipShort(t)

test, cleanup := krew.NewTest(t)
defer cleanup()

plugin := "invalid-plugin"
err := test.WithIndex().Krew("info", plugin).Run()
if err == nil {
t.Errorf("Expected `krew info %s` to fail", plugin)
}
}

func TestKrewList(t *testing.T) {
skipShort(t)

test, cleanup := krew.NewTest(t)
defer cleanup()

initialList := test.WithIndex().Krew("list").RunOrFailOutput()
if bytes.Contains(initialList, []byte(validPlugin)) {
t.Errorf("%q should initially not be installed", validPlugin)
}

test.Krew("install", validPlugin).RunOrFail()

eventualList := test.Krew("list").RunOrFailOutput()
if !bytes.Contains(eventualList, []byte(validPlugin)) {
t.Errorf("%q should eventually be installed", validPlugin)
}
}

func TestKrewVersion(t *testing.T) {
skipShort(t)

test, cleanup := krew.NewTest(t)
defer cleanup()

output := test.Krew("version").RunOrFailOutput()

requiredSubstrings := []string{
"IsPlugin",
fmt.Sprintf(`BasePath\s+%s`, test.Root()),
"ExecutedVersion",
"GitTag",
"GitCommit",
`IndexURI\s+https://github.com/kubernetes-sigs/krew-index.git`,
"IndexPath",
"InstallPath",
"DownloadPath",
"BinPath",
}

for _, p := range requiredSubstrings {
if regexp.MustCompile(p).FindSubmatchIndex(output) == nil {
t.Errorf("Expected to find %q in output of `krew version`", p)
}
}
}

func TestKrewUpdate(t *testing.T) {
skipShort(t)

test, cleanup := krew.NewTest(t)
defer cleanup()

// nb do not call WithIndex() here
test.Krew("update").RunOrFail()
plugins := lines(test.Krew("search").RunOrFailOutput())
if len(plugins) < 10 {
// the first line is the header
t.Errorf("Less than %d plugins found, `krew update` most likely failed unless TestKrewSearchAll also failed", len(plugins)-1)
}
}

func skipShort(t *testing.T) {
@@ -51,3 +191,11 @@ func skipShort(t *testing.T) {
t.Skip("skipping integration test")
}
}

func lines(in []byte) []string {
trimmed := strings.TrimRight(string(in), " \t\n")
if trimmed == "" {
return nil
}
return strings.Split(trimmed, "\n")
}

0 comments on commit 95a9ad7

Please sign in to comment.
You can’t perform that action at this time.