inspect_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package service // import "github.com/docker/docker/integration/service"
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/docker/docker/api/types"
  6. "github.com/docker/docker/api/types/container"
  7. swarmtypes "github.com/docker/docker/api/types/swarm"
  8. "github.com/docker/docker/integration/internal/swarm"
  9. "github.com/google/go-cmp/cmp"
  10. "gotest.tools/v3/assert"
  11. is "gotest.tools/v3/assert/cmp"
  12. "gotest.tools/v3/poll"
  13. "gotest.tools/v3/skip"
  14. )
  15. func TestInspect(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. now := time.Now()
  24. var instances uint64 = 2
  25. serviceSpec := fullSwarmServiceSpec("test-service-inspect"+t.Name(), instances)
  26. resp, err := client.ServiceCreate(ctx, serviceSpec, types.ServiceCreateOptions{
  27. QueryRegistry: false,
  28. })
  29. assert.NilError(t, err)
  30. id := resp.ID
  31. poll.WaitOn(t, swarm.RunningTasksCount(ctx, client, id, instances))
  32. service, _, err := client.ServiceInspectWithRaw(ctx, id, types.ServiceInspectOptions{})
  33. assert.NilError(t, err)
  34. expected := swarmtypes.Service{
  35. ID: id,
  36. Spec: serviceSpec,
  37. Meta: swarmtypes.Meta{
  38. Version: swarmtypes.Version{Index: uint64(11)},
  39. CreatedAt: now,
  40. UpdatedAt: now,
  41. },
  42. }
  43. assert.Check(t, is.DeepEqual(service, expected, cmpServiceOpts()))
  44. }
  45. // TODO: use helpers from gotest.tools/assert/opt when available
  46. func cmpServiceOpts() cmp.Option {
  47. const threshold = 20 * time.Second
  48. metaTimeFields := func(path cmp.Path) bool {
  49. switch path.String() {
  50. case "Meta.CreatedAt", "Meta.UpdatedAt":
  51. return true
  52. }
  53. return false
  54. }
  55. withinThreshold := cmp.Comparer(func(x, y time.Time) bool {
  56. delta := x.Sub(y)
  57. return delta < threshold && delta > -threshold
  58. })
  59. return cmp.FilterPath(metaTimeFields, withinThreshold)
  60. }
  61. func fullSwarmServiceSpec(name string, replicas uint64) swarmtypes.ServiceSpec {
  62. restartDelay := 100 * time.Millisecond
  63. maxAttempts := uint64(4)
  64. return swarmtypes.ServiceSpec{
  65. Annotations: swarmtypes.Annotations{
  66. Name: name,
  67. Labels: map[string]string{
  68. "service-label": "service-label-value",
  69. },
  70. },
  71. TaskTemplate: swarmtypes.TaskSpec{
  72. ContainerSpec: &swarmtypes.ContainerSpec{
  73. Image: "busybox:latest",
  74. Labels: map[string]string{"container-label": "container-value"},
  75. Command: []string{"/bin/top"},
  76. Args: []string{"-d", "5"},
  77. Hostname: "hostname",
  78. Env: []string{"envvar=envvalue"},
  79. Dir: "/work",
  80. User: "root",
  81. StopSignal: "SIGINT",
  82. StopGracePeriod: &restartDelay,
  83. Hosts: []string{"8.8.8.8 google"},
  84. DNSConfig: &swarmtypes.DNSConfig{
  85. Nameservers: []string{"8.8.8.8"},
  86. Search: []string{"somedomain"},
  87. },
  88. Isolation: container.IsolationDefault,
  89. },
  90. RestartPolicy: &swarmtypes.RestartPolicy{
  91. Delay: &restartDelay,
  92. Condition: swarmtypes.RestartPolicyConditionOnFailure,
  93. MaxAttempts: &maxAttempts,
  94. },
  95. Runtime: swarmtypes.RuntimeContainer,
  96. },
  97. Mode: swarmtypes.ServiceMode{
  98. Replicated: &swarmtypes.ReplicatedService{
  99. Replicas: &replicas,
  100. },
  101. },
  102. UpdateConfig: &swarmtypes.UpdateConfig{
  103. Parallelism: 2,
  104. Delay: 200 * time.Second,
  105. FailureAction: swarmtypes.UpdateFailureActionContinue,
  106. Monitor: 2 * time.Second,
  107. MaxFailureRatio: 0.2,
  108. Order: swarmtypes.UpdateOrderStopFirst,
  109. },
  110. RollbackConfig: &swarmtypes.UpdateConfig{
  111. Parallelism: 3,
  112. Delay: 300 * time.Second,
  113. FailureAction: swarmtypes.UpdateFailureActionPause,
  114. Monitor: 3 * time.Second,
  115. MaxFailureRatio: 0.3,
  116. Order: swarmtypes.UpdateOrderStartFirst,
  117. },
  118. }
  119. }