|
6 | 6 | "net/http" |
7 | 7 | "testing" |
8 | 8 |
|
| 9 | + internal "github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/searchindex" |
| 10 | + |
9 | 11 | "github.com/stretchr/testify/mock" |
10 | 12 | "go.mongodb.org/atlas-sdk/v20231115008/admin" |
11 | 13 | "go.mongodb.org/atlas-sdk/v20231115008/mockadmin" |
@@ -128,6 +130,110 @@ func Test_SearchIndexesReconcile(t *testing.T) { |
128 | 130 | assert.False(t, result.IsOk()) |
129 | 131 | }) |
130 | 132 |
|
| 133 | + t.Run("Should cleanup indexes with empty IDs when IDLE", func(t *testing.T) { |
| 134 | + searchIndexConfig := &akov2.AtlasSearchIndexConfig{ |
| 135 | + TypeMeta: metav1.TypeMeta{}, |
| 136 | + ObjectMeta: metav1.ObjectMeta{ |
| 137 | + Name: "testConfig", |
| 138 | + Namespace: "testNamespace", |
| 139 | + }, |
| 140 | + Spec: akov2.AtlasSearchIndexConfigSpec{ |
| 141 | + Analyzer: pointer.MakePtr("testAnalyzer"), |
| 142 | + }, |
| 143 | + Status: status.AtlasSearchIndexConfigStatus{}, |
| 144 | + } |
| 145 | + IDForStatus := "123" |
| 146 | + deployment := &akov2.AtlasDeployment{ |
| 147 | + TypeMeta: metav1.TypeMeta{}, |
| 148 | + ObjectMeta: metav1.ObjectMeta{}, |
| 149 | + Spec: akov2.AtlasDeploymentSpec{ |
| 150 | + DeploymentSpec: &akov2.AdvancedDeploymentSpec{ |
| 151 | + Name: "testDeployment", |
| 152 | + SearchIndexes: []akov2.SearchIndex{ |
| 153 | + { |
| 154 | + Name: "Index1", |
| 155 | + Type: IndexTypeSearch, |
| 156 | + Search: &akov2.Search{ |
| 157 | + Synonyms: &([]akov2.Synonym{ |
| 158 | + { |
| 159 | + Name: "testSynonym", |
| 160 | + Analyzer: "testAnalyzer", |
| 161 | + Source: akov2.Source{ |
| 162 | + Collection: "testCollection", |
| 163 | + }, |
| 164 | + }, |
| 165 | + }), |
| 166 | + Mappings: nil, |
| 167 | + SearchConfigurationRef: common.ResourceRefNamespaced{ |
| 168 | + Name: searchIndexConfig.Name, |
| 169 | + Namespace: searchIndexConfig.Namespace, |
| 170 | + }, |
| 171 | + }, |
| 172 | + }, |
| 173 | + }, |
| 174 | + }, |
| 175 | + }, |
| 176 | + Status: status.AtlasDeploymentStatus{ |
| 177 | + SearchIndexes: []status.DeploymentSearchIndexStatus{ |
| 178 | + { |
| 179 | + Name: "Index1", |
| 180 | + ID: IDForStatus, |
| 181 | + Status: "", |
| 182 | + Message: "", |
| 183 | + }, |
| 184 | + { |
| 185 | + Name: "Index2", |
| 186 | + ID: "", |
| 187 | + Status: "", |
| 188 | + Message: "", |
| 189 | + }, |
| 190 | + }, |
| 191 | + }, |
| 192 | + } |
| 193 | + |
| 194 | + sch := runtime.NewScheme() |
| 195 | + assert.Nil(t, akov2.AddToScheme(sch)) |
| 196 | + assert.Nil(t, corev1.AddToScheme(sch)) |
| 197 | + |
| 198 | + atlasIdxToReturn, err := internal.NewSearchIndexFromAKO(&deployment.Spec.DeploymentSpec.SearchIndexes[0], |
| 199 | + &searchIndexConfig.Spec).ToAtlas() |
| 200 | + assert.NoError(t, err) |
| 201 | + mockSearchAPI := mockadmin.NewAtlasSearchApi(t) |
| 202 | + mockSearchAPI.EXPECT(). |
| 203 | + GetAtlasSearchIndex(context.Background(), mock.Anything, mock.Anything, mock.Anything). |
| 204 | + Return(admin.GetAtlasSearchIndexApiRequest{ApiService: mockSearchAPI}) |
| 205 | + mockSearchAPI.EXPECT(). |
| 206 | + GetAtlasSearchIndexExecute(admin.GetAtlasSearchIndexApiRequest{ApiService: mockSearchAPI}). |
| 207 | + Return( |
| 208 | + atlasIdxToReturn, |
| 209 | + &http.Response{StatusCode: http.StatusCreated}, nil, |
| 210 | + ) |
| 211 | + |
| 212 | + k8sClient := fake.NewClientBuilder(). |
| 213 | + WithScheme(sch). |
| 214 | + WithObjects(deployment, searchIndexConfig). |
| 215 | + Build() |
| 216 | + |
| 217 | + reconciler := searchIndexesReconciler{ |
| 218 | + ctx: &workflow.Context{ |
| 219 | + Log: zap.S(), |
| 220 | + OrgID: "testOrgID", |
| 221 | + Client: nil, |
| 222 | + SdkClient: &admin.APIClient{AtlasSearchApi: mockSearchAPI}, |
| 223 | + Context: context.Background(), |
| 224 | + }, |
| 225 | + deployment: deployment, |
| 226 | + k8sClient: k8sClient, |
| 227 | + projectID: "testProjectID", |
| 228 | + } |
| 229 | + |
| 230 | + result := reconciler.Reconcile() |
| 231 | + assert.True(t, result.IsOk()) |
| 232 | + deployment.UpdateStatus(reconciler.ctx.Conditions(), reconciler.ctx.StatusOptions()...) |
| 233 | + assert.Len(t, deployment.Status.SearchIndexes, 1) |
| 234 | + assert.True(t, deployment.Status.SearchIndexes[0].ID == IDForStatus) |
| 235 | + }) |
| 236 | + |
131 | 237 | t.Run("Should proceed with the index Type Search: CREATE INDEX", func(t *testing.T) { |
132 | 238 | mockSearchAPI := mockadmin.NewAtlasSearchApi(t) |
133 | 239 | mockSearchAPI.EXPECT(). |
|
0 commit comments