Skip to content

Commit bf0e4e9

Browse files
committed
feat(integration): run without attach
Signed-off-by: Lorenzo Fontana <[email protected]>
1 parent 6c22401 commit bf0e4e9

File tree

3 files changed

+102
-2
lines changed

3 files changed

+102
-2
lines changed

Makefile

+7-2
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ GIT_COMMIT := $(if $(shell git status --porcelain --untracked-files=no),${COMMIT
88
GIT_BRANCH ?= $(shell git rev-parse --abbrev-ref HEAD 2>/dev/null)
99
GIT_BRANCH_CLEAN := $(shell echo $(GIT_BRANCH) | sed -e "s/[^[:alnum:]]/-/g")
1010

11-
1211
IMAGE_BPFTRACE_BRANCH := quay.io/fntlnz/kubectl-trace-bpftrace:$(GIT_BRANCH_CLEAN)
1312
IMAGE_BPFTRACE_COMMIT := quay.io/fntlnz/kubectl-trace-bpftrace:$(GIT_COMMIT)
1413
IMAGE_BPFTRACE_LATEST := quay.io/fntlnz/kubectl-trace-bpftrace:latest
1514

1615
IMAGE_BUILD_FLAGS ?= "--no-cache"
1716

1817
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}'
18+
TESTPACKAGES := $(shell go list ./... | grep -v github.com/iovisor/kubectl-trace/integration)
1919

2020
kubectl_trace ?= _output/bin/kubectl-trace
2121
trace_runner ?= _output/bin/trace-runner
@@ -50,4 +50,9 @@ image/latest:
5050

5151
.PHONY: test
5252
test:
53-
$(GO) test -v -race ./...
53+
$(GO) test -v -race $(TESTPACKAGES)
54+
55+
.PHONY: integration
56+
integration:
57+
$(GO) test -v ./integration/...
58+

integration/cmd_run_test.go

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package integration
2+
3+
import (
4+
"regexp"
5+
6+
"github.com/go-check/check"
7+
)
8+
9+
func (k *KubectlTraceSuite) TestRunNode(c *check.C) {
10+
nodes, err := k.kindContext.ListNodes()
11+
c.Assert(err, check.IsNil)
12+
c.Assert(len(nodes), check.Equals, 1)
13+
14+
nodeName := nodes[0].String()
15+
bpftraceProgram := `kprobe:do_sys_open { printf("%s: %s\n", comm, str(arg1)) }'`
16+
out := k.KubectlTraceCmd(c, "run", "-e", bpftraceProgram, nodeName)
17+
match, err := regexp.MatchString("trace (\\w+-){4}\\w+ created", out)
18+
c.Assert(err, check.IsNil)
19+
c.Assert(match, check.Equals, true)
20+
}

integration/suite_test.go

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package integration
2+
3+
import (
4+
"crypto/rand"
5+
"fmt"
6+
"os"
7+
"strings"
8+
"testing"
9+
"time"
10+
11+
"github.com/go-check/check"
12+
"gotest.tools/icmd"
13+
"sigs.k8s.io/kind/pkg/cluster"
14+
"sigs.k8s.io/kind/pkg/cluster/config/encoding"
15+
)
16+
17+
var (
18+
KubectlTraceBinary = os.Getenv("TEST_KUBECTLTRACE_BINARY")
19+
KindImageTag = os.Getenv("TEST_KIND_IMAGETAG")
20+
)
21+
22+
type KubectlTraceSuite struct {
23+
kubeConfigPath string
24+
kindContext *cluster.Context
25+
}
26+
27+
func init() {
28+
if KubectlTraceBinary == "" {
29+
KubectlTraceBinary = "kubectl-trace"
30+
}
31+
32+
if KindImageTag == "" {
33+
KindImageTag = "kindest/node:v1.12.3"
34+
}
35+
check.Suite(&KubectlTraceSuite{})
36+
}
37+
38+
func (k *KubectlTraceSuite) SetUpSuite(c *check.C) {
39+
cfg, err := encoding.Load("")
40+
c.Assert(err, check.IsNil)
41+
retain := false
42+
wait := time.Duration(0)
43+
44+
err = cfg.Validate()
45+
c.Assert(err, check.IsNil)
46+
47+
clusterName, err := generateClusterName()
48+
c.Assert(err, check.IsNil)
49+
ctx := cluster.NewContext(clusterName)
50+
err = ctx.Create(cfg, retain, wait)
51+
c.Assert(err, check.IsNil)
52+
k.kindContext = ctx
53+
}
54+
55+
func (s *KubectlTraceSuite) TearDownSuite(c *check.C) {
56+
err := s.kindContext.Delete()
57+
c.Assert(err, check.IsNil)
58+
}
59+
60+
func Test(t *testing.T) { check.TestingT(t) }
61+
62+
func (k *KubectlTraceSuite) KubectlTraceCmd(c *check.C, args ...string) string {
63+
args = append([]string{fmt.Sprintf("--kubeconfig=%s", k.kindContext.KubeConfigPath())}, args...)
64+
res := icmd.RunCommand(KubectlTraceBinary, args...)
65+
c.Assert(res.ExitCode, check.Equals, icmd.Success.ExitCode)
66+
return res.Combined()
67+
}
68+
69+
func generateClusterName() (string, error) {
70+
buf := make([]byte, 10)
71+
if _, err := rand.Read(buf); err != nil {
72+
return "", err
73+
}
74+
return strings.ToLower(fmt.Sprintf("%X", buf)), nil
75+
}

0 commit comments

Comments
 (0)