Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
Signed-off-by: Hang Yan <yhang@vmware.com>
  • Loading branch information
hangyan committed Nov 14, 2024
1 parent 12e1973 commit 32682e8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 20 deletions.
8 changes: 4 additions & 4 deletions pkg/agent/packetcapture/packetcapture_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,10 +453,10 @@ func (c *Controller) performCapture(
// set SnapLength here to make tcpdump on Mac OSX works. By default, its value is
// 0 and means unlimited, but tcpdump on Mac OSX will complain:
// 'tcpdump: pcap_loop: invalid packet capture length <len>, bigger than snaplen of 524288'
defaultNgInterface := pcapgo.DefaultNgInterface
defaultNgInterface.SnapLength = 524288
defaultNgInterface.LinkType = layers.LinkTypeEthernet
pcapngWriter, err := pcapgo.NewNgWriterInterface(file, defaultNgInterface, pcapgo.DefaultNgWriterOptions)
ngInterface := pcapgo.DefaultNgInterface
ngInterface.SnapLength = 65536
ngInterface.LinkType = layers.LinkTypeEthernet
pcapngWriter, err := pcapgo.NewNgWriterInterface(file, ngInterface, pcapgo.DefaultNgWriterOptions)
if err != nil {
return false, fmt.Errorf("couldn't initialize a pcap writer: %w", err)
}
Expand Down
29 changes: 13 additions & 16 deletions test/e2e/packetcapture_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@ func runPacketCaptureTest(t *testing.T, data *TestData, tc pcTestCase) {
t.Errorf("CR status not match, actual: %+v, expected: %+v", pc.Status, tc.expectedStatus)
}

if tc.pc.Spec.FileServer == nil || tc.expectedStatus.NumberCaptured == 0 {
if tc.expectedStatus.NumberCaptured == 0 {
return
}
// verify packets.
Expand All @@ -616,15 +616,14 @@ func runPacketCaptureTest(t *testing.T, data *TestData, tc pcTestCase) {
fileName := fmt.Sprintf("%s.pcapng", tc.pc.Name)
dstFileName := filepath.Join(os.TempDir(), fileName)
packetFile := filepath.Join("/tmp", "antrea", "packetcapture", "packets", fileName)
err = data.copyPodFiles(antreaPodName, "antrea-agent", "kube-system", packetFile, os.TempDir())
require.NoError(t, err)
require.NoError(t, data.copyPodFiles(antreaPodName, "antrea-agent", "kube-system", packetFile, os.TempDir()))
file, err := os.Open(dstFileName)
require.NoError(t, err)
defer file.Close()
err = verifyPacketFile(t, tc.pc, file, tc.expectedStatus.NumberCaptured, *srcPodIPs.IPv4, *dstPodIPs.IPv4)
require.NoError(t, err)
err = os.Remove(dstFileName)
require.NoError(t, err)
defer func() {
defer file.Close()
defer os.Remove(dstFileName)
}()
require.NoError(t, verifyPacketFile(t, tc.pc, file, tc.expectedStatus.NumberCaptured, *srcPodIPs.IPv4, *dstPodIPs.IPv4))
}

func (data *TestData) waitForPacketCapture(t *testing.T, name string, specTimeout int, fn func(*crdv1alpha1.PacketCapture) bool) (*crdv1alpha1.PacketCapture, error) {
Expand Down Expand Up @@ -724,7 +723,7 @@ func verifyPacketFile(t *testing.T, pc *crdv1alpha1.PacketCapture, reader io.Rea
}
packet := gopacket.NewPacket(data, layers.LayerTypeEthernet, gopacket.Default)
ipLayer := packet.Layer(layers.LayerTypeIPv4)
assert.NotNil(t, ipLayer)
require.NotNil(t, ipLayer)
ip, _ := ipLayer.(*layers.IPv4)
assert.Equal(t, srcIP.String(), ip.SrcIP.String())
assert.Equal(t, dstIP.String(), ip.DstIP.String())
Expand All @@ -740,7 +739,7 @@ func verifyPacketFile(t *testing.T, pc *crdv1alpha1.PacketCapture, reader io.Rea
}
if strings.ToUpper(proto.StrVal) == "TCP" || proto.IntVal == 6 {
tcpLayer := packet.Layer(layers.LayerTypeTCP)
assert.NotNil(t, tcpLayer)
require.NotNil(t, tcpLayer)
tcp, _ := tcpLayer.(*layers.TCP)
if packetSpec.TransportHeader.TCP != nil {
ports := packetSpec.TransportHeader.TCP
Expand All @@ -751,10 +750,9 @@ func verifyPacketFile(t *testing.T, pc *crdv1alpha1.PacketCapture, reader io.Rea
assert.Equal(t, *ports.SrcPort, int32(tcp.SrcPort))
}
}
}
if strings.ToUpper(proto.StrVal) == "UDP" || proto.IntVal == 17 {
} else if strings.ToUpper(proto.StrVal) == "UDP" || proto.IntVal == 17 {
udpLayer := packet.Layer(layers.LayerTypeUDP)
assert.NotNil(t, udpLayer)
require.NotNil(t, udpLayer)
udp, _ := udpLayer.(*layers.UDP)
if packetSpec.TransportHeader.UDP != nil {
ports := packetSpec.TransportHeader.UDP
Expand All @@ -765,10 +763,9 @@ func verifyPacketFile(t *testing.T, pc *crdv1alpha1.PacketCapture, reader io.Rea
assert.Equal(t, *ports.SrcPort, int32(udp.SrcPort))
}
}
}
if strings.ToUpper(proto.StrVal) == "ICMP" || proto.IntVal == 1 {
} else if strings.ToUpper(proto.StrVal) == "ICMP" || proto.IntVal == 1 {
icmpLayer := packet.Layer(layers.LayerTypeICMPv4)
assert.NotNil(t, icmpLayer)
require.NotNil(t, icmpLayer)
}
}
return nil
Expand Down

0 comments on commit 32682e8

Please sign in to comment.