@@ -12,6 +12,7 @@ import (
12
12
"helm.sh/helm/v3/pkg/action"
13
13
"helm.sh/helm/v3/pkg/chart"
14
14
"helm.sh/helm/v3/pkg/release"
15
+ "helm.sh/helm/v3/pkg/storage"
15
16
"helm.sh/helm/v3/pkg/storage/driver"
16
17
featuregatetesting "k8s.io/component-base/featuregate/testing"
17
18
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -46,6 +47,7 @@ type mockActionGetter struct {
46
47
reconcileErr error
47
48
desiredRel * release.Release
48
49
currentRel * release.Release
50
+ config * action.Configuration
49
51
}
50
52
51
53
func (mag * mockActionGetter ) ActionClientFor (ctx context.Context , obj client.Object ) (helmclient.ActionInterface , error ) {
@@ -94,6 +96,12 @@ func (mag *mockActionGetter) Reconcile(rel *release.Release) error {
94
96
return mag .reconcileErr
95
97
}
96
98
99
+ func (mag * mockActionGetter ) Config () * action.Configuration {
100
+ // TODO
101
+ // storage.Init(driver.NewMemory()).
102
+ return mag .config
103
+ }
104
+
97
105
var (
98
106
// required for unmockable call to convert.RegistryV1ToHelmChart
99
107
validFS = fstest.MapFS {
@@ -144,7 +152,8 @@ func TestApply_Base(t *testing.T) {
144
152
145
153
t .Run ("fails trying to obtain an action client" , func (t * testing.T ) {
146
154
mockAcg := & mockActionGetter {actionClientForErr : errors .New ("failed getting action client" )}
147
- helmApplier := applier.Helm {ActionClientGetter : mockAcg }
155
+ helmApplier , err := applier .NewHelm (mockAcg , nil , "" )
156
+ require .NoError (t , err )
148
157
149
158
objs , state , err := helmApplier .Apply (context .TODO (), validFS , testCE , testObjectLabels , testStorageLabels )
150
159
require .Error (t , err )
@@ -155,7 +164,8 @@ func TestApply_Base(t *testing.T) {
155
164
156
165
t .Run ("fails getting current release and !driver.ErrReleaseNotFound" , func (t * testing.T ) {
157
166
mockAcg := & mockActionGetter {getClientErr : errors .New ("failed getting current release" )}
158
- helmApplier := applier.Helm {ActionClientGetter : mockAcg }
167
+ helmApplier , err := applier .NewHelm (mockAcg , nil , "" )
168
+ require .NoError (t , err )
159
169
160
170
objs , state , err := helmApplier .Apply (context .TODO (), validFS , testCE , testObjectLabels , testStorageLabels )
161
171
require .Error (t , err )
@@ -165,13 +175,80 @@ func TestApply_Base(t *testing.T) {
165
175
})
166
176
}
167
177
178
+ func TestApply_InterruptedRelease (t * testing.T ) {
179
+ t .Run ("fails removing an interrupted install release" , func (t * testing.T ) {
180
+ testRel := & release.Release {Name : "testrel" , Version : 0 , Info : & release.Info {Status : release .StatusPendingInstall }}
181
+ testStorage := storage .Init (driver .NewMemory ())
182
+
183
+ mockAcg := & mockActionGetter {currentRel : testRel , config : & action.Configuration {Releases : testStorage }}
184
+ helmApplier , err := applier .NewHelm (mockAcg , nil , "" )
185
+ require .NoError (t , err )
186
+
187
+ objs , state , err := helmApplier .Apply (context .TODO (), validFS , testCE , testObjectLabels , testStorageLabels )
188
+ require .Error (t , err )
189
+ require .ErrorContains (t , err , "removing interrupted release" )
190
+ require .Nil (t , objs )
191
+ require .Empty (t , state )
192
+ })
193
+
194
+ t .Run ("fails removing an interrupted upgrade release" , func (t * testing.T ) {
195
+ testRel := & release.Release {Name : "testrel" , Version : 0 , Info : & release.Info {Status : release .StatusPendingUpgrade }}
196
+ testStorage := storage .Init (driver .NewMemory ())
197
+
198
+ mockAcg := & mockActionGetter {currentRel : testRel , config : & action.Configuration {Releases : testStorage }}
199
+ helmApplier , err := applier .NewHelm (mockAcg , nil , "" )
200
+ require .NoError (t , err )
201
+
202
+ objs , state , err := helmApplier .Apply (context .TODO (), validFS , testCE , testObjectLabels , testStorageLabels )
203
+ require .Error (t , err )
204
+ require .ErrorContains (t , err , "removing interrupted release" )
205
+ require .Nil (t , objs )
206
+ require .Empty (t , state )
207
+ })
208
+
209
+ t .Run ("successfully removes an interrupted install release" , func (t * testing.T ) {
210
+ testRel := & release.Release {Name : "testrel" , Version : 0 , Info : & release.Info {Status : release .StatusPendingInstall }}
211
+ testStorage := storage .Init (driver .NewMemory ())
212
+ err := testStorage .Create (testRel )
213
+ require .NoError (t , err )
214
+
215
+ mockAcg := & mockActionGetter {currentRel : testRel , config : & action.Configuration {Releases : testStorage }}
216
+ helmApplier , err := applier .NewHelm (mockAcg , nil , "" )
217
+ require .NoError (t , err )
218
+
219
+ objs , state , err := helmApplier .Apply (context .TODO (), validFS , testCE , testObjectLabels , testStorageLabels )
220
+ require .Error (t , err )
221
+ require .ErrorContains (t , err , "removed interrupted release" )
222
+ require .Nil (t , objs )
223
+ require .Empty (t , state )
224
+ })
225
+
226
+ t .Run ("successfully removes an interrupted upgrade release" , func (t * testing.T ) {
227
+ testRel := & release.Release {Name : "testrel" , Version : 0 , Info : & release.Info {Status : release .StatusPendingUpgrade }}
228
+ testStorage := storage .Init (driver .NewMemory ())
229
+ err := testStorage .Create (testRel )
230
+ require .NoError (t , err )
231
+
232
+ mockAcg := & mockActionGetter {currentRel : testRel , config : & action.Configuration {Releases : testStorage }}
233
+ helmApplier , err := applier .NewHelm (mockAcg , nil , "" )
234
+ require .NoError (t , err )
235
+
236
+ objs , state , err := helmApplier .Apply (context .TODO (), validFS , testCE , testObjectLabels , testStorageLabels )
237
+ require .Error (t , err )
238
+ require .ErrorContains (t , err , "removed interrupted release" )
239
+ require .Nil (t , objs )
240
+ require .Empty (t , state )
241
+ })
242
+ }
243
+
168
244
func TestApply_Installation (t * testing.T ) {
169
245
t .Run ("fails during dry-run installation" , func (t * testing.T ) {
170
246
mockAcg := & mockActionGetter {
171
247
getClientErr : driver .ErrReleaseNotFound ,
172
248
dryRunInstallErr : errors .New ("failed attempting to dry-run install chart" ),
173
249
}
174
- helmApplier := applier.Helm {ActionClientGetter : mockAcg }
250
+ helmApplier , err := applier .NewHelm (mockAcg , nil , "" )
251
+ require .NoError (t , err )
175
252
176
253
objs , state , err := helmApplier .Apply (context .TODO (), validFS , testCE , testObjectLabels , testStorageLabels )
177
254
require .Error (t , err )
@@ -186,7 +263,8 @@ func TestApply_Installation(t *testing.T) {
186
263
installErr : errors .New ("failed installing chart" ),
187
264
}
188
265
mockPf := & mockPreflight {installErr : errors .New ("failed during install pre-flight check" )}
189
- helmApplier := applier.Helm {ActionClientGetter : mockAcg , Preflights : []applier.Preflight {mockPf }}
266
+ helmApplier , err := applier .NewHelm (mockAcg , []applier.Preflight {mockPf }, "" )
267
+ require .NoError (t , err )
190
268
191
269
objs , state , err := helmApplier .Apply (context .TODO (), validFS , testCE , testObjectLabels , testStorageLabels )
192
270
require .Error (t , err )
@@ -200,7 +278,8 @@ func TestApply_Installation(t *testing.T) {
200
278
getClientErr : driver .ErrReleaseNotFound ,
201
279
installErr : errors .New ("failed installing chart" ),
202
280
}
203
- helmApplier := applier.Helm {ActionClientGetter : mockAcg }
281
+ helmApplier , err := applier .NewHelm (mockAcg , nil , "" )
282
+ require .NoError (t , err )
204
283
205
284
objs , state , err := helmApplier .Apply (context .TODO (), validFS , testCE , testObjectLabels , testStorageLabels )
206
285
require .Error (t , err )
@@ -217,7 +296,8 @@ func TestApply_Installation(t *testing.T) {
217
296
Manifest : validManifest ,
218
297
},
219
298
}
220
- helmApplier := applier.Helm {ActionClientGetter : mockAcg }
299
+ helmApplier , err := applier .NewHelm (mockAcg , nil , "" )
300
+ require .NoError (t , err )
221
301
222
302
objs , state , err := helmApplier .Apply (context .TODO (), validFS , testCE , testObjectLabels , testStorageLabels )
223
303
require .NoError (t , err )
@@ -236,7 +316,8 @@ func TestApply_InstallationWithPreflightPermissionsEnabled(t *testing.T) {
236
316
getClientErr : driver .ErrReleaseNotFound ,
237
317
dryRunInstallErr : errors .New ("failed attempting to dry-run install chart" ),
238
318
}
239
- helmApplier := applier.Helm {ActionClientGetter : mockAcg }
319
+ helmApplier , err := applier .NewHelm (mockAcg , nil , "" )
320
+ require .NoError (t , err )
240
321
241
322
objs , state , err := helmApplier .Apply (context .TODO (), validFS , testCE , testObjectLabels , testStorageLabels )
242
323
require .Error (t , err )
@@ -251,7 +332,8 @@ func TestApply_InstallationWithPreflightPermissionsEnabled(t *testing.T) {
251
332
installErr : errors .New ("failed installing chart" ),
252
333
}
253
334
mockPf := & mockPreflight {installErr : errors .New ("failed during install pre-flight check" )}
254
- helmApplier := applier.Helm {ActionClientGetter : mockAcg , Preflights : []applier.Preflight {mockPf }}
335
+ helmApplier , err := applier .NewHelm (mockAcg , []applier.Preflight {mockPf }, "" )
336
+ require .NoError (t , err )
255
337
256
338
objs , state , err := helmApplier .Apply (context .TODO (), validFS , testCE , testObjectLabels , testStorageLabels )
257
339
require .Error (t , err )
@@ -265,7 +347,8 @@ func TestApply_InstallationWithPreflightPermissionsEnabled(t *testing.T) {
265
347
getClientErr : driver .ErrReleaseNotFound ,
266
348
installErr : errors .New ("failed installing chart" ),
267
349
}
268
- helmApplier := applier.Helm {ActionClientGetter : mockAcg }
350
+ helmApplier , err := applier .NewHelm (mockAcg , nil , "" )
351
+ require .NoError (t , err )
269
352
270
353
objs , state , err := helmApplier .Apply (context .TODO (), validFS , testCE , testObjectLabels , testStorageLabels )
271
354
require .Error (t , err )
@@ -282,7 +365,8 @@ func TestApply_InstallationWithPreflightPermissionsEnabled(t *testing.T) {
282
365
Manifest : validManifest ,
283
366
},
284
367
}
285
- helmApplier := applier.Helm {ActionClientGetter : mockAcg }
368
+ helmApplier , err := applier .NewHelm (mockAcg , nil , "" )
369
+ require .NoError (t , err )
286
370
287
371
objs , state , err := helmApplier .Apply (context .TODO (), validFS , testCE , testObjectLabels , testStorageLabels )
288
372
require .NoError (t , err )
@@ -302,7 +386,8 @@ func TestApply_Upgrade(t *testing.T) {
302
386
mockAcg := & mockActionGetter {
303
387
dryRunUpgradeErr : errors .New ("failed attempting to dry-run upgrade chart" ),
304
388
}
305
- helmApplier := applier.Helm {ActionClientGetter : mockAcg }
389
+ helmApplier , err := applier .NewHelm (mockAcg , nil , "" )
390
+ require .NoError (t , err )
306
391
307
392
objs , state , err := helmApplier .Apply (context .TODO (), validFS , testCE , testObjectLabels , testStorageLabels )
308
393
require .Error (t , err )
@@ -321,7 +406,8 @@ func TestApply_Upgrade(t *testing.T) {
321
406
desiredRel : & testDesiredRelease ,
322
407
}
323
408
mockPf := & mockPreflight {upgradeErr : errors .New ("failed during upgrade pre-flight check" )}
324
- helmApplier := applier.Helm {ActionClientGetter : mockAcg , Preflights : []applier.Preflight {mockPf }}
409
+ helmApplier , err := applier .NewHelm (mockAcg , []applier.Preflight {mockPf }, "" )
410
+ require .NoError (t , err )
325
411
326
412
objs , state , err := helmApplier .Apply (context .TODO (), validFS , testCE , testObjectLabels , testStorageLabels )
327
413
require .Error (t , err )
@@ -340,7 +426,8 @@ func TestApply_Upgrade(t *testing.T) {
340
426
desiredRel : & testDesiredRelease ,
341
427
}
342
428
mockPf := & mockPreflight {}
343
- helmApplier := applier.Helm {ActionClientGetter : mockAcg , Preflights : []applier.Preflight {mockPf }}
429
+ helmApplier , err := applier .NewHelm (mockAcg , []applier.Preflight {mockPf }, "" )
430
+ require .NoError (t , err )
344
431
345
432
objs , state , err := helmApplier .Apply (context .TODO (), validFS , testCE , testObjectLabels , testStorageLabels )
346
433
require .Error (t , err )
@@ -359,7 +446,8 @@ func TestApply_Upgrade(t *testing.T) {
359
446
desiredRel : & testDesiredRelease ,
360
447
}
361
448
mockPf := & mockPreflight {}
362
- helmApplier := applier.Helm {ActionClientGetter : mockAcg , Preflights : []applier.Preflight {mockPf }}
449
+ helmApplier , err := applier .NewHelm (mockAcg , []applier.Preflight {mockPf }, "" )
450
+ require .NoError (t , err )
363
451
364
452
objs , state , err := helmApplier .Apply (context .TODO (), validFS , testCE , testObjectLabels , testStorageLabels )
365
453
require .Error (t , err )
@@ -376,7 +464,8 @@ func TestApply_Upgrade(t *testing.T) {
376
464
currentRel : testCurrentRelease ,
377
465
desiredRel : & testDesiredRelease ,
378
466
}
379
- helmApplier := applier.Helm {ActionClientGetter : mockAcg }
467
+ helmApplier , err := applier .NewHelm (mockAcg , nil , "" )
468
+ require .NoError (t , err )
380
469
381
470
objs , state , err := helmApplier .Apply (context .TODO (), validFS , testCE , testObjectLabels , testStorageLabels )
382
471
require .NoError (t , err )
0 commit comments