@@ -215,7 +215,7 @@ func TestServiceWithPredefinedNetwork(t *testing.T) {
215215 swarm .ServiceWithNetwork (hostName ),
216216 )
217217
218- poll .WaitOn (t , serviceRunningCount (c , serviceID , instances ), swarm .ServicePoll )
218+ poll .WaitOn (t , swarm . RunningTasksCount (c , serviceID , instances ), swarm .ServicePoll )
219219
220220 _ , _ , err := c .ServiceInspectWithRaw (context .Background (), serviceID , types.ServiceInspectOptions {})
221221 assert .NilError (t , err )
@@ -254,7 +254,7 @@ func TestServiceRemoveKeepsIngressNetwork(t *testing.T) {
254254 }),
255255 )
256256
257- poll .WaitOn (t , serviceRunningCount (c , serviceID , instances ), swarm .ServicePoll )
257+ poll .WaitOn (t , swarm . RunningTasksCount (c , serviceID , instances ), swarm .ServicePoll )
258258
259259 ctx := context .Background ()
260260 _ , _ , err := c .ServiceInspectWithRaw (ctx , serviceID , types.ServiceInspectOptions {})
@@ -279,20 +279,6 @@ func TestServiceRemoveKeepsIngressNetwork(t *testing.T) {
279279 assert .Assert (t , ok , "ingress-sbox not present in ingress network" )
280280}
281281
282- func serviceRunningCount (client client.ServiceAPIClient , serviceID string , instances uint64 ) func (log poll.LogT ) poll.Result {
283- return func (log poll.LogT ) poll.Result {
284- services , err := client .ServiceList (context .Background (), types.ServiceListOptions {})
285- if err != nil {
286- return poll .Error (err )
287- }
288-
289- if len (services ) != int (instances ) {
290- return poll .Continue ("Service count at %d waiting for %d" , len (services ), instances )
291- }
292- return poll .Success ()
293- }
294- }
295-
296282func swarmIngressReady (client client.NetworkAPIClient ) func (log poll.LogT ) poll.Result {
297283 return func (log poll.LogT ) poll.Result {
298284 netInfo , err := client .NetworkInspect (context .Background (), ingressNet , types.NetworkInspectOptions {
@@ -333,77 +319,86 @@ func TestServiceWithDataPathPortInit(t *testing.T) {
333319 skip .If (t , testEnv .OSType == "windows" )
334320 skip .If (t , versions .LessThan (testEnv .DaemonAPIVersion (), "1.40" ), "DataPathPort was added in API v1.40" )
335321 defer setupTest (t )()
336- var ops = []func (* daemon.Daemon ){}
337322 var datapathPort uint32 = 7777
338- ops = append (ops , daemon .WithSwarmDataPathPort (datapathPort ))
339- d := swarm .NewSwarm (t , testEnv , ops ... )
340-
323+ d := swarm .NewSwarm (t , testEnv , daemon .WithSwarmDataPathPort (datapathPort ))
341324 c := d .NewClientT (t )
342- defer c .Close ()
343-
325+ ctx := context .Background ()
344326 // Create a overlay network
345327 name := "saanvisthira" + t .Name ()
346- network .CreateNoError (context .Background (), t , c , name ,
328+ overlayID := network .CreateNoError (context .Background (), t , c , name ,
347329 network .WithDriver ("overlay" ))
348330
349331 var instances uint64 = 1
350332 serviceID := swarm .CreateService (t , d ,
351333 swarm .ServiceWithReplicas (instances ),
334+ swarm .ServiceWithName (name ),
352335 swarm .ServiceWithNetwork (name ),
353336 )
354337
355- poll .WaitOn (t , serviceRunningCount (c , serviceID , instances ), swarm .ServicePoll )
338+ poll .WaitOn (t , swarm . RunningTasksCount (c , serviceID , instances ), swarm .ServicePoll )
356339
357340 info := d .Info (t )
358341 assert .Equal (t , info .Swarm .Cluster .DataPathPort , datapathPort )
359- err := c .ServiceRemove (context .Background (), serviceID )
342+ err := c .ServiceRemove (ctx , serviceID )
343+ assert .NilError (t , err )
344+ poll .WaitOn (t , noServices (ctx , c ), swarm .ServicePoll )
345+ poll .WaitOn (t , swarm .NoTasks (ctx , c ), swarm .ServicePoll )
346+ err = c .NetworkRemove (ctx , overlayID )
347+ assert .NilError (t , err )
348+ c .Close ()
349+ err = d .SwarmLeave (t , true )
360350 assert .NilError (t , err )
361- d .SwarmLeave (t , true )
362351 d .Stop (t )
363352
364353 // Clean up , set it back to original one to make sure other tests don't fail
365354 // call without datapath port option.
366- ops = [] func ( * daemon. Daemon ){}
367- d = swarm . NewSwarm ( t , testEnv , ops ... )
368- c = d .NewClientT (t )
369-
355+ d = swarm . NewSwarm ( t , testEnv )
356+ defer d . Stop ( t )
357+ nc : = d .NewClientT (t )
358+ defer nc . Close ()
370359 // Create a overlay network
371- name = "saanvisthira" + t .Name ()
372- network .CreateNoError (context . Background () , t , c , name ,
360+ name = "not- saanvisthira" + t .Name ()
361+ overlayID = network .CreateNoError (ctx , t , nc , name ,
373362 network .WithDriver ("overlay" ))
374363
375364 serviceID = swarm .CreateService (t , d ,
376365 swarm .ServiceWithReplicas (instances ),
366+ swarm .ServiceWithName (name ),
377367 swarm .ServiceWithNetwork (name ),
378368 )
379369
380- poll .WaitOn (t , serviceRunningCount ( c , serviceID , instances ), swarm .ServicePoll )
370+ poll .WaitOn (t , swarm . RunningTasksCount ( nc , serviceID , instances ), swarm .ServicePoll )
381371
382372 info = d .Info (t )
383373 var defaultDataPathPort uint32 = 4789
384374 assert .Equal (t , info .Swarm .Cluster .DataPathPort , defaultDataPathPort )
385- err = c .ServiceRemove (context .Background (), serviceID )
375+ err = nc .ServiceRemove (ctx , serviceID )
376+ assert .NilError (t , err )
377+ poll .WaitOn (t , noServices (ctx , nc ), swarm .ServicePoll )
378+ poll .WaitOn (t , swarm .NoTasks (ctx , nc ), swarm .ServicePoll )
379+ err = nc .NetworkRemove (ctx , overlayID )
380+ assert .NilError (t , err )
381+ err = d .SwarmLeave (t , true )
386382 assert .NilError (t , err )
387- d .SwarmLeave (t , true )
388- defer d .Stop (t )
389383}
390384
391385func TestServiceWithDefaultAddressPoolInit (t * testing.T ) {
392386 skip .If (t , testEnv .OSType == "windows" )
393387 defer setupTest (t )()
394- var ops = []func (* daemon.Daemon ){}
395- ipAddr := []string {"20.20.0.0/16" }
396- ops = append (ops , daemon .WithSwarmDefaultAddrPool (ipAddr ))
397- ops = append (ops , daemon .WithSwarmDefaultAddrPoolSubnetSize (24 ))
398- d := swarm .NewSwarm (t , testEnv , ops ... )
399-
388+ d := swarm .NewSwarm (t , testEnv ,
389+ daemon .WithSwarmDefaultAddrPool ([]string {"20.20.0.0/16" }),
390+ daemon .WithSwarmDefaultAddrPoolSubnetSize (24 ))
391+ defer d .Stop (t )
400392 cli := d .NewClientT (t )
401393 defer cli .Close ()
394+ ctx := context .Background ()
402395
403396 // Create a overlay network
404- name := "saanvisthira" + t .Name ()
405- network .CreateNoError (context .Background (), t , cli , name ,
406- network .WithDriver ("overlay" ))
397+ name := "sthira" + t .Name ()
398+ overlayID := network .CreateNoError (ctx , t , cli , name ,
399+ network .WithDriver ("overlay" ),
400+ network .WithCheckDuplicate (),
401+ )
407402
408403 var instances uint64 = 1
409404 serviceName := "TestService" + t .Name ()
@@ -413,27 +408,24 @@ func TestServiceWithDefaultAddressPoolInit(t *testing.T) {
413408 swarm .ServiceWithNetwork (name ),
414409 )
415410
416- poll .WaitOn (t , serviceRunningCount (cli , serviceID , instances ), swarm .ServicePoll )
411+ poll .WaitOn (t , swarm . RunningTasksCount (cli , serviceID , instances ), swarm .ServicePoll )
417412
418- _ , _ , err := cli .ServiceInspectWithRaw (context . Background () , serviceID , types.ServiceInspectOptions {})
413+ _ , _ , err := cli .ServiceInspectWithRaw (ctx , serviceID , types.ServiceInspectOptions {})
419414 assert .NilError (t , err )
420415
421- out , err := cli .NetworkInspect (context . Background (), name , types.NetworkInspectOptions {})
416+ out , err := cli .NetworkInspect (ctx , overlayID , types.NetworkInspectOptions {Verbose : true })
422417 assert .NilError (t , err )
423418 t .Logf ("%s: NetworkInspect: %+v" , t .Name (), out )
424419 assert .Assert (t , len (out .IPAM .Config ) > 0 )
425420 assert .Equal (t , out .IPAM .Config [0 ].Subnet , "20.20.0.0/24" )
426421
427- err = cli .ServiceRemove (context .Background (), serviceID )
422+ err = cli .ServiceRemove (ctx , serviceID )
423+ poll .WaitOn (t , noServices (ctx , cli ), swarm .ServicePoll )
424+ poll .WaitOn (t , swarm .NoTasks (ctx , cli ), swarm .ServicePoll )
425+ assert .NilError (t , err )
426+ err = cli .NetworkRemove (ctx , overlayID )
427+ assert .NilError (t , err )
428+ err = d .SwarmLeave (t , true )
428429 assert .NilError (t , err )
429- d .SwarmLeave (t , true )
430- d .Stop (t )
431430
432- // Clean up , set it back to original one to make sure other tests don't fail
433- ipAddr = []string {"10.0.0.0/8" }
434- ops = append (ops , daemon .WithSwarmDefaultAddrPool (ipAddr ))
435- ops = append (ops , daemon .WithSwarmDefaultAddrPoolSubnetSize (24 ))
436- d = swarm .NewSwarm (t , testEnv , ops ... )
437- d .SwarmLeave (t , true )
438- defer d .Stop (t )
439431}
0 commit comments