Skip to content

Commit 0a0282b

Browse files
authoredApr 22, 2021
Merge pull request operator-framework#642 from gallettilance/deprecate-bug
Bug 1951387: (bug) add after deprecation
2 parents 0f5a9cf + b5f4883 commit 0a0282b

File tree

2 files changed

+147
-0
lines changed

2 files changed

+147
-0
lines changed
 

‎pkg/registry/populator_test.go

+134
Original file line numberDiff line numberDiff line change
@@ -911,6 +911,140 @@ func TestDeprecateBundle(t *testing.T) {
911911
}
912912
}
913913

914+
func TestAddAfterDeprecate(t *testing.T) {
915+
type args struct {
916+
firstBundles []string
917+
deprecatedBundles []string
918+
secondBundles []string
919+
}
920+
type pkgChannel map[string][]string
921+
type expected struct {
922+
err error
923+
remainingBundles []string
924+
deprecatedBundles []string
925+
remainingPkgChannels pkgChannel
926+
}
927+
tests := []struct {
928+
description string
929+
args args
930+
expected expected
931+
}{
932+
{
933+
description: "SimpleAdd",
934+
args: args{
935+
firstBundles: []string{
936+
"prometheus.0.14.0",
937+
"prometheus.0.15.0",
938+
},
939+
deprecatedBundles: []string{
940+
"quay.io/test/prometheus.0.15.0",
941+
},
942+
secondBundles: []string{
943+
"prometheus.0.22.2",
944+
},
945+
},
946+
expected: expected{
947+
err: nil,
948+
remainingBundles: []string{
949+
"quay.io/test/prometheus.0.15.0/preview",
950+
"quay.io/test/prometheus.0.15.0/stable",
951+
"quay.io/test/prometheus.0.22.2/preview",
952+
},
953+
deprecatedBundles: []string{
954+
"quay.io/test/prometheus.0.15.0/preview",
955+
"quay.io/test/prometheus.0.15.0/stable",
956+
},
957+
remainingPkgChannels: pkgChannel{
958+
"prometheus": []string{
959+
"preview",
960+
"stable",
961+
},
962+
},
963+
},
964+
},
965+
}
966+
967+
for _, tt := range tests {
968+
t.Run(tt.description, func(t *testing.T) {
969+
logrus.SetLevel(logrus.DebugLevel)
970+
db, cleanup := CreateTestDb(t)
971+
defer cleanup()
972+
973+
load, err := sqlite.NewSQLLiteLoader(db, sqlite.WithEnableAlpha(true))
974+
require.NoError(t, err)
975+
err = load.Migrate(context.TODO())
976+
require.NoError(t, err)
977+
query := sqlite.NewSQLLiteQuerierFromDb(db)
978+
979+
graphLoader, err := sqlite.NewSQLGraphLoaderFromDB(db)
980+
require.NoError(t, err)
981+
982+
populate := func(names []string) error {
983+
refMap := make(map[image.Reference]string, 0)
984+
for _, name := range names {
985+
refMap[image.SimpleReference("quay.io/test/"+name)] = "../../bundles/" + name
986+
}
987+
return registry.NewDirectoryPopulator(
988+
load,
989+
graphLoader,
990+
query,
991+
refMap,
992+
make(map[string]map[image.Reference]string, 0), false).Populate(registry.ReplacesMode)
993+
}
994+
// Initialize index with some bundles
995+
require.NoError(t, populate(tt.args.firstBundles))
996+
997+
deprecator := sqlite.NewSQLDeprecatorForBundles(load, tt.args.deprecatedBundles)
998+
err = deprecator.Deprecate()
999+
require.Equal(t, tt.expected.err, err)
1000+
1001+
require.NoError(t, populate(tt.args.secondBundles))
1002+
1003+
// Ensure remaining bundlePaths in db match
1004+
bundles, err := query.ListBundles(context.Background())
1005+
require.NoError(t, err)
1006+
var bundlePaths []string
1007+
for _, bundle := range bundles {
1008+
bundlePaths = append(bundlePaths, strings.Join([]string{bundle.BundlePath, bundle.ChannelName}, "/"))
1009+
}
1010+
require.ElementsMatch(t, tt.expected.remainingBundles, bundlePaths)
1011+
1012+
// Ensure deprecated bundles match
1013+
var deprecatedBundles []string
1014+
deprecatedProperty, err := json.Marshal(registry.DeprecatedProperty{})
1015+
require.NoError(t, err)
1016+
for _, bundle := range bundles {
1017+
for _, prop := range bundle.Properties {
1018+
if prop.Type == registry.DeprecatedType && prop.Value == string(deprecatedProperty) {
1019+
deprecatedBundles = append(deprecatedBundles, strings.Join([]string{bundle.BundlePath, bundle.ChannelName}, "/"))
1020+
}
1021+
}
1022+
}
1023+
1024+
require.ElementsMatch(t, tt.expected.deprecatedBundles, deprecatedBundles)
1025+
1026+
// Ensure remaining channels match
1027+
packages, err := query.ListPackages(context.Background())
1028+
require.NoError(t, err)
1029+
1030+
for _, pkg := range packages {
1031+
channelEntries, err := query.GetChannelEntriesFromPackage(context.Background(), pkg)
1032+
require.NoError(t, err)
1033+
1034+
uniqueChannels := make(map[string]struct{})
1035+
var channels []string
1036+
for _, ch := range channelEntries {
1037+
uniqueChannels[ch.ChannelName] = struct{}{}
1038+
}
1039+
for k := range uniqueChannels {
1040+
channels = append(channels, k)
1041+
}
1042+
require.ElementsMatch(t, tt.expected.remainingPkgChannels[pkg], channels)
1043+
}
1044+
})
1045+
}
1046+
}
1047+
9141048
func TestOverwrite(t *testing.T) {
9151049
type args struct {
9161050
firstAdd map[image.Reference]string

‎pkg/sqlite/load.go

+13
Original file line numberDiff line numberDiff line change
@@ -1397,6 +1397,12 @@ func (s *sqlLoader) DeprecateBundle(path string) error {
13971397
tx.Rollback()
13981398
}()
13991399

1400+
nullifyBundleReplaces, err := tx.Prepare("update operatorbundle set replaces = NULL where name = ?")
1401+
if err != nil {
1402+
return err
1403+
}
1404+
defer nullifyBundleReplaces.Close()
1405+
14001406
name, version, err := getBundleNameAndVersionForImage(tx, path)
14011407
if err != nil {
14021408
return err
@@ -1426,6 +1432,13 @@ func (s *sqlLoader) DeprecateBundle(path string) error {
14261432
return err
14271433
}
14281434

1435+
// update replaces field of deprecated bundle so that inserting a new bundle
1436+
// (which rebuilds the graph) is possible
1437+
_, err = nullifyBundleReplaces.Exec(name)
1438+
if err != nil {
1439+
return err
1440+
}
1441+
14291442
return tx.Commit()
14301443
}
14311444

0 commit comments

Comments
 (0)
Please sign in to comment.