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