@@ -33,6 +33,7 @@ import (
3333
3434 "github.com/mongodb/mongodb-atlas-kubernetes/v2/api"
3535 akov2 "github.com/mongodb/mongodb-atlas-kubernetes/v2/api/v1"
36+ "github.com/mongodb/mongodb-atlas-kubernetes/v2/api/v1/status"
3637 "github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/controller/atlas"
3738 "github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/controller/reconciler"
3839 atlasmock "github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/mocks/atlas"
@@ -42,6 +43,16 @@ import (
4243 "github.com/mongodb/mongodb-atlas-kubernetes/v2/pkg/state"
4344)
4445
46+ const (
47+ fakeID = "fake-id"
48+
49+ fakeURL = "https://example.com/fake"
50+
51+ fakeSecret = "fake-secret"
52+
53+ matchingFakeHash = "5842a3ca8c9cdff1f658a278930fb49d2d5a8c493380f28e39bc7ebbbe21872c"
54+ )
55+
4556var sampleWebhookIntegration = akov2.AtlasThirdPartyIntegration {
4657 ObjectMeta : metav1.ObjectMeta {Name : "webhook-test" },
4758 Spec : akov2.AtlasThirdPartyIntegrationSpec {
@@ -53,11 +64,21 @@ var sampleWebhookIntegration = akov2.AtlasThirdPartyIntegration{
5364 },
5465 },
5566 },
67+ Status : status.AtlasThirdPartyIntegrationStatus {
68+ ID : fakeID ,
69+ },
5670}
5771
5872var sampleWebhookSecret = & corev1.Secret {
5973 ObjectMeta : metav1.ObjectMeta {
6074 Name : "webhook-secret" ,
75+ Annotations : map [string ]string {
76+ AnnotationContentHash : matchingFakeHash ,
77+ },
78+ },
79+ Data : map [string ][]byte {
80+ "url" : ([]byte )(fakeURL ),
81+ "secret" : ([]byte )(fakeSecret ),
6182 },
6283}
6384
@@ -68,6 +89,9 @@ func TestHandleUpsert(t *testing.T) {
6889 require .NoError (t , akov2 .AddToScheme (scheme ))
6990 ctx := context .Background ()
7091
92+ unannotatedSampleWebhookSecret := sampleWebhookSecret .DeepCopy ()
93+ unannotatedSampleWebhookSecret .Annotations = nil
94+
7195 for _ , tc := range []struct {
7296 name string
7397 state state.ResourceState
@@ -95,7 +119,7 @@ func TestHandleUpsert(t *testing.T) {
95119 integrationsService .EXPECT ().Create (mock .Anything , "testProjectID" , mock .Anything ).
96120 Return (& thirdpartyintegration.ThirdPartyIntegration {
97121 AtlasThirdPartyIntegrationSpec : sampleWebhookIntegration .Spec ,
98- ID : "fake-id" ,
122+ ID : fakeID ,
99123 }, nil )
100124 return integrationsService
101125 },
@@ -122,17 +146,17 @@ func TestHandleUpsert(t *testing.T) {
122146 integrationsService .EXPECT ().Get (mock .Anything , "testProjectID" , "WEBHOOK" ).
123147 Return (& thirdpartyintegration.ThirdPartyIntegration {
124148 AtlasThirdPartyIntegrationSpec : sampleWebhookIntegration .Spec ,
125- ID : "fake-id" ,
149+ ID : fakeID ,
126150 }, nil )
127151 integrationsService .EXPECT ().Update (mock .Anything , "testProjectID" , mock .Anything ).
128152 Return (& thirdpartyintegration.ThirdPartyIntegration {
129153 AtlasThirdPartyIntegrationSpec : sampleWebhookIntegration .Spec ,
130- ID : "fake-id" ,
154+ ID : fakeID ,
131155 }, nil )
132156 return integrationsService
133157 },
134158 input : & sampleWebhookIntegration ,
135- objects : []client.Object {sampleWebhookSecret },
159+ objects : []client.Object {unannotatedSampleWebhookSecret },
136160 want : ctrlstate.Result {
137161 NextState : "Updated" ,
138162 StateMsg : "Updated Atlas Third Party Integration for WEBHOOK." ,
@@ -171,7 +195,7 @@ func TestHandleUpsert(t *testing.T) {
171195 integrationsService .EXPECT ().Create (mock .Anything , "testProjectID" , mock .Anything ).
172196 Return (& thirdpartyintegration.ThirdPartyIntegration {
173197 AtlasThirdPartyIntegrationSpec : sampleWebhookIntegration .Spec ,
174- ID : "fake-id" ,
198+ ID : fakeID ,
175199 }, nil )
176200 return integrationsService
177201 },
@@ -191,19 +215,19 @@ func TestHandleUpsert(t *testing.T) {
191215 },
192216
193217 {
194- name : "updated updates" ,
218+ name : "updated updates due unhashed secret " ,
195219 state : state .StateUpdated ,
196220 serviceBuilder : func (_ * atlas.ClientSet ) thirdpartyintegration.ThirdPartyIntegrationService {
197221 integrationsService := mocks .NewThirdPartyIntegrationServiceMock (t )
198222 integrationsService .EXPECT ().Get (mock .Anything , "testProjectID" , "WEBHOOK" ).
199223 Return (& thirdpartyintegration.ThirdPartyIntegration {
200224 AtlasThirdPartyIntegrationSpec : sampleWebhookIntegration .Spec ,
201- ID : "fake-id" ,
225+ ID : fakeID ,
202226 }, nil )
203227 integrationsService .EXPECT ().Update (mock .Anything , "testProjectID" , mock .Anything ).
204228 Return (& thirdpartyintegration.ThirdPartyIntegration {
205229 AtlasThirdPartyIntegrationSpec : sampleWebhookIntegration .Spec ,
206- ID : "fake-id" ,
230+ ID : fakeID ,
207231 }, nil )
208232 return integrationsService
209233 },
@@ -215,12 +239,146 @@ func TestHandleUpsert(t *testing.T) {
215239 },
216240 },
217241 input : & sampleWebhookIntegration ,
218- objects : []client.Object {sampleWebhookSecret },
242+ objects : []client.Object {unannotatedSampleWebhookSecret },
219243 want : ctrlstate.Result {
220244 NextState : "Updated" ,
221245 StateMsg : "Updated Atlas Third Party Integration for WEBHOOK." ,
222246 },
223247 },
248+
249+ {
250+ name : "imported does not update" ,
251+ state : state .StateInitial ,
252+ serviceBuilder : func (_ * atlas.ClientSet ) thirdpartyintegration.ThirdPartyIntegrationService {
253+ integrationsService := mocks .NewThirdPartyIntegrationServiceMock (t )
254+ integrationsService .EXPECT ().Get (mock .Anything , "testProjectID" , "WEBHOOK" ).
255+ Return (& thirdpartyintegration.ThirdPartyIntegration {
256+ AtlasThirdPartyIntegrationSpec : sampleWebhookIntegration .Spec ,
257+ ID : fakeID ,
258+ WebhookSecrets : & thirdpartyintegration.WebhookSecrets {
259+ URL : fakeURL ,
260+ Secret : fakeSecret ,
261+ },
262+ }, nil )
263+ return integrationsService
264+ },
265+ provider : & atlasmock.TestProvider {
266+ SdkClientSetFunc : func (ctx context.Context , creds * atlas.Credentials , log * zap.SugaredLogger ) (* atlas.ClientSet , error ) {
267+ return & atlas.ClientSet {
268+ SdkClient20231115008 : & admin20231115008.APIClient {ProjectsApi : mockFindFakeParentProject (t )},
269+ }, nil
270+ },
271+ },
272+ input : & sampleWebhookIntegration ,
273+ objects : []client.Object {sampleWebhookSecret },
274+ want : ctrlstate.Result {
275+ NextState : "Created" ,
276+ StateMsg : "Synced WEBHOOK Atlas Third Party Integration for testProjectID." ,
277+ },
278+ },
279+
280+ {
281+ name : "id diff does not update" ,
282+ state : state .StateUpdated ,
283+ serviceBuilder : func (_ * atlas.ClientSet ) thirdpartyintegration.ThirdPartyIntegrationService {
284+ integrationsService := mocks .NewThirdPartyIntegrationServiceMock (t )
285+ integrationsService .EXPECT ().Get (mock .Anything , "testProjectID" , "WEBHOOK" ).
286+ Return (& thirdpartyintegration.ThirdPartyIntegration {
287+ AtlasThirdPartyIntegrationSpec : sampleWebhookIntegration .Spec ,
288+ ID : "other-fake-id" ,
289+ WebhookSecrets : & thirdpartyintegration.WebhookSecrets {
290+ URL : fakeURL ,
291+ Secret : fakeSecret ,
292+ },
293+ }, nil )
294+ return integrationsService
295+ },
296+ provider : & atlasmock.TestProvider {
297+ SdkClientSetFunc : func (ctx context.Context , creds * atlas.Credentials , log * zap.SugaredLogger ) (* atlas.ClientSet , error ) {
298+ return & atlas.ClientSet {
299+ SdkClient20231115008 : & admin20231115008.APIClient {ProjectsApi : mockFindFakeParentProject (t )},
300+ }, nil
301+ },
302+ },
303+ input : & sampleWebhookIntegration ,
304+ objects : []client.Object {sampleWebhookSecret },
305+ want : ctrlstate.Result {
306+ NextState : "Updated" ,
307+ StateMsg : "Synced WEBHOOK Atlas Third Party Integration for testProjectID." ,
308+ },
309+ },
310+
311+ {
312+ name : "different integration updates" ,
313+ state : state .StateUpdated ,
314+ serviceBuilder : func (_ * atlas.ClientSet ) thirdpartyintegration.ThirdPartyIntegrationService {
315+ integrationsService := mocks .NewThirdPartyIntegrationServiceMock (t )
316+ integrationsService .EXPECT ().Get (mock .Anything , "testProjectID" , "SLACK" ).
317+ Return (& thirdpartyintegration.ThirdPartyIntegration {
318+ AtlasThirdPartyIntegrationSpec : akov2.AtlasThirdPartyIntegrationSpec {
319+ ProjectDualReference : referenceFakeProject ,
320+ Type : "SLACK" ,
321+ Slack : & akov2.SlackIntegration {
322+ ChannelName : "other-channel" ,
323+ TeamName : "other-team" ,
324+ },
325+ },
326+ ID : fakeID ,
327+ SlackSecrets : & thirdpartyintegration.SlackSecrets {
328+ APIToken : "fake-token" ,
329+ },
330+ }, nil )
331+ integrationsService .EXPECT ().Update (mock .Anything , "testProjectID" , mock .Anything ).
332+ Return (& thirdpartyintegration.ThirdPartyIntegration {
333+ AtlasThirdPartyIntegrationSpec : akov2.AtlasThirdPartyIntegrationSpec {
334+ ProjectDualReference : referenceFakeProject ,
335+ Type : "SLACK" ,
336+ Slack : & akov2.SlackIntegration {
337+ ChannelName : "spec-channel" ,
338+ TeamName : "spec-team" ,
339+ },
340+ },
341+ ID : fakeID ,
342+ }, nil )
343+ return integrationsService
344+ },
345+ provider : & atlasmock.TestProvider {
346+ SdkClientSetFunc : func (ctx context.Context , creds * atlas.Credentials , log * zap.SugaredLogger ) (* atlas.ClientSet , error ) {
347+ return & atlas.ClientSet {
348+ SdkClient20231115008 : & admin20231115008.APIClient {ProjectsApi : mockFindFakeParentProject (t )},
349+ }, nil
350+ },
351+ },
352+ input : & akov2.AtlasThirdPartyIntegration {
353+ ObjectMeta : metav1.ObjectMeta {Name : "slack-integration" },
354+ Spec : akov2.AtlasThirdPartyIntegrationSpec {
355+ ProjectDualReference : referenceFakeProject ,
356+ Type : "SLACK" ,
357+ Slack : & akov2.SlackIntegration {
358+ APITokenSecretRef : api.LocalObjectReference {
359+ Name : "slack-secret" ,
360+ },
361+ ChannelName : "spec-channel" ,
362+ TeamName : "spec-team" ,
363+ },
364+ },
365+ },
366+ objects : []client.Object {& corev1.Secret {
367+ ObjectMeta : metav1.ObjectMeta {
368+ Name : "slack-secret" ,
369+ Annotations : map [string ]string {
370+ AnnotationContentHash : "b651a9a05aa41a0555c56f3757d1ab44ae08a7cde4d8fe4477f729e3961cebc6" ,
371+ },
372+ },
373+ Data : map [string ][]byte {
374+ "apiToken" : ([]byte )("fake-token" ),
375+ },
376+ }},
377+ want : ctrlstate.Result {
378+ NextState : "Updated" ,
379+ StateMsg : "Updated Atlas Third Party Integration for SLACK." ,
380+ },
381+ },
224382 } {
225383 t .Run (tc .name , func (t * testing.T ) {
226384 k8sClient := fake .NewClientBuilder ().
0 commit comments