|
| 1 | +package action_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "slices" |
| 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 | + olmv1catalogd "github.com/operator-framework/catalogd/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("CatalogDelete", 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 because of both resource name and --all specifier being present", func() { |
| 37 | + cfg := setupEnv(setupTestCatalogs(2)...) |
| 38 | + |
| 39 | + deleter := internalaction.NewCatalogDelete(&cfg) |
| 40 | + deleter.CatalogName = "name" |
| 41 | + deleter.DeleteAll = true |
| 42 | + catNames, err := deleter.Run(context.TODO()) |
| 43 | + Expect(err).NotTo(BeNil()) |
| 44 | + Expect(catNames).To(BeEmpty()) |
| 45 | + |
| 46 | + validateExistingCatalogs(cfg.Client, []string{"cat1", "cat2"}) |
| 47 | + }) |
| 48 | + |
| 49 | + It("fails deleting a non-existing catalog", func() { |
| 50 | + cfg := setupEnv(setupTestCatalogs(2)...) |
| 51 | + |
| 52 | + deleter := internalaction.NewCatalogDelete(&cfg) |
| 53 | + deleter.CatalogName = "does-not-exist" |
| 54 | + catNames, err := deleter.Run(context.TODO()) |
| 55 | + Expect(err).NotTo(BeNil()) |
| 56 | + Expect(catNames).To(BeEmpty()) |
| 57 | + |
| 58 | + validateExistingCatalogs(cfg.Client, []string{"cat1", "cat2"}) |
| 59 | + }) |
| 60 | + |
| 61 | + It("successfully deletes an existing catalog", func() { |
| 62 | + cfg := setupEnv(setupTestCatalogs(3)...) |
| 63 | + |
| 64 | + deleter := internalaction.NewCatalogDelete(&cfg) |
| 65 | + deleter.CatalogName = "cat2" |
| 66 | + catNames, err := deleter.Run(context.TODO()) |
| 67 | + Expect(err).To(BeNil()) |
| 68 | + Expect(catNames).To(BeEmpty()) |
| 69 | + |
| 70 | + validateExistingCatalogs(cfg.Client, []string{"cat1", "cat3"}) |
| 71 | + }) |
| 72 | + |
| 73 | + It("fails deleting catalogs because there are none", func() { |
| 74 | + cfg := setupEnv() |
| 75 | + |
| 76 | + deleter := internalaction.NewCatalogDelete(&cfg) |
| 77 | + deleter.DeleteAll = true |
| 78 | + catNames, err := deleter.Run(context.TODO()) |
| 79 | + Expect(err).NotTo(BeNil()) |
| 80 | + Expect(catNames).To(BeEmpty()) |
| 81 | + |
| 82 | + validateExistingCatalogs(cfg.Client, []string{}) |
| 83 | + }) |
| 84 | + |
| 85 | + It("successfully deletes all catalogs", func() { |
| 86 | + cfg := setupEnv(setupTestCatalogs(3)...) |
| 87 | + |
| 88 | + deleter := internalaction.NewCatalogDelete(&cfg) |
| 89 | + deleter.DeleteAll = true |
| 90 | + catNames, err := deleter.Run(context.TODO()) |
| 91 | + Expect(err).To(BeNil()) |
| 92 | + Expect(catNames).To(ContainElements([]string{"cat1", "cat2", "cat3"})) |
| 93 | + |
| 94 | + validateExistingCatalogs(cfg.Client, []string{}) |
| 95 | + }) |
| 96 | +}) |
| 97 | + |
| 98 | +func validateExistingCatalogs(c client.Client, wantedNames []string) { |
| 99 | + var catalogsList olmv1catalogd.ClusterCatalogList |
| 100 | + err := c.List(context.TODO(), &catalogsList) |
| 101 | + Expect(err).To(BeNil()) |
| 102 | + |
| 103 | + catalogs := catalogsList.Items |
| 104 | + Expect(catalogs).To(HaveLen(len(wantedNames))) |
| 105 | + for _, wantedName := range wantedNames { |
| 106 | + Expect(slices.ContainsFunc(catalogs, func(cat olmv1catalogd.ClusterCatalog) bool { |
| 107 | + return cat.Name == wantedName |
| 108 | + })).To(BeTrue()) |
| 109 | + } |
| 110 | +} |
0 commit comments