1
1
package variablesources_test
2
2
3
3
import (
4
- "context"
5
4
"encoding/json"
5
+ "fmt"
6
6
"testing"
7
7
8
8
"github.com/operator-framework/deppy/pkg/deppy"
9
9
"github.com/operator-framework/operator-registry/alpha/declcfg"
10
10
"github.com/operator-framework/operator-registry/alpha/property"
11
+ rukpakv1alpha1 "github.com/operator-framework/rukpak/api/v1alpha1"
11
12
"github.com/stretchr/testify/assert"
12
13
"github.com/stretchr/testify/require"
14
+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
13
15
featuregatetesting "k8s.io/component-base/featuregate/testing"
14
16
15
17
"github.com/operator-framework/operator-controller/internal/catalogmetadata"
16
- olmvariables "github.com/operator-framework/operator-controller/internal/resolution/variables"
17
18
"github.com/operator-framework/operator-controller/internal/resolution/variablesources"
18
19
"github.com/operator-framework/operator-controller/pkg/features"
19
20
)
20
21
21
- func TestInstalledPackageVariableSource (t * testing.T ) {
22
+ func TestMakeInstalledPackageVariables (t * testing.T ) {
22
23
someOtherPackageChannel := catalogmetadata.Channel {Channel : declcfg.Channel {
23
24
Name : "stable" ,
24
25
Package : "some-other-package" ,
@@ -81,7 +82,7 @@ func TestInstalledPackageVariableSource(t *testing.T) {
81
82
},
82
83
},
83
84
}}
84
- bundleList := []* catalogmetadata.Bundle {
85
+ allBundles := []* catalogmetadata.Bundle {
85
86
{Bundle : declcfg.Bundle {
86
87
Name : "test-package.v0.0.1" ,
87
88
Package : "test-package" ,
@@ -201,19 +202,41 @@ func TestInstalledPackageVariableSource(t *testing.T) {
201
202
},
202
203
}
203
204
205
+ fakeBundleDeployments := func (bundleImages ... string ) []rukpakv1alpha1.BundleDeployment {
206
+ bundleDeployments := []rukpakv1alpha1.BundleDeployment {}
207
+ for idx , bundleImage := range bundleImages {
208
+ bd := rukpakv1alpha1.BundleDeployment {
209
+ ObjectMeta : metav1.ObjectMeta {
210
+ Name : fmt .Sprintf ("bd-%d" , idx ),
211
+ },
212
+ Spec : rukpakv1alpha1.BundleDeploymentSpec {
213
+ Template : & rukpakv1alpha1.BundleTemplate {
214
+ Spec : rukpakv1alpha1.BundleSpec {
215
+ Source : rukpakv1alpha1.BundleSource {
216
+ Image : & rukpakv1alpha1.ImageSource {
217
+ Ref : bundleImage ,
218
+ },
219
+ },
220
+ },
221
+ },
222
+ },
223
+ }
224
+ bundleDeployments = append (bundleDeployments , bd )
225
+ }
226
+
227
+ return bundleDeployments
228
+ }
229
+
204
230
t .Run ("with ForceSemverUpgradeConstraints feature gate enabled" , func (t * testing.T ) {
205
231
defer featuregatetesting .SetFeatureGateDuringTest (t , features .OperatorControllerFeatureGate , features .ForceSemverUpgradeConstraints , true )()
206
232
207
233
t .Run ("with non-zero major version" , func (t * testing.T ) {
208
234
const bundleImage = "registry.io/repo/[email protected] "
209
- ipvs , err := variablesources .NewInstalledPackageVariableSource ( bundleList , bundleImage )
235
+ installedPackages , err := variablesources .MakeInstalledPackageVariables ( allBundles , fakeBundleDeployments ( bundleImage ) )
210
236
require .NoError (t , err )
211
237
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 )
238
+ require .Len (t , installedPackages , 1 )
239
+ packageVariable := installedPackages [0 ]
217
240
assert .Equal (t , deppy .IdentifierFromString ("installed package test-package" ), packageVariable .Identifier ())
218
241
219
242
// ensure bundles are in version order (high to low)
@@ -227,14 +250,11 @@ func TestInstalledPackageVariableSource(t *testing.T) {
227
250
t .Run ("with zero major version" , func (t * testing.T ) {
228
251
t .Run ("with zero minor version" , func (t * testing.T ) {
229
252
const bundleImage = "registry.io/repo/[email protected] "
230
- ipvs , err := variablesources .NewInstalledPackageVariableSource ( bundleList , bundleImage )
253
+ installedPackages , err := variablesources .MakeInstalledPackageVariables ( allBundles , fakeBundleDeployments ( bundleImage ) )
231
254
require .NoError (t , err )
232
255
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 )
256
+ require .Len (t , installedPackages , 1 )
257
+ packageVariable := installedPackages [0 ]
238
258
assert .Equal (t , deppy .IdentifierFromString ("installed package test-package" ), packageVariable .Identifier ())
239
259
240
260
// No upgrades are allowed in major version zero when minor version is also zero
@@ -245,14 +265,11 @@ func TestInstalledPackageVariableSource(t *testing.T) {
245
265
246
266
t .Run ("with non-zero minor version" , func (t * testing.T ) {
247
267
const bundleImage = "registry.io/repo/[email protected] "
248
- ipvs , err := variablesources .NewInstalledPackageVariableSource ( bundleList , bundleImage )
268
+ installedPackages , err := variablesources .MakeInstalledPackageVariables ( allBundles , fakeBundleDeployments ( bundleImage ) )
249
269
require .NoError (t , err )
250
270
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 )
271
+ require .Len (t , installedPackages , 1 )
272
+ packageVariable := installedPackages [0 ]
256
273
assert .Equal (t , deppy .IdentifierFromString ("installed package test-package" ), packageVariable .Identifier ())
257
274
258
275
// Patch version upgrades are allowed, but not minor upgrades
@@ -268,14 +285,11 @@ func TestInstalledPackageVariableSource(t *testing.T) {
268
285
defer featuregatetesting .SetFeatureGateDuringTest (t , features .OperatorControllerFeatureGate , features .ForceSemverUpgradeConstraints , false )()
269
286
270
287
const bundleImage = "registry.io/repo/[email protected] "
271
- ipvs , err := variablesources .NewInstalledPackageVariableSource ( bundleList , bundleImage )
288
+ installedPackages , err := variablesources .MakeInstalledPackageVariables ( allBundles , fakeBundleDeployments ( bundleImage ) )
272
289
require .NoError (t , err )
273
290
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 )
291
+ require .Len (t , installedPackages , 1 )
292
+ packageVariable := installedPackages [0 ]
279
293
assert .Equal (t , deppy .IdentifierFromString ("installed package test-package" ), packageVariable .Identifier ())
280
294
281
295
// ensure bundles are in version order (high to low)
@@ -284,4 +298,11 @@ func TestInstalledPackageVariableSource(t *testing.T) {
284
298
assert .Equal (t , "test-package.v2.1.0" , packageVariable .Bundles ()[0 ].Name )
285
299
assert .Equal (t , "test-package.v2.0.0" , packageVariable .Bundles ()[1 ].Name )
286
300
})
301
+
302
+ t .Run ("installed bundle not found" , func (t * testing.T ) {
303
+ const bundleImage = "registry.io/repo/[email protected] "
304
+ installedPackages , err := variablesources .MakeInstalledPackageVariables (allBundles , fakeBundleDeployments (bundleImage ))
305
+ assert .Nil (t , installedPackages )
306
+ assert .
ErrorContains (
t ,
err ,
`bundleImage "registry.io/repo/[email protected] " not found` )
307
+ })
287
308
}
0 commit comments