service.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package daemon
  2. import (
  3. "context"
  4. "testing"
  5. "time"
  6. "github.com/docker/docker/api/types"
  7. "github.com/docker/docker/api/types/filters"
  8. "github.com/docker/docker/api/types/swarm"
  9. "gotest.tools/v3/assert"
  10. )
  11. // ServiceConstructor defines a swarm service constructor function
  12. type ServiceConstructor func(*swarm.Service)
  13. func (d *Daemon) createServiceWithOptions(ctx context.Context, t testing.TB, opts types.ServiceCreateOptions, f ...ServiceConstructor) string {
  14. t.Helper()
  15. var service swarm.Service
  16. for _, fn := range f {
  17. fn(&service)
  18. }
  19. cli := d.NewClientT(t)
  20. defer cli.Close()
  21. ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
  22. defer cancel()
  23. res, err := cli.ServiceCreate(ctx, service.Spec, opts)
  24. assert.NilError(t, err)
  25. return res.ID
  26. }
  27. // CreateService creates a swarm service given the specified service constructor
  28. func (d *Daemon) CreateService(ctx context.Context, t testing.TB, f ...ServiceConstructor) string {
  29. t.Helper()
  30. return d.createServiceWithOptions(ctx, t, types.ServiceCreateOptions{}, f...)
  31. }
  32. // GetService returns the swarm service corresponding to the specified id
  33. func (d *Daemon) GetService(ctx context.Context, t testing.TB, id string) *swarm.Service {
  34. t.Helper()
  35. cli := d.NewClientT(t)
  36. defer cli.Close()
  37. service, _, err := cli.ServiceInspectWithRaw(ctx, id, types.ServiceInspectOptions{})
  38. assert.NilError(t, err)
  39. return &service
  40. }
  41. // GetServiceTasks returns the swarm tasks for the specified service
  42. func (d *Daemon) GetServiceTasks(ctx context.Context, t testing.TB, service string, additionalFilters ...filters.KeyValuePair) []swarm.Task {
  43. t.Helper()
  44. cli := d.NewClientT(t)
  45. defer cli.Close()
  46. filterArgs := filters.NewArgs(
  47. filters.Arg("desired-state", "running"),
  48. filters.Arg("service", service),
  49. )
  50. for _, filter := range additionalFilters {
  51. filterArgs.Add(filter.Key, filter.Value)
  52. }
  53. options := types.TaskListOptions{
  54. Filters: filterArgs,
  55. }
  56. tasks, err := cli.TaskList(ctx, options)
  57. assert.NilError(t, err)
  58. return tasks
  59. }
  60. // UpdateService updates a swarm service with the specified service constructor
  61. func (d *Daemon) UpdateService(ctx context.Context, t testing.TB, service *swarm.Service, f ...ServiceConstructor) {
  62. t.Helper()
  63. cli := d.NewClientT(t)
  64. defer cli.Close()
  65. for _, fn := range f {
  66. fn(service)
  67. }
  68. _, err := cli.ServiceUpdate(ctx, service.ID, service.Version, service.Spec, types.ServiceUpdateOptions{})
  69. assert.NilError(t, err)
  70. }
  71. // RemoveService removes the specified service
  72. func (d *Daemon) RemoveService(ctx context.Context, t testing.TB, id string) {
  73. t.Helper()
  74. cli := d.NewClientT(t)
  75. defer cli.Close()
  76. err := cli.ServiceRemove(ctx, id)
  77. assert.NilError(t, err)
  78. }
  79. // ListServices returns the list of the current swarm services
  80. func (d *Daemon) ListServices(ctx context.Context, t testing.TB) []swarm.Service {
  81. t.Helper()
  82. cli := d.NewClientT(t)
  83. defer cli.Close()
  84. services, err := cli.ServiceList(ctx, types.ServiceListOptions{})
  85. assert.NilError(t, err)
  86. return services
  87. }
  88. // GetTask returns the swarm task identified by the specified id
  89. func (d *Daemon) GetTask(ctx context.Context, t testing.TB, id string) swarm.Task {
  90. t.Helper()
  91. cli := d.NewClientT(t)
  92. defer cli.Close()
  93. task, _, err := cli.TaskInspectWithRaw(ctx, id)
  94. assert.NilError(t, err)
  95. return task
  96. }