Chore: Replace re-initialized variables with create new ones

This commit is contained in:
mowangdk 2023-04-26 15:15:12 +08:00
parent f7ce6f834a
commit 152c1a0272

View File

@ -1258,24 +1258,23 @@ func TestNeedsCleanup(t *testing.T) {
func TestNeedsUpdate(t *testing.T) { func TestNeedsUpdate(t *testing.T) {
var oldSvc, newSvc *v1.Service
testCases := []struct { testCases := []struct {
testName string //Name of the test case testName string //Name of the test case
updateFn func() //Function to update the service object updateFn func() (*v1.Service, *v1.Service) //Function to update the service object
expectedNeedsUpdate bool //needsupdate always returns bool expectedNeedsUpdate bool //needsupdate always returns bool
}{{ }{{
testName: "If the service type is changed from LoadBalancer to ClusterIP", testName: "If the service type is changed from LoadBalancer to ClusterIP",
updateFn: func() { updateFn: func() (oldSvc *v1.Service, newSvc *v1.Service) {
oldSvc = defaultExternalService() oldSvc = defaultExternalService()
newSvc = defaultExternalService() newSvc = defaultExternalService()
newSvc.Spec.Type = v1.ServiceTypeClusterIP newSvc.Spec.Type = v1.ServiceTypeClusterIP
return
}, },
expectedNeedsUpdate: true, expectedNeedsUpdate: true,
}, { }, {
testName: "If the Ports are different", testName: "If the Ports are different",
updateFn: func() { updateFn: func() (oldSvc *v1.Service, newSvc *v1.Service) {
oldSvc = defaultExternalService() oldSvc = defaultExternalService()
newSvc = defaultExternalService() newSvc = defaultExternalService()
oldSvc.Spec.Ports = []v1.ServicePort{ oldSvc.Spec.Ports = []v1.ServicePort{
@ -1300,121 +1299,134 @@ func TestNeedsUpdate(t *testing.T) {
Port: 10001, Port: 10001,
}, },
} }
return
}, },
expectedNeedsUpdate: true, expectedNeedsUpdate: true,
}, { }, {
testName: "If external ip counts are different", testName: "If external ip counts are different",
updateFn: func() { updateFn: func() (oldSvc *v1.Service, newSvc *v1.Service) {
oldSvc = defaultExternalService() oldSvc = defaultExternalService()
newSvc = defaultExternalService() newSvc = defaultExternalService()
oldSvc.Spec.ExternalIPs = []string{"old.IP.1"} oldSvc.Spec.ExternalIPs = []string{"old.IP.1"}
newSvc.Spec.ExternalIPs = []string{"new.IP.1", "new.IP.2"} newSvc.Spec.ExternalIPs = []string{"new.IP.1", "new.IP.2"}
return
}, },
expectedNeedsUpdate: true, expectedNeedsUpdate: true,
}, { }, {
testName: "If external ips are different", testName: "If external ips are different",
updateFn: func() { updateFn: func() (oldSvc *v1.Service, newSvc *v1.Service) {
oldSvc = defaultExternalService() oldSvc = defaultExternalService()
newSvc = defaultExternalService() newSvc = defaultExternalService()
oldSvc.Spec.ExternalIPs = []string{"old.IP.1", "old.IP.2"} oldSvc.Spec.ExternalIPs = []string{"old.IP.1", "old.IP.2"}
newSvc.Spec.ExternalIPs = []string{"new.IP.1", "new.IP.2"} newSvc.Spec.ExternalIPs = []string{"new.IP.1", "new.IP.2"}
return
}, },
expectedNeedsUpdate: true, expectedNeedsUpdate: true,
}, { }, {
testName: "If UID is different", testName: "If UID is different",
updateFn: func() { updateFn: func() (oldSvc *v1.Service, newSvc *v1.Service) {
oldSvc = defaultExternalService() oldSvc = defaultExternalService()
newSvc = defaultExternalService() newSvc = defaultExternalService()
oldSvc.UID = types.UID("UID old") oldSvc.UID = types.UID("UID old")
newSvc.UID = types.UID("UID new") newSvc.UID = types.UID("UID new")
return
}, },
expectedNeedsUpdate: true, expectedNeedsUpdate: true,
}, { }, {
testName: "If ExternalTrafficPolicy is different", testName: "If ExternalTrafficPolicy is different",
updateFn: func() { updateFn: func() (oldSvc *v1.Service, newSvc *v1.Service) {
oldSvc = defaultExternalService() oldSvc = defaultExternalService()
newSvc = defaultExternalService() newSvc = defaultExternalService()
newSvc.Spec.ExternalTrafficPolicy = v1.ServiceExternalTrafficPolicyLocal newSvc.Spec.ExternalTrafficPolicy = v1.ServiceExternalTrafficPolicyLocal
return
}, },
expectedNeedsUpdate: true, expectedNeedsUpdate: true,
}, { }, {
testName: "If HealthCheckNodePort is different", testName: "If HealthCheckNodePort is different",
updateFn: func() { updateFn: func() (oldSvc *v1.Service, newSvc *v1.Service) {
oldSvc = defaultExternalService() oldSvc = defaultExternalService()
newSvc = defaultExternalService() newSvc = defaultExternalService()
newSvc.Spec.HealthCheckNodePort = 30123 newSvc.Spec.HealthCheckNodePort = 30123
return
}, },
expectedNeedsUpdate: true, expectedNeedsUpdate: true,
}, { }, {
testName: "If TargetGroup is different 1", testName: "If TargetGroup is different 1",
updateFn: func() { 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 = oldSvc.DeepCopy()
newSvc.Spec.Ports[0].TargetPort = intstr.Parse("21") newSvc.Spec.Ports[0].TargetPort = intstr.Parse("21")
return
}, },
expectedNeedsUpdate: true, expectedNeedsUpdate: true,
}, { }, {
testName: "If TargetGroup is different 2", testName: "If TargetGroup is different 2",
updateFn: func() { updateFn: func() (oldSvc *v1.Service, newSvc *v1.Service) {
oldSvc = newService("tcp-service", v1.ServiceTypeLoadBalancer, tweakAddPorts(v1.ProtocolTCP, 22)) oldSvc = newService("tcp-service", v1.ServiceTypeLoadBalancer, tweakAddPorts(v1.ProtocolTCP, 22))
newSvc = oldSvc.DeepCopy() newSvc = oldSvc.DeepCopy()
newSvc.Spec.Ports[0].TargetPort = intstr.Parse("dns") newSvc.Spec.Ports[0].TargetPort = intstr.Parse("dns")
return
}, },
expectedNeedsUpdate: true, expectedNeedsUpdate: true,
}, { }, {
testName: "If appProtocol is the same", testName: "If appProtocol is the same",
updateFn: func() { updateFn: func() (oldSvc *v1.Service, newSvc *v1.Service) {
oldSvc = newService("tcp-service", v1.ServiceTypeLoadBalancer, tweakAddPorts(v1.ProtocolTCP, 22)) oldSvc = newService("tcp-service", v1.ServiceTypeLoadBalancer, tweakAddPorts(v1.ProtocolTCP, 22))
newSvc = oldSvc.DeepCopy() newSvc = oldSvc.DeepCopy()
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() { 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, tweakAddPorts(v1.ProtocolTCP, 22), tweakAddAppProtocol("http"), tweakAddIPFamilies(v1.IPv4Protocol))
newSvc = oldSvc.DeepCopy() newSvc = oldSvc.DeepCopy()
newSvc.Spec.IPFamilies = []v1.IPFamily{v1.IPv4Protocol, v1.IPv6Protocol} newSvc.Spec.IPFamilies = []v1.IPFamily{v1.IPv4Protocol, v1.IPv6Protocol}
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() { 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, tweakAddPorts(v1.ProtocolTCP, 22), tweakAddAppProtocol("http"), tweakAddIPFamilies(v1.IPv4Protocol, v1.IPv6Protocol))
newSvc = oldSvc.DeepCopy() newSvc = oldSvc.DeepCopy()
newSvc.Spec.IPFamilies = []v1.IPFamily{v1.IPv4Protocol} newSvc.Spec.IPFamilies = []v1.IPFamily{v1.IPv4Protocol}
return
}, },
expectedNeedsUpdate: true, expectedNeedsUpdate: true,
}, { }, {
testName: "If service IPFamilies not change", testName: "If service IPFamilies not change",
updateFn: func() { 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, tweakAddPorts(v1.ProtocolTCP, 22), tweakAddAppProtocol("http"), tweakAddIPFamilies(v1.IPv4Protocol))
newSvc = oldSvc.DeepCopy() newSvc = oldSvc.DeepCopy()
return
}, },
expectedNeedsUpdate: false, expectedNeedsUpdate: false,
}, { }, {
testName: "If appProtocol is set when previously unset", testName: "If appProtocol is set when previously unset",
updateFn: func() { updateFn: func() (oldSvc *v1.Service, newSvc *v1.Service) {
oldSvc = newService("tcp-service", v1.ServiceTypeLoadBalancer, tweakAddPorts(v1.ProtocolTCP, 22)) oldSvc = newService("tcp-service", v1.ServiceTypeLoadBalancer, tweakAddPorts(v1.ProtocolTCP, 22))
newSvc = oldSvc.DeepCopy() newSvc = oldSvc.DeepCopy()
protocol := "http" protocol := "http"
newSvc.Spec.Ports[0].AppProtocol = &protocol newSvc.Spec.Ports[0].AppProtocol = &protocol
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() { 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, tweakAddPorts(v1.ProtocolTCP, 22), tweakAddAppProtocol("http"))
newSvc = oldSvc.DeepCopy() newSvc = oldSvc.DeepCopy()
newProtocol := "tcp" newProtocol := "tcp"
newSvc.Spec.Ports[0].AppProtocol = &newProtocol newSvc.Spec.Ports[0].AppProtocol = &newProtocol
return
}, },
expectedNeedsUpdate: true, expectedNeedsUpdate: true,
}} }}
controller, _, _ := newController() controller, _, _ := newController()
for _, tc := range testCases { for _, tc := range testCases {
tc.updateFn() oldSvc, newSvc := tc.updateFn()
obtainedResult := controller.needsUpdate(oldSvc, newSvc) obtainedResult := controller.needsUpdate(oldSvc, newSvc)
if obtainedResult != tc.expectedNeedsUpdate { if obtainedResult != tc.expectedNeedsUpdate {
t.Errorf("%v needsUpdate() should have returned %v but returned %v", tc.testName, tc.expectedNeedsUpdate, obtainedResult) t.Errorf("%v needsUpdate() should have returned %v but returned %v", tc.testName, tc.expectedNeedsUpdate, obtainedResult)