inspect_test.go 3.8 KB

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