github.com/aporeto-inc/trireme-lib@v10.358.0+incompatible/monitor/internal/pod/resync_test.go (about) 1 // +build !windows 2 3 package podmonitor 4 5 import ( 6 "context" 7 "testing" 8 9 . "github.com/smartystreets/goconvey/convey" 10 11 corev1 "k8s.io/api/core/v1" 12 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 13 14 fakeclient "sigs.k8s.io/controller-runtime/pkg/client/fake" 15 "sigs.k8s.io/controller-runtime/pkg/event" 16 ) 17 18 func TestResyncWithAllPods(t *testing.T) { 19 Convey("Given a client, two pods and an event channel", t, func() { 20 ctx := context.TODO() 21 evCh := make(chan event.GenericEvent, 100) 22 resyncInfo := NewResyncInfoChan() 23 pod1 := &corev1.Pod{ 24 ObjectMeta: metav1.ObjectMeta{ 25 Name: "pod1", 26 Namespace: "default", 27 }, 28 Spec: corev1.PodSpec{ 29 NodeName: "node", 30 }, 31 } 32 pod2 := &corev1.Pod{ 33 ObjectMeta: metav1.ObjectMeta{ 34 Name: "pod2", 35 Namespace: "default", 36 }, 37 Spec: corev1.PodSpec{ 38 NodeName: "node", 39 }, 40 } 41 c := fakeclient.NewFakeClient(pod1, pod2) 42 43 Convey("resync should fail if there is no client", func() { 44 err := ResyncWithAllPods(ctx, nil, resyncInfo, evCh, "node") 45 So(err, ShouldNotBeNil) 46 So(err.Error(), ShouldEqual, "pod: no client available") 47 }) 48 49 Convey("resync should fail if there is no event channel", func() { 50 err := ResyncWithAllPods(ctx, c, resyncInfo, nil, "node") 51 So(err, ShouldNotBeNil) 52 So(err.Error(), ShouldEqual, "pod: no event source available") 53 }) 54 55 Convey("resync should successfully send messages with all pods", func() { 56 resyncInfo.EnableNeedsInfo() 57 resyncInfo.SendInfo("default/pod1") 58 resyncInfo.SendInfo("default/pod2") 59 err := ResyncWithAllPods(ctx, c, resyncInfo, evCh, "node") 60 So(err, ShouldBeNil) 61 allPods := []string{"pod1", "pod2"} 62 collectedPods := []string{} 63 64 obj1 := <-evCh 65 So(obj1.Meta.GetName(), ShouldBeIn, allPods) 66 collectedPods = append(collectedPods, obj1.Meta.GetName()) 67 obj2 := <-evCh 68 So(obj2.Meta.GetName(), ShouldBeIn, allPods) 69 collectedPods = append(collectedPods, obj2.Meta.GetName()) 70 So("pod1", ShouldBeIn, collectedPods) 71 So("pod2", ShouldBeIn, collectedPods) 72 So(obj1.Meta.GetName(), ShouldNotEqual, obj2.Meta.GetName()) 73 }) 74 }) 75 }