jobs_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package service
  2. import (
  3. "context"
  4. "testing"
  5. "github.com/docker/docker/api/types"
  6. swarmtypes "github.com/docker/docker/api/types/swarm"
  7. "github.com/docker/docker/integration/internal/swarm"
  8. "gotest.tools/v3/assert"
  9. "gotest.tools/v3/poll"
  10. "gotest.tools/v3/skip"
  11. )
  12. // The file jobs_test.go contains tests that verify that services which are in
  13. // the mode ReplicatedJob or GlobalJob.
  14. // TestCreateJob tests that a Service can be created and run with
  15. // mode ReplicatedJob
  16. func TestCreateJob(t *testing.T) {
  17. skip.If(t, testEnv.IsRemoteDaemon)
  18. skip.If(t, testEnv.DaemonInfo.OSType == "windows")
  19. defer setupTest(t)
  20. d := swarm.NewSwarm(t, testEnv)
  21. defer d.Stop(t)
  22. client := d.NewClientT(t)
  23. defer client.Close()
  24. for _, mode := range []swarmtypes.ServiceMode{
  25. {ReplicatedJob: &swarmtypes.ReplicatedJob{}},
  26. {GlobalJob: &swarmtypes.GlobalJob{}},
  27. } {
  28. id := swarm.CreateService(t, d, swarm.ServiceWithMode(mode))
  29. poll.WaitOn(t, swarm.RunningTasksCount(client, id, 1), swarm.ServicePoll)
  30. }
  31. }
  32. // TestReplicatedJob tests that running a replicated job starts the requisite
  33. // number of tasks,
  34. func TestReplicatedJob(t *testing.T) {
  35. skip.If(t, testEnv.IsRemoteDaemon)
  36. skip.If(t, testEnv.DaemonInfo.OSType == "windows")
  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. defer setupTest(t)
  48. d := swarm.NewSwarm(t, testEnv)
  49. defer d.Stop(t)
  50. client := d.NewClientT(t)
  51. defer client.Close()
  52. id := swarm.CreateService(t, d,
  53. swarm.ServiceWithMode(swarmtypes.ServiceMode{
  54. ReplicatedJob: &swarmtypes.ReplicatedJob{
  55. MaxConcurrent: &maxConcurrent,
  56. TotalCompletions: &total,
  57. },
  58. }),
  59. // just run a command to execute and exit peacefully.
  60. swarm.ServiceWithCommand([]string{"true"}),
  61. )
  62. service, _, err := client.ServiceInspectWithRaw(
  63. context.Background(), id, types.ServiceInspectOptions{},
  64. )
  65. assert.NilError(t, err)
  66. poll.WaitOn(t, swarm.JobComplete(client, service), swarm.ServicePoll)
  67. }
  68. // TestUpdateJob tests that a job can be updated, and that it runs with the
  69. // correct parameters.
  70. func TestUpdateReplicatedJob(t *testing.T) {
  71. skip.If(t, testEnv.IsRemoteDaemon)
  72. skip.If(t, testEnv.DaemonInfo.OSType == "windows")
  73. defer setupTest(t)()
  74. d := swarm.NewSwarm(t, testEnv)
  75. defer d.Stop(t)
  76. client := d.NewClientT(t)
  77. defer client.Close()
  78. // avoid writing "context.Background()" over and over again
  79. ctx := context.Background()
  80. // Create the job service
  81. id := swarm.CreateService(t, d,
  82. swarm.ServiceWithMode(swarmtypes.ServiceMode{
  83. ReplicatedJob: &swarmtypes.ReplicatedJob{
  84. // use the default, empty values.
  85. },
  86. }),
  87. // run "true" so the task exits with 0
  88. swarm.ServiceWithCommand([]string{"true"}),
  89. )
  90. service, _, err := client.ServiceInspectWithRaw(
  91. ctx, id, types.ServiceInspectOptions{},
  92. )
  93. assert.NilError(t, err)
  94. // wait for the job to completed
  95. poll.WaitOn(t, swarm.JobComplete(client, service), swarm.ServicePoll)
  96. // update the job.
  97. spec := service.Spec
  98. spec.TaskTemplate.ForceUpdate++
  99. _, err = client.ServiceUpdate(
  100. ctx, id, service.Version, spec, types.ServiceUpdateOptions{},
  101. )
  102. assert.NilError(t, err)
  103. service2, _, err := client.ServiceInspectWithRaw(
  104. ctx, id, types.ServiceInspectOptions{},
  105. )
  106. assert.NilError(t, err)
  107. // assert that the job iteration has increased
  108. assert.Assert(t,
  109. service.JobStatus.JobIteration.Index < service2.JobStatus.JobIteration.Index,
  110. )
  111. // now wait for the service to complete a second time.
  112. poll.WaitOn(t, swarm.JobComplete(client, service2), swarm.ServicePoll)
  113. }