@@ -103,6 +103,24 @@ func (m *mockK8sDiscoveryCommand) GetNodes() ([]byte, error) {
103
103
}
104
104
]
105
105
}
106
+ },
107
+ {
108
+ "status" : {
109
+ "addresses" : [
110
+ {
111
+ "address" : "10.100.69.147",
112
+ "type" : "InternalIP"
113
+ },
114
+ {
115
+ "address" : "heorot.example.com",
116
+ "type" : "Hostname"
117
+ },
118
+ {
119
+ "address" : "heorot.example.com",
120
+ "type" : "InternalDNS"
121
+ }
122
+ ]
123
+ }
106
124
}
107
125
]
108
126
}
@@ -113,22 +131,25 @@ func (m *mockK8sDiscoveryCommand) GetNodes() ([]byte, error) {
113
131
func Test_NewK8sAPIDiscoverer (t * testing.T ) {
114
132
Convey ("NewK8sAPIDiscoverer()" , t , func () {
115
133
Convey ("returns a properly configured K8sAPIDiscoverer" , func () {
116
- disco := NewK8sAPIDiscoverer ("127.0.0.1" , 443 , "heorot" , 3 * time .Second , credsPath )
134
+ disco := NewK8sAPIDiscoverer ("127.0.0.1" , 443 , "heorot" , 3 * time .Second , credsPath , true , "hrothgar" )
117
135
118
136
So (disco .discoveredSvcs , ShouldNotBeNil )
119
137
So (disco .Namespace , ShouldEqual , "heorot" )
138
+ So (disco .announceAllNodes , ShouldBeTrue )
139
+ So (disco .hostname , ShouldEqual , "hrothgar" )
120
140
So (disco .Command , ShouldNotBeNil )
121
141
122
142
command := disco .Command .(* KubeAPIDiscoveryCommand )
123
143
So (command .KubeHost , ShouldEqual , "127.0.0.1" )
124
144
So (command .KubePort , ShouldEqual , 443 )
145
+
125
146
})
126
147
})
127
148
}
128
149
129
150
func Test_K8sHealthCheck (t * testing.T ) {
130
151
Convey ("HealthCheck() always returns 'AlwaysSuccessful'" , t , func () {
131
- disco := NewK8sAPIDiscoverer ("127.0.0.1" , 443 , "heorot" , 3 * time .Second , credsPath )
152
+ disco := NewK8sAPIDiscoverer ("127.0.0.1" , 443 , "heorot" , 3 * time .Second , credsPath , true , "hrothgar" )
132
153
check , args := disco .HealthCheck (nil )
133
154
So (check , ShouldEqual , "AlwaysSuccessful" )
134
155
So (args , ShouldBeEmpty )
@@ -137,15 +158,15 @@ func Test_K8sHealthCheck(t *testing.T) {
137
158
138
159
func Test_K8sListeners (t * testing.T ) {
139
160
Convey ("Listeners() always returns and empty slice" , t , func () {
140
- disco := NewK8sAPIDiscoverer ("127.0.0.1" , 443 , "heorot" , 3 * time .Second , credsPath )
161
+ disco := NewK8sAPIDiscoverer ("127.0.0.1" , 443 , "heorot" , 3 * time .Second , credsPath , true , "hrothgar" )
141
162
listeners := disco .Listeners ()
142
163
So (listeners , ShouldBeEmpty )
143
164
})
144
165
}
145
166
146
167
func Test_K8sGetServices (t * testing.T ) {
147
168
Convey ("GetServices()" , t , func () {
148
- disco := NewK8sAPIDiscoverer ("127.0.0.1" , 443 , "heorot" , 3 * time .Second , credsPath )
169
+ disco := NewK8sAPIDiscoverer ("127.0.0.1" , 443 , "heorot" , 3 * time .Second , credsPath , true , "hrothgar" )
149
170
mock := & mockK8sDiscoveryCommand {}
150
171
disco .Command = mock
151
172
@@ -188,7 +209,7 @@ func Test_K8sGetServices(t *testing.T) {
188
209
189
210
func Test_K8sGetNodes (t * testing.T ) {
190
211
Convey ("GetNodes()" , t , func () {
191
- disco := NewK8sAPIDiscoverer ("127.0.0.1" , 443 , "heorot" , 3 * time .Second , credsPath )
212
+ disco := NewK8sAPIDiscoverer ("127.0.0.1" , 443 , "heorot" , 3 * time .Second , credsPath , true , "hrothgar" )
192
213
mock := & mockK8sDiscoveryCommand {}
193
214
disco .Command = mock
194
215
@@ -203,7 +224,7 @@ func Test_K8sGetNodes(t *testing.T) {
203
224
So (capture .String (), ShouldNotContainSubstring , "error" )
204
225
So (disco .discoveredNodes , ShouldNotBeNil )
205
226
So (disco .discoveredNodes , ShouldNotEqual , & K8sNodes {})
206
- So (len (disco .discoveredNodes .Items ), ShouldEqual , 1 )
227
+ So (len (disco .discoveredNodes .Items ), ShouldEqual , 2 )
207
228
So (len (disco .discoveredNodes .Items [0 ].Status .Addresses ), ShouldEqual , 3 )
208
229
})
209
230
@@ -231,16 +252,43 @@ func Test_K8sGetNodes(t *testing.T) {
231
252
232
253
func Test_K8sServices (t * testing.T ) {
233
254
Convey ("Services()" , t , func () {
234
- disco := NewK8sAPIDiscoverer ("127.0.0.1" , 443 , "heorot" , 3 * time .Second , credsPath )
235
255
mock := & mockK8sDiscoveryCommand {}
236
- disco .Command = mock
237
256
238
257
Convey ("works on a newly-created Discoverer" , func () {
258
+ disco := NewK8sAPIDiscoverer ("127.0.0.1" , 443 , "heorot" , 3 * time .Second , credsPath , true , "hrothgar" )
259
+ disco .Command = mock
260
+
239
261
services := disco .Services ()
240
262
So (len (services ), ShouldEqual , 0 )
241
263
})
242
264
243
- Convey ("returns the list of cached services" , func () {
265
+ Convey ("when discovering for all nodes" , func () {
266
+ disco := NewK8sAPIDiscoverer ("127.0.0.1" , 443 , "heorot" , 3 * time .Second , credsPath , true , "hrothgar" )
267
+ disco .Command = mock
268
+
269
+ Convey ("returns the list of cached services" , func () {
270
+ disco .Run (director .NewFreeLooper (director .ONCE , nil ))
271
+ services := disco .Services ()
272
+
273
+ So (len (services ), ShouldEqual , 2 )
274
+ svc := services [0 ]
275
+ So (svc .ID , ShouldEqual , "107b5bbf-9640-4fd0-b5de-1e898e8ae9f7" )
276
+ So (svc .Name , ShouldEqual , "chopper" )
277
+ So (svc .Image , ShouldEqual , "chopper:kubernetes-hosted" )
278
+ So (svc .Created .String (), ShouldEqual , "2022-11-07 13:18:03 +0000 UTC" )
279
+ So (svc .Hostname , ShouldEqual , "beowulf.example.com" )
280
+ So (svc .ProxyMode , ShouldEqual , "http" )
281
+ So (svc .Status , ShouldEqual , service .ALIVE )
282
+ So (svc .Updated .Unix (), ShouldBeGreaterThan , time .Now ().UTC ().Add (- 2 * time .Second ).Unix ())
283
+ So (len (svc .Ports ), ShouldEqual , 1 )
284
+ So (svc .Ports [0 ].IP , ShouldEqual , "10.100.69.136" )
285
+ })
286
+ })
287
+
288
+ Convey ("when discovering for only this node" , func () {
289
+ disco := NewK8sAPIDiscoverer ("127.0.0.1" , 443 , "heorot" , 3 * time .Second , credsPath , false , "heorot.example.com" )
290
+ disco .Command = mock
291
+
244
292
disco .Run (director .NewFreeLooper (director .ONCE , nil ))
245
293
services := disco .Services ()
246
294
@@ -250,12 +298,12 @@ func Test_K8sServices(t *testing.T) {
250
298
So (svc .Name , ShouldEqual , "chopper" )
251
299
So (svc .Image , ShouldEqual , "chopper:kubernetes-hosted" )
252
300
So (svc .Created .String (), ShouldEqual , "2022-11-07 13:18:03 +0000 UTC" )
253
- So (svc .Hostname , ShouldEqual , "beowulf .example.com" )
301
+ So (svc .Hostname , ShouldEqual , "heorot .example.com" )
254
302
So (svc .ProxyMode , ShouldEqual , "http" )
255
303
So (svc .Status , ShouldEqual , service .ALIVE )
256
304
So (svc .Updated .Unix (), ShouldBeGreaterThan , time .Now ().UTC ().Add (- 2 * time .Second ).Unix ())
257
305
So (len (svc .Ports ), ShouldEqual , 1 )
258
- So (svc .Ports [0 ].IP , ShouldEqual , "10.100.69.136 " )
306
+ So (svc .Ports [0 ].IP , ShouldEqual , "10.100.69.147 " )
259
307
})
260
308
})
261
309
}
0 commit comments