github.com/aporeto-inc/trireme-lib@v10.358.0+incompatible/monitor/internal/pod/watcher_test.go (about) 1 // +build !windows 2 3 package podmonitor 4 5 import ( 6 "testing" 7 8 . "github.com/smartystreets/goconvey/convey" 9 10 corev1 "k8s.io/api/core/v1" 11 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 12 "sigs.k8s.io/controller-runtime/pkg/handler" 13 ) 14 15 func TestWatchPodMapper(t *testing.T) { 16 Convey("Given a watch pod mapper and a pod", t, func() { 17 m := WatchPodMapper{ 18 // client is currently not in use for this mapper 19 client: nil, 20 nodeName: "testing-node", 21 enableHostPods: true, 22 } 23 p := &corev1.Pod{ 24 ObjectMeta: metav1.ObjectMeta{ 25 Name: "p1", 26 }, 27 Spec: corev1.PodSpec{ 28 NodeName: "testing-node", 29 }, 30 } 31 Convey("do not reconcile if the object is not a pod", func() { 32 svc := &corev1.Service{ 33 ObjectMeta: metav1.ObjectMeta{ 34 Name: "service", 35 }, 36 } 37 reqs := m.Map(handler.MapObject{Meta: svc.GetObjectMeta(), Object: svc}) 38 So(reqs, ShouldHaveLength, 0) 39 }) 40 Convey("do not reconcile if the node name does not match", func() { 41 p.Spec.NodeName = "wrong" 42 reqs := m.Map(handler.MapObject{Meta: p.GetObjectMeta(), Object: p}) 43 So(reqs, ShouldHaveLength, 0) 44 }) 45 Convey("do not reconcile if enabling host pods is not enabled, but the pod has HostNetwork set to true", func() { 46 m.enableHostPods = false 47 p.Spec.HostNetwork = true 48 reqs := m.Map(handler.MapObject{Meta: p.GetObjectMeta(), Object: p}) 49 So(reqs, ShouldHaveLength, 0) 50 }) 51 Convey("reconcile if the node name matches", func() { 52 reqs := m.Map(handler.MapObject{Meta: p.GetObjectMeta(), Object: p}) 53 So(reqs, ShouldHaveLength, 1) 54 }) 55 }) 56 }