Skip to content

Commit

Permalink
Updating trace_open test to support testing in k8s
Browse files Browse the repository at this point in the history
Signed-off-by: pawarpranav83 <[email protected]>
  • Loading branch information
pawarpranav83 committed May 31, 2024
1 parent 7767816 commit e0aae9a
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 73 deletions.
16 changes: 14 additions & 2 deletions gadgets/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ GADGET_TAG ?= $(shell ../tools/image-tag branch)
GADGET_REPOSITORY ?= ghcr.io/inspektor-gadget/gadget
BUILDER_IMAGE ?= ghcr.io/inspektor-gadget/ebpf-builder:latest
IG ?= ig
KUBECTL_GADGET ?= kubectl-gadget
COSIGN ?= cosign
GADGETS = \
trace_dns \
Expand Down Expand Up @@ -61,10 +62,21 @@ clean:
done

.PHONY:
test: build
test-ig-local: build
IG_PATH=$(IG) \
IG_RUNTIME=docker \
CGO_ENABLED=0 \
IG_EXPERIMENTAL=true \
GADGET_REPOSITORY=$(GADGET_REPOSITORY) \
GADGET_TAG=$(GADGET_TAG) \
go test -exec 'sudo -E' -v ./...

.PHONY:
test-k8s: build
IG_PATH=$(KUBECTL_GADGET) \
IG_RUNTIME=kubernetes \
CGO_ENABLED=0 \
IG_EXPERIMENTAL=true \
GADGET_REPOSITORY=$(GADGET_REPOSITORY) \
GADGET_TAG=$(GADGET_TAG) \
IG=$(IG) \
go test -exec 'sudo -E' -v ./...
8 changes: 6 additions & 2 deletions gadgets/testing/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ import (
)

func RequireEnvironmentVariables(t testing.TB) {
if os.Getenv("IG") == "" {
t.Skip("environment variable IG undefined")
if os.Getenv("IG_PATH") == "" {
t.Skip("environment variable IG_PATH undefined")
}

if os.Getenv("IG_RUNTIME") == "" {
t.Skip("environment variable IG_RUNTIME undefined")
}
}
117 changes: 61 additions & 56 deletions gadgets/trace_open/test/trace_open_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,84 +25,89 @@ import (
"github.com/inspektor-gadget/inspektor-gadget/pkg/testing/containers"
igrunner "github.com/inspektor-gadget/inspektor-gadget/pkg/testing/ig"
"github.com/inspektor-gadget/inspektor-gadget/pkg/testing/match"
"github.com/inspektor-gadget/inspektor-gadget/pkg/testing/utils"
eventtypes "github.com/inspektor-gadget/inspektor-gadget/pkg/types"
)

type traceOpenEvent struct {
eventtypes.CommonData

MountNsID uint64 `json:"mountnsid"`
Pid uint32 `json:"pid"`
Uid uint32 `json:"uid"`
Gid uint32 `json:"gid"`
Comm string `json:"comm"`
Fd uint32 `json:"fd"`
Err int32 `json:"err"`
Ret int `json:"ret"`
Flags int `json:"flags"`
Mode int `json:"mode"`
FName string `json:"fname"`
// MountNsID and Pid were not used since these are dynamic fields
// and need to be normalized to 0 later
Uid uint32 `json:"uid"`
Gid uint32 `json:"gid"`
Comm string `json:"comm"`
Fd uint32 `json:"fd"`
Err int32 `json:"err"`
Flags int `json:"flags"`
Mode int `json:"mode"`
FName string `json:"fname"`
}

func TestTraceOpen(t *testing.T) {
gadgettesting.RequireEnvironmentVariables(t)
utils.InitTest(t)

runtime := "docker"
containerFactory, err := containers.NewContainerFactory(runtime)
containerFactory, err := containers.NewContainerFactory(utils.Runtime)
require.NoError(t, err, "new container factory")

containerName := "test-trace-open"
containerImage := "docker.io/library/busybox"
containerImage := "docker.io/library/busybox:latest"

var ns string
containerOpts := []containers.ContainerOption{containers.WithContainerImage(containerImage)}

if utils.CurrentTestComponent == utils.KubectlGadgetTestComponent {
ns = utils.GenerateTestNamespaceName("test-trace-open")
containerOpts = append(containerOpts, containers.WithContainerNamespace(ns))
}

testContainer := containerFactory.NewContainer(
containerName,
"while true; do setuidgid 1000:1111 cat /dev/null; sleep 0.1; done",
containers.WithContainerImage(containerImage),
containerOpts...,
)

testContainer.Start(t)
t.Cleanup(func() {
testContainer.Stop(t)
})

traceOpenCmd := igrunner.New(
"trace_open",
igrunner.WithFlags(fmt.Sprintf("--runtimes=%s", runtime), "--timeout=5"),
igrunner.WithValidateOutput(
func(t *testing.T, output string) {
expectedEntry := &traceOpenEvent{
CommonData: eventtypes.CommonData{
Runtime: eventtypes.BasicRuntimeMetadata{
RuntimeName: eventtypes.String2RuntimeName(runtime),
ContainerName: containerName,
ContainerID: testContainer.ID(),
ContainerImageName: containerImage,
},
},
Comm: "cat",
Fd: 3,
Err: 0,
FName: "/dev/null",
Uid: 1000,
Gid: 1111,
Flags: 0,
Mode: 0,
}

normalize := func(e *traceOpenEvent) {
e.MountNsID = 0
e.Pid = 0

// The container image digest is not currently enriched for Docker containers:
// https://github.com/inspektor-gadget/inspektor-gadget/issues/2365
if e.Runtime.RuntimeName == eventtypes.RuntimeNameDocker {
e.Runtime.ContainerImageDigest = ""
}
}

match.ExpectEntriesToMatch(t, output, normalize, expectedEntry)
}),
)

igtesting.RunTestSteps([]igtesting.TestStep{traceOpenCmd}, t)
var runnerOpts []igrunner.Option
var testingOpts []igtesting.Option
commonDataOpts := []utils.CommonDataOption{utils.WithContainerImageName(containerImage), utils.WithContainerID(testContainer.ID())}

switch utils.CurrentTestComponent {
case utils.IgLocalTestComponent:
runnerOpts = append(runnerOpts, igrunner.WithFlags(fmt.Sprintf("-r=%s", utils.Runtime), "--timeout=5"))
case utils.KubectlGadgetTestComponent:
runnerOpts = append(runnerOpts, igrunner.WithFlags(fmt.Sprintf("-n=%s", ns), "--timeout=5"))
testingOpts = append(testingOpts, igtesting.WithCbBeforeCleanup(utils.PrintLogsFn(ns)))
commonDataOpts = append(commonDataOpts, utils.WithK8sNamespace(ns))
}

runnerOpts = append(runnerOpts, igrunner.WithValidateOutput(
func(t *testing.T, output string) {
expectedEntry := &traceOpenEvent{
CommonData: utils.BuildCommonData(containerName, commonDataOpts...),
Comm: "cat",
FName: "/dev/null",
Fd: 3,
Err: 0,
Uid: 1000,
Gid: 1111,
Flags: 0,
Mode: 0,
}

normalize := func(e *traceOpenEvent) {
utils.NormalizeCommonData(&e.CommonData)
}

match.ExpectEntriesToMatch(t, output, normalize, expectedEntry)
},
))

traceOpenCmd := igrunner.New("trace_open", runnerOpts...)

igtesting.RunTestSteps([]igtesting.TestStep{traceOpenCmd}, t, testingOpts...)
}
26 changes: 13 additions & 13 deletions pkg/testing/ig/ig.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,38 +44,38 @@ func (ig *runner) createCmd() {
ig.Cmd = exec.Command(ig.path, args...)
}

type option func(*runner)
type Option func(*runner)

// WithPath used for providing custom path to ig executable.
func WithPath(path string) option {
func WithPath(path string) Option {
return func(ig *runner) {
ig.path = path
}
}

// WithFlags args should be in form: "--flag_name=value" or "-shorthand=value".
func WithFlags(flags ...string) option {
func WithFlags(flags ...string) Option {
return func(ig *runner) {
ig.flags = flags
}
}

// WithStartAndStop used to set StartAndStop value to true.
func WithStartAndStop() option {
func WithStartAndStop() Option {
return func(ig *runner) {
ig.StartAndStop = true
}
}

// WithValidateOutput used to compare the actual output with expected output.
func WithValidateOutput(validateOutput func(t *testing.T, output string)) option {
func WithValidateOutput(validateOutput func(t *testing.T, output string)) Option {
return func(ig *runner) {
ig.ValidateOutput = validateOutput
}
}

// New creates a new IG configured with the options passed as parameters.
func New(image string, opts ...option) igtesting.TestStep {
// New creates a new IG configured with the Options passed as parameters.
func New(image string, opts ...Option) igtesting.TestStep {
commandName := fmt.Sprintf("Run_%s", image)
repository := os.Getenv("GADGET_REPOSITORY")
tag := os.Getenv("GADGET_TAG")
Expand All @@ -86,23 +86,23 @@ func New(image string, opts ...option) igtesting.TestStep {
image = fmt.Sprintf("%s:%s", image, tag)
}

ig := &runner{
factoryRunner := &runner{
path: "ig",
image: image,
Command: command.Command{
Name: commandName,
},
}

if path, ok := os.LookupEnv("IG"); ok {
ig.path = path
if path, ok := os.LookupEnv("IG_PATH"); ok {
factoryRunner.path = path
}

for _, opt := range opts {
opt(ig)
opt(factoryRunner)
}

ig.createCmd()
factoryRunner.createCmd()

return ig
return factoryRunner
}

0 comments on commit e0aae9a

Please sign in to comment.