@@ -10,6 +10,7 @@ import (
10
10
"github.com/stretchr/testify/assert"
11
11
"github.com/stretchr/testify/require"
12
12
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
13
+ "k8s.io/apimachinery/pkg/labels"
13
14
"k8s.io/apimachinery/pkg/util/rand"
14
15
"k8s.io/apimachinery/pkg/util/sets"
15
16
featuregatetesting "k8s.io/component-base/featuregate/testing"
@@ -516,10 +517,22 @@ type getPackageFunc func() (*declcfg.DeclarativeConfig, error)
516
517
517
518
type staticCatalogWalker map [string ]getPackageFunc
518
519
519
- func (w staticCatalogWalker ) WalkCatalogs (ctx context.Context , _ string , f CatalogWalkFunc , _ ... client.ListOption ) error {
520
+ func (w staticCatalogWalker ) WalkCatalogs (ctx context.Context , _ string , f CatalogWalkFunc , opts ... client.ListOption ) error {
520
521
for k , v := range w {
521
522
cat := & catalogd.ClusterCatalog {
522
- ObjectMeta : metav1.ObjectMeta {Name : k },
523
+ ObjectMeta : metav1.ObjectMeta {
524
+ Name : k ,
525
+ Labels : map [string ]string {
526
+ "olm.operatorframework.io/name" : k ,
527
+ },
528
+ },
529
+ }
530
+ options := client.ListOptions {}
531
+ for _ , opt := range opts {
532
+ opt .ApplyToList (& options )
533
+ }
534
+ if ! options .LabelSelector .Matches (labels .Set (cat .ObjectMeta .Labels )) {
535
+ continue
523
536
}
524
537
fbc , fbcErr := v ()
525
538
if err := f (ctx , cat , fbc , fbcErr ); err != nil {
@@ -589,3 +602,97 @@ func genPackage(pkg string) *declcfg.DeclarativeConfig {
589
602
Deprecations : []declcfg.Deprecation {packageDeprecation (pkg )},
590
603
}
591
604
}
605
+
606
+ func TestInvalidClusterExtensionCatalogMatchExpressions (t * testing.T ) {
607
+ r := CatalogResolver {}
608
+ ce := & ocv1alpha1.ClusterExtension {
609
+ ObjectMeta : metav1.ObjectMeta {
610
+ Name : "foo" ,
611
+ },
612
+ Spec : ocv1alpha1.ClusterExtensionSpec {
613
+ PackageName : "foo" ,
614
+ CatalogSelector : metav1.LabelSelector {
615
+ MatchExpressions : []metav1.LabelSelectorRequirement {
616
+ {
617
+ Key : "name" ,
618
+ Operator : metav1 .LabelSelectorOperator ("bad" ),
619
+ Values : []string {"value" },
620
+ },
621
+ },
622
+ },
623
+ },
624
+ }
625
+ _ , _ , _ , err := r .Resolve (context .Background (), ce , nil )
626
+ assert .EqualError (t , err , "desired catalog selector is invalid: \" bad\" is not a valid label selector operator" )
627
+ }
628
+
629
+ func TestInvalidClusterExtensionCatalogMatchLabelsName (t * testing.T ) {
630
+ w := staticCatalogWalker {
631
+ "a" : func () (* declcfg.DeclarativeConfig , error ) { return genPackage ("foo" ), nil },
632
+ }
633
+ r := CatalogResolver {WalkCatalogsFunc : w .WalkCatalogs }
634
+ ce := & ocv1alpha1.ClusterExtension {
635
+ ObjectMeta : metav1.ObjectMeta {
636
+ Name : "foo" ,
637
+ },
638
+ Spec : ocv1alpha1.ClusterExtensionSpec {
639
+ PackageName : "foo" ,
640
+ CatalogSelector : metav1.LabelSelector {
641
+ MatchLabels : map [string ]string {"" : "value" },
642
+ },
643
+ },
644
+ }
645
+ _ , _ , _ , err := r .Resolve (context .Background (), ce , nil )
646
+ assert .ErrorContains (t , err , "desired catalog selector is invalid: key: Invalid value:" )
647
+ }
648
+
649
+ func TestInvalidClusterExtensionCatalogMatchLabelsValue (t * testing.T ) {
650
+ w := staticCatalogWalker {
651
+ "a" : func () (* declcfg.DeclarativeConfig , error ) { return genPackage ("foo" ), nil },
652
+ }
653
+ r := CatalogResolver {WalkCatalogsFunc : w .WalkCatalogs }
654
+ ce := & ocv1alpha1.ClusterExtension {
655
+ ObjectMeta : metav1.ObjectMeta {
656
+ Name : "foo" ,
657
+ },
658
+ Spec : ocv1alpha1.ClusterExtensionSpec {
659
+ PackageName : "foo" ,
660
+ CatalogSelector : metav1.LabelSelector {
661
+ MatchLabels : map [string ]string {"name" : "&value" },
662
+ },
663
+ },
664
+ }
665
+ _ , _ , _ , err := r .Resolve (context .Background (), ce , nil )
666
+ assert .ErrorContains (t , err , "desired catalog selector is invalid: values[0][name]: Invalid value:" )
667
+ }
668
+
669
+ func TestClusterExtensionMatchLabel (t * testing.T ) {
670
+ defer featuregatetesting .SetFeatureGateDuringTest (t , features .OperatorControllerFeatureGate , features .ForceSemverUpgradeConstraints , false )()
671
+ pkgName := randPkg ()
672
+ w := staticCatalogWalker {
673
+ "a" : func () (* declcfg.DeclarativeConfig , error ) { return & declcfg.DeclarativeConfig {}, nil },
674
+ "b" : func () (* declcfg.DeclarativeConfig , error ) { return genPackage (pkgName ), nil },
675
+ }
676
+ r := CatalogResolver {WalkCatalogsFunc : w .WalkCatalogs }
677
+ ce := buildFooClusterExtension (pkgName , "" , "" , ocv1alpha1 .UpgradeConstraintPolicyEnforce )
678
+ ce .Spec .CatalogSelector .MatchLabels = map [string ]string {"olm.operatorframework.io/name" : "b" }
679
+
680
+ _ , _ , _ , err := r .Resolve (context .Background (), ce , nil )
681
+ require .NoError (t , err )
682
+ }
683
+
684
+ func TestClusterExtensionNoMatchLabel (t * testing.T ) {
685
+ defer featuregatetesting .SetFeatureGateDuringTest (t , features .OperatorControllerFeatureGate , features .ForceSemverUpgradeConstraints , false )()
686
+ pkgName := randPkg ()
687
+ w := staticCatalogWalker {
688
+ "a" : func () (* declcfg.DeclarativeConfig , error ) { return & declcfg.DeclarativeConfig {}, nil },
689
+ "b" : func () (* declcfg.DeclarativeConfig , error ) { return genPackage (pkgName ), nil },
690
+ }
691
+ r := CatalogResolver {WalkCatalogsFunc : w .WalkCatalogs }
692
+ ce := buildFooClusterExtension (pkgName , "" , "" , ocv1alpha1 .UpgradeConstraintPolicyEnforce )
693
+ ce .Spec .CatalogSelector .MatchLabels = map [string ]string {"olm.operatorframework.io/name" : "a" }
694
+
695
+ _ , _ , _ , err := r .Resolve (context .Background (), ce , nil )
696
+ require .Error (t , err )
697
+ require .ErrorContains (t , err , fmt .Sprintf ("no package %q found" , pkgName ))
698
+ }
0 commit comments