From 32682e86867384f39d58b0a9cd8d239dce3845f0 Mon Sep 17 00:00:00 2001 From: Hang Yan Date: Thu, 14 Nov 2024 10:22:50 +0800 Subject: [PATCH] update Signed-off-by: Hang Yan --- .../packetcapture/packetcapture_controller.go | 8 ++--- test/e2e/packetcapture_test.go | 29 +++++++++---------- 2 files changed, 17 insertions(+), 20 deletions(-) diff --git a/pkg/agent/packetcapture/packetcapture_controller.go b/pkg/agent/packetcapture/packetcapture_controller.go index 93d15e4875d..6227c8183ad 100644 --- a/pkg/agent/packetcapture/packetcapture_controller.go +++ b/pkg/agent/packetcapture/packetcapture_controller.go @@ -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 , 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) } diff --git a/test/e2e/packetcapture_test.go b/test/e2e/packetcapture_test.go index 29a94ac11fa..062a5de1b84 100644 --- a/test/e2e/packetcapture_test.go +++ b/test/e2e/packetcapture_test.go @@ -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. @@ -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) { @@ -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()) @@ -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 @@ -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 @@ -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