Skip to content

Commit 1135a15

Browse files
committed
Add unit test for WantsL4NetLB function in service_test.go
1 parent 931b4c7 commit 1135a15

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

pkg/annotations/service_test.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,3 +715,94 @@ func TestHasStrongSessionAffinityAnnotation(t *testing.T) {
715715
})
716716
}
717717
}
718+
719+
func TestWantsL4NetLB(t *testing.T) {
720+
// sPtr is a helper to return a pointer to a string,
721+
// useful for setting LoadBalancerClass.
722+
sPtr := func(s string) *string { return &s }
723+
724+
for _, tc := range []struct {
725+
desc string
726+
svc *v1.Service
727+
want bool
728+
}{
729+
{
730+
desc: "Nil service",
731+
svc: nil,
732+
want: false,
733+
},
734+
{
735+
desc: "ClusterIP service should not want L4 NetLB",
736+
svc: &v1.Service{
737+
Spec: v1.ServiceSpec{
738+
Type: v1.ServiceTypeClusterIP,
739+
},
740+
},
741+
want: false,
742+
},
743+
{
744+
desc: "Standard LoadBalancer service defaults to External (NetLB)",
745+
svc: &v1.Service{
746+
Spec: v1.ServiceSpec{
747+
Type: v1.ServiceTypeLoadBalancer,
748+
},
749+
},
750+
want: true,
751+
},
752+
{
753+
desc: "LoadBalancer with Internal annotation should not want NetLB",
754+
svc: &v1.Service{
755+
ObjectMeta: metav1.ObjectMeta{
756+
Annotations: map[string]string{
757+
"cloud.google.com/load-balancer-type": string(LBTypeInternal),
758+
},
759+
},
760+
Spec: v1.ServiceSpec{
761+
Type: v1.ServiceTypeLoadBalancer,
762+
},
763+
},
764+
want: false,
765+
},
766+
{
767+
desc: "LoadBalancer with explicit External annotation wants NetLB",
768+
svc: &v1.Service{
769+
ObjectMeta: metav1.ObjectMeta{
770+
Annotations: map[string]string{
771+
"cloud.google.com/load-balancer-type": "External",
772+
},
773+
},
774+
Spec: v1.ServiceSpec{
775+
Type: v1.ServiceTypeLoadBalancer,
776+
},
777+
},
778+
want: true,
779+
},
780+
{
781+
desc: "LoadBalancer with matching Regional External Class wants NetLB",
782+
svc: &v1.Service{
783+
Spec: v1.ServiceSpec{
784+
Type: v1.ServiceTypeLoadBalancer,
785+
LoadBalancerClass: sPtr(RegionalExternalLoadBalancerClass),
786+
},
787+
},
788+
want: true,
789+
},
790+
{
791+
desc: "LoadBalancer with mismatching Class does not want NetLB",
792+
svc: &v1.Service{
793+
Spec: v1.ServiceSpec{
794+
Type: v1.ServiceTypeLoadBalancer,
795+
LoadBalancerClass: sPtr("some-other-custom-class"),
796+
},
797+
},
798+
want: false,
799+
},
800+
} {
801+
t.Run(tc.desc, func(t *testing.T) {
802+
got, _ := WantsL4NetLB(tc.svc)
803+
if got != tc.want {
804+
t.Errorf("WantsL4NetLB() = %v, want %v", got, tc.want)
805+
}
806+
})
807+
}
808+
}

0 commit comments

Comments
 (0)