From ee7d87125356522d4aab91cbd3a0c19e4c758c3c Mon Sep 17 00:00:00 2001 From: kruskall <99559985+kruskall@users.noreply.github.com> Date: Mon, 16 Dec 2024 23:05:57 +0100 Subject: [PATCH] test: use pcapgo package outside packetbeat to avoid cgo dependency (#42043) * test: use pcapgo package outside packetbeat to avoid cgo dependency pcap requires cgo so switch to pcapgo outside packetbeat to avoid depending on cgo * Update integration_test.go * Update netflow_test.go * lint: fix linter issues * lint: missed one pcap use (cherry picked from commit 6228c7ec1e249207283207013ff1085c14408722) --- libbeat/common/flowhash/communityid_test.go | 18 +++++++++-------- .../input/netflow/integration_test.go | 12 +++++++---- x-pack/filebeat/input/netflow/netflow_test.go | 20 +++++++++++-------- 3 files changed, 30 insertions(+), 20 deletions(-) diff --git a/libbeat/common/flowhash/communityid_test.go b/libbeat/common/flowhash/communityid_test.go index 4398d60d946..be6dbae91c4 100644 --- a/libbeat/common/flowhash/communityid_test.go +++ b/libbeat/common/flowhash/communityid_test.go @@ -23,7 +23,6 @@ import ( "flag" "fmt" "io" - "io/ioutil" "net" "os" "path/filepath" @@ -33,9 +32,10 @@ import ( "github.com/google/gopacket" "github.com/google/gopacket/layers" - "github.com/google/gopacket/pcap" + "github.com/google/gopacket/pcapgo" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) const ( @@ -60,7 +60,7 @@ func TestPCAPFiles(t *testing.T) { if *update { data := strings.Join(result, "") - err = ioutil.WriteFile(goldenName, []byte(data), 0644) + err = os.WriteFile(goldenName, []byte(data), 0644) if err != nil { t.Fatal(err) } @@ -104,11 +104,13 @@ func typeCodeCode(tc uint16) uint8 { func getFlowsFromPCAP(t testing.TB, name, pcapFile string) []string { t.Helper() - r, err := pcap.OpenOffline(pcapFile) - if err != nil { - t.Fatal(err, name) - } - defer r.Close() + f, err := os.Open(pcapFile) + require.NoError(t, err) + + defer f.Close() + + r, err := pcapgo.NewReader(f) + require.NoError(t, err) packetSource := gopacket.NewPacketSource(r, r.LinkType()) var flows []string diff --git a/x-pack/filebeat/input/netflow/integration_test.go b/x-pack/filebeat/input/netflow/integration_test.go index b7bb2a3203d..61e0f21ba2a 100644 --- a/x-pack/filebeat/input/netflow/integration_test.go +++ b/x-pack/filebeat/input/netflow/integration_test.go @@ -30,7 +30,7 @@ import ( "github.com/elastic/elastic-agent-libs/monitoring" "github.com/google/gopacket" - "github.com/google/gopacket/pcap" + "github.com/google/gopacket/pcapgo" "github.com/stretchr/testify/require" ) @@ -217,17 +217,21 @@ func TestNetFlowIntegration(t *testing.T) { conn, err := net.DialUDP("udp", nil, udpAddr) require.NoError(t, err, "failed to open UDP connection") - // for more info look testdata/integration/test.md - f, err := pcap.OpenOffline("testdata/integration/test.pcap") + f, err := os.Open("testdata/integration/test.pcap") require.NoError(t, err, "failed to open pcap file") defer f.Close() + + r, err := pcapgo.NewReader(f) + require.NoError(t, err) + + // for more info look testdata/integration/test.md expectedEventsNumbers := 32 var totalBytes, totalPackets int rateLimit := 3000 limiter := rate.NewLimiter(rate.Limit(rateLimit), rateLimit) - packetSource := gopacket.NewPacketSource(f, f.LinkType()) + packetSource := gopacket.NewPacketSource(r, r.LinkType()) for pkt := range packetSource.Packets() { if totalPackets%rateLimit == 0 { diff --git a/x-pack/filebeat/input/netflow/netflow_test.go b/x-pack/filebeat/input/netflow/netflow_test.go index 127445af1b8..65383df4a98 100644 --- a/x-pack/filebeat/input/netflow/netflow_test.go +++ b/x-pack/filebeat/input/netflow/netflow_test.go @@ -18,7 +18,7 @@ import ( "time" "github.com/google/gopacket" - "github.com/google/gopacket/pcap" + "github.com/google/gopacket/pcapgo" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "gopkg.in/yaml.v2" @@ -123,15 +123,18 @@ func TestNetFlow(t *testing.T) { conn, err := net.DialUDP("udp", nil, udpAddr) require.NoError(t, err) - f, err := pcap.OpenOffline(file) + f, err := os.Open(file) require.NoError(t, err) defer f.Close() + r, err := pcapgo.NewReader(f) + require.NoError(t, err) + goldenData := readGoldenFile(t, filepath.Join(goldenDir, testName+".pcap.golden.json")) // Process packets in PCAP and get flow records. var totalBytes, totalPackets int - packetSource := gopacket.NewPacketSource(f, f.LinkType()) + packetSource := gopacket.NewPacketSource(r, r.LinkType()) for pkt := range packetSource.Packets() { payloadData := pkt.TransportLayer().LayerPayload() @@ -341,11 +344,12 @@ func getFlowsFromDat(t testing.TB, name string, testCase TestCase) TestResult { func getFlowsFromPCAP(t testing.TB, name, pcapFile string) TestResult { t.Helper() - r, err := pcap.OpenOffline(pcapFile) - if err != nil { - t.Fatal(err) - } - defer r.Close() + f, err := os.Open(pcapFile) + require.NoError(t, err) + defer f.Close() + + r, err := pcapgo.NewReader(f) + require.NoError(t, err) config := decoder.NewConfig(). WithProtocols(protocol.Registry.All()...).