From bf0e4e9936f22346bdcbebc9b72e0f1694239e70 Mon Sep 17 00:00:00 2001 From: Lorenzo Fontana Date: Sun, 6 Jan 2019 02:19:04 +0100 Subject: [PATCH] feat(integration): run without attach Signed-off-by: Lorenzo Fontana --- Makefile | 9 ++++- integration/cmd_run_test.go | 20 ++++++++++ integration/suite_test.go | 75 +++++++++++++++++++++++++++++++++++++ 3 files changed, 102 insertions(+), 2 deletions(-) create mode 100644 integration/cmd_run_test.go create mode 100644 integration/suite_test.go diff --git a/Makefile b/Makefile index 69ec425a..70d82a56 100644 --- a/Makefile +++ b/Makefile @@ -8,7 +8,6 @@ GIT_COMMIT := $(if $(shell git status --porcelain --untracked-files=no),${COMMIT GIT_BRANCH ?= $(shell git rev-parse --abbrev-ref HEAD 2>/dev/null) GIT_BRANCH_CLEAN := $(shell echo $(GIT_BRANCH) | sed -e "s/[^[:alnum:]]/-/g") - IMAGE_BPFTRACE_BRANCH := quay.io/fntlnz/kubectl-trace-bpftrace:$(GIT_BRANCH_CLEAN) IMAGE_BPFTRACE_COMMIT := quay.io/fntlnz/kubectl-trace-bpftrace:$(GIT_COMMIT) IMAGE_BPFTRACE_LATEST := quay.io/fntlnz/kubectl-trace-bpftrace:latest @@ -16,6 +15,7 @@ IMAGE_BPFTRACE_LATEST := quay.io/fntlnz/kubectl-trace-bpftrace:latest IMAGE_BUILD_FLAGS ?= "--no-cache" LDFLAGS := -ldflags '-X github.com/iovisor/kubectl-trace/pkg/version.buildTime=$(shell date +%s) -X github.com/iovisor/kubectl-trace/pkg/version.gitCommit=${GIT_COMMIT}' +TESTPACKAGES := $(shell go list ./... | grep -v github.com/iovisor/kubectl-trace/integration) kubectl_trace ?= _output/bin/kubectl-trace trace_runner ?= _output/bin/trace-runner @@ -50,4 +50,9 @@ image/latest: .PHONY: test test: - $(GO) test -v -race ./... + $(GO) test -v -race $(TESTPACKAGES) + +.PHONY: integration +integration: + $(GO) test -v ./integration/... + diff --git a/integration/cmd_run_test.go b/integration/cmd_run_test.go new file mode 100644 index 00000000..eeb065f4 --- /dev/null +++ b/integration/cmd_run_test.go @@ -0,0 +1,20 @@ +package integration + +import ( + "regexp" + + "github.com/go-check/check" +) + +func (k *KubectlTraceSuite) TestRunNode(c *check.C) { + nodes, err := k.kindContext.ListNodes() + c.Assert(err, check.IsNil) + c.Assert(len(nodes), check.Equals, 1) + + nodeName := nodes[0].String() + bpftraceProgram := `kprobe:do_sys_open { printf("%s: %s\n", comm, str(arg1)) }'` + out := k.KubectlTraceCmd(c, "run", "-e", bpftraceProgram, nodeName) + match, err := regexp.MatchString("trace (\\w+-){4}\\w+ created", out) + c.Assert(err, check.IsNil) + c.Assert(match, check.Equals, true) +} diff --git a/integration/suite_test.go b/integration/suite_test.go new file mode 100644 index 00000000..c6c0426a --- /dev/null +++ b/integration/suite_test.go @@ -0,0 +1,75 @@ +package integration + +import ( + "crypto/rand" + "fmt" + "os" + "strings" + "testing" + "time" + + "github.com/go-check/check" + "gotest.tools/icmd" + "sigs.k8s.io/kind/pkg/cluster" + "sigs.k8s.io/kind/pkg/cluster/config/encoding" +) + +var ( + KubectlTraceBinary = os.Getenv("TEST_KUBECTLTRACE_BINARY") + KindImageTag = os.Getenv("TEST_KIND_IMAGETAG") +) + +type KubectlTraceSuite struct { + kubeConfigPath string + kindContext *cluster.Context +} + +func init() { + if KubectlTraceBinary == "" { + KubectlTraceBinary = "kubectl-trace" + } + + if KindImageTag == "" { + KindImageTag = "kindest/node:v1.12.3" + } + check.Suite(&KubectlTraceSuite{}) +} + +func (k *KubectlTraceSuite) SetUpSuite(c *check.C) { + cfg, err := encoding.Load("") + c.Assert(err, check.IsNil) + retain := false + wait := time.Duration(0) + + err = cfg.Validate() + c.Assert(err, check.IsNil) + + clusterName, err := generateClusterName() + c.Assert(err, check.IsNil) + ctx := cluster.NewContext(clusterName) + err = ctx.Create(cfg, retain, wait) + c.Assert(err, check.IsNil) + k.kindContext = ctx +} + +func (s *KubectlTraceSuite) TearDownSuite(c *check.C) { + err := s.kindContext.Delete() + c.Assert(err, check.IsNil) +} + +func Test(t *testing.T) { check.TestingT(t) } + +func (k *KubectlTraceSuite) KubectlTraceCmd(c *check.C, args ...string) string { + args = append([]string{fmt.Sprintf("--kubeconfig=%s", k.kindContext.KubeConfigPath())}, args...) + res := icmd.RunCommand(KubectlTraceBinary, args...) + c.Assert(res.ExitCode, check.Equals, icmd.Success.ExitCode) + return res.Combined() +} + +func generateClusterName() (string, error) { + buf := make([]byte, 10) + if _, err := rand.Read(buf); err != nil { + return "", err + } + return strings.ToLower(fmt.Sprintf("%X", buf)), nil +}