Skip to content

Commit

Permalink
...2
Browse files Browse the repository at this point in the history
  • Loading branch information
pawarpranav83 committed Feb 4, 2024
1 parent 1857850 commit 59d119a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 52 deletions.
23 changes: 19 additions & 4 deletions integration/common/profile_cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,26 @@ import (
cpuprofileTypes "github.com/inspektor-gadget/inspektor-gadget/pkg/gadgets/profile/cpu/types"
)

func NewProfileCPUTestSteps(t *testing.T, profileCPUCmd *integration.Command, ns string) {
func RunProfileCPUTestSteps(t *testing.T, profileCPUCmd *integration.Command, ns string) {
commands := []*integration.Command{
integration.CreateTestNamespaceCommand(ns),
integration.BusyboxPodCommand(ns, "while true; do echo foo > /dev/null; done"),
integration.WaitUntilTestPodReadyCommand(ns),
// integration.WaitUntilTestPodReadyCommand(ns),
profileCPUCmd,
integration.DeleteTestNamespaceCommand(ns),
}

integration.RunTestSteps(commands, t, integration.WithCbBeforeCleanup(integration.PrintLogsFn(ns)))
}

func NewProfileCPUTestCmd(cmd string, ns string, normalize func(e *cpuprofileTypes.Report), options ...integration.CommonDataOption) *integration.Command {
func NewProfileCPUTestCmd(cmd string, ns string, normalize func(e *cpuprofileTypes.Report), commonDataOpts ...integration.CommonDataOption) *integration.Command {
profileCPUCmd := &integration.Command{
Name: "ProfileCpu",
Cmd: cmd,
ValidateOutput: func(t *testing.T, output string) {
expectedEntry := &cpuprofileTypes.Report{
CommonData: integration.BuildCommonData(ns,
options...,
commonDataOpts...,
),
Comm: "sh",
}
Expand All @@ -50,3 +50,18 @@ func NewProfileCPUTestCmd(cmd string, ns string, normalize func(e *cpuprofileTyp
}
return profileCPUCmd
}

func DefaultNormalize(ns string, isDockerRuntime bool, opts ...func(e *cpuprofileTypes.Report)) func(e *cpuprofileTypes.Report) {
return func(e *cpuprofileTypes.Report) {
e.Pid = 0
e.UserStack = nil
e.KernelStack = nil
e.Count = 0
e.Runtime.ContainerID = ""
e.Runtime.ContainerImageDigest = ""

for _, option := range opts {
option(e)
}
}
}
30 changes: 8 additions & 22 deletions integration/ig/k8s/profile_cpu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@ import (
cpuprofileTypes "github.com/inspektor-gadget/inspektor-gadget/pkg/gadgets/profile/cpu/types"
)

func defaultNormalize(ns string, isDockerRuntime bool, opts ...func(e *cpuprofileTypes.Report)) func(e *cpuprofileTypes.Report) {
return func(e *cpuprofileTypes.Report) {
func TestProfileCpu(t *testing.T) {
t.Parallel()
ns := GenerateTestNamespaceName("test-profile-cpu")
isDockerRuntime := *containerRuntime == ContainerRuntimeDocker

normalizeOpts := func(e *cpuprofileTypes.Report) {
// Docker and CRI-O use a custom container name composed, among
// other things, by the pod UID. We don't know the pod UID in
// advance, so we can't match the exact expected container name.
Expand All @@ -35,38 +39,20 @@ func defaultNormalize(ns string, isDockerRuntime bool, opts ...func(e *cpuprofil
e.Runtime.ContainerName = "test-pod"
}

e.Pid = 0
e.UserStack = nil
e.KernelStack = nil
e.Count = 0

e.Runtime.ContainerID = ""
e.Runtime.ContainerImageDigest = ""

// Docker can provide different values for ContainerImageName. See `getContainerImageNamefromImage`
if isDockerRuntime {
e.Runtime.ContainerImageName = ""
}

for _, option := range opts {
option(e)
}
}
}

func TestProfileCpu(t *testing.T) {
t.Parallel()
ns := GenerateTestNamespaceName("test-profile-cpu")
isDockerRuntime := *containerRuntime == ContainerRuntimeDocker

cmd := fmt.Sprintf("ig profile cpu -K -o json --runtimes=%s --timeout 10", *containerRuntime)
profileCPUCmd := common.NewProfileCPUTestCmd(
cmd,
ns,
defaultNormalize(ns, isDockerRuntime),
common.DefaultNormalize(ns, isDockerRuntime, normalizeOpts),
WithRuntimeMetadata(*containerRuntime),
WithContainerImageName("docker.io/library/busybox:latest", isDockerRuntime),
)

common.NewProfileCPUTestSteps(t, profileCPUCmd, ns)
common.RunProfileCPUTestSteps(t, profileCPUCmd, ns)
}
35 changes: 9 additions & 26 deletions integration/inspektor-gadget/profile_cpu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,32 +31,15 @@ func TestProfileCpu(t *testing.T) {
// TODO: Handle it once we support getting container image name from docker
isDockerRuntime := IsDockerRuntime(t)

profileCPUCmd := &Command{
Name: "RunProfileCpuGadget",
Cmd: fmt.Sprintf("$KUBECTL_GADGET profile cpu -n %s -p test-pod -K --timeout 15 -o json", ns),
ValidateOutput: func(t *testing.T, output string) {
expectedEntry := &profilecpuTypes.Report{
CommonData: BuildCommonData(ns, WithContainerImageName("docker.io/library/busybox:latest", isDockerRuntime)),
Comm: "sh",
}

normalize := func(e *profilecpuTypes.Report) {
e.Pid = 0
e.UserStack = nil
e.KernelStack = nil
e.Count = 0

e.K8s.Node = ""
// TODO: Verify container runtime and container name
e.Runtime.RuntimeName = ""
e.Runtime.ContainerName = ""
e.Runtime.ContainerID = ""
e.Runtime.ContainerImageDigest = ""
}

ExpectEntriesToMatch(t, output, normalize, expectedEntry)
},
normalizeOpts := func(e *profilecpuTypes.Report) {
e.K8s.Node = ""
// TODO: Verify container runtime and container name
e.Runtime.RuntimeName = ""
e.Runtime.ContainerName = ""
}

common.NewProfileCPUTestSteps(t, profileCPUCmd, ns)
cmd := fmt.Sprintf("$KUBECTL_GADGET profile cpu -n %s -p test-pod -K --timeout 15 -o json", ns)
profileCPUCmd2 := common.NewProfileCPUTestCmd(cmd, ns, common.DefaultNormalize(ns, isDockerRuntime, normalizeOpts), WithContainerImageName("docker.io/library/busybox:latest", isDockerRuntime))

common.RunProfileCPUTestSteps(t, profileCPUCmd2, ns)
}

0 comments on commit 59d119a

Please sign in to comment.