Skip to content

Commit

Permalink
test: use pcapgo package outside packetbeat to avoid cgo dependency (#…
Browse files Browse the repository at this point in the history
…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 6228c7e)
  • Loading branch information
kruskall authored and mergify[bot] committed Dec 16, 2024
1 parent 08d9b21 commit ee7d871
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 20 deletions.
18 changes: 10 additions & 8 deletions libbeat/common/flowhash/communityid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"flag"
"fmt"
"io"
"io/ioutil"
"net"
"os"
"path/filepath"
Expand All @@ -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 (
Expand All @@ -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)
}
Expand Down Expand Up @@ -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
Expand Down
12 changes: 8 additions & 4 deletions x-pack/filebeat/input/netflow/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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 {
Expand Down
20 changes: 12 additions & 8 deletions x-pack/filebeat/input/netflow/netflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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()...).
Expand Down

0 comments on commit ee7d871

Please sign in to comment.