@@ -12,6 +12,7 @@ import (
1212 "helm.sh/helm/v3/pkg/action"
1313 "helm.sh/helm/v3/pkg/chart"
1414 "helm.sh/helm/v3/pkg/release"
15+ "helm.sh/helm/v3/pkg/storage"
1516 "helm.sh/helm/v3/pkg/storage/driver"
1617 featuregatetesting "k8s.io/component-base/featuregate/testing"
1718 "sigs.k8s.io/controller-runtime/pkg/client"
@@ -46,6 +47,7 @@ type mockActionGetter struct {
4647 reconcileErr error
4748 desiredRel * release.Release
4849 currentRel * release.Release
50+ config * action.Configuration
4951}
5052
5153func (mag * mockActionGetter ) ActionClientFor (ctx context.Context , obj client.Object ) (helmclient.ActionInterface , error ) {
@@ -94,6 +96,12 @@ func (mag *mockActionGetter) Reconcile(rel *release.Release) error {
9496 return mag .reconcileErr
9597}
9698
99+ func (mag * mockActionGetter ) Config () * action.Configuration {
100+ // TODO
101+ // storage.Init(driver.NewMemory()).
102+ return mag .config
103+ }
104+
97105var (
98106 // required for unmockable call to convert.RegistryV1ToHelmChart
99107 validFS = fstest.MapFS {
@@ -144,7 +152,8 @@ func TestApply_Base(t *testing.T) {
144152
145153 t .Run ("fails trying to obtain an action client" , func (t * testing.T ) {
146154 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 )
148157
149158 objs , state , err := helmApplier .Apply (context .TODO (), validFS , testCE , testObjectLabels , testStorageLabels )
150159 require .Error (t , err )
@@ -155,7 +164,8 @@ func TestApply_Base(t *testing.T) {
155164
156165 t .Run ("fails getting current release and !driver.ErrReleaseNotFound" , func (t * testing.T ) {
157166 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 )
159169
160170 objs , state , err := helmApplier .Apply (context .TODO (), validFS , testCE , testObjectLabels , testStorageLabels )
161171 require .Error (t , err )
@@ -165,13 +175,80 @@ func TestApply_Base(t *testing.T) {
165175 })
166176}
167177
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 , "removed 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 , "removing 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 , "removing interrupted release" )
239+ require .Nil (t , objs )
240+ require .Empty (t , state )
241+ })
242+ }
243+
168244func TestApply_Installation (t * testing.T ) {
169245 t .Run ("fails during dry-run installation" , func (t * testing.T ) {
170246 mockAcg := & mockActionGetter {
171247 getClientErr : driver .ErrReleaseNotFound ,
172248 dryRunInstallErr : errors .New ("failed attempting to dry-run install chart" ),
173249 }
174- helmApplier := applier.Helm {ActionClientGetter : mockAcg }
250+ helmApplier , err := applier .NewHelm (mockAcg , nil , "" )
251+ require .NoError (t , err )
175252
176253 objs , state , err := helmApplier .Apply (context .TODO (), validFS , testCE , testObjectLabels , testStorageLabels )
177254 require .Error (t , err )
@@ -186,7 +263,8 @@ func TestApply_Installation(t *testing.T) {
186263 installErr : errors .New ("failed installing chart" ),
187264 }
188265 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 )
190268
191269 objs , state , err := helmApplier .Apply (context .TODO (), validFS , testCE , testObjectLabels , testStorageLabels )
192270 require .Error (t , err )
@@ -200,7 +278,8 @@ func TestApply_Installation(t *testing.T) {
200278 getClientErr : driver .ErrReleaseNotFound ,
201279 installErr : errors .New ("failed installing chart" ),
202280 }
203- helmApplier := applier.Helm {ActionClientGetter : mockAcg }
281+ helmApplier , err := applier .NewHelm (mockAcg , nil , "" )
282+ require .NoError (t , err )
204283
205284 objs , state , err := helmApplier .Apply (context .TODO (), validFS , testCE , testObjectLabels , testStorageLabels )
206285 require .Error (t , err )
@@ -217,7 +296,8 @@ func TestApply_Installation(t *testing.T) {
217296 Manifest : validManifest ,
218297 },
219298 }
220- helmApplier := applier.Helm {ActionClientGetter : mockAcg }
299+ helmApplier , err := applier .NewHelm (mockAcg , nil , "" )
300+ require .NoError (t , err )
221301
222302 objs , state , err := helmApplier .Apply (context .TODO (), validFS , testCE , testObjectLabels , testStorageLabels )
223303 require .NoError (t , err )
@@ -236,7 +316,8 @@ func TestApply_InstallationWithPreflightPermissionsEnabled(t *testing.T) {
236316 getClientErr : driver .ErrReleaseNotFound ,
237317 dryRunInstallErr : errors .New ("failed attempting to dry-run install chart" ),
238318 }
239- helmApplier := applier.Helm {ActionClientGetter : mockAcg }
319+ helmApplier , err := applier .NewHelm (mockAcg , nil , "" )
320+ require .NoError (t , err )
240321
241322 objs , state , err := helmApplier .Apply (context .TODO (), validFS , testCE , testObjectLabels , testStorageLabels )
242323 require .Error (t , err )
@@ -251,7 +332,8 @@ func TestApply_InstallationWithPreflightPermissionsEnabled(t *testing.T) {
251332 installErr : errors .New ("failed installing chart" ),
252333 }
253334 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 )
255337
256338 objs , state , err := helmApplier .Apply (context .TODO (), validFS , testCE , testObjectLabels , testStorageLabels )
257339 require .Error (t , err )
@@ -265,7 +347,8 @@ func TestApply_InstallationWithPreflightPermissionsEnabled(t *testing.T) {
265347 getClientErr : driver .ErrReleaseNotFound ,
266348 installErr : errors .New ("failed installing chart" ),
267349 }
268- helmApplier := applier.Helm {ActionClientGetter : mockAcg }
350+ helmApplier , err := applier .NewHelm (mockAcg , nil , "" )
351+ require .NoError (t , err )
269352
270353 objs , state , err := helmApplier .Apply (context .TODO (), validFS , testCE , testObjectLabels , testStorageLabels )
271354 require .Error (t , err )
@@ -282,7 +365,8 @@ func TestApply_InstallationWithPreflightPermissionsEnabled(t *testing.T) {
282365 Manifest : validManifest ,
283366 },
284367 }
285- helmApplier := applier.Helm {ActionClientGetter : mockAcg }
368+ helmApplier , err := applier .NewHelm (mockAcg , nil , "" )
369+ require .NoError (t , err )
286370
287371 objs , state , err := helmApplier .Apply (context .TODO (), validFS , testCE , testObjectLabels , testStorageLabels )
288372 require .NoError (t , err )
@@ -302,7 +386,8 @@ func TestApply_Upgrade(t *testing.T) {
302386 mockAcg := & mockActionGetter {
303387 dryRunUpgradeErr : errors .New ("failed attempting to dry-run upgrade chart" ),
304388 }
305- helmApplier := applier.Helm {ActionClientGetter : mockAcg }
389+ helmApplier , err := applier .NewHelm (mockAcg , nil , "" )
390+ require .NoError (t , err )
306391
307392 objs , state , err := helmApplier .Apply (context .TODO (), validFS , testCE , testObjectLabels , testStorageLabels )
308393 require .Error (t , err )
@@ -321,7 +406,8 @@ func TestApply_Upgrade(t *testing.T) {
321406 desiredRel : & testDesiredRelease ,
322407 }
323408 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 )
325411
326412 objs , state , err := helmApplier .Apply (context .TODO (), validFS , testCE , testObjectLabels , testStorageLabels )
327413 require .Error (t , err )
@@ -340,7 +426,8 @@ func TestApply_Upgrade(t *testing.T) {
340426 desiredRel : & testDesiredRelease ,
341427 }
342428 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 )
344431
345432 objs , state , err := helmApplier .Apply (context .TODO (), validFS , testCE , testObjectLabels , testStorageLabels )
346433 require .Error (t , err )
@@ -359,7 +446,8 @@ func TestApply_Upgrade(t *testing.T) {
359446 desiredRel : & testDesiredRelease ,
360447 }
361448 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 )
363451
364452 objs , state , err := helmApplier .Apply (context .TODO (), validFS , testCE , testObjectLabels , testStorageLabels )
365453 require .Error (t , err )
@@ -376,7 +464,8 @@ func TestApply_Upgrade(t *testing.T) {
376464 currentRel : testCurrentRelease ,
377465 desiredRel : & testDesiredRelease ,
378466 }
379- helmApplier := applier.Helm {ActionClientGetter : mockAcg }
467+ helmApplier , err := applier .NewHelm (mockAcg , nil , "" )
468+ require .NoError (t , err )
380469
381470 objs , state , err := helmApplier .Apply (context .TODO (), validFS , testCE , testObjectLabels , testStorageLabels )
382471 require .NoError (t , err )
0 commit comments