jobs_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package service
  2. import (
  3. "testing"
  4. "github.com/docker/docker/api/types"
  5. swarmtypes "github.com/docker/docker/api/types/swarm"
  6. "github.com/docker/docker/integration/internal/swarm"
  7. "gotest.tools/v3/assert"
  8. "gotest.tools/v3/poll"
  9. "gotest.tools/v3/skip"
  10. )
  11. // The file jobs_test.go contains tests that verify that services which are in
  12. // the mode ReplicatedJob or GlobalJob.
  13. // TestCreateJob tests that a Service can be created and run with
  14. // mode ReplicatedJob
  15. func TestCreateJob(t *testing.T) {
  16. skip.If(t, testEnv.IsRemoteDaemon)
  17. skip.If(t, testEnv.DaemonInfo.OSType == "windows")
  18. ctx := setupTest(t)
  19. d := swarm.NewSwarm(ctx, t, testEnv)
  20. defer d.Stop(t)
  21. client := d.NewClientT(t)
  22. defer client.Close()
  23. for _, mode := range []swarmtypes.ServiceMode{
  24. {ReplicatedJob: &swarmtypes.ReplicatedJob{}},
  25. {GlobalJob: &swarmtypes.GlobalJob{}},
  26. } {
  27. id := swarm.CreateService(ctx, t, d, swarm.ServiceWithMode(mode))
  28. poll.WaitOn(t, swarm.RunningTasksCount(ctx, client, id, 1), swarm.ServicePoll)
  29. }
  30. }
  31. // TestReplicatedJob tests that running a replicated job starts the requisite
  32. // number of tasks,
  33. func TestReplicatedJob(t *testing.T) {
  34. skip.If(t, testEnv.IsRemoteDaemon)
  35. skip.If(t, testEnv.DaemonInfo.OSType == "windows")
  36. ctx := setupTest(t)
  37. // we need variables, because the replicas field takes a pointer
  38. maxConcurrent := uint64(2)
  39. // there is overhead, especially in the test environment, associated with
  40. // starting tasks. if total is set too high, then the time needed to
  41. // complete the test, even if everything is proceeding ideally, may exceed
  42. // the time the test has to execute
  43. //
  44. // in CI,the test has been seen to time out with as few as 7 completions
  45. // after 15 seconds. this means 7 completions ought not be too many.
  46. total := uint64(7)
  47. d := swarm.NewSwarm(ctx, t, testEnv)
  48. defer d.Stop(t)
  49. client := d.NewClientT(t)
  50. defer client.Close()
  51. id := swarm.CreateService(ctx, t, d,
  52. swarm.ServiceWithMode(swarmtypes.ServiceMode{
  53. ReplicatedJob: &swarmtypes.ReplicatedJob{
  54. MaxConcurrent: &maxConcurrent,
  55. TotalCompletions: &total,
  56. },
  57. }),
  58. // just run a command to execute and exit peacefully.
  59. swarm.ServiceWithCommand([]string{"true"}),
  60. )
  61. service, _, err := client.ServiceInspectWithRaw(
  62. ctx, id, types.ServiceInspectOptions{},
  63. )
  64. assert.NilError(t, err)
  65. poll.WaitOn(t, swarm.JobComplete(ctx, client, service), swarm.ServicePoll)
  66. }
  67. // TestUpdateJob tests that a job can be updated, and that it runs with the
  68. // correct parameters.
  69. func TestUpdateReplicatedJob(t *testing.T) {
  70. skip.If(t, testEnv.IsRemoteDaemon)
  71. skip.If(t, testEnv.DaemonInfo.OSType == "windows")
  72. ctx := setupTest(t)
  73. d := swarm.NewSwarm(ctx, t, testEnv)
  74. defer d.Stop(t)
  75. client := d.NewClientT(t)
  76. defer client.Close()
  77. // Create the job service
  78. id := swarm.CreateService(ctx, t, d,
  79. swarm.ServiceWithMode(swarmtypes.ServiceMode{
  80. ReplicatedJob: &swarmtypes.ReplicatedJob{
  81. // use the default, empty values.
  82. },
  83. }),
  84. // run "true" so the task exits with 0
  85. swarm.ServiceWithCommand([]string{"true"}),
  86. )
  87. service, _, err := client.ServiceInspectWithRaw(
  88. ctx, id, types.ServiceInspectOptions{},
  89. )
  90. assert.NilError(t, err)
  91. // wait for the job to completed
  92. poll.WaitOn(t, swarm.JobComplete(ctx, client, service), swarm.ServicePoll)
  93. // update the job.
  94. spec := service.Spec
  95. spec.TaskTemplate.ForceUpdate++
  96. _, err = client.ServiceUpdate(
  97. ctx, id, service.Version, spec, types.ServiceUpdateOptions{},
  98. )
  99. assert.NilError(t, err)
  100. service2, _, err := client.ServiceInspectWithRaw(
  101. ctx, id, types.ServiceInspectOptions{},
  102. )
  103. assert.NilError(t, err)
  104. // assert that the job iteration has increased
  105. assert.Assert(t,
  106. service.JobStatus.JobIteration.Index < service2.JobStatus.JobIteration.Index,
  107. )
  108. // now wait for the service to complete a second time.
  109. poll.WaitOn(t, swarm.JobComplete(ctx, client, service2), swarm.ServicePoll)
  110. }