inspect_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. "github.com/docker/docker/api/types/filters"
  8. swarmtypes "github.com/docker/docker/api/types/swarm"
  9. "github.com/docker/docker/client"
  10. "github.com/docker/docker/integration/internal/swarm"
  11. "github.com/gotestyourself/gotestyourself/poll"
  12. "github.com/gotestyourself/gotestyourself/skip"
  13. "github.com/stretchr/testify/assert"
  14. "github.com/stretchr/testify/require"
  15. "golang.org/x/net/context"
  16. )
  17. func TestInspect(t *testing.T) {
  18. skip.IfCondition(t, testEnv.IsRemoteDaemon())
  19. defer setupTest(t)()
  20. d := swarm.NewSwarm(t, testEnv)
  21. defer d.Stop(t)
  22. client, err := client.NewClientWithOpts(client.WithHost((d.Sock())))
  23. require.NoError(t, err)
  24. var before = time.Now()
  25. var instances uint64 = 2
  26. serviceSpec := fullSwarmServiceSpec("test-service-inspect", instances)
  27. ctx := context.Background()
  28. resp, err := client.ServiceCreate(ctx, serviceSpec, types.ServiceCreateOptions{
  29. QueryRegistry: false,
  30. })
  31. require.NoError(t, err)
  32. id := resp.ID
  33. poll.WaitOn(t, serviceContainerCount(client, id, instances))
  34. service, _, err := client.ServiceInspectWithRaw(ctx, id, types.ServiceInspectOptions{})
  35. require.NoError(t, err)
  36. assert.Equal(t, serviceSpec, service.Spec)
  37. assert.Equal(t, uint64(11), service.Meta.Version.Index)
  38. assert.Equal(t, id, service.ID)
  39. assert.WithinDuration(t, before, service.CreatedAt, 30*time.Second)
  40. assert.WithinDuration(t, before, service.UpdatedAt, 30*time.Second)
  41. }
  42. func fullSwarmServiceSpec(name string, replicas uint64) swarmtypes.ServiceSpec {
  43. restartDelay := 100 * time.Millisecond
  44. maxAttempts := uint64(4)
  45. return swarmtypes.ServiceSpec{
  46. Annotations: swarmtypes.Annotations{
  47. Name: name,
  48. Labels: map[string]string{
  49. "service-label": "service-label-value",
  50. },
  51. },
  52. TaskTemplate: swarmtypes.TaskSpec{
  53. ContainerSpec: &swarmtypes.ContainerSpec{
  54. Image: "busybox:latest",
  55. Labels: map[string]string{"container-label": "container-value"},
  56. Command: []string{"/bin/top"},
  57. Args: []string{"-u", "root"},
  58. Hostname: "hostname",
  59. Env: []string{"envvar=envvalue"},
  60. Dir: "/work",
  61. User: "root",
  62. StopSignal: "SIGINT",
  63. StopGracePeriod: &restartDelay,
  64. Hosts: []string{"8.8.8.8 google"},
  65. DNSConfig: &swarmtypes.DNSConfig{
  66. Nameservers: []string{"8.8.8.8"},
  67. Search: []string{"somedomain"},
  68. },
  69. Isolation: container.IsolationDefault,
  70. },
  71. RestartPolicy: &swarmtypes.RestartPolicy{
  72. Delay: &restartDelay,
  73. Condition: swarmtypes.RestartPolicyConditionOnFailure,
  74. MaxAttempts: &maxAttempts,
  75. },
  76. Runtime: swarmtypes.RuntimeContainer,
  77. },
  78. Mode: swarmtypes.ServiceMode{
  79. Replicated: &swarmtypes.ReplicatedService{
  80. Replicas: &replicas,
  81. },
  82. },
  83. UpdateConfig: &swarmtypes.UpdateConfig{
  84. Parallelism: 2,
  85. Delay: 200 * time.Second,
  86. FailureAction: swarmtypes.UpdateFailureActionContinue,
  87. Monitor: 2 * time.Second,
  88. MaxFailureRatio: 0.2,
  89. Order: swarmtypes.UpdateOrderStopFirst,
  90. },
  91. RollbackConfig: &swarmtypes.UpdateConfig{
  92. Parallelism: 3,
  93. Delay: 300 * time.Second,
  94. FailureAction: swarmtypes.UpdateFailureActionPause,
  95. Monitor: 3 * time.Second,
  96. MaxFailureRatio: 0.3,
  97. Order: swarmtypes.UpdateOrderStartFirst,
  98. },
  99. }
  100. }
  101. func serviceContainerCount(client client.ServiceAPIClient, id string, count uint64) func(log poll.LogT) poll.Result {
  102. return func(log poll.LogT) poll.Result {
  103. filter := filters.NewArgs()
  104. filter.Add("service", id)
  105. tasks, err := client.TaskList(context.Background(), types.TaskListOptions{
  106. Filters: filter,
  107. })
  108. switch {
  109. case err != nil:
  110. return poll.Error(err)
  111. case len(tasks) == int(count):
  112. return poll.Success()
  113. default:
  114. return poll.Continue("task count at %d waiting for %d", len(tasks), count)
  115. }
  116. }
  117. }