@@ -20,9 +20,11 @@ import (
2020
2121func TestDNSServiceChanged (t * testing.T ) {
2222 testCases := []struct {
23- description string
24- mutate func (* corev1.Service )
25- expect bool
23+ description string
24+ mutateOriginal func (* corev1.Service )
25+ mutate func (* corev1.Service )
26+ expect bool
27+ verify func (* corev1.Service , * corev1.Service , * corev1.Service , * testing.T )
2628 }{
2729 {
2830 description : "if nothing changes" ,
@@ -140,6 +142,31 @@ func TestDNSServiceChanged(t *testing.T) {
140142 },
141143 expect : true ,
142144 },
145+ {
146+ description : "if dual-stack fields exist in current but not in expected, and an update is triggered" ,
147+ mutateOriginal : func (service * corev1.Service ) {
148+ ipFamilyPolicy := corev1 .IPFamilyPolicyPreferDualStack
149+ service .Spec .ClusterIP = "1.2.3.4"
150+ service .Spec .IPFamilies = []corev1.IPFamily {corev1 .IPv4Protocol , corev1 .IPv6Protocol }
151+ service .Spec .IPFamilyPolicy = & ipFamilyPolicy
152+ service .Spec .ClusterIPs = []string {"1.2.3.4" , "fd00::1" }
153+ },
154+ mutate : func (service * corev1.Service ) {
155+ service .Spec .IPFamilies = nil
156+ service .Spec .IPFamilyPolicy = nil
157+ service .Spec .ClusterIPs = nil
158+ service .Spec .Selector = map [string ]string {"foo" : "bar" }
159+ },
160+ expect : true ,
161+ verify : func (original , mutated , updated * corev1.Service , t * testing.T ) {
162+ assert .Equal (t , original .Spec .ClusterIP , updated .Spec .ClusterIP )
163+ assert .Equal (t , original .Spec .ClusterIPs , updated .Spec .ClusterIPs )
164+ assert .Equal (t , original .Spec .IPFamilies , updated .Spec .IPFamilies )
165+ if assert .NotNil (t , updated .Spec .IPFamilyPolicy ) {
166+ assert .Equal (t , * original .Spec .IPFamilyPolicy , * updated .Spec .IPFamilyPolicy )
167+ }
168+ },
169+ },
143170 }
144171
145172 for _ , tc := range testCases {
@@ -151,11 +178,17 @@ func TestDNSServiceChanged(t *testing.T) {
151178 },
152179 Spec : corev1.ServiceSpec {},
153180 }
181+ if tc .mutateOriginal != nil {
182+ tc .mutateOriginal (& original )
183+ }
154184 mutated := original .DeepCopy ()
155185 tc .mutate (mutated )
156186 if changed , updated := serviceChanged (& original , mutated ); changed != tc .expect {
157187 t .Errorf ("%s, expect serviceChanged to be %t, got %t" , tc .description , tc .expect , changed )
158188 } else if changed {
189+ if tc .verify != nil {
190+ tc .verify (& original , mutated , updated , t )
191+ }
159192 if changedAgain , _ := serviceChanged (mutated , updated ); changedAgain {
160193 t .Errorf ("%s, serviceChanged does not behave as a fixed point function" , tc .description )
161194 }
0 commit comments