inspect_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package service
  2. import (
  3. "fmt"
  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. "github.com/docker/docker/client"
  10. "github.com/docker/docker/integration-cli/daemon"
  11. "github.com/docker/docker/integration-cli/request"
  12. "github.com/gotestyourself/gotestyourself/poll"
  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. defer setupTest(t)()
  19. d := newSwarm(t)
  20. defer d.Stop(t)
  21. client, err := request.NewClientForHost(d.Sock())
  22. require.NoError(t, err)
  23. var before = time.Now()
  24. var instances uint64 = 2
  25. serviceSpec := fullSwarmServiceSpec("test-service-inspect", instances)
  26. ctx := context.Background()
  27. resp, err := client.ServiceCreate(ctx, serviceSpec, types.ServiceCreateOptions{
  28. QueryRegistry: false,
  29. })
  30. require.NoError(t, err)
  31. id := resp.ID
  32. poll.WaitOn(t, serviceContainerCount(client, id, instances))
  33. service, _, err := client.ServiceInspectWithRaw(ctx, id, types.ServiceInspectOptions{})
  34. require.NoError(t, err)
  35. assert.Equal(t, serviceSpec, service.Spec)
  36. assert.Equal(t, uint64(11), service.Meta.Version.Index)
  37. assert.Equal(t, id, service.ID)
  38. assert.WithinDuration(t, before, service.CreatedAt, 30*time.Second)
  39. assert.WithinDuration(t, before, service.UpdatedAt, 30*time.Second)
  40. }
  41. func fullSwarmServiceSpec(name string, replicas uint64) swarm.ServiceSpec {
  42. restartDelay := 100 * time.Millisecond
  43. maxAttempts := uint64(4)
  44. return swarm.ServiceSpec{
  45. Annotations: swarm.Annotations{
  46. Name: name,
  47. Labels: map[string]string{
  48. "service-label": "service-label-value",
  49. },
  50. },
  51. TaskTemplate: swarm.TaskSpec{
  52. ContainerSpec: &swarm.ContainerSpec{
  53. Image: "busybox:latest",
  54. Labels: map[string]string{"container-label": "container-value"},
  55. Command: []string{"/bin/top"},
  56. Args: []string{"-u", "root"},
  57. Hostname: "hostname",
  58. Env: []string{"envvar=envvalue"},
  59. Dir: "/work",
  60. User: "root",
  61. StopSignal: "SIGINT",
  62. StopGracePeriod: &restartDelay,
  63. Hosts: []string{"8.8.8.8 google"},
  64. DNSConfig: &swarm.DNSConfig{
  65. Nameservers: []string{"8.8.8.8"},
  66. Search: []string{"somedomain"},
  67. },
  68. },
  69. RestartPolicy: &swarm.RestartPolicy{
  70. Delay: &restartDelay,
  71. Condition: swarm.RestartPolicyConditionOnFailure,
  72. MaxAttempts: &maxAttempts,
  73. },
  74. Runtime: swarm.RuntimeContainer,
  75. },
  76. Mode: swarm.ServiceMode{
  77. Replicated: &swarm.ReplicatedService{
  78. Replicas: &replicas,
  79. },
  80. },
  81. UpdateConfig: &swarm.UpdateConfig{
  82. Parallelism: 2,
  83. Delay: 200 * time.Second,
  84. FailureAction: swarm.UpdateFailureActionContinue,
  85. Monitor: 2 * time.Second,
  86. MaxFailureRatio: 0.2,
  87. Order: swarm.UpdateOrderStopFirst,
  88. },
  89. RollbackConfig: &swarm.UpdateConfig{
  90. Parallelism: 3,
  91. Delay: 300 * time.Second,
  92. FailureAction: swarm.UpdateFailureActionPause,
  93. Monitor: 3 * time.Second,
  94. MaxFailureRatio: 0.3,
  95. Order: swarm.UpdateOrderStartFirst,
  96. },
  97. }
  98. }
  99. const defaultSwarmPort = 2477
  100. func newSwarm(t *testing.T) *daemon.Swarm {
  101. d := &daemon.Swarm{
  102. Daemon: daemon.New(t, "", dockerdBinary, daemon.Config{
  103. Experimental: testEnv.DaemonInfo.ExperimentalBuild,
  104. }),
  105. // TODO: better method of finding an unused port
  106. Port: defaultSwarmPort,
  107. }
  108. // TODO: move to a NewSwarm constructor
  109. d.ListenAddr = fmt.Sprintf("0.0.0.0:%d", d.Port)
  110. // avoid networking conflicts
  111. args := []string{"--iptables=false", "--swarm-default-advertise-addr=lo"}
  112. d.StartWithBusybox(t, args...)
  113. require.NoError(t, d.Init(swarm.InitRequest{}))
  114. return d
  115. }
  116. func serviceContainerCount(client client.ServiceAPIClient, id string, count uint64) func(log poll.LogT) poll.Result {
  117. return func(log poll.LogT) poll.Result {
  118. filter := filters.NewArgs()
  119. filter.Add("service", id)
  120. tasks, err := client.TaskList(context.Background(), types.TaskListOptions{
  121. Filters: filter,
  122. })
  123. switch {
  124. case err != nil:
  125. return poll.Error(err)
  126. case len(tasks) == int(count):
  127. return poll.Success()
  128. default:
  129. return poll.Continue("task count at %d waiting for %d", len(tasks), count)
  130. }
  131. }
  132. }