@@ -13,12 +13,14 @@ import (
1313 "helm.sh/helm/v3/pkg/chart"
1414 "helm.sh/helm/v3/pkg/release"
1515 "helm.sh/helm/v3/pkg/storage/driver"
16+ featuregatetesting "k8s.io/component-base/featuregate/testing"
1617 "sigs.k8s.io/controller-runtime/pkg/client"
1718
1819 helmclient "github.com/operator-framework/helm-operator-plugins/pkg/client"
1920
2021 v1 "github.com/operator-framework/operator-controller/api/v1"
2122 "github.com/operator-framework/operator-controller/internal/applier"
23+ "github.com/operator-framework/operator-controller/internal/features"
2224)
2325
2426type mockPreflight struct {
@@ -226,6 +228,71 @@ func TestApply_Installation(t *testing.T) {
226228 })
227229}
228230
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+
229296func TestApply_Upgrade (t * testing.T ) {
230297 testCurrentRelease := & release.Release {
231298 Info : & release.Info {Status : release .StatusDeployed },
0 commit comments