4
4
"context"
5
5
"fmt"
6
6
"github.com/aws/aws-cloud-map-mcs-controller-for-k8s/mocks/pkg/cloudmap"
7
+ "github.com/aws/aws-cloud-map-mcs-controller-for-k8s/pkg/model"
7
8
"github.com/aws/aws-cloud-map-mcs-controller-for-k8s/test"
8
9
"github.com/aws/aws-sdk-go-v2/aws"
9
10
sd "github.com/aws/aws-sdk-go-v2/service/servicediscovery"
@@ -37,7 +38,7 @@ func TestServiceDiscoveryApi_ListNamespaces_HappyCase(t *testing.T) {
37
38
38
39
namespaces , _ := sdApi .ListNamespaces (context .TODO ())
39
40
assert .True (t , len (namespaces ) == 1 )
40
- assert .Equal (t , test .GetTestDnsNamespace (), namespaces [0 ], "" )
41
+ assert .Equal (t , test .GetTestDnsNamespace (), namespaces [0 ], "No error for happy case " )
41
42
}
42
43
43
44
func TestServiceDiscoveryApi_ListNamespaces_SkipPublicDNSNotSupported (t * testing.T ) {
@@ -60,6 +61,105 @@ func TestServiceDiscoveryApi_ListNamespaces_SkipPublicDNSNotSupported(t *testing
60
61
assert .True (t , len (namespaces ) == 0 , "Successfully skipped DNS_PUBLIC from the output" )
61
62
}
62
63
64
+ func TestServiceDiscoveryApi_ListServices_HappyCase (t * testing.T ) {
65
+ mockController := gomock .NewController (t )
66
+ defer mockController .Finish ()
67
+
68
+ awsFacade := cloudmap .NewMockAwsFacade (mockController )
69
+ sdApi := getServiceDiscoveryApi (t , awsFacade )
70
+
71
+ filter := types.ServiceFilter {
72
+ Name : types .ServiceFilterNameNamespaceId ,
73
+ Values : []string {test .NsId },
74
+ }
75
+
76
+ awsFacade .EXPECT ().ListServices (context .TODO (), & sd.ListServicesInput {Filters : []types.ServiceFilter {filter }}).
77
+ Return (& sd.ListServicesOutput {Services : []types.ServiceSummary {
78
+ {Id : aws .String (test .SvcId ), Name : aws .String (test .SvcName )},
79
+ }}, nil )
80
+
81
+ svcs , err := sdApi .ListServices (context .TODO (), test .NsId )
82
+ assert .Nil (t , err , "No error for happy case" )
83
+ assert .True (t , len (svcs ) == 1 )
84
+ assert .Equal (t , svcs [0 ], & model.Resource {Id : test .SvcId , Name : test .SvcName })
85
+ }
86
+
87
+ func TestServiceDiscoveryApi_ListInstances_HappyCase (t * testing.T ) {
88
+ mockController := gomock .NewController (t )
89
+ defer mockController .Finish ()
90
+
91
+ awsFacade := cloudmap .NewMockAwsFacade (mockController )
92
+ sdApi := getServiceDiscoveryApi (t , awsFacade )
93
+
94
+ awsFacade .EXPECT ().ListInstances (context .TODO (), gomock .Any ()).
95
+ Return (& sd.ListInstancesOutput {
96
+ Instances : []types.InstanceSummary {{
97
+ Id : aws .String (test .EndptId1 ),
98
+ Attributes : map [string ]string {
99
+ model .Ipv4Attr : test .EndptIp1 ,
100
+ model .PortAttr : test .EndptPortStr1 ,
101
+ }}},
102
+ }, nil )
103
+
104
+ insts , err := sdApi .ListInstances (context .TODO (), test .SvcId )
105
+ assert .Nil (t , err , "No error for happy case" )
106
+ assert .True (t , len (insts ) == 1 )
107
+ assert .Equal (t , insts [0 ], test .GetTestEndpoint ())
108
+ }
109
+
110
+ func TestServiceDiscoveryApi_ListOperations_HappyCase (t * testing.T ) {
111
+ mockController := gomock .NewController (t )
112
+ defer mockController .Finish ()
113
+
114
+ awsFacade := cloudmap .NewMockAwsFacade (mockController )
115
+ sdApi := getServiceDiscoveryApi (t , awsFacade )
116
+
117
+ filters := make ([]types.OperationFilter , 0 )
118
+ awsFacade .EXPECT ().ListOperations (context .TODO (), & sd.ListOperationsInput {Filters : filters }).
119
+ Return (& sd.ListOperationsOutput {
120
+ Operations : []types.OperationSummary {
121
+ {Id : aws .String (test .OpId1 ), Status : types .OperationStatusSuccess },
122
+ }}, nil )
123
+
124
+ ops , err := sdApi .ListOperations (context .TODO (), filters )
125
+ assert .Nil (t , err , "No error for happy case" )
126
+ assert .True (t , len (ops ) == 1 )
127
+ assert .Equal (t , ops [test .OpId1 ], types .OperationStatusSuccess )
128
+
129
+ }
130
+
131
+ func TestServiceDiscoveryApi_GetOperation_HappyCase (t * testing.T ) {
132
+ mockController := gomock .NewController (t )
133
+ defer mockController .Finish ()
134
+
135
+ awsFacade := cloudmap .NewMockAwsFacade (mockController )
136
+ sdApi := getServiceDiscoveryApi (t , awsFacade )
137
+
138
+ expectedOp := & types.Operation {Id : aws .String (test .OpId1 ), Status : types .OperationStatusPending }
139
+ awsFacade .EXPECT ().GetOperation (context .TODO (), & sd.GetOperationInput {OperationId : aws .String (test .OpId1 )}).
140
+ Return (& sd.GetOperationOutput {Operation : expectedOp }, nil )
141
+
142
+ op , err := sdApi .GetOperation (context .TODO (), test .OpId1 )
143
+ assert .Nil (t , err , "No error for happy case" )
144
+ assert .Equal (t , expectedOp , op )
145
+ }
146
+
147
+ func TestServiceDiscoveryApi_CreateHttNamespace_HappyCase (t * testing.T ) {
148
+ mockController := gomock .NewController (t )
149
+ defer mockController .Finish ()
150
+
151
+ awsFacade := cloudmap .NewMockAwsFacade (mockController )
152
+ sdApi := getServiceDiscoveryApi (t , awsFacade )
153
+
154
+ awsFacade .EXPECT ().CreateHttpNamespace (context .TODO (), & sd.CreateHttpNamespaceInput {Name : aws .String (test .NsName )}).
155
+ Return (& sd.CreateHttpNamespaceOutput {OperationId : aws .String (test .OpId1 )}, nil )
156
+
157
+ opId , err := sdApi .CreateHttpNamespace (context .TODO (), test .NsName )
158
+ assert .Nil (t , err , "No error for happy case" )
159
+ assert .Equal (t , test .OpId1 , opId )
160
+
161
+ }
162
+
63
163
func TestServiceDiscoveryApi_CreateService_CreateForHttpNamespace (t * testing.T ) {
64
164
mockController := gomock .NewController (t )
65
165
defer mockController .Finish ()
@@ -129,6 +229,18 @@ func TestServiceDiscoveryApi_CreateService_ThrowError(t *testing.T) {
129
229
assert .Equal (t , "dummy error" , fmt .Sprint (err ), "Got error" )
130
230
}
131
231
232
+ func TestServiceDiscoveryApi_RegisterInstance_HappyCase (t * testing.T ) {
233
+ // TODO: Add unit tests
234
+ }
235
+
236
+ func TestServiceDiscoveryApi_DeregisterInstance_HappyCase (t * testing.T ) {
237
+ // TODO: Add unit tests
238
+ }
239
+
240
+ func TestServiceDiscoveryApi_PollCreateNamespace_HappyCase (t * testing.T ) {
241
+ // TODO: Add unit tests
242
+ }
243
+
132
244
func getServiceDiscoveryApi (t * testing.T , awsFacade * cloudmap.MockAwsFacade ) serviceDiscoveryApi {
133
245
return serviceDiscoveryApi {
134
246
log : testingLogger.TestLogger {T : t },
0 commit comments