Chore: clearify need update test cases

This commit is contained in:
mowangdk 2023-05-03 13:58:35 +08:00
parent f3cf92b79b
commit e79daae682

View File

@ -67,7 +67,8 @@ func newService(name string, serviceType v1.ServiceType, tweaks ...serviceTweak)
Namespace: "default", Namespace: "default",
}, },
Spec: v1.ServiceSpec{ Spec: v1.ServiceSpec{
Type: serviceType, Type: serviceType,
Ports: makeServicePort(v1.ProtocolTCP, 0),
}, },
} }
for _, tw := range tweaks { for _, tw := range tweaks {
@ -76,6 +77,14 @@ func newService(name string, serviceType v1.ServiceType, tweaks ...serviceTweak)
return s return s
} }
func copyService(oldSvc *v1.Service, tweaks ...serviceTweak) *v1.Service {
newSvc := oldSvc.DeepCopy()
for _, tw := range tweaks {
tw(newSvc)
}
return newSvc
}
func tweakAddETP(etpType v1.ServiceExternalTrafficPolicyType) serviceTweak { func tweakAddETP(etpType v1.ServiceExternalTrafficPolicyType) serviceTweak {
return func(s *v1.Service) { return func(s *v1.Service) {
s.Spec.ExternalTrafficPolicy = etpType s.Spec.ExternalTrafficPolicy = etpType
@ -126,7 +135,7 @@ func tweakAddAppProtocol(appProtocol string) serviceTweak {
} }
} }
func tweakAddIPFamilies(families ...v1.IPFamily) serviceTweak { func tweakSetIPFamilies(families ...v1.IPFamily) serviceTweak {
return func(s *v1.Service) { return func(s *v1.Service) {
s.Spec.IPFamilies = families s.Spec.IPFamilies = families
} }
@ -207,7 +216,7 @@ func TestSyncLoadBalancerIfNeeded(t *testing.T) {
expectPatchFinalizer: true, expectPatchFinalizer: true,
}, { }, {
desc: "tcp service that wants LB", desc: "tcp service that wants LB",
service: newService("basic-service1", v1.ServiceTypeLoadBalancer, tweakAddPorts(v1.ProtocolTCP, 0)), service: newService("basic-service1", v1.ServiceTypeLoadBalancer),
expectOp: ensureLoadBalancer, expectOp: ensureLoadBalancer,
expectCreateAttempt: true, expectCreateAttempt: true,
expectPatchStatus: true, expectPatchStatus: true,
@ -237,7 +246,7 @@ func TestSyncLoadBalancerIfNeeded(t *testing.T) {
expectPatchFinalizer: true, expectPatchFinalizer: true,
}, { }, {
desc: "service that needs cleanup", desc: "service that needs cleanup",
service: newService("basic-service1", v1.ServiceTypeLoadBalancer, tweakAddLBIngress("8.8.8.8"), tweakAddPorts(v1.ProtocolTCP, 0), tweakAddFinalizers(servicehelper.LoadBalancerCleanupFinalizer), tweakAddDeletionTimestamp(time.Now())), service: newService("basic-service1", v1.ServiceTypeLoadBalancer, tweakAddLBIngress("8.8.8.8"), tweakAddFinalizers(servicehelper.LoadBalancerCleanupFinalizer), tweakAddDeletionTimestamp(time.Now())),
lbExists: true, lbExists: true,
expectOp: deleteLoadBalancer, expectOp: deleteLoadBalancer,
expectDeleteAttempt: true, expectDeleteAttempt: true,
@ -245,7 +254,7 @@ func TestSyncLoadBalancerIfNeeded(t *testing.T) {
expectPatchFinalizer: true, expectPatchFinalizer: true,
}, { }, {
desc: "service with finalizer that wants LB", desc: "service with finalizer that wants LB",
service: newService("basic-service1", v1.ServiceTypeLoadBalancer, tweakAddPorts(v1.ProtocolTCP, 0), tweakAddFinalizers(servicehelper.LoadBalancerCleanupFinalizer)), service: newService("basic-service1", v1.ServiceTypeLoadBalancer, tweakAddFinalizers(servicehelper.LoadBalancerCleanupFinalizer)),
expectOp: ensureLoadBalancer, expectOp: ensureLoadBalancer,
expectCreateAttempt: true, expectCreateAttempt: true,
expectPatchStatus: true, expectPatchStatus: true,
@ -1359,8 +1368,7 @@ func TestNeedsUpdate(t *testing.T) {
testName: "If TargetGroup is different 1", testName: "If TargetGroup is different 1",
updateFn: func() (oldSvc *v1.Service, newSvc *v1.Service) { updateFn: func() (oldSvc *v1.Service, newSvc *v1.Service) {
oldSvc = newService("tcp-service", v1.ServiceTypeLoadBalancer, tweakAddPorts(v1.ProtocolTCP, 20)) oldSvc = newService("tcp-service", v1.ServiceTypeLoadBalancer, tweakAddPorts(v1.ProtocolTCP, 20))
newSvc = oldSvc.DeepCopy() newSvc = copyService(oldSvc, tweakAddPorts(v1.ProtocolTCP, 21))
newSvc.Spec.Ports[0].TargetPort = intstr.Parse("21")
return return
}, },
expectedNeedsUpdate: true, expectedNeedsUpdate: true,
@ -1376,54 +1384,48 @@ func TestNeedsUpdate(t *testing.T) {
}, { }, {
testName: "If appProtocol is the same", testName: "If appProtocol is the same",
updateFn: func() (oldSvc *v1.Service, newSvc *v1.Service) { updateFn: func() (oldSvc *v1.Service, newSvc *v1.Service) {
oldSvc = newService("tcp-service", v1.ServiceTypeLoadBalancer, tweakAddPorts(v1.ProtocolTCP, 22)) oldSvc = newService("tcp-service", v1.ServiceTypeLoadBalancer)
newSvc = oldSvc.DeepCopy() newSvc = copyService(oldSvc)
return return
}, },
expectedNeedsUpdate: false, expectedNeedsUpdate: false,
}, { }, {
testName: "If service IPFamilies from single stack to dual stack", testName: "If service IPFamilies from single stack to dual stack",
updateFn: func() (oldSvc *v1.Service, newSvc *v1.Service) { updateFn: func() (oldSvc *v1.Service, newSvc *v1.Service) {
oldSvc = newService("tcp-service", v1.ServiceTypeLoadBalancer, tweakAddPorts(v1.ProtocolTCP, 22), tweakAddAppProtocol("http"), tweakAddIPFamilies(v1.IPv4Protocol)) oldSvc = newService("tcp-service", v1.ServiceTypeLoadBalancer, tweakSetIPFamilies(v1.IPv4Protocol))
newSvc = oldSvc.DeepCopy() newSvc = copyService(oldSvc, tweakSetIPFamilies(v1.IPv4Protocol, v1.IPv6Protocol))
newSvc.Spec.IPFamilies = []v1.IPFamily{v1.IPv4Protocol, v1.IPv6Protocol}
return return
}, },
expectedNeedsUpdate: true, expectedNeedsUpdate: true,
}, { }, {
testName: "If service IPFamilies from dual stack to single stack", testName: "If service IPFamilies from dual stack to single stack",
updateFn: func() (oldSvc *v1.Service, newSvc *v1.Service) { updateFn: func() (oldSvc *v1.Service, newSvc *v1.Service) {
oldSvc = newService("tcp-service", v1.ServiceTypeLoadBalancer, tweakAddPorts(v1.ProtocolTCP, 22), tweakAddAppProtocol("http"), tweakAddIPFamilies(v1.IPv4Protocol, v1.IPv6Protocol)) oldSvc = newService("tcp-service", v1.ServiceTypeLoadBalancer, tweakSetIPFamilies(v1.IPv4Protocol, v1.IPv6Protocol))
newSvc = oldSvc.DeepCopy() newSvc = copyService(oldSvc, tweakSetIPFamilies(v1.IPv4Protocol))
newSvc.Spec.IPFamilies = []v1.IPFamily{v1.IPv4Protocol}
return return
}, },
expectedNeedsUpdate: true, expectedNeedsUpdate: true,
}, { }, {
testName: "If service IPFamilies not change", testName: "If service IPFamilies not change",
updateFn: func() (oldSvc *v1.Service, newSvc *v1.Service) { updateFn: func() (oldSvc *v1.Service, newSvc *v1.Service) {
oldSvc = newService("tcp-service", v1.ServiceTypeLoadBalancer, tweakAddPorts(v1.ProtocolTCP, 22), tweakAddAppProtocol("http"), tweakAddIPFamilies(v1.IPv4Protocol)) oldSvc = newService("tcp-service", v1.ServiceTypeLoadBalancer, tweakSetIPFamilies(v1.IPv4Protocol))
newSvc = oldSvc.DeepCopy() newSvc = copyService(oldSvc)
return return
}, },
expectedNeedsUpdate: false, expectedNeedsUpdate: false,
}, { }, {
testName: "If appProtocol is set when previously unset", testName: "If appProtocol is set when previously unset",
updateFn: func() (oldSvc *v1.Service, newSvc *v1.Service) { updateFn: func() (oldSvc *v1.Service, newSvc *v1.Service) {
oldSvc = newService("tcp-service", v1.ServiceTypeLoadBalancer, tweakAddPorts(v1.ProtocolTCP, 22)) oldSvc = newService("tcp-service", v1.ServiceTypeLoadBalancer)
newSvc = oldSvc.DeepCopy() newSvc = copyService(oldSvc, tweakAddAppProtocol("http"))
protocol := "http"
newSvc.Spec.Ports[0].AppProtocol = &protocol
return return
}, },
expectedNeedsUpdate: true, expectedNeedsUpdate: true,
}, { }, {
testName: "If appProtocol is set to a different value", testName: "If appProtocol is set to a different value",
updateFn: func() (oldSvc *v1.Service, newSvc *v1.Service) { updateFn: func() (oldSvc *v1.Service, newSvc *v1.Service) {
oldSvc = newService("tcp-service", v1.ServiceTypeLoadBalancer, tweakAddPorts(v1.ProtocolTCP, 22), tweakAddAppProtocol("http")) oldSvc = newService("tcp-service", v1.ServiceTypeLoadBalancer, tweakAddAppProtocol("http"))
newSvc = oldSvc.DeepCopy() newSvc = copyService(oldSvc, tweakAddAppProtocol("tcp"))
newProtocol := "tcp"
newSvc.Spec.Ports[0].AppProtocol = &newProtocol
return return
}, },
expectedNeedsUpdate: true, expectedNeedsUpdate: true,