Skip to content

Commit

Permalink
Merge pull request bpfman#1041 from anfredette/probe-int-tests
Browse files Browse the repository at this point in the history
Add kprobe and uprobe k8s integration tests
  • Loading branch information
mergify[bot] authored Mar 27, 2024
2 parents 163586d + 1ad0111 commit 5882007
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 0 deletions.
70 changes: 70 additions & 0 deletions bpfman-operator/test/integration/kprobe_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
//go:build integration_tests
// +build integration_tests

package integration

import (
"bytes"
"context"
"io"
"regexp"
"strconv"
"testing"
"time"

"github.com/kong/kubernetes-testing-framework/pkg/clusters"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

const (
kprobeGoCounterKustomize = "../../../examples/config/default/go-kprobe-counter"
kprobeGoCounterUserspaceNs = "go-kprobe-counter"
kprobeGoCounterUserspaceDsName = "go-kprobe-counter-ds"
)

func TestKprobeGoCounter(t *testing.T) {
t.Log("deploying kprobe counter program")
require.NoError(t, clusters.KustomizeDeployForCluster(ctx, env.Cluster(), kprobeGoCounterKustomize))
addCleanup(func(context.Context) error {
cleanupLog("cleaning up kprobe counter program")
return clusters.KustomizeDeleteForCluster(ctx, env.Cluster(), kprobeGoCounterKustomize)
})

t.Log("waiting for go kprobe counter userspace daemon to be available")
require.Eventually(t, func() bool {
daemon, err := env.Cluster().Client().AppsV1().DaemonSets(kprobeGoCounterUserspaceNs).Get(ctx, kprobeGoCounterUserspaceDsName, metav1.GetOptions{})
require.NoError(t, err)
return daemon.Status.DesiredNumberScheduled == daemon.Status.NumberAvailable
},
// Wait 5 minutes since cosign is slow, https://github.com/bpfman/bpfman/issues/1043
5*time.Minute, 10*time.Second)

pods, err := env.Cluster().Client().CoreV1().Pods(kprobeGoCounterUserspaceNs).List(ctx, metav1.ListOptions{LabelSelector: "name=go-kprobe-counter"})
require.NoError(t, err)
goKprobeCounterPod := pods.Items[0]

want := regexp.MustCompile(`Kprobe count: ([0-9]+)`)
req := env.Cluster().Client().CoreV1().Pods(kprobeGoCounterUserspaceNs).GetLogs(goKprobeCounterPod.Name, &corev1.PodLogOptions{})
require.Eventually(t, func() bool {
logs, err := req.Stream(ctx)
require.NoError(t, err)
defer logs.Close()
output := new(bytes.Buffer)
_, err = io.Copy(output, logs)
require.NoError(t, err)
t.Logf("counter pod log %s", output.String())

matches := want.FindAllStringSubmatch(output.String(), -1)
if len(matches) >= 1 && len(matches[0]) >= 2 {
count, err := strconv.Atoi(matches[0][1])
require.NoError(t, err)
if count > 0 {
t.Logf("counted %d kprobe executions so far, BPF program is functioning", count)
return true
}
}
return false
}, 30*time.Second, time.Second)
}
89 changes: 89 additions & 0 deletions bpfman-operator/test/integration/uprobe_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
//go:build integration_tests
// +build integration_tests

package integration

import (
"bytes"
"context"
"io"
"regexp"
"strconv"
"testing"
"time"

"github.com/kong/kubernetes-testing-framework/pkg/clusters"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

const (
uprobeGoCounterKustomize = "../../../examples/config/default/go-uprobe-counter"
uprobeGoCounterUserspaceNs = "go-uprobe-counter"
uprobeGoCounterUserspaceDsName = "go-uprobe-counter-ds"
targetKustomize = "../../../examples/config/default/go-target"
targetUserspaceNs = "go-target"
targetUserspaceDsName = "go-target-ds"
)

func TestUprobeGoCounter(t *testing.T) {
t.Log("deploying target for uprobe counter program")
require.NoError(t, clusters.KustomizeDeployForCluster(ctx, env.Cluster(), targetKustomize))
addCleanup(func(context.Context) error {
cleanupLog("cleaning up target program")
return clusters.KustomizeDeleteForCluster(ctx, env.Cluster(), targetKustomize)
})

t.Log("waiting for go target userspace daemon to be available")
require.Eventually(t, func() bool {
daemon, err := env.Cluster().Client().AppsV1().DaemonSets(targetUserspaceNs).Get(ctx, targetUserspaceDsName, metav1.GetOptions{})
require.NoError(t, err)
return daemon.Status.DesiredNumberScheduled == daemon.Status.NumberAvailable
},
// Wait 5 minutes since cosign is slow, https://github.com/bpfman/bpfman/issues/1043
5*time.Minute, 10*time.Second)

t.Log("deploying uprobe counter program")
require.NoError(t, clusters.KustomizeDeployForCluster(ctx, env.Cluster(), uprobeGoCounterKustomize))
addCleanup(func(context.Context) error {
cleanupLog("cleaning up uprobe counter program")
return clusters.KustomizeDeleteForCluster(ctx, env.Cluster(), uprobeGoCounterKustomize)
})

t.Log("waiting for go uprobe counter userspace daemon to be available")
require.Eventually(t, func() bool {
daemon, err := env.Cluster().Client().AppsV1().DaemonSets(uprobeGoCounterUserspaceNs).Get(ctx, uprobeGoCounterUserspaceDsName, metav1.GetOptions{})
require.NoError(t, err)
return daemon.Status.DesiredNumberScheduled == daemon.Status.NumberAvailable
},
// Wait 5 minutes since cosign is slow, https://github.com/bpfman/bpfman/issues/1043
5*time.Minute, 10*time.Second)

pods, err := env.Cluster().Client().CoreV1().Pods(uprobeGoCounterUserspaceNs).List(ctx, metav1.ListOptions{LabelSelector: "name=go-uprobe-counter"})
require.NoError(t, err)
goUprobeCounterPod := pods.Items[0]

want := regexp.MustCompile(`Uprobe count: ([0-9]+)`)
req := env.Cluster().Client().CoreV1().Pods(uprobeGoCounterUserspaceNs).GetLogs(goUprobeCounterPod.Name, &corev1.PodLogOptions{})
require.Eventually(t, func() bool {
logs, err := req.Stream(ctx)
require.NoError(t, err)
defer logs.Close()
output := new(bytes.Buffer)
_, err = io.Copy(output, logs)
require.NoError(t, err)
t.Logf("counter pod log %s", output.String())

matches := want.FindAllStringSubmatch(output.String(), -1)
if len(matches) >= 1 && len(matches[0]) >= 2 {
count, err := strconv.Atoi(matches[0][1])
require.NoError(t, err)
if count > 0 {
t.Logf("counted %d uprobe executions so far, BPF program is functioning", count)
return true
}
}
return false
}, 30*time.Second, time.Second)
}
4 changes: 4 additions & 0 deletions examples/config/base/go-target/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources: [deployment.yaml]
10 changes: 10 additions & 0 deletions examples/config/default/go-target/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
# Patch the deployment.yaml to change container image in Daemonset
# to new tag on the image.
images:
- name: quay.io/bpfman-userspace/go-target
newName: quay.io/bpfman-userspace/go-target
newTag: latest
resources:
- ../../base/go-target

0 comments on commit 5882007

Please sign in to comment.