@@ -40,6 +40,13 @@ func TestServiceDiscoveryClient_ListServices_HappyCase(t *testing.T) {
40
40
model .PortAttr : test .EndptPortStr1 ,
41
41
},
42
42
},
43
+ {
44
+ Id : aws .String (test .EndptId2 ),
45
+ Attributes : map [string ]string {
46
+ model .Ipv4Attr : test .EndptIp2 ,
47
+ model .PortAttr : test .EndptPortStr2 ,
48
+ },
49
+ },
43
50
}, nil )
44
51
45
52
sdc := getTestSdClient (t , sdApi )
@@ -52,7 +59,7 @@ func TestServiceDiscoveryClient_ListServices_HappyCase(t *testing.T) {
52
59
cachedSvc , _ := sdc .serviceIdCache .Get (fmt .Sprintf ("%s/%s" , test .NsName , test .SvcName ))
53
60
assert .Equal (t , test .SvcId , cachedSvc , "Happy case caches service ID" )
54
61
cachedEndpts , _ := sdc .endpointCache .Get (test .SvcId )
55
- assert .Equal (t , []* model.Endpoint {test .GetTestEndpoint ()}, cachedEndpts , "Happy case caches endpoints" )
62
+ assert .Equal (t , []* model.Endpoint {test .GetTestEndpoint (), test . GetTestEndpoint2 () }, cachedEndpts , "Happy case caches endpoints" )
56
63
}
57
64
58
65
func TestServiceDiscoveryClient_ListServices_HappyCaseCachedResults (t * testing.T ) {
@@ -65,7 +72,7 @@ func TestServiceDiscoveryClient_ListServices_HappyCaseCachedResults(t *testing.T
65
72
66
73
sdc := getTestSdClient (t , sdApi )
67
74
sdc .namespaceCache .Add (test .NsName , * test .GetTestHttpNamespace (), time .Minute )
68
- sdc .endpointCache .Add (test .SvcId , []* model.Endpoint {test .GetTestEndpoint ()}, time .Minute )
75
+ sdc .endpointCache .Add (test .SvcId , []* model.Endpoint {test .GetTestEndpoint (), test . GetTestEndpoint2 () }, time .Minute )
69
76
70
77
svcs , err := sdc .ListServices (context .TODO (), test .NsName )
71
78
assert .Equal (t , []* model.Service {test .GetTestService ()}, svcs )
@@ -284,23 +291,61 @@ func TestServiceDiscoveryClient_CreateService_CreatesNamespace_CreateNsError(t *
284
291
assert .Equal (t , nsErr , err )
285
292
}
286
293
287
- func TestServiceDiscoveryClient_GetService (t * testing.T ) {
288
- // TODO: Add unit tests
294
+ func TestServiceDiscoveryClient_GetService_HappyCase (t * testing.T ) {
295
+ mockController := gomock .NewController (t )
296
+ defer mockController .Finish ()
297
+
298
+ sdApi := cloudmap .NewMockServiceDiscoveryApi (mockController )
299
+ sdApi .EXPECT ().ListNamespaces (context .TODO ()).Return ([]* model.Namespace {{Id : test .NsId , Name : test .NsName }}, nil )
300
+ sdApi .EXPECT ().ListServices (context .TODO (), test .NsId ).
301
+ Return ([]* model.Resource {{Id : test .SvcId , Name : test .SvcName }}, nil )
302
+ sdApi .EXPECT ().ListInstances (context .TODO (), test .SvcId ).
303
+ Return ([]types.InstanceSummary {
304
+ {
305
+ Id : aws .String (test .EndptId1 ),
306
+ Attributes : map [string ]string {
307
+ model .Ipv4Attr : test .EndptIp1 ,
308
+ model .PortAttr : test .EndptPortStr1 ,
309
+ },
310
+ },
311
+ {
312
+ Id : aws .String (test .EndptId2 ),
313
+ Attributes : map [string ]string {
314
+ model .Ipv4Attr : test .EndptIp2 ,
315
+ model .PortAttr : test .EndptPortStr2 ,
316
+ },
317
+ },
318
+ }, nil )
319
+ sdc := getTestSdClient (t , sdApi )
320
+
321
+ svc , err := sdc .GetService (context .TODO (), test .NsName , test .SvcName )
322
+ assert .Nil (t , err )
323
+ assert .Equal (t , test .GetTestService (), svc )
289
324
}
290
325
291
326
func TestServiceDiscoveryClient_GetService_CachedValues (t * testing.T ) {
292
- // TODO: Add unit tests
327
+ mockController := gomock .NewController (t )
328
+ defer mockController .Finish ()
329
+
330
+ sdApi := cloudmap .NewMockServiceDiscoveryApi (mockController )
331
+ sdc := getTestSdClient (t , sdApi )
332
+ sdc .namespaceCache .Add (test .NsName , * test .GetTestHttpNamespace (), time .Minute )
333
+ sdc .serviceIdCache .Add (fmt .Sprintf ("%s/%s" , test .NsName , test .SvcName ), test .SvcId , time .Minute )
334
+ sdc .endpointCache .Add (test .SvcId , []* model.Endpoint {test .GetTestEndpoint (), test .GetTestEndpoint2 ()}, time .Minute )
335
+
336
+ svc , err := sdc .GetService (context .TODO (), test .NsName , test .SvcName )
337
+ assert .Nil (t , err )
338
+ assert .Equal (t , test .GetTestService (), svc )
293
339
}
294
340
295
341
func TestServiceDiscoveryClient_RegisterEndpoints (t * testing.T ) {
296
342
mockController := gomock .NewController (t )
297
343
defer mockController .Finish ()
298
344
299
345
sdApi := cloudmap .NewMockServiceDiscoveryApi (mockController )
300
-
301
346
sdc := getTestSdClient (t , sdApi )
302
- sdc .namespaceCache .Add (test .NsName , test .NsId , time .Minute )
303
347
sdc .serviceIdCache .Add (fmt .Sprintf ("%s/%s" , test .NsName , test .SvcName ), test .SvcId , time .Minute )
348
+ sdc .endpointCache .Add (test .SvcId , model.Endpoint {}, time .Minute )
304
349
305
350
attrs1 := map [string ]string {"AWS_INSTANCE_IPV4" : test .EndptIp1 , "AWS_INSTANCE_PORT" : test .EndptPortStr1 }
306
351
attrs2 := map [string ]string {"AWS_INSTANCE_IPV4" : test .EndptIp2 , "AWS_INSTANCE_PORT" : test .EndptPortStr2 }
@@ -329,6 +374,8 @@ func TestServiceDiscoveryClient_RegisterEndpoints(t *testing.T) {
329
374
})
330
375
331
376
assert .Nil (t , err )
377
+ _ , entryCached := sdc .endpointCache .Get (test .SvcId )
378
+ assert .False (t , entryCached , "Cache entry evicted after register" )
332
379
}
333
380
334
381
func TestServiceDiscoveryClient_DeleteEndpoints (t * testing.T ) {
@@ -338,8 +385,8 @@ func TestServiceDiscoveryClient_DeleteEndpoints(t *testing.T) {
338
385
sdApi := cloudmap .NewMockServiceDiscoveryApi (mockController )
339
386
340
387
sdc := getTestSdClient (t , sdApi )
341
- sdc .namespaceCache .Add (test .NsName , test .NsId , time .Minute )
342
388
sdc .serviceIdCache .Add (fmt .Sprintf ("%s/%s" , test .NsName , test .SvcName ), test .SvcId , time .Minute )
389
+ sdc .endpointCache .Add (test .SvcId , model.Endpoint {}, time .Minute )
343
390
344
391
sdApi .EXPECT ().DeregisterInstance (context .TODO (), test .SvcId , test .EndptId1 ).Return (test .OpId1 , nil )
345
392
sdApi .EXPECT ().DeregisterInstance (context .TODO (), test .SvcId , test .EndptId2 ).Return (test .OpId2 , nil )
@@ -352,6 +399,8 @@ func TestServiceDiscoveryClient_DeleteEndpoints(t *testing.T) {
352
399
[]* model.Endpoint {{Id : test .EndptId1 }, {Id : test .EndptId2 }})
353
400
354
401
assert .Nil (t , err )
402
+ _ , entryCached := sdc .endpointCache .Get (test .SvcId )
403
+ assert .False (t , entryCached , "Cache entry evicted after de-register" )
355
404
}
356
405
357
406
func TestServiceDiscoveryClient_getNamespace_HappyCase (t * testing.T ) {
0 commit comments