inspect_test.go 4.3 KB

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