From f18c6002ead8b944f0f3c3f1d330a84dc842f627 Mon Sep 17 00:00:00 2001 From: Sanskar Sharma Date: Mon, 5 Aug 2024 18:30:36 +0530 Subject: [PATCH 1/4] Create trace_tcpretrans_test.go Signed-off-by: Sanskar Sharma --- .../test/trace_tcpretrans_test.go | 125 ++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 gadgets/trace_tcpretrans/test/trace_tcpretrans_test.go diff --git a/gadgets/trace_tcpretrans/test/trace_tcpretrans_test.go b/gadgets/trace_tcpretrans/test/trace_tcpretrans_test.go new file mode 100644 index 0000000000..0eda8c28a8 --- /dev/null +++ b/gadgets/trace_tcpretrans/test/trace_tcpretrans_test.go @@ -0,0 +1,125 @@ +// Copyright 2024 The Inspektor Gadget authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package tests + +import ( + "fmt" + "testing" + + "github.com/stretchr/testify/require" + + gadgettesting "github.com/inspektor-gadget/inspektor-gadget/gadgets/testing" + igtesting "github.com/inspektor-gadget/inspektor-gadget/pkg/testing" + "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 traceTCPretransEvent struct { + eventtypes.CommonData + + Timestamp string `json:"timestamp"` + MntNsID uint64 `json:"mntns_id"` + NetNs uint64 `json:"netns_id"` + + Pid uint32 `json:"pid"` + Tid uint32 `json:"tid"` + Uid uint32 `json:"uid"` + Gid uint32 `json:"gid"` + + Comm string `json:"comm"` + Type string `json:"type"` + Dst utils.L4Endpoint `json:"dst"` +} + +func TestTraceTCPretrans(t *testing.T) { + gadgettesting.RequireEnvironmentVariables(t) + utils.InitTest(t) + + containerFactory, err := containers.NewContainerFactory(utils.Runtime) + require.NoError(t, err, "new container factory") + containerName := "test-trace-tcpretrans" + containerImage := "wbitt/network-multitool:latest" + + var ns string + //run the container with privileged mode to be able to use tc command + containerOpts := []containers.ContainerOption{containers.WithContainerImage(containerImage), containers.WithPrivileged()} + + if utils.CurrentTestComponent == utils.KubectlGadgetTestComponent { + ns = utils.GenerateTestNamespaceName(t, "test-trace-tcpretrans") + containerOpts = append(containerOpts, containers.WithContainerNamespace(ns)) + } + //for testing purpose drop 25% of packets and wget 1.1.1.1 every 0.1 seconds + cmds := "tc qdisc add dev eth0 root netem drop 30% && while true; do wget --no-check-certificate -q -O /dev/null 1.1.1.1; sleep 0.1; done" + testContainer := containerFactory.NewContainer( + containerName, + cmds, + containerOpts..., + ) + testContainer.Start(t) + t.Cleanup(func() { + testContainer.Stop(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) { + expectedEntries := &traceTCPretransEvent{ + CommonData: utils.BuildCommonData(containerName, commonDataOpts...), + Dst: utils.L4Endpoint{ + Addr: "1.1.1.1", + Version: 4, + Port: utils.NormalizedInt, + Proto: 6, + }, + Uid: 0, + Gid: 0, + + Comm: "wget", + Type: "RETRANS", + + Timestamp: utils.NormalizedStr, + MntNsID: utils.NormalizedInt, + NetNs: utils.NormalizedInt, + Pid: utils.NormalizedInt, + Tid: utils.NormalizedInt, + } + normalize := func(e *traceTCPretransEvent) { + utils.NormalizeCommonData(&e.CommonData) + utils.NormalizeInt(&e.MntNsID) + utils.NormalizeString(&e.Timestamp) + utils.NormalizeInt(&e.Pid) + utils.NormalizeInt(&e.Tid) + utils.NormalizeInt(&e.NetNs) + } + match.MatchEntries(t, match.JSONMultiObjectMode, output, normalize, expectedEntries) + }, + )) + traceTCPCmd := igrunner.New("trace_tcpretrans", runnerOpts...) + + igtesting.RunTestSteps([]igtesting.TestStep{traceTCPCmd}, t, testingOpts...) +} From d0ed4cabea562ef1693b784ee2f1bcf973965cbd Mon Sep 17 00:00:00 2001 From: Sanskar Sharma Date: Mon, 5 Aug 2024 18:45:00 +0530 Subject: [PATCH 2/4] Update trace_tcpretrans_test.go Signed-off-by: Sanskar Sharma --- gadgets/trace_tcpretrans/test/trace_tcpretrans_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/gadgets/trace_tcpretrans/test/trace_tcpretrans_test.go b/gadgets/trace_tcpretrans/test/trace_tcpretrans_test.go index 0eda8c28a8..9425a67a73 100644 --- a/gadgets/trace_tcpretrans/test/trace_tcpretrans_test.go +++ b/gadgets/trace_tcpretrans/test/trace_tcpretrans_test.go @@ -115,6 +115,7 @@ func TestTraceTCPretrans(t *testing.T) { utils.NormalizeInt(&e.Pid) utils.NormalizeInt(&e.Tid) utils.NormalizeInt(&e.NetNs) + utils.NormalizeInt(&e.Dst.Port) } match.MatchEntries(t, match.JSONMultiObjectMode, output, normalize, expectedEntries) }, From e7a91a2d9cd46d336e2701df84958846fa7e1a96 Mon Sep 17 00:00:00 2001 From: Sanskar Sharma Date: Wed, 7 Aug 2024 16:34:30 +0530 Subject: [PATCH 3/4] Update trace_tcpretrans_test.go Signed-off-by: Sanskar Sharma --- .../test/trace_tcpretrans_test.go | 24 ++++++++++++++----- 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/gadgets/trace_tcpretrans/test/trace_tcpretrans_test.go b/gadgets/trace_tcpretrans/test/trace_tcpretrans_test.go index 9425a67a73..d30f1ff0c6 100644 --- a/gadgets/trace_tcpretrans/test/trace_tcpretrans_test.go +++ b/gadgets/trace_tcpretrans/test/trace_tcpretrans_test.go @@ -43,6 +43,7 @@ type traceTCPretransEvent struct { Comm string `json:"comm"` Type string `json:"type"` + Src utils.L4Endpoint `json:"src"` Dst utils.L4Endpoint `json:"dst"` } @@ -50,21 +51,25 @@ func TestTraceTCPretrans(t *testing.T) { gadgettesting.RequireEnvironmentVariables(t) utils.InitTest(t) + if utils.CurrentTestComponent == utils.KubectlGadgetTestComponent { + t.Skip("Skipping test as K8s doesn't support privileged containers") + } + containerFactory, err := containers.NewContainerFactory(utils.Runtime) require.NoError(t, err, "new container factory") containerName := "test-trace-tcpretrans" - containerImage := "wbitt/network-multitool:latest" + containerImage := "docker.io/wbitt/network-multitool:latest" var ns string - //run the container with privileged mode to be able to use tc command + // run the container with privileged mode to be able to use tc command containerOpts := []containers.ContainerOption{containers.WithContainerImage(containerImage), containers.WithPrivileged()} if utils.CurrentTestComponent == utils.KubectlGadgetTestComponent { ns = utils.GenerateTestNamespaceName(t, "test-trace-tcpretrans") containerOpts = append(containerOpts, containers.WithContainerNamespace(ns)) } - //for testing purpose drop 25% of packets and wget 1.1.1.1 every 0.1 seconds - cmds := "tc qdisc add dev eth0 root netem drop 30% && while true; do wget --no-check-certificate -q -O /dev/null 1.1.1.1; sleep 0.1; done" + // for testing purpose drop 30% of packets and wget 1.1.1.1 every 0.1 seconds + cmds := "tc qdisc add dev eth0 root netem drop 30% && while true; do wget --no-check-certificate -q -O /dev/null https://1.1.1.1; sleep 0.1; done" testContainer := containerFactory.NewContainer( containerName, cmds, @@ -90,10 +95,16 @@ func TestTraceTCPretrans(t *testing.T) { func(t *testing.T, output string) { expectedEntries := &traceTCPretransEvent{ CommonData: utils.BuildCommonData(containerName, commonDataOpts...), + Src: utils.L4Endpoint{ + Addr: utils.NormalizedStr, + Version: 4, + Port: utils.NormalizedInt, + Proto: 6, + }, Dst: utils.L4Endpoint{ Addr: "1.1.1.1", Version: 4, - Port: utils.NormalizedInt, + Port: 443, Proto: 6, }, Uid: 0, @@ -115,7 +126,8 @@ func TestTraceTCPretrans(t *testing.T) { utils.NormalizeInt(&e.Pid) utils.NormalizeInt(&e.Tid) utils.NormalizeInt(&e.NetNs) - utils.NormalizeInt(&e.Dst.Port) + utils.NormalizeString(&e.Src.Addr) + utils.NormalizeInt(&e.Src.Port) } match.MatchEntries(t, match.JSONMultiObjectMode, output, normalize, expectedEntries) }, From 0359abd47c033d2d7ec615cb3fea40ef64a7d1bc Mon Sep 17 00:00:00 2001 From: Sanskar Sharma Date: Wed, 7 Aug 2024 17:17:31 +0530 Subject: [PATCH 4/4] Update trace_tcpretrans_test.go Signed-off-by: Sanskar Sharma --- gadgets/trace_tcpretrans/test/trace_tcpretrans_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/gadgets/trace_tcpretrans/test/trace_tcpretrans_test.go b/gadgets/trace_tcpretrans/test/trace_tcpretrans_test.go index d30f1ff0c6..38b1532db5 100644 --- a/gadgets/trace_tcpretrans/test/trace_tcpretrans_test.go +++ b/gadgets/trace_tcpretrans/test/trace_tcpretrans_test.go @@ -53,6 +53,8 @@ func TestTraceTCPretrans(t *testing.T) { if utils.CurrentTestComponent == utils.KubectlGadgetTestComponent { t.Skip("Skipping test as K8s doesn't support privileged containers") + } else if utils.CurrentTestComponent == utils.IgLocalTestComponent && utils.Runtime == "containerd" { + t.Skip("Skipping test as containerd test utils can't use the network") } containerFactory, err := containers.NewContainerFactory(utils.Runtime)