Skip to content

Commit

Permalink
Add a small test which runs a basic plugin
Browse files Browse the repository at this point in the history
This change also modifies our Makefile and CI scripts so that the tests
can be run on a separate kind cluster.

Signed-off-by: Bridget McErlean <[email protected]>
  • Loading branch information
zubron committed Sep 26, 2019
1 parent aae48ef commit a0f5716
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ install: true
script:
- export BRANCH=$(if [ "$TRAVIS_PULL_REQUEST" == "false" ]; then echo $TRAVIS_BRANCH; else echo $TRAVIS_PULL_REQUEST_BRANCH; fi)
- echo "TRAVIS_BRANCH=$TRAVIS_BRANCH, PR=$TRAVIS_PULL_REQUEST, BRANCH=$BRANCH"
- VERBOSE=true make test stress int
- VERBOSE=true make test stress
- ./scripts/run_integration_tests.sh
- ./travis-ci.sh
before_install:
- curl -L https://github.com/kubernetes-sigs/kind/releases/download/v0.4.0/kind-linux-amd64
Expand Down
10 changes: 6 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ DOCKER ?= docker
LINUX_ARCH := amd64 arm64
DOCKERFILE :=
PLATFORMS := $(subst $(SPACE),$(COMMA),$(foreach arch,$(LINUX_ARCH),linux/$(arch)))
KIND_CLUSTER = kind

# Not used for pushing images, just for local building on other GOOS. Defaults to
# grabbing from the local go env but can be set manually to avoid that requirement.
Expand Down Expand Up @@ -68,8 +69,8 @@ VET = go vet $(TEST_PKGS)
GOLINT_FLAGS ?= -set_exit_status
LINT = golint $(GOLINT_FLAGS) $(TEST_PKGS)

WORKDIR ?= /sonobuoy
DOCKER_BUILD ?= $(DOCKER) run --rm -v $(DIR):$(BUILDMNT) -w $(BUILDMNT) $(BUILD_IMAGE) /bin/sh -c
DOCKER_FLAGS =
DOCKER_BUILD ?= $(DOCKER) run --rm -v $(DIR):$(BUILDMNT) $(DOCKER_FLAGS) -w $(BUILDMNT) $(BUILD_IMAGE) /bin/sh -c
GO_BUILD ?= CGO_ENABLED=0 $(GO_SYSTEM_FLAGS) go build -o $(BINARY) $(VERBOSE_FLAG) -ldflags="-s -w -X $(GOTARGET)/pkg/buildinfo.Version=$(GIT_VERSION) -X $(GOTARGET)/pkg/buildinfo.GitSHA=$(GIT_REF_LONG)" $(GOTARGET)

.PHONY: all container push clean test local-test local generate plugins int
Expand All @@ -88,6 +89,7 @@ stress: sonobuoy
$(DOCKER_BUILD) 'CGO_ENABLED=0 $(STRESS_TEST)'

# Integration tests
int: DOCKER_FLAGS=-v $(KUBECONFIG):/root/.kube/kubeconfig --env KUBECONFIG=/root/.kube/kubeconfig --network host
int: sonobuoy
$(DOCKER_BUILD) 'CGO_ENABLED=0 $(INT_TEST)'

Expand Down Expand Up @@ -183,8 +185,8 @@ clean:
$(MAKE) clean_image TARGET=$(TARGET)-$$arch; \
done

deploy_kind:
kind load docker-image $(REGISTRY)/$(TARGET):$(IMAGE_VERSION) || true
deploy_kind: container
kind load docker-image --name $(KIND_CLUSTER) $(REGISTRY)/$(TARGET):$(IMAGE_VERSION) || true

native:
$(GO_BUILD)
22 changes: 22 additions & 0 deletions scripts/run_integration_tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash

set -e

SCRIPTS_DIR="$( cd "$( dirname "$0" )" >/dev/null 2>&1 && pwd )"
DIR=$(cd $SCRIPTS_DIR; cd ..; pwd)

cluster="integration"

if ! kind get clusters | grep -q "^$cluster$"; then
kind create cluster --name $cluster
# Although the cluster has been created, not all the pods in kube-system are created/available
sleep 20
fi

# Build and load the test plugin image
make -C $DIR/test/integration/testImage
kind load docker-image --name $cluster sonobuoy/testimage:v0.1

# Build and load the sonobuoy image and run integration tests
make -C $DIR KIND_CLUSTER=$cluster deploy_kind
KUBECONFIG="$(kind get kubeconfig-path --name="$cluster")" VERBOSE=true make -C $DIR int
37 changes: 37 additions & 0 deletions test/integration/sonobuoy_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ package integration

import (
"bytes"
"context"
"fmt"
"os"
"os/exec"
"strings"
"testing"
"time"
)

const (
Expand All @@ -32,6 +34,41 @@ func findSonobuoyCLI() (string, error) {
return sonobuoyPath, nil
}

// TestRunAndDelete runs a simple plugin to check that it runs successfully and can be deleted
func TestRunAndDelete(t *testing.T) {
var stdout, stderr bytes.Buffer

ns := randomNamespace()
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()

args := fmt.Sprintf("run --image-pull-policy IfNotPresent --wait -p testImage/yaml/job-junit-passing-singlefile.yaml -n %v", ns)
runCommand := exec.CommandContext(ctx, sonobuoy, strings.Fields(args)...)
runCommand.Stdout = &stdout
runCommand.Stderr = &stderr

t.Logf("Running %q\n", runCommand.String())
if err := runCommand.Run(); err != nil {
t.Errorf("Sonobuoy exited with an error: %v", err)
t.Log(stderr.String())
t.FailNow()
}

// TODO: ensure that cleanup happens even in the case where we fail to run the plugin
deleteCommand := exec.Command(sonobuoy, "delete", "-n", ns)
deleteCommand.Stdout = &stdout
deleteCommand.Stderr = &stderr

t.Logf("Running %q\n", deleteCommand.String())
if err := deleteCommand.Run(); err != nil {
t.Errorf("Sonobuoy exited with an error: %v", err)
t.Log(stderr.String())
t.FailNow()
}

// TODO: Check that we actually cleaned up the resources
}

// TestSonobuoyVersion checks that all fields in the output from `version` are non-empty
func TestSonobuoyVersion(t *testing.T) {
var stdout, stderr bytes.Buffer
Expand Down
21 changes: 21 additions & 0 deletions test/integration/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package integration

import (
"math/rand"
"time"
)

var seededRand = rand.New(rand.NewSource(time.Now().UnixNano()))

func stringWithCharset(length int, charset string) string {
b := make([]byte, length)
for i := range b {
b[i] = charset[seededRand.Intn(len(charset))]
}
return string(b)
}

func randomNamespace() string {
charset := "abcdefghijklmnopqrstuvwxyz"
return "integration-" + stringWithCharset(5, charset)
}
2 changes: 1 addition & 1 deletion travis-ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ if [ $e2eCode -ne 0 ]; then
mkdir results; tar xzf $outFile -C results

echo "Full contents of tarball:"
find results
find results

echo "Printing data on the following files:"
find results/plugins -type f
Expand Down

0 comments on commit a0f5716

Please sign in to comment.