|
| 1 | +package action_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + |
| 7 | + . "github.com/onsi/ginkgo" |
| 8 | + . "github.com/onsi/gomega" |
| 9 | + |
| 10 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 11 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 12 | + |
| 13 | + olmv1catalogd "github.com/operator-framework/catalogd/api/v1" |
| 14 | + |
| 15 | + internalaction "github.com/operator-framework/kubectl-operator/internal/pkg/v1/action" |
| 16 | +) |
| 17 | + |
| 18 | +type mockCreateClient struct { |
| 19 | + *mockCreator |
| 20 | + *mockGetter |
| 21 | + *mockDeleter |
| 22 | + createCatalog *olmv1catalogd.ClusterCatalog |
| 23 | +} |
| 24 | + |
| 25 | +func (mcc *mockCreateClient) Create(ctx context.Context, obj client.Object, opts ...client.CreateOption) error { |
| 26 | + mcc.createCatalog = obj.(*olmv1catalogd.ClusterCatalog) |
| 27 | + return mcc.mockCreator.Create(ctx, obj, opts...) |
| 28 | +} |
| 29 | + |
| 30 | +var _ = Describe("CatalogCreate", func() { |
| 31 | + pollInterval := 20 |
| 32 | + expectedCatalog := olmv1catalogd.ClusterCatalog{ |
| 33 | + ObjectMeta: metav1.ObjectMeta{ |
| 34 | + Name: "testcatalog", |
| 35 | + Labels: map[string]string{"a": "b"}, |
| 36 | + }, |
| 37 | + Spec: olmv1catalogd.ClusterCatalogSpec{ |
| 38 | + Source: olmv1catalogd.CatalogSource{ |
| 39 | + Type: olmv1catalogd.SourceTypeImage, |
| 40 | + Image: &olmv1catalogd.ImageSource{ |
| 41 | + Ref: "testcatalog:latest", |
| 42 | + PollIntervalMinutes: &pollInterval, |
| 43 | + }, |
| 44 | + }, |
| 45 | + Priority: 77, |
| 46 | + AvailabilityMode: olmv1catalogd.AvailabilityModeAvailable, |
| 47 | + }, |
| 48 | + } |
| 49 | + |
| 50 | + It("fails creating catalog", func() { |
| 51 | + expectedErr := errors.New("create failed") |
| 52 | + mockClient := &mockCreateClient{&mockCreator{createErr: expectedErr}, nil, nil, &expectedCatalog} |
| 53 | + |
| 54 | + creator := internalaction.NewCatalogCreate(mockClient) |
| 55 | + creator.Available = true |
| 56 | + creator.CatalogName = expectedCatalog.Name |
| 57 | + creator.ImageSourceRef = expectedCatalog.Spec.Source.Image.Ref |
| 58 | + creator.Priority = expectedCatalog.Spec.Priority |
| 59 | + creator.Labels = expectedCatalog.Labels |
| 60 | + creator.PollIntervalMinutes = *expectedCatalog.Spec.Source.Image.PollIntervalMinutes |
| 61 | + err := creator.Run(context.TODO()) |
| 62 | + |
| 63 | + Expect(err).NotTo(BeNil()) |
| 64 | + Expect(err).To(MatchError(expectedErr)) |
| 65 | + Expect(mockClient.createCalled).To(Equal(1)) |
| 66 | + |
| 67 | + // there is no way of testing a happy path in unit tests because we have no way to |
| 68 | + // set/mock the catalog status condition we're waiting for in waitUntilCatalogStatusCondition |
| 69 | + // but we can still at least verify that CR would have been created with expected attribute values |
| 70 | + validateCreateCatalog(mockClient.createCatalog, &expectedCatalog) |
| 71 | + }) |
| 72 | + |
| 73 | + It("fails waiting for created catalog status, successfully cleans up", func() { |
| 74 | + expectedErr := errors.New("get failed") |
| 75 | + mockClient := &mockCreateClient{&mockCreator{}, &mockGetter{getErr: expectedErr}, &mockDeleter{}, nil} |
| 76 | + |
| 77 | + creator := internalaction.NewCatalogCreate(mockClient) |
| 78 | + err := creator.Run(context.TODO()) |
| 79 | + |
| 80 | + Expect(err).NotTo(BeNil()) |
| 81 | + Expect(err).To(MatchError(expectedErr)) |
| 82 | + Expect(mockClient.createCalled).To(Equal(1)) |
| 83 | + Expect(mockClient.getCalled).To(Equal(1)) |
| 84 | + Expect(mockClient.deleteCalled).To(Equal(1)) |
| 85 | + }) |
| 86 | + |
| 87 | + It("fails waiting for created catalog status, fails clean up", func() { |
| 88 | + getErr := errors.New("get failed") |
| 89 | + deleteErr := errors.New("delete failed") |
| 90 | + mockClient := &mockCreateClient{&mockCreator{}, &mockGetter{getErr: getErr}, &mockDeleter{deleteErr: deleteErr}, nil} |
| 91 | + |
| 92 | + creator := internalaction.NewCatalogCreate(mockClient) |
| 93 | + err := creator.Run(context.TODO()) |
| 94 | + |
| 95 | + Expect(err).NotTo(BeNil()) |
| 96 | + Expect(err).To(MatchError(getErr)) |
| 97 | + Expect(mockClient.createCalled).To(Equal(1)) |
| 98 | + Expect(mockClient.getCalled).To(Equal(1)) |
| 99 | + Expect(mockClient.deleteCalled).To(Equal(1)) |
| 100 | + }) |
| 101 | +}) |
| 102 | + |
| 103 | +func validateCreateCatalog(actual, expected *olmv1catalogd.ClusterCatalog) { |
| 104 | + Expect(actual.Spec.Source.Image.Ref).To(Equal(expected.Spec.Source.Image.Ref)) |
| 105 | + Expect(actual.Spec.Source.Image.PollIntervalMinutes).To(Equal(expected.Spec.Source.Image.PollIntervalMinutes)) |
| 106 | + Expect(actual.Spec.AvailabilityMode).To(Equal(expected.Spec.AvailabilityMode)) |
| 107 | + Expect(actual.Labels).To(HaveLen(len(expected.Labels))) |
| 108 | + for k, v := range expected.Labels { |
| 109 | + Expect(actual.Labels).To(HaveKeyWithValue(k, v)) |
| 110 | + } |
| 111 | + Expect(actual.Spec.Priority).To(Equal(expected.Spec.Priority)) |
| 112 | +} |
0 commit comments