@@ -10,6 +10,7 @@ import (
10
10
"github.com/hashicorp/nomad/ci"
11
11
"github.com/hashicorp/nomad/nomad/mock"
12
12
"github.com/hashicorp/nomad/nomad/structs"
13
+ "github.com/shoenig/test/must"
13
14
"github.com/stretchr/testify/require"
14
15
)
15
16
@@ -157,6 +158,7 @@ func TestHTTPServer_ServiceRegistrationRequest(t *testing.T) {
157
158
name string
158
159
}{
159
160
{
161
+ name : "delete by ID" ,
160
162
testFn : func (s * TestAgent ) {
161
163
162
164
// Grab the state, so we can manipulate it and test against it.
@@ -186,9 +188,9 @@ func TestHTTPServer_ServiceRegistrationRequest(t *testing.T) {
186
188
require .Nil (t , out )
187
189
require .NoError (t , err )
188
190
},
189
- name : "delete by ID" ,
190
191
},
191
192
{
193
+ name : "get service by name" ,
192
194
testFn : func (s * TestAgent ) {
193
195
194
196
// Grab the state, so we can manipulate it and test against it.
@@ -214,9 +216,99 @@ func TestHTTPServer_ServiceRegistrationRequest(t *testing.T) {
214
216
require .NotZero (t , respW .Header ().Get ("X-Nomad-Index" ))
215
217
require .Equal (t , serviceReg , obj .([]* structs.ServiceRegistration )[0 ])
216
218
},
217
- name : "get service by name" ,
218
219
},
219
220
{
221
+ name : "get service using choose" ,
222
+ testFn : func (s * TestAgent ) {
223
+ // Grab the state so we can manipulate and test against it.
224
+ testState := s .Agent .server .State ()
225
+
226
+ err := testState .UpsertServiceRegistrations (
227
+ structs .MsgTypeTestSetup , 10 ,
228
+ []* structs.ServiceRegistration {{
229
+ ID : "978d519a-46ad-fb04-966b-000000000001" ,
230
+ ServiceName : "redis" ,
231
+ Namespace : "default" ,
232
+ NodeID : "node1" ,
233
+ Datacenter : "dc1" ,
234
+ JobID : "job1" ,
235
+ AllocID : "8b83191f-cb29-e23a-d955-220b65ef676d" ,
236
+ Tags : nil ,
237
+ Address : "10.0.0.1" ,
238
+ Port : 8080 ,
239
+ CreateIndex : 10 ,
240
+ ModifyIndex : 10 ,
241
+ }, {
242
+ ID : "978d519a-46ad-fb04-966b-000000000002" ,
243
+ ServiceName : "redis" ,
244
+ Namespace : "default" ,
245
+ NodeID : "node2" ,
246
+ Datacenter : "dc1" ,
247
+ JobID : "job1" ,
248
+ AllocID : "df6de93c-9376-a774-bcdf-3bd817e18078" ,
249
+ Tags : nil ,
250
+ Address : "10.0.0.2" ,
251
+ Port : 8080 ,
252
+ CreateIndex : 10 ,
253
+ ModifyIndex : 10 ,
254
+ }, {
255
+ ID : "978d519a-46ad-fb04-966b-000000000003" ,
256
+ ServiceName : "redis" ,
257
+ Namespace : "default" ,
258
+ NodeID : "node3" ,
259
+ Datacenter : "dc1" ,
260
+ JobID : "job1" ,
261
+ AllocID : "df6de93c-9376-a774-bcdf-3bd817e18078" ,
262
+ Tags : nil ,
263
+ Address : "10.0.0.3" ,
264
+ Port : 8080 ,
265
+ CreateIndex : 10 ,
266
+ ModifyIndex : 10 ,
267
+ }},
268
+ )
269
+ must .NoError (t , err )
270
+
271
+ // Build the HTTP request for 1 instance of the service, using key=abc123
272
+ req , err := http .NewRequest (http .MethodGet , "/v1/service/redis?choose=1|abc123" , nil )
273
+ must .NoError (t , err )
274
+ respW := httptest .NewRecorder ()
275
+
276
+ // Send the HTTP request.
277
+ obj , err := s .Server .ServiceRegistrationRequest (respW , req )
278
+ must .NoError (t , err )
279
+
280
+ // Check we got the correct type back.
281
+ services , ok := (obj ).([]* structs.ServiceRegistration )
282
+ must .True (t , ok )
283
+
284
+ // Check we got the expected number of services back.
285
+ must .Len (t , 1 , services )
286
+
287
+ // Build the HTTP request for 2 instances of the service, still using key=abc123
288
+ req2 , err := http .NewRequest (http .MethodGet , "/v1/service/redis?choose=2|abc123" , nil )
289
+ must .NoError (t , err )
290
+ respW2 := httptest .NewRecorder ()
291
+
292
+ // Send the 2nd HTTP request.
293
+ obj2 , err := s .Server .ServiceRegistrationRequest (respW2 , req2 )
294
+ must .NoError (t , err )
295
+
296
+ // Check we got the correct type back.
297
+ services2 , ok := (obj2 ).([]* structs.ServiceRegistration )
298
+ must .True (t , ok )
299
+
300
+ // Check we got the expected number of services back.
301
+ must .Len (t , 2 , services2 )
302
+
303
+ // Check the first service is the same as the previous service.
304
+ must .Eq (t , services [0 ], services2 [0 ])
305
+
306
+ // Check the second service is not the same as the first service.
307
+ must .NotEq (t , services2 [0 ], services2 [1 ])
308
+ },
309
+ },
310
+ {
311
+ name : "incorrect URI format" ,
220
312
testFn : func (s * TestAgent ) {
221
313
222
314
// Build the HTTP request.
@@ -230,9 +322,9 @@ func TestHTTPServer_ServiceRegistrationRequest(t *testing.T) {
230
322
require .Contains (t , err .Error (), "invalid URI" )
231
323
require .Nil (t , obj )
232
324
},
233
- name : "incorrect URI format" ,
234
325
},
235
326
{
327
+ name : "get service empty name" ,
236
328
testFn : func (s * TestAgent ) {
237
329
238
330
// Build the HTTP request.
@@ -246,9 +338,9 @@ func TestHTTPServer_ServiceRegistrationRequest(t *testing.T) {
246
338
require .Contains (t , err .Error (), "missing service name" )
247
339
require .Nil (t , obj )
248
340
},
249
- name : "get service empty name" ,
250
341
},
251
342
{
343
+ name : "get service incorrect method" ,
252
344
testFn : func (s * TestAgent ) {
253
345
254
346
// Build the HTTP request.
@@ -262,9 +354,9 @@ func TestHTTPServer_ServiceRegistrationRequest(t *testing.T) {
262
354
require .Contains (t , err .Error (), "Invalid method" )
263
355
require .Nil (t , obj )
264
356
},
265
- name : "get service incorrect method" ,
266
357
},
267
358
{
359
+ name : "delete service empty id" ,
268
360
testFn : func (s * TestAgent ) {
269
361
270
362
// Build the HTTP request.
@@ -278,9 +370,9 @@ func TestHTTPServer_ServiceRegistrationRequest(t *testing.T) {
278
370
require .Contains (t , err .Error (), "missing service id" )
279
371
require .Nil (t , obj )
280
372
},
281
- name : "delete service empty id" ,
282
373
},
283
374
{
375
+ name : "delete service incorrect method" ,
284
376
testFn : func (s * TestAgent ) {
285
377
286
378
// Build the HTTP request.
@@ -294,7 +386,6 @@ func TestHTTPServer_ServiceRegistrationRequest(t *testing.T) {
294
386
require .Contains (t , err .Error (), "Invalid method" )
295
387
require .Nil (t , obj )
296
388
},
297
- name : "delete service incorrect method" ,
298
389
},
299
390
}
300
391
0 commit comments