|
| 1 | +package controllers |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | + ocv1 "github.com/operator-framework/operator-controller/api/v1" |
| 11 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 12 | + "k8s.io/apimachinery/pkg/runtime" |
| 13 | + "k8s.io/apimachinery/pkg/types" |
| 14 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 15 | + "sigs.k8s.io/controller-runtime/pkg/client/fake" |
| 16 | + "sigs.k8s.io/controller-runtime/pkg/reconcile" |
| 17 | +) |
| 18 | + |
| 19 | +type mockCatalogService struct { |
| 20 | + updateCatalogCalled bool |
| 21 | + removeCatalogCalled bool |
| 22 | + updateError error |
| 23 | + removeError error |
| 24 | + lastCatalogName string |
| 25 | + lastBaseURL string |
| 26 | +} |
| 27 | + |
| 28 | +func (m *mockCatalogService) UpdateCatalog(catalogName string, baseURL string) error { |
| 29 | + m.updateCatalogCalled = true |
| 30 | + m.lastCatalogName = catalogName |
| 31 | + m.lastBaseURL = baseURL |
| 32 | + return m.updateError |
| 33 | +} |
| 34 | + |
| 35 | +func (m *mockCatalogService) RemoveCatalog(catalogName string) error { |
| 36 | + m.removeCatalogCalled = true |
| 37 | + m.lastCatalogName = catalogName |
| 38 | + return m.removeError |
| 39 | +} |
| 40 | + |
| 41 | +func createTestReconciler(objects ...client.Object) (*ClusterCatalogReconciler, *mockCatalogService) { |
| 42 | + scheme := runtime.NewScheme() |
| 43 | + _ = ocv1.AddToScheme(scheme) |
| 44 | + |
| 45 | + fakeClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(objects...).Build() |
| 46 | + mockService := &mockCatalogService{} |
| 47 | + |
| 48 | + return &ClusterCatalogReconciler{ |
| 49 | + Client: fakeClient, |
| 50 | + Scheme: scheme, |
| 51 | + catalogService: mockService, |
| 52 | + }, mockService |
| 53 | +} |
| 54 | + |
| 55 | +func TestReconcile_ClusterCatalogNotFound(t *testing.T) { |
| 56 | + reconciler, mockService := createTestReconciler() |
| 57 | + |
| 58 | + req := reconcile.Request{ |
| 59 | + NamespacedName: types.NamespacedName{ |
| 60 | + Name: "test-catalog", |
| 61 | + }, |
| 62 | + } |
| 63 | + |
| 64 | + result, err := reconciler.Reconcile(context.Background(), req) |
| 65 | + |
| 66 | + require.NoError(t, err) |
| 67 | + assert.Equal(t, reconcile.Result{}, result) |
| 68 | + assert.True(t, mockService.removeCatalogCalled) |
| 69 | + assert.Equal(t, "test-catalog", mockService.lastCatalogName) |
| 70 | +} |
| 71 | + |
| 72 | +func TestReconcile_ClusterCatalogNoURLs(t *testing.T) { |
| 73 | + clusterCatalog := &ocv1.ClusterCatalog{ |
| 74 | + ObjectMeta: metav1.ObjectMeta{ |
| 75 | + Name: "test-catalog", |
| 76 | + }, |
| 77 | + Status: ocv1.ClusterCatalogStatus{ |
| 78 | + URLs: nil, |
| 79 | + }, |
| 80 | + } |
| 81 | + |
| 82 | + reconciler, mockService := createTestReconciler(clusterCatalog) |
| 83 | + |
| 84 | + req := reconcile.Request{ |
| 85 | + NamespacedName: types.NamespacedName{ |
| 86 | + Name: "test-catalog", |
| 87 | + }, |
| 88 | + } |
| 89 | + |
| 90 | + result, err := reconciler.Reconcile(context.Background(), req) |
| 91 | + |
| 92 | + require.Error(t, err) |
| 93 | + assert.Equal(t, "URLs field is empty for clustercatalog test-catalog", err.Error()) |
| 94 | + assert.Equal(t, reconcile.Result{}, result) |
| 95 | + assert.False(t, mockService.updateCatalogCalled) |
| 96 | + assert.False(t, mockService.removeCatalogCalled) |
| 97 | +} |
| 98 | + |
| 99 | +func TestReconcile_ClusterCatalogEmptyBaseURL(t *testing.T) { |
| 100 | + clusterCatalog := &ocv1.ClusterCatalog{ |
| 101 | + ObjectMeta: metav1.ObjectMeta{ |
| 102 | + Name: "test-catalog", |
| 103 | + }, |
| 104 | + Status: ocv1.ClusterCatalogStatus{ |
| 105 | + URLs: &ocv1.ClusterCatalogURLs{ |
| 106 | + Base: "", |
| 107 | + }, |
| 108 | + }, |
| 109 | + } |
| 110 | + |
| 111 | + reconciler, mockService := createTestReconciler(clusterCatalog) |
| 112 | + |
| 113 | + req := reconcile.Request{ |
| 114 | + NamespacedName: types.NamespacedName{ |
| 115 | + Name: "test-catalog", |
| 116 | + }, |
| 117 | + } |
| 118 | + |
| 119 | + result, err := reconciler.Reconcile(context.Background(), req) |
| 120 | + |
| 121 | + require.Error(t, err) |
| 122 | + assert.Equal(t, "base URL is empty for clustercatalog test-catalog", err.Error()) |
| 123 | + assert.Equal(t, reconcile.Result{}, result) |
| 124 | + assert.False(t, mockService.updateCatalogCalled) |
| 125 | + assert.False(t, mockService.removeCatalogCalled) |
| 126 | +} |
| 127 | + |
| 128 | +func TestReconcile_ClusterCatalogSuccess(t *testing.T) { |
| 129 | + clusterCatalog := &ocv1.ClusterCatalog{ |
| 130 | + ObjectMeta: metav1.ObjectMeta{ |
| 131 | + Name: "test-catalog", |
| 132 | + }, |
| 133 | + Status: ocv1.ClusterCatalogStatus{ |
| 134 | + URLs: &ocv1.ClusterCatalogURLs{ |
| 135 | + Base: "https://example.com/catalog", |
| 136 | + }, |
| 137 | + }, |
| 138 | + } |
| 139 | + |
| 140 | + reconciler, mockService := createTestReconciler(clusterCatalog) |
| 141 | + |
| 142 | + req := reconcile.Request{ |
| 143 | + NamespacedName: types.NamespacedName{ |
| 144 | + Name: "test-catalog", |
| 145 | + }, |
| 146 | + } |
| 147 | + |
| 148 | + result, err := reconciler.Reconcile(context.Background(), req) |
| 149 | + |
| 150 | + require.NoError(t, err) |
| 151 | + assert.Equal(t, reconcile.Result{}, result) |
| 152 | + assert.True(t, mockService.updateCatalogCalled) |
| 153 | + assert.Equal(t, "test-catalog", mockService.lastCatalogName) |
| 154 | + assert.Equal(t, "https://example.com/catalog", mockService.lastBaseURL) |
| 155 | +} |
| 156 | + |
| 157 | +func TestReconcile_UpdateCatalogError(t *testing.T) { |
| 158 | + clusterCatalog := &ocv1.ClusterCatalog{ |
| 159 | + ObjectMeta: metav1.ObjectMeta{ |
| 160 | + Name: "test-catalog", |
| 161 | + }, |
| 162 | + Status: ocv1.ClusterCatalogStatus{ |
| 163 | + URLs: &ocv1.ClusterCatalogURLs{ |
| 164 | + Base: "https://example.com/catalog", |
| 165 | + }, |
| 166 | + }, |
| 167 | + } |
| 168 | + |
| 169 | + reconciler, mockService := createTestReconciler(clusterCatalog) |
| 170 | + mockService.updateError = errors.New("update failed") |
| 171 | + |
| 172 | + req := reconcile.Request{ |
| 173 | + NamespacedName: types.NamespacedName{ |
| 174 | + Name: "test-catalog", |
| 175 | + }, |
| 176 | + } |
| 177 | + |
| 178 | + result, err := reconciler.Reconcile(context.Background(), req) |
| 179 | + |
| 180 | + require.Error(t, err) |
| 181 | + assert.Equal(t, reconcile.Result{}, result) |
| 182 | + assert.True(t, mockService.updateCatalogCalled) |
| 183 | +} |
| 184 | + |
| 185 | +func TestReconcile_RemoveCatalogError(t *testing.T) { |
| 186 | + reconciler, mockService := createTestReconciler() |
| 187 | + mockService.removeError = errors.New("remove failed") |
| 188 | + |
| 189 | + req := reconcile.Request{ |
| 190 | + NamespacedName: types.NamespacedName{ |
| 191 | + Name: "test-catalog", |
| 192 | + }, |
| 193 | + } |
| 194 | + |
| 195 | + result, err := reconciler.Reconcile(context.Background(), req) |
| 196 | + |
| 197 | + require.Error(t, err) |
| 198 | + assert.Equal(t, reconcile.Result{}, result) |
| 199 | + assert.True(t, mockService.removeCatalogCalled) |
| 200 | +} |
0 commit comments