@@ -13,12 +13,14 @@ import (
13
13
"helm.sh/helm/v3/pkg/chart"
14
14
"helm.sh/helm/v3/pkg/release"
15
15
"helm.sh/helm/v3/pkg/storage/driver"
16
+ featuregatetesting "k8s.io/component-base/featuregate/testing"
16
17
"sigs.k8s.io/controller-runtime/pkg/client"
17
18
18
19
helmclient "github.com/operator-framework/helm-operator-plugins/pkg/client"
19
20
20
21
v1 "github.com/operator-framework/operator-controller/api/v1"
21
22
"github.com/operator-framework/operator-controller/internal/applier"
23
+ "github.com/operator-framework/operator-controller/internal/features"
22
24
)
23
25
24
26
type mockPreflight struct {
@@ -226,6 +228,71 @@ func TestApply_Installation(t *testing.T) {
226
228
})
227
229
}
228
230
231
+ func TestApply_InstallationWithPreflightPermissionsEnabled (t * testing.T ) {
232
+ featuregatetesting .SetFeatureGateDuringTest (t , features .OperatorControllerFeatureGate , features .PreflightPermissions , true )
233
+
234
+ t .Run ("fails during dry-run installation" , func (t * testing.T ) {
235
+ mockAcg := & mockActionGetter {
236
+ getClientErr : driver .ErrReleaseNotFound ,
237
+ dryRunInstallErr : errors .New ("failed attempting to dry-run install chart" ),
238
+ }
239
+ helmApplier := applier.Helm {ActionClientGetter : mockAcg }
240
+
241
+ objs , state , err := helmApplier .Apply (context .TODO (), validFS , testCE , testObjectLabels , testStorageLabels )
242
+ require .Error (t , err )
243
+ require .ErrorContains (t , err , "attempting to dry-run install chart" )
244
+ require .Nil (t , objs )
245
+ require .Empty (t , state )
246
+ })
247
+
248
+ t .Run ("fails during pre-flight installation" , func (t * testing.T ) {
249
+ mockAcg := & mockActionGetter {
250
+ getClientErr : driver .ErrReleaseNotFound ,
251
+ installErr : errors .New ("failed installing chart" ),
252
+ }
253
+ mockPf := & mockPreflight {installErr : errors .New ("failed during install pre-flight check" )}
254
+ helmApplier := applier.Helm {ActionClientGetter : mockAcg , Preflights : []applier.Preflight {mockPf }}
255
+
256
+ objs , state , err := helmApplier .Apply (context .TODO (), validFS , testCE , testObjectLabels , testStorageLabels )
257
+ require .Error (t , err )
258
+ require .ErrorContains (t , err , "install pre-flight check" )
259
+ require .Equal (t , applier .StateNeedsInstall , state )
260
+ require .Nil (t , objs )
261
+ })
262
+
263
+ t .Run ("fails during installation" , func (t * testing.T ) {
264
+ mockAcg := & mockActionGetter {
265
+ getClientErr : driver .ErrReleaseNotFound ,
266
+ installErr : errors .New ("failed installing chart" ),
267
+ }
268
+ helmApplier := applier.Helm {ActionClientGetter : mockAcg }
269
+
270
+ objs , state , err := helmApplier .Apply (context .TODO (), validFS , testCE , testObjectLabels , testStorageLabels )
271
+ require .Error (t , err )
272
+ require .ErrorContains (t , err , "installing chart" )
273
+ require .Equal (t , applier .StateNeedsInstall , state )
274
+ require .Nil (t , objs )
275
+ })
276
+
277
+ t .Run ("successful installation" , func (t * testing.T ) {
278
+ mockAcg := & mockActionGetter {
279
+ getClientErr : driver .ErrReleaseNotFound ,
280
+ desiredRel : & release.Release {
281
+ Info : & release.Info {Status : release .StatusDeployed },
282
+ Manifest : validManifest ,
283
+ },
284
+ }
285
+ helmApplier := applier.Helm {ActionClientGetter : mockAcg }
286
+
287
+ objs , state , err := helmApplier .Apply (context .TODO (), validFS , testCE , testObjectLabels , testStorageLabels )
288
+ require .NoError (t , err )
289
+ require .Equal (t , applier .StateNeedsInstall , state )
290
+ require .NotNil (t , objs )
291
+ assert .Equal (t , "service-a" , objs [0 ].GetName ())
292
+ assert .Equal (t , "service-b" , objs [1 ].GetName ())
293
+ })
294
+ }
295
+
229
296
func TestApply_Upgrade (t * testing.T ) {
230
297
testCurrentRelease := & release.Release {
231
298
Info : & release.Info {Status : release .StatusDeployed },
0 commit comments