11package variablesources_test
22
33import (
4- "context"
54 "encoding/json"
65 "testing"
76
87 "github.com/operator-framework/deppy/pkg/deppy"
98 "github.com/operator-framework/operator-registry/alpha/declcfg"
109 "github.com/operator-framework/operator-registry/alpha/property"
10+ rukpakv1alpha1 "github.com/operator-framework/rukpak/api/v1alpha1"
1111 "github.com/stretchr/testify/assert"
1212 "github.com/stretchr/testify/require"
13+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1314 featuregatetesting "k8s.io/component-base/featuregate/testing"
1415
1516 "github.com/operator-framework/operator-controller/internal/catalogmetadata"
16- olmvariables "github.com/operator-framework/operator-controller/internal/resolution/variables"
1717 "github.com/operator-framework/operator-controller/internal/resolution/variablesources"
1818 "github.com/operator-framework/operator-controller/pkg/features"
1919)
2020
21- func TestInstalledPackageVariableSource (t * testing.T ) {
21+ func TestMakeInstalledPackageVariables (t * testing.T ) {
2222 someOtherPackageChannel := catalogmetadata.Channel {Channel : declcfg.Channel {
2323 Name : "stable" ,
2424 Package : "some-other-package" ,
@@ -81,7 +81,7 @@ func TestInstalledPackageVariableSource(t *testing.T) {
8181 },
8282 },
8383 }}
84- bundleList := []* catalogmetadata.Bundle {
84+ allBundles := []* catalogmetadata.Bundle {
8585 {Bundle : declcfg.Bundle {
8686 Name : "test-package.v0.0.1" ,
8787 Package : "test-package" ,
@@ -201,19 +201,40 @@ func TestInstalledPackageVariableSource(t *testing.T) {
201201 },
202202 }
203203
204+ fakeBundleDeployment := func (name , bundleImage string ) rukpakv1alpha1.BundleDeployment {
205+ return rukpakv1alpha1.BundleDeployment {
206+ ObjectMeta : metav1.ObjectMeta {
207+ Name : name ,
208+ },
209+ Spec : rukpakv1alpha1.BundleDeploymentSpec {
210+ Template : & rukpakv1alpha1.BundleTemplate {
211+ Spec : rukpakv1alpha1.BundleSpec {
212+ Source : rukpakv1alpha1.BundleSource {
213+ Image : & rukpakv1alpha1.ImageSource {
214+ Ref : bundleImage ,
215+ },
216+ },
217+ },
218+ },
219+ },
220+ }
221+ }
222+
204223 t .Run ("with ForceSemverUpgradeConstraints feature gate enabled" , func (t * testing.T ) {
205224 defer featuregatetesting .SetFeatureGateDuringTest (t , features .OperatorControllerFeatureGate , features .ForceSemverUpgradeConstraints , true )()
206225
207226 t .Run ("with non-zero major version" , func (t * testing.T ) {
208227 const bundleImage = "registry.io/repo/test-package@v2.0.0"
209- ipvs , err := variablesources .NewInstalledPackageVariableSource (bundleList , bundleImage )
228+ installedPackages , err := variablesources .MakeInstalledPackageVariables (
229+ allBundles ,
230+ []rukpakv1alpha1.BundleDeployment {
231+ fakeBundleDeployment ("test-bd" , bundleImage ),
232+ },
233+ )
210234 require .NoError (t , err )
211235
212- variables , err := ipvs .GetVariables (context .TODO ())
213- require .NoError (t , err )
214- require .Len (t , variables , 1 )
215- packageVariable , ok := variables [0 ].(* olmvariables.InstalledPackageVariable )
216- assert .True (t , ok )
236+ require .Len (t , installedPackages , 1 )
237+ packageVariable := installedPackages [0 ]
217238 assert .Equal (t , deppy .IdentifierFromString ("installed package test-package" ), packageVariable .Identifier ())
218239
219240 // ensure bundles are in version order (high to low)
@@ -227,14 +248,16 @@ func TestInstalledPackageVariableSource(t *testing.T) {
227248 t .Run ("with zero major version" , func (t * testing.T ) {
228249 t .Run ("with zero minor version" , func (t * testing.T ) {
229250 const bundleImage = "registry.io/repo/test-package@v0.0.1"
230- ipvs , err := variablesources .NewInstalledPackageVariableSource (bundleList , bundleImage )
251+ installedPackages , err := variablesources .MakeInstalledPackageVariables (
252+ allBundles ,
253+ []rukpakv1alpha1.BundleDeployment {
254+ fakeBundleDeployment ("test-bd" , bundleImage ),
255+ },
256+ )
231257 require .NoError (t , err )
232258
233- variables , err := ipvs .GetVariables (context .TODO ())
234- require .NoError (t , err )
235- require .Len (t , variables , 1 )
236- packageVariable , ok := variables [0 ].(* olmvariables.InstalledPackageVariable )
237- assert .True (t , ok )
259+ require .Len (t , installedPackages , 1 )
260+ packageVariable := installedPackages [0 ]
238261 assert .Equal (t , deppy .IdentifierFromString ("installed package test-package" ), packageVariable .Identifier ())
239262
240263 // No upgrades are allowed in major version zero when minor version is also zero
@@ -245,14 +268,16 @@ func TestInstalledPackageVariableSource(t *testing.T) {
245268
246269 t .Run ("with non-zero minor version" , func (t * testing.T ) {
247270 const bundleImage = "registry.io/repo/test-package@v0.1.0"
248- ipvs , err := variablesources .NewInstalledPackageVariableSource (bundleList , bundleImage )
271+ installedPackages , err := variablesources .MakeInstalledPackageVariables (
272+ allBundles ,
273+ []rukpakv1alpha1.BundleDeployment {
274+ fakeBundleDeployment ("test-bd" , bundleImage ),
275+ },
276+ )
249277 require .NoError (t , err )
250278
251- variables , err := ipvs .GetVariables (context .TODO ())
252- require .NoError (t , err )
253- require .Len (t , variables , 1 )
254- packageVariable , ok := variables [0 ].(* olmvariables.InstalledPackageVariable )
255- assert .True (t , ok )
279+ require .Len (t , installedPackages , 1 )
280+ packageVariable := installedPackages [0 ]
256281 assert .Equal (t , deppy .IdentifierFromString ("installed package test-package" ), packageVariable .Identifier ())
257282
258283 // Patch version upgrades are allowed, but not minor upgrades
@@ -268,14 +293,16 @@ func TestInstalledPackageVariableSource(t *testing.T) {
268293 defer featuregatetesting .SetFeatureGateDuringTest (t , features .OperatorControllerFeatureGate , features .ForceSemverUpgradeConstraints , false )()
269294
270295 const bundleImage = "registry.io/repo/test-package@v2.0.0"
271- ipvs , err := variablesources .NewInstalledPackageVariableSource (bundleList , bundleImage )
296+ installedPackages , err := variablesources .MakeInstalledPackageVariables (
297+ allBundles ,
298+ []rukpakv1alpha1.BundleDeployment {
299+ fakeBundleDeployment ("test-bd" , bundleImage ),
300+ },
301+ )
272302 require .NoError (t , err )
273303
274- variables , err := ipvs .GetVariables (context .TODO ())
275- require .NoError (t , err )
276- require .Len (t , variables , 1 )
277- packageVariable , ok := variables [0 ].(* olmvariables.InstalledPackageVariable )
278- assert .True (t , ok )
304+ require .Len (t , installedPackages , 1 )
305+ packageVariable := installedPackages [0 ]
279306 assert .Equal (t , deppy .IdentifierFromString ("installed package test-package" ), packageVariable .Identifier ())
280307
281308 // ensure bundles are in version order (high to low)
@@ -284,4 +311,16 @@ func TestInstalledPackageVariableSource(t *testing.T) {
284311 assert .Equal (t , "test-package.v2.1.0" , packageVariable .Bundles ()[0 ].Name )
285312 assert .Equal (t , "test-package.v2.0.0" , packageVariable .Bundles ()[1 ].Name )
286313 })
314+
315+ t .Run ("installed bundle not found" , func (t * testing.T ) {
316+ const bundleImage = "registry.io/repo/test-package@v9.0.0"
317+ installedPackages , err := variablesources .MakeInstalledPackageVariables (
318+ allBundles ,
319+ []rukpakv1alpha1.BundleDeployment {
320+ fakeBundleDeployment ("test-bd" , bundleImage ),
321+ },
322+ )
323+ assert .Nil (t , installedPackages )
324+ assert .ErrorContains (t , err , `bundleImage "registry.io/repo/test-package@v9.0.0" not found` )
325+ })
287326}
0 commit comments