@@ -40,6 +40,13 @@ func TestServiceDiscoveryClient_ListServices_HappyCase(t *testing.T) {
4040 model .PortAttr : test .EndptPortStr1 ,
4141 },
4242 },
43+ {
44+ Id : aws .String (test .EndptId2 ),
45+ Attributes : map [string ]string {
46+ model .Ipv4Attr : test .EndptIp2 ,
47+ model .PortAttr : test .EndptPortStr2 ,
48+ },
49+ },
4350 }, nil )
4451
4552 sdc := getTestSdClient (t , sdApi )
@@ -52,7 +59,7 @@ func TestServiceDiscoveryClient_ListServices_HappyCase(t *testing.T) {
5259 cachedSvc , _ := sdc .serviceIdCache .Get (fmt .Sprintf ("%s/%s" , test .NsName , test .SvcName ))
5360 assert .Equal (t , test .SvcId , cachedSvc , "Happy case caches service ID" )
5461 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" )
5663}
5764
5865func TestServiceDiscoveryClient_ListServices_HappyCaseCachedResults (t * testing.T ) {
@@ -65,7 +72,7 @@ func TestServiceDiscoveryClient_ListServices_HappyCaseCachedResults(t *testing.T
6572
6673 sdc := getTestSdClient (t , sdApi )
6774 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 )
6976
7077 svcs , err := sdc .ListServices (context .TODO (), test .NsName )
7178 assert .Equal (t , []* model.Service {test .GetTestService ()}, svcs )
@@ -284,23 +291,61 @@ func TestServiceDiscoveryClient_CreateService_CreatesNamespace_CreateNsError(t *
284291 assert .Equal (t , nsErr , err )
285292}
286293
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 )
289324}
290325
291326func 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 )
293339}
294340
295341func TestServiceDiscoveryClient_RegisterEndpoints (t * testing.T ) {
296342 mockController := gomock .NewController (t )
297343 defer mockController .Finish ()
298344
299345 sdApi := cloudmap .NewMockServiceDiscoveryApi (mockController )
300-
301346 sdc := getTestSdClient (t , sdApi )
302- sdc .namespaceCache .Add (test .NsName , test .NsId , time .Minute )
303347 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 )
304349
305350 attrs1 := map [string ]string {"AWS_INSTANCE_IPV4" : test .EndptIp1 , "AWS_INSTANCE_PORT" : test .EndptPortStr1 }
306351 attrs2 := map [string ]string {"AWS_INSTANCE_IPV4" : test .EndptIp2 , "AWS_INSTANCE_PORT" : test .EndptPortStr2 }
@@ -329,6 +374,8 @@ func TestServiceDiscoveryClient_RegisterEndpoints(t *testing.T) {
329374 })
330375
331376 assert .Nil (t , err )
377+ _ , entryCached := sdc .endpointCache .Get (test .SvcId )
378+ assert .False (t , entryCached , "Cache entry evicted after register" )
332379}
333380
334381func TestServiceDiscoveryClient_DeleteEndpoints (t * testing.T ) {
@@ -338,8 +385,8 @@ func TestServiceDiscoveryClient_DeleteEndpoints(t *testing.T) {
338385 sdApi := cloudmap .NewMockServiceDiscoveryApi (mockController )
339386
340387 sdc := getTestSdClient (t , sdApi )
341- sdc .namespaceCache .Add (test .NsName , test .NsId , time .Minute )
342388 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 )
343390
344391 sdApi .EXPECT ().DeregisterInstance (context .TODO (), test .SvcId , test .EndptId1 ).Return (test .OpId1 , nil )
345392 sdApi .EXPECT ().DeregisterInstance (context .TODO (), test .SvcId , test .EndptId2 ).Return (test .OpId2 , nil )
@@ -352,6 +399,8 @@ func TestServiceDiscoveryClient_DeleteEndpoints(t *testing.T) {
352399 []* model.Endpoint {{Id : test .EndptId1 }, {Id : test .EndptId2 }})
353400
354401 assert .Nil (t , err )
402+ _ , entryCached := sdc .endpointCache .Get (test .SvcId )
403+ assert .False (t , entryCached , "Cache entry evicted after de-register" )
355404}
356405
357406func TestServiceDiscoveryClient_getNamespace_HappyCase (t * testing.T ) {
0 commit comments