Skip to content

Commit

Permalink
feat(integration): run without attach
Browse files Browse the repository at this point in the history
Signed-off-by: Lorenzo Fontana <lo@linux.com>
  • Loading branch information
fntlnz committed Jan 6, 2019
1 parent 6c22401 commit bf0e4e9
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 2 deletions.
9 changes: 7 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ 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

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
Expand Down Expand Up @@ -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/...

20 changes: 20 additions & 0 deletions integration/cmd_run_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
75 changes: 75 additions & 0 deletions integration/suite_test.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit bf0e4e9

Please sign in to comment.