inspect_test.go 4.2 KB

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