55 "fmt"
66 "testing"
77
8+ "github.com/google/go-cmp/cmp"
9+ "github.com/google/go-cmp/cmp/cmpopts"
810 "github.com/stretchr/testify/assert"
911 "github.com/stretchr/testify/mock"
1012 "github.com/stretchr/testify/require"
@@ -13,6 +15,7 @@ import (
1315 "go.uber.org/zap/zaptest"
1416 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1517
18+ "github.com/mongodb/mongodb-atlas-kubernetes/v2/api"
1619 akov2 "github.com/mongodb/mongodb-atlas-kubernetes/v2/api/v1"
1720 "github.com/mongodb/mongodb-atlas-kubernetes/v2/api/v1/provider"
1821 "github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/controller/atlas"
@@ -215,41 +218,81 @@ func TestPrivateEndpointsNonGreedyBehaviour(t *testing.T) {
215218 atlasPEids []string
216219 wantRemoved []string
217220 wantResult workflow.Result
221+ conditions []api.Condition
218222 }{
219223 {
220- title : "no last applied no removal in Atlas " ,
224+ title : "empty last applied, empty spec, two entries in atlas: shouldn't remove entries " ,
221225 lastAppliedPEids : []string {},
222226 specPEids : []string {},
223227 atlasPEids : []string {"pe1" , "pe2" },
224228 wantRemoved : []string {},
225229 wantResult : workflow .OK (),
230+ conditions : []api.Condition {},
226231 },
227232 {
228- title : "removed from last applied removes from Atlas" ,
233+ title : "empty last applied, a entry in spec, two entries in atlas: shouldn't remove missing entry" ,
234+ lastAppliedPEids : []string {},
235+ specPEids : []string {"pe1" },
236+ atlasPEids : []string {"pe1" , "pe2" },
237+ wantRemoved : []string {},
238+ wantResult : workflow .OK ().WithMessage ("Interface Private Endpoint awaits configuration" ),
239+ conditions : []api.Condition {
240+ api .TrueCondition (api .PrivateEndpointServiceReadyType ).
241+ WithMessageRegexp ("Interface Private Endpoint awaits configuration" ),
242+ },
243+ },
244+ {
245+ title : "empty last applied, one entry in spec, empty atlas: no remove action" ,
246+ lastAppliedPEids : []string {},
247+ specPEids : []string {"pe1" },
248+ atlasPEids : []string {},
249+ wantRemoved : []string {},
250+ wantResult : notReadyServiceResult ,
251+ conditions : []api.Condition {
252+ api .FalseCondition (api .PrivateEndpointServiceReadyType ).
253+ WithReason (string (workflow .ProjectPEServiceIsNotReadyInAtlas )).
254+ WithMessageRegexp ("Private Endpoint Service is not ready" ),
255+ },
256+ },
257+ {
258+ title : "two entries in last applied, one entry in spec, two entries in atlas: should remove one entry" ,
229259 lastAppliedPEids : []string {"pe1" , "pe2" },
230260 specPEids : []string {"pe1" },
231261 atlasPEids : []string {"pe1" , "pe2" },
232262 wantRemoved : []string {"pe2" },
233263 wantResult : workflow .InProgress (
234264 workflow .ProjectPEServiceIsNotReadyInAtlas , "Private Endpoint is deleting" ),
265+ conditions : []api.Condition {
266+ api .FalseCondition (api .PrivateEndpointServiceReadyType ).
267+ WithReason (string (workflow .ProjectPEServiceIsNotReadyInAtlas )).
268+ WithMessageRegexp ("Private Endpoint is deleting" ),
269+ },
235270 },
236271 {
237- title : "removed all from last applied removes all from Atlas " ,
272+ title : "two entries in last applied, empty spec, two entries in atlas: should remove entries " ,
238273 lastAppliedPEids : []string {"pe1" , "pe2" },
239274 specPEids : []string {},
240275 atlasPEids : []string {"pe1" , "pe2" },
241276 wantRemoved : []string {"pe1" , "pe2" },
242277 wantResult : workflow .InProgress (
243278 workflow .ProjectPEServiceIsNotReadyInAtlas , "Private Endpoint is deleting" ),
279+ conditions : []api.Condition {
280+ api .FalseCondition (api .PrivateEndpointServiceReadyType ).
281+ WithReason (string (workflow .ProjectPEServiceIsNotReadyInAtlas )).
282+ WithMessageRegexp ("Private Endpoint is deleting" ),
283+ },
244284 },
245285 {
246- title : "not in last applied not removed from Atlas " ,
286+ title : "an entry in last applied, an entry in spec, two entries in atlas: shouldn't remove an entry " ,
247287 lastAppliedPEids : []string {"pe1" },
248288 specPEids : []string {"pe1" },
249289 atlasPEids : []string {"pe1" , "pe2" },
250290 wantRemoved : []string {},
251- wantResult : workflow .InProgress (
252- workflow .ProjectPrivateEndpointIsNotReadyInAtlas , "Interface Private Endpoint is not ready" ),
291+ wantResult : workflow .OK ().WithMessage ("Interface Private Endpoint awaits configuration" ),
292+ conditions : []api.Condition {
293+ api .TrueCondition (api .PrivateEndpointServiceReadyType ).
294+ WithMessageRegexp ("Interface Private Endpoint awaits configuration" ),
295+ },
253296 },
254297 } {
255298 t .Run (tc .title , func (t * testing.T ) {
@@ -279,20 +322,18 @@ func TestPrivateEndpointsNonGreedyBehaviour(t *testing.T) {
279322
280323 removals := len (tc .wantRemoved )
281324 if removals > 0 {
282- privateEndpointsAPI .EXPECT ().DeletePrivateEndpointServiceWithParams (
283- mock .Anything , mock .Anything ,
284- ).Return (admin.DeletePrivateEndpointServiceApiRequest {ApiService : privateEndpointsAPI }).Times (removals )
325+ privateEndpointsAPI .EXPECT ().DeletePrivateEndpointServiceWithParams (mock .Anything , mock .Anything ).
326+ Return (admin.DeletePrivateEndpointServiceApiRequest {ApiService : privateEndpointsAPI }).Times (removals )
285327 privateEndpointsAPI .EXPECT ().DeletePrivateEndpointServiceExecute (
286328 mock .AnythingOfType ("admin.DeletePrivateEndpointServiceApiRequest" )).Return (
287329 nil , nil , nil ,
288330 ).Times (removals )
289331 }
290- privateEndpointsAPI .EXPECT ().CreatePrivateEndpointWithParams (
291- mock .Anything , mock .Anything ,
292- ).Return (admin.CreatePrivateEndpointApiRequest {ApiService : privateEndpointsAPI }).Maybe ()
293- privateEndpointsAPI .EXPECT ().CreatePrivateEndpointExecute (
294- mock .AnythingOfType ("admin.CreatePrivateEndpointApiRequest" )).Return (
295- nil , nil , nil ,
332+ privateEndpointsAPI .EXPECT ().CreatePrivateEndpointService (mock .Anything , mock .Anything , mock .Anything ).
333+ Return (admin.CreatePrivateEndpointServiceApiRequest {ApiService : privateEndpointsAPI }).Maybe ()
334+ privateEndpointsAPI .EXPECT ().CreatePrivateEndpointServiceExecute (
335+ mock .AnythingOfType ("admin.CreatePrivateEndpointServiceApiRequest" )).Return (
336+ & admin.EndpointService {CloudProvider : "AWS" , RegionName : pointer .MakePtr ("fake-region-pe1" )}, nil , nil ,
296337 ).Maybe ()
297338
298339 workflowCtx := workflow.Context {
@@ -307,6 +348,19 @@ func TestPrivateEndpointsNonGreedyBehaviour(t *testing.T) {
307348
308349 result := ensurePrivateEndpoint (& workflowCtx , prj )
309350 require .Equal (t , tc .wantResult , result )
351+ t .Log (cmp .Diff (
352+ tc .conditions ,
353+ workflowCtx .Conditions (),
354+ cmpopts .IgnoreFields (api.Condition {}, "LastTransitionTime" ),
355+ ))
356+ assert .True (
357+ t ,
358+ cmp .Equal (
359+ tc .conditions ,
360+ workflowCtx .Conditions (),
361+ cmpopts .IgnoreFields (api.Condition {}, "LastTransitionTime" ),
362+ ),
363+ )
310364 })
311365 }
312366}
@@ -328,7 +382,6 @@ func synthesizePEs(peIDs []string) []akov2.PrivateEndpoint {
328382 for _ , id := range peIDs {
329383 pes = append (pes , akov2.PrivateEndpoint {
330384 Provider : "AWS" ,
331- ID : id ,
332385 Region : fmt .Sprintf ("fake-region-%s" , id ),
333386 })
334387 }
@@ -340,7 +393,7 @@ func synthesizeAtlasPEs(peIDs []string) []admin.EndpointService {
340393 for _ , id := range peIDs {
341394 atlasPEs = append (atlasPEs , admin.EndpointService {
342395 CloudProvider : "AWS" ,
343- Id : pointer .MakePtr (id ),
396+ Id : pointer .MakePtr (id + "-id" ),
344397 RegionName : pointer .MakePtr (fmt .Sprintf ("fake-region-%s" , id )),
345398 Status : pointer .MakePtr ("AVAILABLE" ),
346399 })
0 commit comments