|
| 1 | +package action_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "maps" |
| 6 | + |
| 7 | + . "github.com/onsi/ginkgo" |
| 8 | + . "github.com/onsi/gomega" |
| 9 | + |
| 10 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 11 | + "sigs.k8s.io/controller-runtime/pkg/client/fake" |
| 12 | + |
| 13 | + olmv1 "github.com/operator-framework/operator-controller/api/v1" |
| 14 | + |
| 15 | + internalaction "github.com/operator-framework/kubectl-operator/internal/pkg/v1/action" |
| 16 | + "github.com/operator-framework/kubectl-operator/pkg/action" |
| 17 | +) |
| 18 | + |
| 19 | +var _ = Describe("CatalogUpdate", func() { |
| 20 | + setupEnv := func(catalogs ...client.Object) action.Configuration { |
| 21 | + var cfg action.Configuration |
| 22 | + |
| 23 | + sch, err := action.NewScheme() |
| 24 | + Expect(err).To(BeNil()) |
| 25 | + |
| 26 | + cl := fake.NewClientBuilder(). |
| 27 | + WithObjects(catalogs...). |
| 28 | + WithScheme(sch). |
| 29 | + Build() |
| 30 | + cfg.Scheme = sch |
| 31 | + cfg.Client = cl |
| 32 | + |
| 33 | + return cfg |
| 34 | + } |
| 35 | + |
| 36 | + It("fails finding existing catalog", func() { |
| 37 | + cfg := setupEnv() |
| 38 | + |
| 39 | + updater := internalaction.NewCatalogUpdate(&cfg) |
| 40 | + updater.CatalogName = "does-not-exist" |
| 41 | + cat, err := updater.Run(context.TODO()) |
| 42 | + |
| 43 | + Expect(err).NotTo(BeNil()) |
| 44 | + Expect(err.Error()).To(ContainSubstring("not found")) |
| 45 | + Expect(cat).To(BeNil()) |
| 46 | + }) |
| 47 | + |
| 48 | + It("fails to handle catalog with unknown source type", func() { |
| 49 | + cfg := setupEnv(buildCatalog("test", withCatalogSourceType("invalid-type"))) |
| 50 | + |
| 51 | + updater := internalaction.NewCatalogUpdate(&cfg) |
| 52 | + updater.CatalogName = "test" |
| 53 | + _, err := updater.Run(context.TODO()) |
| 54 | + |
| 55 | + Expect(err).NotTo(BeNil()) |
| 56 | + Expect(err.Error()).To(ContainSubstring("unrecognized source type")) |
| 57 | + }) |
| 58 | + |
| 59 | + It("successfully updates catalog", func() { |
| 60 | + testCatalog := buildCatalog( |
| 61 | + "testCatalog", |
| 62 | + withCatalogSourceType(olmv1.SourceTypeImage), |
| 63 | + withCatalogPollInterval(5, "testCatalog"), |
| 64 | + withCatalogSourcePriority(1), |
| 65 | + ) |
| 66 | + cfg := setupEnv(testCatalog) |
| 67 | + |
| 68 | + updater := internalaction.NewCatalogUpdate(&cfg) |
| 69 | + updater.CatalogName = "testCatalog" |
| 70 | + updater.Priority = int32(1) |
| 71 | + updater.Labels = map[string]string{"c": "d"} |
| 72 | + updater.AvailabilityMode = string(olmv1.AvailabilityModeAvailable) |
| 73 | + updater.PollIntervalMinutes = int(5) |
| 74 | + catalog, err := updater.Run(context.TODO()) |
| 75 | + |
| 76 | + Expect(err).To(BeNil()) |
| 77 | + Expect(testCatalog).NotTo(BeNil()) |
| 78 | + Expect(maps.Equal(catalog.Labels, updater.Labels)).To(BeTrue()) |
| 79 | + Expect(catalog.Spec.Priority).To(Equal(updater.Priority)) |
| 80 | + Expect(catalog.Spec.Source.Image.PollIntervalMinutes).ToNot(BeNil()) |
| 81 | + Expect(*catalog.Spec.Source.Image.PollIntervalMinutes).To(Equal(int(5))) |
| 82 | + Expect(catalog.Spec.AvailabilityMode).To(Equal(olmv1.AvailabilityMode(updater.AvailabilityMode))) |
| 83 | + }) |
| 84 | +}) |
0 commit comments