@@ -3,6 +3,7 @@ package registry_test
3
3
import (
4
4
"context"
5
5
"database/sql"
6
+ "encoding/json"
6
7
"fmt"
7
8
"math/rand"
8
9
"os"
@@ -18,6 +19,7 @@ import (
18
19
"github.com/operator-framework/operator-registry/pkg/registry"
19
20
"github.com/operator-framework/operator-registry/pkg/sqlite"
20
21
22
+ "k8s.io/apimachinery/pkg/util/errors"
21
23
utilerrors "k8s.io/apimachinery/pkg/util/errors"
22
24
)
23
25
@@ -109,6 +111,10 @@ func TestQuerierForImage(t *testing.T) {
109
111
Name : "alpha" ,
110
112
CurrentCSVName : "etcdoperator.v0.9.2" ,
111
113
},
114
+ {
115
+ Name : "beta" ,
116
+ CurrentCSVName : "etcdoperator.v0.9.0" ,
117
+ },
112
118
{
113
119
Name : "stable" ,
114
120
CurrentCSVName : "etcdoperator.v0.9.2" ,
@@ -185,12 +191,14 @@ func TestQuerierForImage(t *testing.T) {
185
191
{"etcd" , "alpha" , "etcdoperator.v0.9.2" , "etcdoperator.v0.9.0" },
186
192
{"etcd" , "stable" , "etcdoperator.v0.9.0" , "" },
187
193
{"etcd" , "stable" , "etcdoperator.v0.9.2" , "etcdoperator.v0.9.1" },
188
- {"etcd" , "stable" , "etcdoperator.v0.9.2" , "etcdoperator.v0.9.0" }}, etcdChannelEntriesThatProvide )
194
+ {"etcd" , "stable" , "etcdoperator.v0.9.2" , "etcdoperator.v0.9.0" },
195
+ {"etcd" , "beta" , "etcdoperator.v0.9.0" , "" }}, etcdChannelEntriesThatProvide )
189
196
190
197
etcdLatestChannelEntriesThatProvide , err := store .GetLatestChannelEntriesThatProvide (context .TODO (), "etcd.database.coreos.com" , "v1beta2" , "EtcdCluster" )
191
198
require .NoError (t , err )
192
199
require .ElementsMatch (t , []* registry.ChannelEntry {{"etcd" , "alpha" , "etcdoperator.v0.9.2" , "etcdoperator.v0.9.0" },
193
- {"etcd" , "stable" , "etcdoperator.v0.9.2" , "etcdoperator.v0.9.0" }}, etcdLatestChannelEntriesThatProvide )
200
+ {"etcd" , "stable" , "etcdoperator.v0.9.2" , "etcdoperator.v0.9.0" },
201
+ {"etcd" , "beta" , "etcdoperator.v0.9.0" , "" }}, etcdLatestChannelEntriesThatProvide )
194
202
195
203
etcdBundleByProvides , err := store .GetBundleThatProvides (context .TODO (), "etcd.database.coreos.com" , "v1beta2" , "EtcdCluster" )
196
204
require .NoError (t , err )
@@ -229,7 +237,7 @@ func TestQuerierForImage(t *testing.T) {
229
237
230
238
listChannels , err := store .ListChannels (context .TODO (), "etcd" )
231
239
require .NoError (t , err )
232
- expectedListChannels := []string {"alpha" , "stable" }
240
+ expectedListChannels := []string {"alpha" , "stable" , "beta" }
233
241
require .ElementsMatch (t , expectedListChannels , listChannels )
234
242
235
243
currentCSVName , err := store .GetCurrentCSVNameForChannel (context .TODO (), "etcd" , "alpha" )
@@ -670,3 +678,173 @@ func CheckBundlesHaveContentsIfNoPath(t *testing.T, db *sql.DB) {
670
678
require .NotZero (t , bundlelen .Int64 , "length of bundle for %s should not be zero, it has no bundle path" , name .String )
671
679
}
672
680
}
681
+
682
+ func TestDeprecateBundle (t * testing.T ) {
683
+ type args struct {
684
+ bundles []string
685
+ }
686
+ type pkgChannel map [string ][]string
687
+ type expected struct {
688
+ err error
689
+ remainingBundles []string
690
+ deprecatedBundles []string
691
+ remainingPkgChannels pkgChannel
692
+ }
693
+ tests := []struct {
694
+ description string
695
+ args args
696
+ expected expected
697
+ }{
698
+ {
699
+ description : "BundleDeprecated/IgnoreIfNotInIndex" ,
700
+ args : args {
701
+ bundles : []string {
702
+ "quay.io/test/etcd.0.6.0" ,
703
+ },
704
+ },
705
+ expected : expected {
706
+ err : errors .NewAggregate ([]error {fmt .Errorf ("error deprecating bundle quay.io/test/etcd.0.6.0: %s" , registry .ErrBundleImageNotInDatabase )}),
707
+ remainingBundles : []string {
708
+ "quay.io/test/etcd.0.9.0" ,
709
+ "quay.io/test/etcd.0.9.2" ,
710
+ "quay.io/test/prometheus.0.22.2" ,
711
+ "quay.io/test/prometheus.0.14.0" ,
712
+ "quay.io/test/prometheus.0.15.0" ,
713
+ },
714
+ deprecatedBundles : []string {},
715
+ remainingPkgChannels : pkgChannel {
716
+ "etcd" : []string {
717
+ "beta" ,
718
+ "alpha" ,
719
+ "stable" ,
720
+ },
721
+ "prometheus" : []string {
722
+ "preview" ,
723
+ "stable" ,
724
+ },
725
+ },
726
+ },
727
+ },
728
+ {
729
+ description : "BundleDeprecated/SingleChannel" ,
730
+ args : args {
731
+ bundles : []string {
732
+ "quay.io/test/prometheus.0.15.0" ,
733
+ },
734
+ },
735
+ expected : expected {
736
+ err : nil ,
737
+ remainingBundles : []string {
738
+ "quay.io/test/etcd.0.9.0" ,
739
+ "quay.io/test/etcd.0.9.2" ,
740
+ "quay.io/test/prometheus.0.22.2" ,
741
+ "quay.io/test/prometheus.0.15.0" ,
742
+ },
743
+ deprecatedBundles : []string {
744
+ "quay.io/test/prometheus.0.15.0" ,
745
+ },
746
+ remainingPkgChannels : pkgChannel {
747
+ "etcd" : []string {
748
+ "beta" ,
749
+ "alpha" ,
750
+ "stable" ,
751
+ },
752
+ "prometheus" : []string {
753
+ "preview" ,
754
+ "stable" ,
755
+ },
756
+ },
757
+ },
758
+ },
759
+ {
760
+ description : "BundleDeprecated/ChannelRemoved" ,
761
+ args : args {
762
+ bundles : []string {
763
+ "quay.io/test/etcd.0.9.2" ,
764
+ },
765
+ },
766
+ expected : expected {
767
+ err : nil ,
768
+ remainingBundles : []string {
769
+ "quay.io/test/etcd.0.9.2" ,
770
+ "quay.io/test/prometheus.0.22.2" ,
771
+ "quay.io/test/prometheus.0.14.0" ,
772
+ "quay.io/test/prometheus.0.15.0" ,
773
+ },
774
+ deprecatedBundles : []string {
775
+ "quay.io/test/etcd.0.9.2" ,
776
+ },
777
+ remainingPkgChannels : pkgChannel {
778
+ "etcd" : []string {
779
+ "alpha" ,
780
+ "stable" ,
781
+ },
782
+ "prometheus" : []string {
783
+ "preview" ,
784
+ "stable" ,
785
+ },
786
+ },
787
+ },
788
+ },
789
+ }
790
+
791
+ for _ , tt := range tests {
792
+ t .Run (tt .description , func (t * testing.T ) {
793
+ logrus .SetLevel (logrus .DebugLevel )
794
+ db , cleanup := CreateTestDb (t )
795
+ defer cleanup ()
796
+
797
+ querier , err := createAndPopulateDB (db )
798
+ require .NoError (t , err )
799
+
800
+ store , err := sqlite .NewSQLLiteLoader (db )
801
+ require .NoError (t , err )
802
+
803
+ deprecator := sqlite .NewSQLDeprecatorForBundles (store , tt .args .bundles )
804
+ err = deprecator .Deprecate ()
805
+ require .Equal (t , tt .expected .err , err )
806
+
807
+ // Ensure remaining bundlePaths in db match
808
+ bundles , err := querier .ListBundles (context .Background ())
809
+ require .NoError (t , err )
810
+ var bundlePaths []string
811
+ for _ , bundle := range bundles {
812
+ bundlePaths = append (bundlePaths , bundle .BundlePath )
813
+ }
814
+ require .ElementsMatch (t , tt .expected .remainingBundles , bundlePaths )
815
+
816
+ // Ensure deprecated bundles match
817
+ var deprecatedBundles []string
818
+ deprecatedProperty , err := json .Marshal (registry.DeprecatedProperty {})
819
+ require .NoError (t , err )
820
+ for _ , bundle := range bundles {
821
+ for _ , prop := range bundle .Properties {
822
+ if prop .Type == registry .DeprecatedType && prop .Value == string (deprecatedProperty ) {
823
+ deprecatedBundles = append (deprecatedBundles , bundle .BundlePath )
824
+ }
825
+ }
826
+ }
827
+
828
+ require .ElementsMatch (t , tt .expected .deprecatedBundles , deprecatedBundles )
829
+
830
+ // Ensure remaining channels match
831
+ packages , err := querier .ListPackages (context .Background ())
832
+ require .NoError (t , err )
833
+
834
+ for _ , pkg := range packages {
835
+ channelEntries , err := querier .GetChannelEntriesFromPackage (context .Background (), pkg )
836
+ require .NoError (t , err )
837
+
838
+ uniqueChannels := make (map [string ]struct {})
839
+ var channels []string
840
+ for _ , ch := range channelEntries {
841
+ uniqueChannels [ch .ChannelName ] = struct {}{}
842
+ }
843
+ for k := range uniqueChannels {
844
+ channels = append (channels , k )
845
+ }
846
+ require .ElementsMatch (t , tt .expected .remainingPkgChannels [pkg ], channels )
847
+ }
848
+ })
849
+ }
850
+ }
0 commit comments