Skip to content

Commit

Permalink
update test
Browse files Browse the repository at this point in the history
Signed-off-by: Hang Yan <[email protected]>
  • Loading branch information
hangyan committed Mar 1, 2024
1 parent 87d0ce7 commit ae9df19
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,8 @@ func (c *Controller) startPacketSampling(ps *crdv1alpha1.PacketSampling) error {
}
}()

klog.Info("???")

if err != nil {
return err
}
Expand Down
203 changes: 173 additions & 30 deletions pkg/agent/controller/packetsampling/packetsampling_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
package packetsampling

import (
"bytes"
"net"
"os"
"testing"

"github.com/google/gopacket/layers"
Expand All @@ -28,9 +30,13 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/fake"
"k8s.io/client-go/util/workqueue"
"k8s.io/klog/v2"

crdv1beta1 "antrea.io/antrea/pkg/apis/crd/v1beta1"

"antrea.io/libOpenflow/protocol"

Expand All @@ -46,9 +52,10 @@ import (
)

var (
pod1IPv4 = "192.168.10.10"
pod2IPv4 = "192.168.11.10"
// dstIPv4 = "192.168.99.99"
pod1IPv4 = "192.168.10.10"
pod2IPv4 = "192.168.11.10"
service1IPv4 = "10.96.0.10"
dstIPv4 = "192.168.99.99"
pod1MAC, _ = net.ParseMAC("aa:bb:cc:dd:ee:0f")
pod2MAC, _ = net.ParseMAC("aa:bb:cc:dd:ee:00")
ofPortPod1 = uint32(1)
Expand Down Expand Up @@ -79,6 +86,16 @@ var (
Namespace: "default",
},
}

service1 = v1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: "service-1",
Namespace: "default",
},
Spec: v1.ServiceSpec{
ClusterIP: service1IPv4,
},
}
)

type fakePacketSamplingController struct {
Expand All @@ -88,15 +105,18 @@ type fakePacketSamplingController struct {
mockOFClient *openflowtest.MockClient
crdClient *fakeversioned.Clientset
crdInformerFactory crdinformers.SharedInformerFactory
informerFactory informers.SharedInformerFactory
}

func newFakePacketSamplingController(t *testing.T, initObjects []runtime.Object, networkConfig *config.NetworkConfig, nodeConfig *config.NodeConfig) *fakePacketSamplingController {
controller := gomock.NewController(t)
kubeClient := fake.NewSimpleClientset(&pod1, &pod2, &pod3, generateTestSecret())
kubeClient := fake.NewSimpleClientset(&pod1, &pod2, &pod3, &service1, generateTestSecret())
mockOFClient := openflowtest.NewMockClient(controller)
crdClient := fakeversioned.NewSimpleClientset(initObjects...)
crdInformerFactory := crdinformers.NewSharedInformerFactory(crdClient, 0)
packetSamplingInformer := crdInformerFactory.Crd().V1alpha1().PacketSamplings()
informerFactory := informers.NewSharedInformerFactory(kubeClient, 0)
serviceInformer := informerFactory.Core().V1().Services()

ifaceStore := interfacestore.NewInterfaceStore()
addPodInterface(ifaceStore, pod1.Namespace, pod1.Name, pod1IPv4, pod1MAC.String(), int32(ofPortPod1))
Expand All @@ -110,6 +130,8 @@ func newFakePacketSamplingController(t *testing.T, initObjects []runtime.Object,
packetSamplingInformer: packetSamplingInformer,
packetSamplingLister: packetSamplingInformer.Lister(),
packetSamplingSynced: packetSamplingInformer.Informer().HasSynced,
serviceListerSynced: serviceInformer.Informer().HasSynced,
serviceLister: serviceInformer.Lister(),
ofClient: mockOFClient,
interfaceStore: ifaceStore,
networkConfig: networkConfig,
Expand All @@ -127,6 +149,7 @@ func newFakePacketSamplingController(t *testing.T, initObjects []runtime.Object,
mockOFClient: mockOFClient,
crdClient: crdClient,
crdInformerFactory: crdInformerFactory,
informerFactory: informerFactory,
}
}

Expand Down Expand Up @@ -314,7 +337,7 @@ func TestPreparePacket(t *testing.T) {
},
},
{
name: "tcp packet with out flags",
name: "tcp packet without flags",
ps: &crdv1alpha1.PacketSampling{
ObjectMeta: metav1.ObjectMeta{Name: "ps6", UID: "uid6"},
Spec: crdv1alpha1.PacketSamplingSpec{
Expand Down Expand Up @@ -399,48 +422,49 @@ func TestPreparePacket(t *testing.T) {
},
},
{
name: "destination IP family different from packet",
name: "destination Pod unavailable",
ps: &crdv1alpha1.PacketSampling{
ObjectMeta: metav1.ObjectMeta{Name: "ps9", UID: "uid9"},
ObjectMeta: metav1.ObjectMeta{Name: "ps11", UID: "uid11"},
Spec: crdv1alpha1.PacketSamplingSpec{
Destination: crdv1alpha1.Destination{
IP: "192.168.1.2",
},
Packet: crdv1alpha1.Packet{
IPv6Header: &crdv1alpha1.IPv6Header{},
Pod: "unknown pod",
Namespace: "default",
},
},
},
expectedErr: "destination IP does not match the IP header family",
expectedErr: "failed to get the destination pod default/unknown pod: pods \"unknown pod\"",
},
{
name: "source IP family different from packet for receiver only case",
name: "to service packet",
ps: &crdv1alpha1.PacketSampling{
ObjectMeta: metav1.ObjectMeta{Name: "ps10", UID: "ps10"},
ObjectMeta: metav1.ObjectMeta{Name: "ps12", UID: "uid12"},
Spec: crdv1alpha1.PacketSamplingSpec{
Source: crdv1alpha1.Source{
IP: "192.168.1.2",
Namespace: pod1.Namespace,
Pod: pod1.Name,
},
Destination: crdv1alpha1.Destination{
Service: service1.Name,
Namespace: service1.Namespace,
},
Packet: crdv1alpha1.Packet{
IPv6Header: &crdv1alpha1.IPv6Header{},
TransportHeader: crdv1alpha1.TransportHeader{
TCP: &crdv1alpha1.TCPHeader{
SrcPort: 80,
DstPort: 81,
Flags: 11,
},
},
},
},
},
receiverOnly: true,
expectedErr: "source IP does not match the IP header family",
},
{
name: "destination Pod unavailable",
ps: &crdv1alpha1.PacketSampling{
ObjectMeta: metav1.ObjectMeta{Name: "ps11", UID: "uid11"},
Spec: crdv1alpha1.PacketSamplingSpec{
Destination: crdv1alpha1.Destination{
Pod: "unknown pod",
Namespace: "default",
},
},
expectedPacket: &binding.Packet{
DestinationIP: net.ParseIP(service1IPv4).To4(),
IPProto: protocol.Type_TCP,
SourcePort: 80,
DestinationPort: 81,
TCPFlags: 11,
},
expectedErr: "failed to get the destination pod default/unknown pod: pods \"unknown pod\"",
},
}
for _, ps := range pss {
Expand All @@ -450,6 +474,12 @@ func TestPreparePacket(t *testing.T) {
if ps.intf != nil {
podInterfaces[0] = ps.intf
}
stopCh := make(chan struct{})
defer close(stopCh)
psc.crdInformerFactory.Start(stopCh)
psc.crdInformerFactory.WaitForCacheSync(stopCh)
psc.informerFactory.Start(stopCh)
psc.informerFactory.WaitForCacheSync(stopCh)

pkt, err := psc.preparePacket(ps.ps, podInterfaces[0], ps.receiverOnly)
if ps.expectedErr == "" {
Expand Down Expand Up @@ -666,3 +696,116 @@ func TestValidateTraceflow(t *testing.T) {
})
}
}

func TestStartPacketSampling(t *testing.T) {
tcs := []struct {
name string
ps *crdv1alpha1.PacketSampling
ofPort uint32
receiverOnly bool
packet *binding.Packet
expectedCalls func(mockOFClient *openflowtest.MockClient)
nodeConfig *config.NodeConfig
expectedErr string
expectedErrLog string
}{
{
name: "Pod-to-Pod PacketSampling",
ps: &crdv1alpha1.PacketSampling{
ObjectMeta: metav1.ObjectMeta{Name: "ps1", UID: "uid1"},
Spec: crdv1alpha1.PacketSamplingSpec{
Source: crdv1alpha1.Source{
Namespace: pod1.Namespace,
Pod: pod1.Name,
},
Destination: crdv1alpha1.Destination{
Namespace: pod2.Namespace,
Pod: pod2.Name,
},
FirstNSamplingConfig: &crdv1alpha1.FirstNSamplingConfig{
Number: 5,
},
},

Status: crdv1alpha1.PacketSamplingStatus{
Phase: crdv1alpha1.PacketSamplingRunning,
DataplaneTag: 1,
},
},
ofPort: ofPortPod1,
packet: &binding.Packet{
SourceIP: net.ParseIP(pod1IPv4),
SourceMAC: pod1MAC,
DestinationIP: net.ParseIP(pod2IPv4),
DestinationMAC: pod2MAC,
IPProto: 1,
TTL: 64,
ICMPType: 8,
},
expectedCalls: func(mockOFClient *openflowtest.MockClient) {
mockOFClient.EXPECT().InstallPacketSamplingFlows(uint8(1), false, false, false, nil, ofPortPod1, uint16(crdv1beta1.DefaultTraceflowTimeout))
},
},
{
name: "Pod-to-IPv4 packetsampling",
ps: &crdv1alpha1.PacketSampling{
ObjectMeta: metav1.ObjectMeta{Name: "ps2", UID: "uid2"},
Spec: crdv1alpha1.PacketSamplingSpec{
Source: crdv1alpha1.Source{
Namespace: pod1.Namespace,
Pod: pod1.Name,
},
Destination: crdv1alpha1.Destination{
IP: dstIPv4,
},
FirstNSamplingConfig: &crdv1alpha1.FirstNSamplingConfig{
Number: 5,
},
},
Status: crdv1alpha1.PacketSamplingStatus{
Phase: crdv1alpha1.PacketSamplingRunning,
DataplaneTag: 1,
},
},
ofPort: ofPortPod1,
packet: &binding.Packet{
SourceIP: net.ParseIP(pod1IPv4),
SourceMAC: pod1MAC,
DestinationIP: net.ParseIP(dstIPv4),
IPProto: 1,
TTL: 64,
ICMPType: 8,
},
expectedCalls: func(mockOFClient *openflowtest.MockClient) {
mockOFClient.EXPECT().InstallPacketSamplingFlows(uint8(1), false, false, false, nil, ofPortPod1, uint16(crdv1beta1.DefaultTraceflowTimeout))
},
},
}

for _, tt := range tcs {
t.Run(tt.name, func(t *testing.T) {
tfc := newFakePacketSamplingController(t, []runtime.Object{tt.ps}, nil, tt.nodeConfig)
if tt.expectedCalls != nil {
tt.expectedCalls(tfc.mockOFClient)
}

bufWriter := bytes.NewBuffer(nil)
klog.SetOutput(bufWriter)
klog.LogToStderr(false)
defer func() {
klog.SetOutput(os.Stderr)
klog.LogToStderr(true)
}()

err := tfc.startPacketSampling(tt.ps)
if tt.expectedErr != "" {
assert.ErrorContains(t, err, tt.expectedErr)
} else {
require.NoError(t, err)
}
if tt.expectedErrLog != "" {
assert.Contains(t, bufWriter.String(), tt.expectedErrLog)
}
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func TestSupportBundleCollectionAdd(t *testing.T) {
supportBundleCollection: generateSupportbundleCollection("supportBundle3", "https://10.220.175.92:22/root/supportbundle"),
expectedCompleted: false,
agentDumper: &mockAgentDumper{},
uploader: &testUploader{},
uploader: &testFailedUploader{},
},
{
name: "Add SupportBundleCollection with retry logics",
Expand Down
2 changes: 1 addition & 1 deletion pkg/util/ftp/ftp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestParseFTPUploadUrl(t *testing.T) {
},
},
{
url: "http://127.0.0.1:22/path",
url: "https://10.220.175.92:22/root/supportbundle",
expectedError: "not sftp protocol",
},
}
Expand Down

0 comments on commit ae9df19

Please sign in to comment.