Skip to content

Commit b97dd1f

Browse files
authored
Merge pull request #3010 from 08volt/test-want-lb
Add unit test for WantsL4NetLB function in service_test.go
2 parents e4a08d4 + f7fa5bc commit b97dd1f

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed

pkg/annotations/service_test.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,3 +715,90 @@ 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 Regional Internal Class does not wants NetLB",
768+
svc: &v1.Service{
769+
Spec: v1.ServiceSpec{
770+
Type: v1.ServiceTypeLoadBalancer,
771+
LoadBalancerClass: sPtr(RegionalInternalLoadBalancerClass),
772+
},
773+
},
774+
want: false,
775+
},
776+
{
777+
desc: "LoadBalancer with matching Regional External Class wants NetLB",
778+
svc: &v1.Service{
779+
Spec: v1.ServiceSpec{
780+
Type: v1.ServiceTypeLoadBalancer,
781+
LoadBalancerClass: sPtr(RegionalExternalLoadBalancerClass),
782+
},
783+
},
784+
want: true,
785+
},
786+
{
787+
desc: "LoadBalancer with mismatching Class does not want NetLB",
788+
svc: &v1.Service{
789+
Spec: v1.ServiceSpec{
790+
Type: v1.ServiceTypeLoadBalancer,
791+
LoadBalancerClass: sPtr("some-other-custom-class"),
792+
},
793+
},
794+
want: false,
795+
},
796+
} {
797+
t.Run(tc.desc, func(t *testing.T) {
798+
got, _ := WantsL4NetLB(tc.svc)
799+
if got != tc.want {
800+
t.Errorf("WantsL4NetLB() = %v, want %v", got, tc.want)
801+
}
802+
})
803+
}
804+
}

0 commit comments

Comments
 (0)