Skip to content
Permalink
Browse files

Add basic integration tests (#203)

* Add util to set up krew integration tests

* Add utilities to initialize the krew index

For faster tests, the index is only cloned once per test run.
Additionally, the index is peristently cached between test runs.

* Add simple integration tests to cover basic krew commands

* Review comments

* Remove flawed test cases

* Add basic integration test for `krew help`

* Setup PATH variable in tests so that installed plugins can be executed

Also, optionally link to a krew binary as specified via the KREW_BINARY env varible.

* Make it possible to call plugins other than `krew`

* Enable integration tests on travis

* Fix boilerplate header

* Move IndexURI to constants

This is important, because importing the URI in integration tests
triggers the init functions in the cmd package. This has very nasty
and hard to find side-effects.

* Review comments

- rename package integration -> test
- disable integration tests in run-tests.sh
- remove obsolete tempdir helper

* Enable integration tests on travis

- Use a script run-integration-tests.sh for that
- Add instructions how to run this script locally

* Rename test types to avoid stuttering
  • Loading branch information...
corneliusweig authored and k8s-ci-robot committed Jun 10, 2019
1 parent da41409 commit 04288cb15904a3390a41d2aba52336b519961adb
Showing with 396 additions and 10 deletions.
  1. +2 βˆ’1 .travis.yml
  2. +3 βˆ’6 cmd/krew/cmd/update.go
  3. +2 βˆ’1 cmd/krew/cmd/version.go
  4. +19 βˆ’0 docs/CONTRIBUTOR_GUIDE.md
  5. +49 βˆ’0 hack/run-integration-tests.sh
  6. +1 βˆ’1 hack/run-lint.sh
  7. +1 βˆ’1 hack/run-tests.sh
  8. +3 βˆ’0 pkg/constants/constants.go
  9. +99 βˆ’0 test/krew/index.go
  10. +164 βˆ’0 test/krew/krew.go
  11. +53 βˆ’0 test/krew_test.go
@@ -13,8 +13,9 @@ script:
- hack/verify-boilerplate.sh
- hack/verify-gofmt.sh
- hack/run-lint.sh
- go test -v -coverprofile=coverage.txt -covermode=atomic ./...
- go test -v -short -coverprofile=coverage.txt -covermode=atomic ./...
- hack/make-all.sh
- hack/run-integration-tests.sh
after_success:
- bash <(curl -s https://codecov.io/bash)
env:
@@ -19,15 +19,12 @@ import (
"os"

"github.com/golang/glog"
"sigs.k8s.io/krew/pkg/gitutil"

"github.com/pkg/errors"
"github.com/spf13/cobra"
"sigs.k8s.io/krew/pkg/constants"
"sigs.k8s.io/krew/pkg/gitutil"
)

// IndexURI points to the upstream index.
const IndexURI = "https://github.com/kubernetes-sigs/krew-index.git"

// updateCmd represents the update command
var updateCmd = &cobra.Command{
Use: "update",
@@ -45,7 +42,7 @@ Remarks:

func ensureIndexUpdated(_ *cobra.Command, _ []string) error {
glog.V(1).Infof("Updating the local copy of plugin index (%s)", paths.IndexPath())
if err := gitutil.EnsureUpdated(IndexURI, paths.IndexPath()); err != nil {
if err := gitutil.EnsureUpdated(constants.IndexURI, paths.IndexPath()); err != nil {
return errors.Wrap(err, "failed to update the local index")
}
fmt.Fprintln(os.Stderr, "Updated the local copy of plugin index.")
@@ -21,6 +21,7 @@ import (
"github.com/golang/glog"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"sigs.k8s.io/krew/pkg/constants"
"sigs.k8s.io/krew/pkg/environment"
"sigs.k8s.io/krew/pkg/version"
)
@@ -58,7 +59,7 @@ Remarks:
{"ExecutedVersion", executedVersion},
{"GitTag", version.GitTag()},
{"GitCommit", version.GitCommit()},
{"IndexURI", IndexURI},
{"IndexURI", constants.IndexURI},
{"BasePath", paths.BasePath()},
{"IndexPath", paths.IndexPath()},
{"InstallPath", paths.InstallPath()},
@@ -47,9 +47,28 @@ To run tests locally, the easiest way to get started is with
hack/run-tests.sh
```

This will run all unit tests and code quality tools.
To run a single tool independently of the other code checks, have a look at the
other scripts in [`hack/`](../hack).

In addition, there are integration tests to cover high-level krew functionality.
To run integration tests, you will need to build the `krew` binary beforehand:

```bash
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.
@@ -0,0 +1,49 @@
#!/usr/bin/env bash

# Copyright 2019 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -euo pipefail

SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
BINDIR="${SCRIPTDIR}/../out/bin"
KREW_BINARY_DEFAULT="$BINDIR/krew-linux_amd64"

if [[ "$#" -gt 0 && ( "$1" = '-h' || "$1" = '--help' ) ]]; then
cat <<EOF
Usage:
$0 krew # uses the given krew binary for running integration tests
$0 # assumes a pre-built krew binary at $KREW_BINARY_DEFAULT
EOF
exit 0
fi

install_kubectl_if_needed() {
if hash kubectl 2>/dev/null; then
echo 'using kubectl from the host system'
else
# install kubectl
local -r KUBECTL_VERSION='v1.14.2'
local -r KUBECTL_BINARY="$BINDIR/kubectl"
curl -fSsLo "$KUBECTL_BINARY" https://storage.googleapis.com/kubernetes-release/release/${KUBECTL_VERSION}/bin/linux/amd64/kubectl
chmod +x "$KUBECTL_BINARY"
export PATH="$BINDIR:$PATH"
fi
}

install_kubectl_if_needed
KREW_BINARY=$(readlink -f "${1:-$KREW_BINARY_DEFAULT}") # needed for `kubectl krew` in tests
export KREW_BINARY

go test -v ./...
@@ -16,7 +16,7 @@

set -euo pipefail

HACK=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
SCRIPTDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)

if ! [[ -x "$GOPATH/bin/golangci-lint" ]]
then
@@ -44,7 +44,7 @@ print_with_color "$color_blue" 'Running gofmt'
"$SCRIPTDIR"/verify-gofmt.sh

print_with_color "$color_blue" 'Running tests'
go test -v -race sigs.k8s.io/krew/...
go test -v -short -race sigs.k8s.io/krew/...

print_with_color "$color_blue" 'Running linter'
"$SCRIPTDIR"/run-lint.sh
@@ -17,4 +17,7 @@ package constants
const (
CurrentAPIVersion = "krew.googlecontainertools.github.com/v1alpha2"
PluginKind = "Plugin"

// IndexURI points to the upstream index.
IndexURI = "https://github.com/kubernetes-sigs/krew-index.git"
)
@@ -0,0 +1,99 @@
// Copyright 2019 The Kubernetes Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package krew

import (
"bytes"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"sync"

"github.com/golang/glog"
"sigs.k8s.io/krew/pkg/constants"
)

const (
persistentIndexCache = "krew-persistent-index-cache"
)

var (
once sync.Once
indexTar []byte
)

// InitializeIndex initializes the krew index in `$root/index` with the actual krew-index.
// It caches the index tree as in-memory tar after the first run.
func (it *ITest) initializeIndex() {
once.Do(func() {
persistentCacheFile := filepath.Join(os.TempDir(), persistentIndexCache)
fileInfo, err := os.Stat(persistentCacheFile)

if err == nil && fileInfo.Mode().IsRegular() {
it.t.Logf("Using persistent index cache from file %q", persistentCacheFile)
if indexTar, err = ioutil.ReadFile(persistentCacheFile); err == nil {
return
}
}

if indexTar, err = initFromGitClone(); err != nil {
it.t.Fatalf("cannot clone repository: %s", err)
}

ioutil.WriteFile(persistentCacheFile, indexTar, 0600)
})

indexDir := filepath.Join(it.Root(), "index")
if err := os.Mkdir(indexDir, 0777); err != nil {
if os.IsExist(err) {
it.t.Log("initializeIndex should only be called once")
return
}
it.t.Fatal(err)
}

cmd := exec.Command("tar", "xzf", "-", "-C", indexDir)
cmd.Stdin = bytes.NewReader(indexTar)
if err := cmd.Run(); err != nil {
it.t.Fatalf("cannot restore index from cache: %s", err)
}
}

func initFromGitClone() ([]byte, error) {
const tarName = "index.tar"
indexRoot, err := ioutil.TempDir("", "krew-index-cache")
if err != nil {
return nil, err
}
defer func() {
err := os.RemoveAll(indexRoot)
glog.V(1).Infoln("cannot remove temporary directory:", err)
}()

cmd := exec.Command("git", "clone", "--depth=1", "--single-branch", "--no-tags", constants.IndexURI)
cmd.Dir = indexRoot
if err = cmd.Run(); err != nil {
return nil, err
}

cmd = exec.Command("tar", "czf", tarName, "-C", "krew-index", ".")
cmd.Dir = indexRoot
if err = cmd.Run(); err != nil {
return nil, err
}

return ioutil.ReadFile(filepath.Join(indexRoot, tarName))
}

0 comments on commit 04288cb

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