@@ -80,13 +80,11 @@ func (s *State) checkInit() error {
80
80
return nil
81
81
}
82
82
83
- // Update the current state with the passed in object
84
- func (s * State ) Update (obj typed.YAMLObject , version fieldpath.APIVersion , manager string ) error {
85
- obj = FixTabsOrDie (obj )
86
- if err := s .checkInit (); err != nil {
83
+ func (s * State ) UpdateObject (tv * typed.TypedValue , version fieldpath.APIVersion , manager string ) error {
84
+ err := s .checkInit ()
85
+ if err != nil {
87
86
return err
88
87
}
89
- tv , err := s .Parser .FromYAML (obj )
90
88
s .Live , err = s .Updater .Converter .Convert (s .Live , version )
91
89
if err != nil {
92
90
return err
@@ -101,13 +99,17 @@ func (s *State) Update(obj typed.YAMLObject, version fieldpath.APIVersion, manag
101
99
return nil
102
100
}
103
101
104
- // Apply the passed in object to the current state
105
- func (s * State ) Apply (obj typed.YAMLObject , version fieldpath.APIVersion , manager string , force bool ) error {
106
- obj = FixTabsOrDie (obj )
107
- if err := s . checkInit (); err != nil {
102
+ // Update the current state with the passed in object
103
+ func (s * State ) Update (obj typed.YAMLObject , version fieldpath.APIVersion , manager string ) error {
104
+ tv , err := s . Parser . FromYAML ( FixTabsOrDie (obj ) )
105
+ if err != nil {
108
106
return err
109
107
}
110
- tv , err := s .Parser .FromYAML (obj )
108
+ return s .UpdateObject (tv , version , manager )
109
+ }
110
+
111
+ func (s * State ) ApplyObject (tv * typed.TypedValue , version fieldpath.APIVersion , manager string , force bool ) error {
112
+ err := s .checkInit ()
111
113
if err != nil {
112
114
return err
113
115
}
@@ -125,6 +127,15 @@ func (s *State) Apply(obj typed.YAMLObject, version fieldpath.APIVersion, manage
125
127
return nil
126
128
}
127
129
130
+ // Apply the passed in object to the current state
131
+ func (s * State ) Apply (obj typed.YAMLObject , version fieldpath.APIVersion , manager string , force bool ) error {
132
+ tv , err := s .Parser .FromYAML (FixTabsOrDie (obj ))
133
+ if err != nil {
134
+ return err
135
+ }
136
+ return s .ApplyObject (tv , version , manager , force )
137
+ }
138
+
128
139
// CompareLive takes a YAML string and returns the comparison with the
129
140
// current live object or an error.
130
141
func (s * State ) CompareLive (obj typed.YAMLObject ) (* typed.Comparison , error ) {
@@ -159,6 +170,7 @@ func (dummyConverter) IsMissingVersionError(err error) bool {
159
170
// Operation is a step that will run when building a table-driven test.
160
171
type Operation interface {
161
172
run (* State ) error
173
+ preprocess (typed.ParseableType ) (Operation , error )
162
174
}
163
175
164
176
func hasConflict (conflicts merge.Conflicts , conflict merge.Conflict ) bool {
@@ -194,7 +206,37 @@ type Apply struct {
194
206
var _ Operation = & Apply {}
195
207
196
208
func (a Apply ) run (state * State ) error {
197
- err := state .Apply (a .Object , a .APIVersion , a .Manager , false )
209
+ p , err := a .preprocess (state .Parser )
210
+ if err != nil {
211
+ return err
212
+ }
213
+ return p .run (state )
214
+ }
215
+
216
+ func (a Apply ) preprocess (parser typed.ParseableType ) (Operation , error ) {
217
+ tv , err := parser .FromYAML (FixTabsOrDie (a .Object ))
218
+ if err != nil {
219
+ return nil , err
220
+ }
221
+ return ApplyObject {
222
+ Manager : a .Manager ,
223
+ APIVersion : a .APIVersion ,
224
+ Object : tv ,
225
+ Conflicts : a .Conflicts ,
226
+ }, nil
227
+ }
228
+
229
+ type ApplyObject struct {
230
+ Manager string
231
+ APIVersion fieldpath.APIVersion
232
+ Object * typed.TypedValue
233
+ Conflicts merge.Conflicts
234
+ }
235
+
236
+ var _ Operation = & ApplyObject {}
237
+
238
+ func (a ApplyObject ) run (state * State ) error {
239
+ err := state .ApplyObject (a .Object , a .APIVersion , a .Manager , false )
198
240
if err != nil {
199
241
if _ , ok := err .(merge.Conflicts ); ! ok || a .Conflicts == nil {
200
242
return err
@@ -215,7 +257,10 @@ func (a Apply) run(state *State) error {
215
257
}
216
258
}
217
259
return nil
260
+ }
218
261
262
+ func (a ApplyObject ) preprocess (parser typed.ParseableType ) (Operation , error ) {
263
+ return a , nil
219
264
}
220
265
221
266
// ForceApply is a type of operation. It is a forced-apply run by a
@@ -232,6 +277,36 @@ func (f ForceApply) run(state *State) error {
232
277
return state .Apply (f .Object , f .APIVersion , f .Manager , true )
233
278
}
234
279
280
+ func (f ForceApply ) preprocess (parser typed.ParseableType ) (Operation , error ) {
281
+ tv , err := parser .FromYAML (FixTabsOrDie (f .Object ))
282
+ if err != nil {
283
+ return nil , err
284
+ }
285
+ return ForceApplyObject {
286
+ Manager : f .Manager ,
287
+ APIVersion : f .APIVersion ,
288
+ Object : tv ,
289
+ }, nil
290
+ }
291
+
292
+ // ForceApplyObject is a type of operation. It is a forced-apply run by
293
+ // a manager with a given object. Any error will be returned.
294
+ type ForceApplyObject struct {
295
+ Manager string
296
+ APIVersion fieldpath.APIVersion
297
+ Object * typed.TypedValue
298
+ }
299
+
300
+ var _ Operation = & ForceApplyObject {}
301
+
302
+ func (f ForceApplyObject ) run (state * State ) error {
303
+ return state .ApplyObject (f .Object , f .APIVersion , f .Manager , true )
304
+ }
305
+
306
+ func (f ForceApplyObject ) preprocess (parser typed.ParseableType ) (Operation , error ) {
307
+ return f , nil
308
+ }
309
+
235
310
// Update is a type of operation. It is a controller type of
236
311
// update. Errors are passed along.
237
312
type Update struct {
@@ -246,6 +321,36 @@ func (u Update) run(state *State) error {
246
321
return state .Update (u .Object , u .APIVersion , u .Manager )
247
322
}
248
323
324
+ func (u Update ) preprocess (parser typed.ParseableType ) (Operation , error ) {
325
+ tv , err := parser .FromYAML (FixTabsOrDie (u .Object ))
326
+ if err != nil {
327
+ return nil , err
328
+ }
329
+ return UpdateObject {
330
+ Manager : u .Manager ,
331
+ APIVersion : u .APIVersion ,
332
+ Object : tv ,
333
+ }, nil
334
+ }
335
+
336
+ // UpdateObject is a type of operation. It is a controller type of
337
+ // update. Errors are passed along.
338
+ type UpdateObject struct {
339
+ Manager string
340
+ APIVersion fieldpath.APIVersion
341
+ Object * typed.TypedValue
342
+ }
343
+
344
+ var _ Operation = & Update {}
345
+
346
+ func (u UpdateObject ) run (state * State ) error {
347
+ return state .UpdateObject (u .Object , u .APIVersion , u .Manager )
348
+ }
349
+
350
+ func (f UpdateObject ) preprocess (parser typed.ParseableType ) (Operation , error ) {
351
+ return f , nil
352
+ }
353
+
249
354
// TestCase is the list of operations that need to be run, as well as
250
355
// the object/managedfields as they are supposed to look like after all
251
356
// the operations have been successfully performed. If Object/Managed is
@@ -276,6 +381,18 @@ func (tc TestCase) Bench(parser typed.ParseableType) error {
276
381
return tc .BenchWithConverter (parser , & dummyConverter {})
277
382
}
278
383
384
+ // Preprocess all the operations by parsing the yaml before-hand.
385
+ func (tc TestCase ) PreprocessOperations (parser typed.ParseableType ) error {
386
+ for i := range tc .Ops {
387
+ op , err := tc .Ops [i ].preprocess (parser )
388
+ if err != nil {
389
+ return err
390
+ }
391
+ tc .Ops [i ] = op
392
+ }
393
+ return nil
394
+ }
395
+
279
396
// BenchWithConverter runs the test-case using the given parser and converter,
280
397
// but doesn't do any comparison operations aftewards; you should probably run
281
398
// TestWithConverter once and reset the benchmark, to make sure the test case
0 commit comments