@@ -939,6 +939,179 @@ func TestDeprecateBundle(t *testing.T) {
939939 }
940940}
941941
942+ func TestDeprecatePackage (t * testing.T ) {
943+ type args struct {
944+ bundles []string
945+ }
946+ type pkgChannel map [string ][]string
947+ type expected struct {
948+ err error
949+ remainingBundles []string
950+ deprecatedBundles []string
951+ remainingPkgChannels pkgChannel
952+ }
953+ tests := []struct {
954+ description string
955+ args args
956+ expected expected
957+ }{
958+ {
959+ description : "RemoveEntirePackage/BundlesAreAllHeadsOfChannels" ,
960+ args : args {
961+ bundles : []string {
962+ "quay.io/test/etcd.0.9.2" ,
963+ "quay.io/test/etcd.0.9.0" ,
964+ },
965+ },
966+ expected : expected {
967+ err : nil ,
968+ remainingBundles : []string {
969+ "quay.io/test/prometheus.0.22.2/preview" ,
970+ "quay.io/test/prometheus.0.15.0/preview" ,
971+ "quay.io/test/prometheus.0.15.0/stable" ,
972+ "quay.io/test/prometheus.0.14.0/preview" ,
973+ "quay.io/test/prometheus.0.14.0/stable" ,
974+ },
975+ deprecatedBundles : []string {},
976+ remainingPkgChannels : pkgChannel {
977+ "prometheus" : []string {
978+ "preview" ,
979+ "stable" ,
980+ },
981+ },
982+ },
983+ },
984+ {
985+ description : "RemoveHeadOfDefaultChannelWithoutAllChannelHeads/Error" ,
986+ args : args {
987+ bundles : []string {
988+ "quay.io/test/etcd.0.9.2" ,
989+ },
990+ },
991+ expected : expected {
992+ err : utilerrors .NewAggregate ([]error {fmt .Errorf ("cannot deprecate default channel head from package without removing all other channel heads in package %s: must deprecate %s, head of channel %s" , "etcd" , "quay.io/test/etcd.0.9.0" , "beta" )}),
993+ remainingBundles : []string {
994+ "quay.io/test/etcd.0.9.0/alpha" ,
995+ "quay.io/test/etcd.0.9.0/beta" ,
996+ "quay.io/test/etcd.0.9.0/stable" ,
997+ "quay.io/test/etcd.0.9.2/stable" ,
998+ "quay.io/test/etcd.0.9.2/alpha" ,
999+ "quay.io/test/prometheus.0.14.0/preview" ,
1000+ "quay.io/test/prometheus.0.14.0/stable" ,
1001+ "quay.io/test/prometheus.0.15.0/preview" ,
1002+ "quay.io/test/prometheus.0.15.0/stable" ,
1003+ "quay.io/test/prometheus.0.22.2/preview" ,
1004+ },
1005+ deprecatedBundles : []string {},
1006+ remainingPkgChannels : pkgChannel {
1007+ "etcd" : []string {
1008+ "alpha" ,
1009+ "beta" ,
1010+ "stable" ,
1011+ },
1012+ "prometheus" : []string {
1013+ "preview" ,
1014+ "stable" ,
1015+ },
1016+ },
1017+ },
1018+ },
1019+ {
1020+ description : "RemoveEntirePackage/AndDeprecateAdditionalBundle" ,
1021+ args : args {
1022+ bundles : []string {
1023+ "quay.io/test/etcd.0.9.2" ,
1024+ "quay.io/test/etcd.0.9.0" ,
1025+ "quay.io/test/prometheus.0.14.0" ,
1026+ },
1027+ },
1028+ expected : expected {
1029+ err : nil ,
1030+ remainingBundles : []string {
1031+ "quay.io/test/prometheus.0.22.2/preview" ,
1032+ "quay.io/test/prometheus.0.15.0/preview" ,
1033+ "quay.io/test/prometheus.0.15.0/stable" ,
1034+ "quay.io/test/prometheus.0.14.0/preview" ,
1035+ "quay.io/test/prometheus.0.14.0/stable" ,
1036+ },
1037+ deprecatedBundles : []string {
1038+ "quay.io/test/prometheus.0.14.0/preview" ,
1039+ "quay.io/test/prometheus.0.14.0/stable" ,
1040+ },
1041+ remainingPkgChannels : pkgChannel {
1042+ "prometheus" : []string {
1043+ "preview" ,
1044+ "stable" ,
1045+ },
1046+ },
1047+ },
1048+ },
1049+ }
1050+
1051+ for _ , tt := range tests {
1052+ t .Run (tt .description , func (t * testing.T ) {
1053+ logrus .SetLevel (logrus .DebugLevel )
1054+ db , cleanup := CreateTestDb (t )
1055+ defer cleanup ()
1056+
1057+ querier , err := createAndPopulateDB (db )
1058+ require .NoError (t , err )
1059+
1060+ store , err := sqlite .NewSQLLiteLoader (db )
1061+ require .NoError (t , err )
1062+
1063+ deprecator := sqlite .NewSQLDeprecatorForBundles (store , tt .args .bundles )
1064+ packageDeprecator := sqlite .NewSQLDeprecatorForBundlesAndPackages (deprecator , querier )
1065+ require .Equal (t , tt .expected .err , packageDeprecator .MaybeRemovePackages ())
1066+ if len (tt .expected .deprecatedBundles ) > 0 {
1067+ require .Equal (t , tt .expected .err , deprecator .Deprecate ())
1068+ }
1069+
1070+ // Ensure remaining bundlePaths in db match
1071+ bundles , err := querier .ListBundles (context .Background ())
1072+ require .NoError (t , err )
1073+ var bundlePaths []string
1074+ for _ , bundle := range bundles {
1075+ bundlePaths = append (bundlePaths , strings .Join ([]string {bundle .BundlePath , bundle .ChannelName }, "/" ))
1076+ }
1077+ require .ElementsMatch (t , tt .expected .remainingBundles , bundlePaths )
1078+
1079+ // Ensure deprecated bundles match
1080+ var deprecatedBundles []string
1081+ deprecatedProperty , err := json .Marshal (registry.DeprecatedProperty {})
1082+ require .NoError (t , err )
1083+ for _ , bundle := range bundles {
1084+ for _ , prop := range bundle .Properties {
1085+ if prop .Type == registry .DeprecatedType && prop .Value == string (deprecatedProperty ) {
1086+ deprecatedBundles = append (deprecatedBundles , strings .Join ([]string {bundle .BundlePath , bundle .ChannelName }, "/" ))
1087+ }
1088+ }
1089+ }
1090+
1091+ require .ElementsMatch (t , tt .expected .deprecatedBundles , deprecatedBundles )
1092+
1093+ // Ensure remaining channels match
1094+ packages , err := querier .ListPackages (context .Background ())
1095+ require .NoError (t , err )
1096+
1097+ for _ , pkg := range packages {
1098+ channelEntries , err := querier .GetChannelEntriesFromPackage (context .Background (), pkg )
1099+ require .NoError (t , err )
1100+
1101+ uniqueChannels := make (map [string ]struct {})
1102+ var channels []string
1103+ for _ , ch := range channelEntries {
1104+ uniqueChannels [ch .ChannelName ] = struct {}{}
1105+ }
1106+ for k := range uniqueChannels {
1107+ channels = append (channels , k )
1108+ }
1109+ require .ElementsMatch (t , tt .expected .remainingPkgChannels [pkg ], channels )
1110+ }
1111+ })
1112+ }
1113+ }
1114+
9421115func TestAddAfterDeprecate (t * testing.T ) {
9431116 type args struct {
9441117 existing []string
0 commit comments