66 "flag"
77 "fmt"
88 "reflect"
9+ "strings"
910 "testing"
1011
1112 "github.com/google/uuid"
@@ -619,6 +620,26 @@ func TestUpdate(t *testing.T) {
619620 serviceCheck ("adifferentsvc" , "test" ).check ,
620621 negativeServiceCheck ("mysvc" , "test" ),
621622 },
623+ }, {
624+ name : "exposePodsByName handles pod delete" ,
625+ k8sObjects : []runtime.Object {
626+ f .skupperNetworkStatus ("test" , f .networkStatusInfo ("mysite" , "test" , map [string ]string {"mysvc" : "mysvc" , "mysvc.mypod-1" : "mysvc@mypod-1" , "mysvc.mypod-2" : "mysvc@mypod-2" }, map [string ]string {"mysvc" : "mysvc" , "mysvc.mypod-1" : "10.1.1.10" , "mysvc.mypod-2" : "10.1.1.11" }).info ()),
627+ f .pod ("mypod-1" , "test" , map [string ]string {"app" : "foo" }, nil , f .podStatus ("10.1.1.10" , corev1 .PodRunning , f .podCondition (corev1 .PodReady , corev1 .ConditionTrue ))),
628+ f .pod ("mypod-2" , "test" , map [string ]string {"app" : "foo" }, nil , f .podStatus ("10.1.1.11" , corev1 .PodRunning , f .podCondition (corev1 .PodReady , corev1 .ConditionTrue ))),
629+ },
630+ skupperObjects : []runtime.Object {
631+ f .site ("mysite" , "test" , "" , false , false ),
632+ f .connectorWithExposePodsByName ("myconnector" , "test" , "mysvc" , "app=foo" , 8080 ),
633+ f .listenerWithExposePodsByName ("mylistener" , "test" , "mysvc" , "mysvc" , 8080 ),
634+ },
635+ functions : []WaitFunction {
636+ isConnectorStatusConditionTrue ("myconnector" , "test" , skupperv2alpha1 .CONDITION_TYPE_CONFIGURED ),
637+ isListenerStatusConditionTrue ("mylistener" , "test" , skupperv2alpha1 .CONDITION_TYPE_CONFIGURED ),
638+ serviceCheck ("mysvc" , "test" ).check ,
639+ serviceCheck ("mypod-1" , "test" ).check ,
640+ deleteTargetPod ("mypod-1" , "test" ),
641+ serviceCheck ("mypod-1" , "test" ).checkAbsent ,
642+ },
622643 },
623644 }
624645 for _ , tt := range testTable {
@@ -641,6 +662,7 @@ func TestUpdate(t *testing.T) {
641662 for i := 0 ; i < len (tt .k8sObjects )+ len (tt .skupperObjects ); i ++ {
642663 controller .controller .TestProcess ()
643664 }
665+
644666 for _ , f := range tt .functions {
645667 for ! f (t , clients ) {
646668 controller .controller .TestProcess ()
@@ -1645,26 +1667,6 @@ func serviceCheck(name string, namespace string) *ServiceCheck {
16451667 }
16461668}
16471669
1648- func (s * ServiceCheck ) selector (selector map [string ]string ) * ServiceCheck {
1649- s .selector_ = selector
1650- return s
1651- }
1652-
1653- func (s * ServiceCheck ) ports (ports ... corev1.ServicePort ) * ServiceCheck {
1654- s .ports_ = ports
1655- return s
1656- }
1657-
1658- func (s * ServiceCheck ) labels (labels map [string ]string ) * ServiceCheck {
1659- s .labels_ = labels
1660- return s
1661- }
1662-
1663- func (s * ServiceCheck ) annotations (annotations map [string ]string ) * ServiceCheck {
1664- s .annotations_ = annotations
1665- return s
1666- }
1667-
16681670func (s * ServiceCheck ) check (t * testing.T , clients internalclient.Clients ) bool {
16691671 actual , err := clients .GetKubeClient ().CoreV1 ().Services (s .namespace ).Get (context .Background (), s .name , metav1.GetOptions {})
16701672 assert .Assert (t , err )
@@ -1692,6 +1694,18 @@ func (s *ServiceCheck) check(t *testing.T, clients internalclient.Clients) bool
16921694 return true
16931695}
16941696
1697+ func (s * ServiceCheck ) checkAbsent (t * testing.T , clients internalclient.Clients ) bool {
1698+ _ , err := clients .GetKubeClient ().CoreV1 ().Services (s .namespace ).Get (context .Background (), s .name , metav1.GetOptions {})
1699+ if err == nil {
1700+ return false
1701+ }
1702+ if errors .IsNotFound (err ) {
1703+ return true
1704+ }
1705+ assert .Assert (t , err )
1706+ return false
1707+ }
1708+
16951709func updateListener (name string , namespace string , host string , port int ) WaitFunction {
16961710 return func (t * testing.T , clients internalclient.Clients ) bool {
16971711 ctxt := context .Background ()
@@ -1713,3 +1727,34 @@ func negativeServiceCheck(name string, namespace string) WaitFunction {
17131727 return true
17141728 }
17151729}
1730+
1731+ func deleteTargetPod (name string , namespace string ) WaitFunction {
1732+ return func (t * testing.T , clients internalclient.Clients ) bool {
1733+ ctxt := context .Background ()
1734+ err := clients .GetKubeClient ().CoreV1 ().Pods (namespace ).Delete (ctxt , name , metav1.DeleteOptions {})
1735+ assert .Assert (t , err )
1736+ cm , err := clients .GetKubeClient ().CoreV1 ().ConfigMaps (namespace ).Get (ctxt , "skupper-network-status" , metav1.GetOptions {})
1737+ assert .Assert (t , err )
1738+ cm = cm .DeepCopy ()
1739+ var status network.NetworkStatusInfo
1740+ assert .Assert (t , json .Unmarshal ([]byte (cm .Data ["NetworkStatus" ]), & status ))
1741+ suffix := "." + name
1742+ for sIdx := range status .SiteStatus {
1743+ for rIdx , rs := range status .SiteStatus [sIdx ].RouterStatus {
1744+ connectors := rs .Connectors [:0 ]
1745+ for _ , c := range rs .Connectors {
1746+ if ! strings .HasSuffix (c .Address , suffix ) {
1747+ connectors = append (connectors , c )
1748+ }
1749+ }
1750+ status .SiteStatus [sIdx ].RouterStatus [rIdx ].Connectors = connectors
1751+ }
1752+ }
1753+ sb , err := json .Marshal (status )
1754+ assert .Assert (t , err )
1755+ cm .Data ["NetworkStatus" ] = string (sb )
1756+ _ , err = clients .GetKubeClient ().CoreV1 ().ConfigMaps (namespace ).Update (ctxt , cm , metav1.UpdateOptions {})
1757+ assert .Assert (t , err )
1758+ return true
1759+ }
1760+ }
0 commit comments