66 "fmt"
77 "math/rand"
88 "os"
9+ "reflect"
910 "testing"
1011 "time"
1112
@@ -16,6 +17,8 @@ import (
1617 "github.com/operator-framework/operator-registry/pkg/image"
1718 "github.com/operator-framework/operator-registry/pkg/registry"
1819 "github.com/operator-framework/operator-registry/pkg/sqlite"
20+
21+ utilerrors "k8s.io/apimachinery/pkg/util/errors"
1922)
2023
2124func init () {
@@ -246,6 +249,7 @@ func TestImageLoading(t *testing.T) {
246249 addImage img
247250 wantPackages []* registry.Package
248251 wantErr bool
252+ err error
249253 }{
250254 {
251255 name : "OneChannel/AddBundleToTwoChannels" ,
@@ -386,6 +390,76 @@ func TestImageLoading(t *testing.T) {
386390 },
387391 wantErr : false ,
388392 },
393+ {
394+ name : "AddBundleAlreadyExists" ,
395+ initImages : []img {
396+ {
397+ // this is in the "preview" channel
398+ ref : image .SimpleReference ("quay.io/prometheus/operator:0.14.0" ),
399+ dir : "../../bundles/prometheus.0.14.0" ,
400+ },
401+ },
402+ addImage : img {
403+ //Adding same bundle different bundle
404+ ref : image .SimpleReference ("quay.io/prometheus/operator-test:testing" ),
405+ dir : "../../bundles/prometheus.0.14.0" ,
406+ },
407+ wantPackages : []* registry.Package {
408+ {
409+ Name : "prometheus" ,
410+ DefaultChannel : "stable" ,
411+ Channels : map [string ]registry.Channel {
412+ "preview" : {
413+ Head : registry.BundleKey {
414+ BundlePath : "quay.io/prometheus/operator:0.14.0" ,
415+ Version : "0.14.0" ,
416+ CsvName : "prometheusoperator.0.14.0" ,
417+ },
418+ Nodes : map [registry.BundleKey ]map [registry.BundleKey ]struct {}{
419+ {BundlePath : "quay.io/prometheus/operator:0.14.0" , Version : "0.14.0" , CsvName : "prometheusoperator.0.14.0" }: {},
420+ },
421+ },
422+ },
423+ },
424+ },
425+ wantErr : true ,
426+ err : registry.PackageVersionAlreadyAddedErr {},
427+ },
428+ {
429+ name : "AddExactBundleAlreadyExists" ,
430+ initImages : []img {
431+ {
432+ // this is in the "preview" channel
433+ ref : image .SimpleReference ("quay.io/prometheus/operator:0.14.0" ),
434+ dir : "../../bundles/prometheus.0.14.0" ,
435+ },
436+ },
437+ addImage : img {
438+ // Add the same package
439+ ref : image .SimpleReference ("quay.io/prometheus/operator:0.14.0" ),
440+ dir : "../../bundles/prometheus.0.14.0" ,
441+ },
442+ wantPackages : []* registry.Package {
443+ {
444+ Name : "prometheus" ,
445+ DefaultChannel : "stable" ,
446+ Channels : map [string ]registry.Channel {
447+ "preview" : {
448+ Head : registry.BundleKey {
449+ BundlePath : "quay.io/prometheus/operator:0.14.0" ,
450+ Version : "0.14.0" ,
451+ CsvName : "prometheusoperator.0.14.0" ,
452+ },
453+ Nodes : map [registry.BundleKey ]map [registry.BundleKey ]struct {}{
454+ {BundlePath : "quay.io/prometheus/operator:0.14.0" , Version : "0.14.0" , CsvName : "prometheusoperator.0.14.0" }: {},
455+ },
456+ },
457+ },
458+ },
459+ },
460+ wantErr : true ,
461+ err : registry.BundleImageAlreadyAddedErr {},
462+ },
389463 }
390464 for _ , tt := range tests {
391465 t .Run (tt .name , func (t * testing.T ) {
@@ -411,7 +485,12 @@ func TestImageLoading(t *testing.T) {
411485 graphLoader ,
412486 query ,
413487 map [image.Reference ]string {tt .addImage .ref : tt .addImage .dir })
414- require .NoError (t , add .Populate (registry .ReplacesMode ))
488+ err = add .Populate (registry .ReplacesMode )
489+ if tt .wantErr {
490+ require .True (t , checkAggErr (err , tt .err ))
491+ return
492+ }
493+ require .NoError (t , err )
415494
416495 for _ , p := range tt .wantPackages {
417496 graphLoader , err := sqlite .NewSQLGraphLoaderFromDB (db )
@@ -426,6 +505,18 @@ func TestImageLoading(t *testing.T) {
426505 }
427506}
428507
508+ func checkAggErr (aggErr , wantErr error ) bool {
509+ if a , ok := aggErr .(utilerrors.Aggregate ); ok {
510+ for _ , e := range a .Errors () {
511+ if reflect .TypeOf (e ).String () == reflect .TypeOf (wantErr ).String () {
512+ return true
513+ }
514+ }
515+ return false
516+ }
517+ return reflect .TypeOf (aggErr ).String () == reflect .TypeOf (wantErr ).String ()
518+ }
519+
429520func TestQuerierForDependencies (t * testing.T ) {
430521 logrus .SetLevel (logrus .DebugLevel )
431522 db , cleanup := CreateTestDb (t )
0 commit comments