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 25, 2019
1 parent e30312b commit 01acced
Show file tree
Hide file tree
Showing 7 changed files with 92 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
9 changes: 5 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_BUILD_INT ?= $(DOCKER) run --rm -v $(DIR):$(BUILDMNT) -v $(KUBECONFIG):/root/.kube/kubeconfig --env KUBECONFIG=/root/.kube/kubeconfig --network host -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 @@ -89,7 +90,7 @@ stress: sonobuoy

# Integration tests
int: sonobuoy
$(DOCKER_BUILD) 'CGO_ENABLED=0 $(INT_TEST)'
$(DOCKER_BUILD_INT) 'CGO_ENABLED=0 $(INT_TEST)'

lint:
$(DOCKER_BUILD) '$(LINT)'
Expand Down Expand Up @@ -183,8 +184,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)
14 changes: 14 additions & 0 deletions scripts/run_integration_tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash

set -e

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

make KIND_CLUSTER=$cluster deploy_kind
KUBECONFIG="$(kind get kubeconfig-path --name="$cluster")" VERBOSE=true make int
36 changes: 36 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,40 @@ 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(), 45*time.Second)
defer cancel()

runCommand := exec.CommandContext(ctx, sonobuoy, "run", "--image-pull-policy", "IfNotPresent", "--wait", "-p", "testdata/basic_plugin.yaml", "-n", ns)
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
13 changes: 13 additions & 0 deletions test/integration/testdata/basic_plugin.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
sonobuoy-config:
driver: Job
plugin-name: basic-plugin
spec:
command: ["/bin/sh","-c"]
args: ["echo done >> output.txt && echo -n $(pwd)/output.txt >> /tmp/results/done"]
image: debian:stretch-slim
name: plugin
resources: {}
volumeMounts:
- mountPath: /tmp/results
name: results

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 01acced

Please sign in to comment.