do some cleanup on TestNormalizeClusterIPs

This commit is contained in:
jornshen 2021-02-16 00:32:00 +08:00
parent f81235605a
commit 1e09a758c5

View File

@ -109,6 +109,15 @@ func makeValidServiceCustom(tweaks ...func(svc *api.Service)) *api.Service {
return svc return svc
} }
func makeServiceWithClusterIp(clusterIP string, clusterIPs []string) *api.Service {
return &api.Service{
Spec: api.ServiceSpec{
ClusterIP: clusterIP,
ClusterIPs: clusterIPs,
},
}
}
// TODO: This should be done on types that are not part of our API // TODO: This should be done on types that are not part of our API
func TestBeforeUpdate(t *testing.T) { func TestBeforeUpdate(t *testing.T) {
testCases := []struct { testCases := []struct {
@ -457,275 +466,115 @@ func TestNormalizeClusterIPs(t *testing.T) {
expectedClusterIP string expectedClusterIP string
expectedClusterIPs []string expectedClusterIPs []string
}{ }{
{ {
name: "new - only clusterip used", name: "new - only clusterip used",
oldService: nil, oldService: nil,
newService: &api.Service{ newService: makeServiceWithClusterIp("10.0.0.10", nil),
Spec: api.ServiceSpec{
ClusterIP: "10.0.0.10",
ClusterIPs: nil,
},
},
expectedClusterIP: "10.0.0.10", expectedClusterIP: "10.0.0.10",
expectedClusterIPs: []string{"10.0.0.10"}, expectedClusterIPs: []string{"10.0.0.10"},
}, },
{ {
name: "new - only clusterips used", name: "new - only clusterips used",
oldService: nil, oldService: nil,
newService: &api.Service{ newService: makeServiceWithClusterIp("", []string{"10.0.0.10"}),
Spec: api.ServiceSpec{
ClusterIP: "",
ClusterIPs: []string{"10.0.0.10"},
},
},
expectedClusterIP: "", // this is a validation issue, and validation will catch it expectedClusterIP: "", // this is a validation issue, and validation will catch it
expectedClusterIPs: []string{"10.0.0.10"}, expectedClusterIPs: []string{"10.0.0.10"},
}, },
{ {
name: "new - both used", name: "new - both used",
oldService: nil, oldService: nil,
newService: &api.Service{ newService: makeServiceWithClusterIp("10.0.0.10", []string{"10.0.0.10"}),
Spec: api.ServiceSpec{
ClusterIP: "10.0.0.10",
ClusterIPs: []string{"10.0.0.10"},
},
},
expectedClusterIP: "10.0.0.10", expectedClusterIP: "10.0.0.10",
expectedClusterIPs: []string{"10.0.0.10"}, expectedClusterIPs: []string{"10.0.0.10"},
}, },
{ {
name: "update - no change", name: "update - no change",
oldService: &api.Service{ oldService: makeServiceWithClusterIp("10.0.0.10", []string{"10.0.0.10"}),
Spec: api.ServiceSpec{ newService: makeServiceWithClusterIp("10.0.0.10", []string{"10.0.0.10"}),
ClusterIP: "10.0.0.10",
ClusterIPs: []string{"10.0.0.10"},
},
},
newService: &api.Service{
Spec: api.ServiceSpec{
ClusterIP: "10.0.0.10",
ClusterIPs: []string{"10.0.0.10"},
},
},
expectedClusterIP: "10.0.0.10", expectedClusterIP: "10.0.0.10",
expectedClusterIPs: []string{"10.0.0.10"}, expectedClusterIPs: []string{"10.0.0.10"},
}, },
{ {
name: "update - malformed change", name: "update - malformed change",
oldService: &api.Service{ oldService: makeServiceWithClusterIp("10.0.0.10", []string{"10.0.0.10"}),
Spec: api.ServiceSpec{ newService: makeServiceWithClusterIp("10.0.0.11", []string{"10.0.0.11"}),
ClusterIP: "10.0.0.10",
ClusterIPs: []string{"10.0.0.10"},
},
},
newService: &api.Service{
Spec: api.ServiceSpec{
ClusterIP: "10.0.0.11",
ClusterIPs: []string{"10.0.0.11"},
},
},
expectedClusterIP: "10.0.0.11", expectedClusterIP: "10.0.0.11",
expectedClusterIPs: []string{"10.0.0.11"}, expectedClusterIPs: []string{"10.0.0.11"},
}, },
{ {
name: "update - malformed change on secondary ip", name: "update - malformed change on secondary ip",
oldService: &api.Service{ oldService: makeServiceWithClusterIp("10.0.0.10", []string{"10.0.0.10", "2000::1"}),
Spec: api.ServiceSpec{ newService: makeServiceWithClusterIp("10.0.0.11", []string{"10.0.0.11", "3000::1"}),
ClusterIP: "10.0.0.10",
ClusterIPs: []string{"10.0.0.10", "2000::1"},
},
},
newService: &api.Service{
Spec: api.ServiceSpec{
ClusterIP: "10.0.0.11",
ClusterIPs: []string{"10.0.0.11", "3000::1"},
},
},
expectedClusterIP: "10.0.0.11", expectedClusterIP: "10.0.0.11",
expectedClusterIPs: []string{"10.0.0.11", "3000::1"}, expectedClusterIPs: []string{"10.0.0.11", "3000::1"},
}, },
{ {
name: "update - upgrade", name: "update - upgrade",
oldService: &api.Service{ oldService: makeServiceWithClusterIp("10.0.0.10", []string{"10.0.0.10"}),
Spec: api.ServiceSpec{ newService: makeServiceWithClusterIp("10.0.0.10", []string{"10.0.0.10", "2000::1"}),
ClusterIP: "10.0.0.10",
ClusterIPs: []string{"10.0.0.10"},
},
},
newService: &api.Service{
Spec: api.ServiceSpec{
ClusterIP: "10.0.0.10",
ClusterIPs: []string{"10.0.0.10", "2000::1"},
},
},
expectedClusterIP: "10.0.0.10", expectedClusterIP: "10.0.0.10",
expectedClusterIPs: []string{"10.0.0.10", "2000::1"}, expectedClusterIPs: []string{"10.0.0.10", "2000::1"},
}, },
{ {
name: "update - downgrade", name: "update - downgrade",
oldService: &api.Service{ oldService: makeServiceWithClusterIp("10.0.0.10", []string{"10.0.0.10", "2000::1"}),
Spec: api.ServiceSpec{ newService: makeServiceWithClusterIp("10.0.0.10", []string{"10.0.0.10"}),
ClusterIP: "10.0.0.10",
ClusterIPs: []string{"10.0.0.10", "2000::1"},
},
},
newService: &api.Service{
Spec: api.ServiceSpec{
ClusterIP: "10.0.0.10",
ClusterIPs: []string{"10.0.0.10"},
},
},
expectedClusterIP: "10.0.0.10", expectedClusterIP: "10.0.0.10",
expectedClusterIPs: []string{"10.0.0.10"}, expectedClusterIPs: []string{"10.0.0.10"},
}, },
{ {
name: "update - user cleared cluster IP", name: "update - user cleared cluster IP",
oldService: &api.Service{ oldService: makeServiceWithClusterIp("10.0.0.10", []string{"10.0.0.10"}),
Spec: api.ServiceSpec{ newService: makeServiceWithClusterIp("", []string{"10.0.0.10"}),
ClusterIP: "10.0.0.10",
ClusterIPs: []string{"10.0.0.10"},
},
},
newService: &api.Service{
Spec: api.ServiceSpec{
ClusterIP: "",
ClusterIPs: []string{"10.0.0.10"},
},
},
expectedClusterIP: "", expectedClusterIP: "",
expectedClusterIPs: nil, expectedClusterIPs: nil,
}, },
{ {
name: "update - user cleared clusterIPs", // *MUST* REMAIN FOR OLD CLIENTS name: "update - user cleared clusterIPs", // *MUST* REMAIN FOR OLD CLIENTS
oldService: &api.Service{ oldService: makeServiceWithClusterIp("10.0.0.10", []string{"10.0.0.10"}),
Spec: api.ServiceSpec{ newService: makeServiceWithClusterIp("10.0.0.10", nil),
ClusterIP: "10.0.0.10",
ClusterIPs: []string{"10.0.0.10"},
},
},
newService: &api.Service{
Spec: api.ServiceSpec{
ClusterIP: "10.0.0.10",
ClusterIPs: nil,
},
},
expectedClusterIP: "10.0.0.10", expectedClusterIP: "10.0.0.10",
expectedClusterIPs: []string{"10.0.0.10"}, expectedClusterIPs: []string{"10.0.0.10"},
}, },
{ {
name: "update - user cleared both", name: "update - user cleared both",
oldService: &api.Service{ oldService: makeServiceWithClusterIp("10.0.0.10", []string{"10.0.0.10"}),
Spec: api.ServiceSpec{ newService: makeServiceWithClusterIp("", nil),
ClusterIP: "10.0.0.10",
ClusterIPs: []string{"10.0.0.10"},
},
},
newService: &api.Service{
Spec: api.ServiceSpec{
ClusterIP: "",
ClusterIPs: nil,
},
},
expectedClusterIP: "", expectedClusterIP: "",
expectedClusterIPs: nil, expectedClusterIPs: nil,
}, },
{ {
name: "update - user cleared ClusterIP but changed clusterIPs", name: "update - user cleared ClusterIP but changed clusterIPs",
oldService: &api.Service{ oldService: makeServiceWithClusterIp("10.0.0.10", []string{"10.0.0.10"}),
Spec: api.ServiceSpec{ newService: makeServiceWithClusterIp("", []string{"10.0.0.11"}),
ClusterIP: "10.0.0.10",
ClusterIPs: []string{"10.0.0.10"},
},
},
newService: &api.Service{
Spec: api.ServiceSpec{
ClusterIP: "",
ClusterIPs: []string{"10.0.0.11"},
},
},
expectedClusterIP: "", /* validation catches this */ expectedClusterIP: "", /* validation catches this */
expectedClusterIPs: []string{"10.0.0.11"}, expectedClusterIPs: []string{"10.0.0.11"},
}, },
{ {
name: "update - user cleared ClusterIPs but changed ClusterIP", name: "update - user cleared ClusterIPs but changed ClusterIP",
oldService: &api.Service{ oldService: makeServiceWithClusterIp("10.0.0.10", []string{"10.0.0.10", "2000::1"}),
Spec: api.ServiceSpec{ newService: makeServiceWithClusterIp("10.0.0.11", nil),
ClusterIP: "10.0.0.10",
ClusterIPs: []string{"10.0.0.10", "2000::1"},
},
},
newService: &api.Service{
Spec: api.ServiceSpec{
ClusterIP: "10.0.0.11",
ClusterIPs: nil,
},
},
expectedClusterIP: "10.0.0.11", expectedClusterIP: "10.0.0.11",
expectedClusterIPs: nil, expectedClusterIPs: nil,
}, },
{ {
name: "update - user changed from None to ClusterIP", name: "update - user changed from None to ClusterIP",
oldService: &api.Service{ oldService: makeServiceWithClusterIp("None", []string{"None"}),
Spec: api.ServiceSpec{ newService: makeServiceWithClusterIp("10.0.0.10", []string{"None"}),
ClusterIP: "None",
ClusterIPs: []string{"None"},
},
},
newService: &api.Service{
Spec: api.ServiceSpec{
ClusterIP: "10.0.0.10",
ClusterIPs: []string{"None"},
},
},
expectedClusterIP: "10.0.0.10", expectedClusterIP: "10.0.0.10",
expectedClusterIPs: []string{"10.0.0.10"}, expectedClusterIPs: []string{"10.0.0.10"},
}, },
{ {
name: "update - user changed from ClusterIP to None", name: "update - user changed from ClusterIP to None",
oldService: &api.Service{ oldService: makeServiceWithClusterIp("10.0.0.10", []string{"10.0.0.10"}),
Spec: api.ServiceSpec{ newService: makeServiceWithClusterIp("None", []string{"10.0.0.10"}),
ClusterIP: "10.0.0.10",
ClusterIPs: []string{"10.0.0.10"},
},
},
newService: &api.Service{
Spec: api.ServiceSpec{
ClusterIP: "None",
ClusterIPs: []string{"10.0.0.10"},
},
},
expectedClusterIP: "None", expectedClusterIP: "None",
expectedClusterIPs: []string{"None"}, expectedClusterIPs: []string{"None"},
}, },
{ {
name: "update - user changed from ClusterIP to None and changed ClusterIPs in a dual stack (new client making a mistake)", name: "update - user changed from ClusterIP to None and changed ClusterIPs in a dual stack (new client making a mistake)",
oldService: &api.Service{ oldService: makeServiceWithClusterIp("10.0.0.10", []string{"10.0.0.10", "2000::1"}),
Spec: api.ServiceSpec{ newService: makeServiceWithClusterIp("None", []string{"10.0.0.11", "2000::1"}),
ClusterIP: "10.0.0.10",
ClusterIPs: []string{"10.0.0.10", "2000::1"},
},
},
newService: &api.Service{
Spec: api.ServiceSpec{
ClusterIP: "None",
ClusterIPs: []string{"10.0.0.11", "2000::1"},
},
},
expectedClusterIP: "None", expectedClusterIP: "None",
expectedClusterIPs: []string{"10.0.0.11", "2000::1"}, expectedClusterIPs: []string{"10.0.0.11", "2000::1"},
}, },