@@ -32,7 +32,10 @@ func Test_FindLateInitializedFieldNames_EmptyFieldConfig(t *testing.T) {
3232 require .NotNil (crd )
3333 // NO fieldConfig
3434 assert .Empty (crd .Config ().ResourceFields (crd .Names .Original ))
35- assert .Empty (code .FindLateInitializedFieldNames (crd .Config (), crd , "lateInitializeFieldNames" , 1 ))
35+ expected :=
36+ ` var lateInitializeFieldNames = []string{}
37+ `
38+ assert .Equal (expected , code .FindLateInitializedFieldNames (crd .Config (), crd , "lateInitializeFieldNames" , 1 ))
3639}
3740
3841func Test_FindLateInitializedFieldNames_NoLateInitializations (t * testing.T ) {
@@ -46,7 +49,10 @@ func Test_FindLateInitializedFieldNames_NoLateInitializations(t *testing.T) {
4649 // FieldConfig without lateInitialize
4750 assert .NotEmpty (crd .Config ().ResourceFields (crd .Names .Original )["Name" ])
4851 assert .Nil (crd .Config ().ResourceFields (crd .Names .Original )["Name" ].LateInitialize )
49- assert .Empty (code .FindLateInitializedFieldNames (crd .Config (), crd , "lateInitializeFieldNames" , 1 ))
52+ expected :=
53+ ` var lateInitializeFieldNames = []string{}
54+ `
55+ assert .Equal (expected , code .FindLateInitializedFieldNames (crd .Config (), crd , "lateInitializeFieldNames" , 1 ))
5056}
5157
5258func Test_FindLateInitializedFieldNames (t * testing.T ) {
@@ -77,7 +83,8 @@ func Test_LateInitializeFromReadOne_NoFieldsToLateInitialize(t *testing.T) {
7783 require .NotNil (crd )
7884 // NO fieldConfig
7985 assert .Empty (crd .Config ().ResourceFields (crd .Names .Original ))
80- assert .Empty (code .LateInitializeFromReadOne (crd .Config (), crd , "observed" , "koWithDefaults" , 1 ))
86+ expected := " return latest"
87+ assert .Equal (expected , code .LateInitializeFromReadOne (crd .Config (), crd , "observed" , "latest" , 1 ))
8188}
8289
8390func Test_LateInitializeFromReadOne_NonNestedPath (t * testing.T ) {
@@ -93,14 +100,16 @@ func Test_LateInitializeFromReadOne_NonNestedPath(t *testing.T) {
93100 assert .NotNil (crd .Config ().ResourceFields (crd .Names .Original )["Name" ].LateInitialize )
94101 assert .NotNil (crd .Config ().ResourceFields (crd .Names .Original )["ImageTagMutability" ].LateInitialize )
95102 expected :=
96- ` if observed.Spec.ImageTagMutability != nil && koWithDefaults.Spec.ImageTagMutability == nil {
97- koWithDefaults.Spec.ImageTagMutability = observed.Spec.ImageTagMutability
103+ ` observedKo := observed.ko
104+ latestKo := latest.ko
105+ if observedKo.Spec.ImageTagMutability != nil && latestKo.Spec.ImageTagMutability == nil {
106+ latestKo.Spec.ImageTagMutability = observedKo.Spec.ImageTagMutability
98107 }
99- if observed .Spec.Name != nil && koWithDefaults .Spec.Name == nil {
100- koWithDefaults .Spec.Name = observed .Spec.Name
108+ if observedKo .Spec.Name != nil && latestKo .Spec.Name == nil {
109+ latestKo .Spec.Name = observedKo .Spec.Name
101110 }
102- `
103- assert .Equal (expected , code .LateInitializeFromReadOne (crd .Config (), crd , "observed" , "koWithDefaults " , 1 ))
111+ return latest `
112+ assert .Equal (expected , code .LateInitializeFromReadOne (crd .Config (), crd , "observed" , "latest " , 1 ))
104113}
105114
106115func Test_LateInitializeFromReadOne_NestedPath (t * testing.T ) {
@@ -116,44 +125,59 @@ func Test_LateInitializeFromReadOne_NestedPath(t *testing.T) {
116125 assert .NotNil (crd .Config ().ResourceFields (crd .Names .Original )["Name" ].LateInitialize )
117126 assert .NotNil (crd .Config ().ResourceFields (crd .Names .Original )["ImageScanningConfiguration.ScanOnPush" ].LateInitialize )
118127 expected :=
119- ` if observed.Spec.ImageScanningConfiguration != nil && koWithDefaults.Spec.ImageScanningConfiguration != nil {
120- if observed.Spec.ImageScanningConfiguration.ScanOnPush != nil && koWithDefaults.Spec.ImageScanningConfiguration.ScanOnPush == nil {
121- koWithDefaults.Spec.ImageScanningConfiguration.ScanOnPush = observed.Spec.ImageScanningConfiguration.ScanOnPush
128+ ` observedKo := observed.ko
129+ latestKo := latest.ko
130+ if observedKo.Spec.ImageScanningConfiguration != nil && latestKo.Spec.ImageScanningConfiguration != nil {
131+ if observedKo.Spec.ImageScanningConfiguration.ScanOnPush != nil && latestKo.Spec.ImageScanningConfiguration.ScanOnPush == nil {
132+ latestKo.Spec.ImageScanningConfiguration.ScanOnPush = observedKo.Spec.ImageScanningConfiguration.ScanOnPush
122133 }
123134 }
124- if observed .Spec.Name != nil && koWithDefaults .Spec.Name == nil {
125- koWithDefaults .Spec.Name = observed .Spec.Name
135+ if observedKo .Spec.Name != nil && latestKo .Spec.Name == nil {
136+ latestKo .Spec.Name = observedKo .Spec.Name
126137 }
127- if observed .Spec.another != nil && koWithDefaults .Spec.another != nil {
128- if observed .Spec.another.map != nil && koWithDefaults .Spec.another.map != nil {
129- if observed .Spec.another.map["lastfield"] != nil && koWithDefaults .Spec.another.map["lastfield"] == nil {
130- koWithDefaults .Spec.another.map["lastfield"] = observed .Spec.another.map["lastfield"]
138+ if observedKo .Spec.another != nil && latestKo .Spec.another != nil {
139+ if observedKo .Spec.another.map != nil && latestKo .Spec.another.map != nil {
140+ if observedKo .Spec.another.map["lastfield"] != nil && latestKo .Spec.another.map["lastfield"] == nil {
141+ latestKo .Spec.another.map["lastfield"] = observedKo .Spec.another.map["lastfield"]
131142 }
132143 }
133144 }
134- if observed .Spec.map != nil && koWithDefaults .Spec.map != nil {
135- if observed .Spec.map["subfield"] != nil && koWithDefaults .Spec.map["subfield"] != nil {
136- if observed .Spec.map["subfield"].x != nil && koWithDefaults .Spec.map["subfield"].x == nil {
137- koWithDefaults .Spec.map["subfield"].x = observed .Spec.map["subfield"].x
145+ if observedKo .Spec.map != nil && latestKo .Spec.map != nil {
146+ if observedKo .Spec.map["subfield"] != nil && latestKo .Spec.map["subfield"] != nil {
147+ if observedKo .Spec.map["subfield"].x != nil && latestKo .Spec.map["subfield"].x == nil {
148+ latestKo .Spec.map["subfield"].x = observedKo .Spec.map["subfield"].x
138149 }
139150 }
140151 }
141- if observed .Spec.some != nil && koWithDefaults .Spec.some != nil {
142- if observed .Spec.some.list != nil && koWithDefaults .Spec.some.list == nil {
143- koWithDefaults .Spec.some.list = observed .Spec.some.list
152+ if observedKo .Spec.some != nil && latestKo .Spec.some != nil {
153+ if observedKo .Spec.some.list != nil && latestKo .Spec.some.list == nil {
154+ latestKo .Spec.some.list = observedKo .Spec.some.list
144155 }
145156 }
146- if observed .Spec.structA != nil && koWithDefaults .Spec.structA != nil {
147- if observed .Spec.structA.mapB != nil && koWithDefaults .Spec.structA.mapB != nil {
148- if observed .Spec.structA.mapB["structC"] != nil && koWithDefaults .Spec.structA.mapB["structC"] != nil {
149- if observed .Spec.structA.mapB["structC"].valueD != nil && koWithDefaults .Spec.structA.mapB["structC"].valueD == nil {
150- koWithDefaults .Spec.structA.mapB["structC"].valueD = observed .Spec.structA.mapB["structC"].valueD
157+ if observedKo .Spec.structA != nil && latestKo .Spec.structA != nil {
158+ if observedKo .Spec.structA.mapB != nil && latestKo .Spec.structA.mapB != nil {
159+ if observedKo .Spec.structA.mapB["structC"] != nil && latestKo .Spec.structA.mapB["structC"] != nil {
160+ if observedKo .Spec.structA.mapB["structC"].valueD != nil && latestKo .Spec.structA.mapB["structC"].valueD == nil {
161+ latestKo .Spec.structA.mapB["structC"].valueD = observedKo .Spec.structA.mapB["structC"].valueD
151162 }
152163 }
153164 }
154165 }
155- `
156- assert .Equal (expected , code .LateInitializeFromReadOne (crd .Config (), crd , "observed" , "koWithDefaults" , 1 ))
166+ return latest`
167+ assert .Equal (expected , code .LateInitializeFromReadOne (crd .Config (), crd , "observed" , "latest" , 1 ))
168+ }
169+
170+ func Test_IncompleteLateInitialization_NoFieldsToLateInitialization (t * testing.T ) {
171+ assert := assert .New (t )
172+ require := require .New (t )
173+
174+ g := testutil .NewModelForServiceWithOptions (t , "ecr" , & testutil.TestingModelOptions {GeneratorConfigFile : "generator-with-field-config.yaml" })
175+
176+ crd := testutil .GetCRDByName (t , g , "Repository" )
177+ require .NotNil (crd )
178+ expected :=
179+ ` return false`
180+ assert .Equal (expected , code .IncompleteLateInitialization (crd .Config (), crd , "latestWithDefaults" , 1 ))
157181}
158182
159183func Test_IncompleteLateInitialization (t * testing.T ) {
@@ -206,7 +230,6 @@ func Test_IncompleteLateInitialization(t *testing.T) {
206230 }
207231 }
208232 }
209- return false
210- `
233+ return false`
211234 assert .Equal (expected , code .IncompleteLateInitialization (crd .Config (), crd , "latestWithDefaults" , 1 ))
212235}
0 commit comments