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 basic integration tests #203

Merged
merged 14 commits into from
Jun 10, 2019
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
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
9 changes: 3 additions & 6 deletions cmd/krew/cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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.")
Expand Down
3 changes: 2 additions & 1 deletion cmd/krew/cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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()},
Expand Down
19 changes: 19 additions & 0 deletions docs/CONTRIBUTOR_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
49 changes: 49 additions & 0 deletions hack/run-integration-tests.sh
Original file line number Diff line number Diff line change
@@ -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 ./...
2 changes: 1 addition & 1 deletion hack/run-lint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion hack/run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions pkg/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
99 changes: 99 additions & 0 deletions test/krew/index.go
Original file line number Diff line number Diff line change
@@ -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))
}
Loading