Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
Signed-off-by: Hang Yan <[email protected]>
  • Loading branch information
hangyan committed Nov 13, 2024
1 parent 12e1973 commit e011320
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 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 = 524288
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
19 changes: 8 additions & 11 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 @@ -621,10 +621,9 @@ func runPacketCaptureTest(t *testing.T, data *TestData, tc pcTestCase) {
file, err := os.Open(dstFileName)
require.NoError(t, err)
defer file.Close()
defer os.Remove(dstFileName)
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)
}

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 e011320

Please sign in to comment.