daemon_swarm.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. package daemon // import "github.com/docker/docker/integration-cli/daemon"
  2. import (
  3. "context"
  4. "fmt"
  5. "strings"
  6. "testing"
  7. "github.com/docker/docker/api/types"
  8. "github.com/docker/docker/api/types/filters"
  9. "github.com/docker/docker/api/types/swarm"
  10. "github.com/docker/docker/errdefs"
  11. "gotest.tools/v3/assert"
  12. )
  13. // CheckServiceTasksInState returns the number of tasks with a matching state,
  14. // and optional message substring.
  15. func (d *Daemon) CheckServiceTasksInState(ctx context.Context, service string, state swarm.TaskState, message string) func(*testing.T) (interface{}, string) {
  16. return func(c *testing.T) (interface{}, string) {
  17. tasks := d.GetServiceTasks(ctx, c, service)
  18. var count int
  19. for _, task := range tasks {
  20. if task.Status.State == state {
  21. if message == "" || strings.Contains(task.Status.Message, message) {
  22. count++
  23. }
  24. }
  25. }
  26. return count, ""
  27. }
  28. }
  29. // CheckServiceTasksInStateWithError returns the number of tasks with a matching state,
  30. // and optional message substring.
  31. func (d *Daemon) CheckServiceTasksInStateWithError(ctx context.Context, service string, state swarm.TaskState, errorMessage string) func(*testing.T) (interface{}, string) {
  32. return func(c *testing.T) (interface{}, string) {
  33. tasks := d.GetServiceTasks(ctx, c, service)
  34. var count int
  35. for _, task := range tasks {
  36. if task.Status.State == state {
  37. if errorMessage == "" || strings.Contains(task.Status.Err, errorMessage) {
  38. count++
  39. }
  40. }
  41. }
  42. return count, ""
  43. }
  44. }
  45. // CheckServiceRunningTasks returns the number of running tasks for the specified service
  46. func (d *Daemon) CheckServiceRunningTasks(ctx context.Context, service string) func(*testing.T) (interface{}, string) {
  47. return d.CheckServiceTasksInState(ctx, service, swarm.TaskStateRunning, "")
  48. }
  49. // CheckServiceUpdateState returns the current update state for the specified service
  50. func (d *Daemon) CheckServiceUpdateState(ctx context.Context, service string) func(*testing.T) (interface{}, string) {
  51. return func(c *testing.T) (interface{}, string) {
  52. service := d.GetService(ctx, c, service)
  53. if service.UpdateStatus == nil {
  54. return "", ""
  55. }
  56. return service.UpdateStatus.State, ""
  57. }
  58. }
  59. // CheckPluginRunning returns the runtime state of the plugin
  60. func (d *Daemon) CheckPluginRunning(ctx context.Context, plugin string) func(c *testing.T) (interface{}, string) {
  61. return func(c *testing.T) (interface{}, string) {
  62. apiclient := d.NewClientT(c)
  63. resp, _, err := apiclient.PluginInspectWithRaw(ctx, plugin)
  64. if errdefs.IsNotFound(err) {
  65. return false, fmt.Sprintf("%v", err)
  66. }
  67. assert.NilError(c, err)
  68. return resp.Enabled, fmt.Sprintf("%+v", resp)
  69. }
  70. }
  71. // CheckPluginImage returns the runtime state of the plugin
  72. func (d *Daemon) CheckPluginImage(ctx context.Context, plugin string) func(c *testing.T) (interface{}, string) {
  73. return func(c *testing.T) (interface{}, string) {
  74. apiclient := d.NewClientT(c)
  75. resp, _, err := apiclient.PluginInspectWithRaw(ctx, plugin)
  76. if errdefs.IsNotFound(err) {
  77. return false, fmt.Sprintf("%v", err)
  78. }
  79. assert.NilError(c, err)
  80. return resp.PluginReference, fmt.Sprintf("%+v", resp)
  81. }
  82. }
  83. // CheckServiceTasks returns the number of tasks for the specified service
  84. func (d *Daemon) CheckServiceTasks(ctx context.Context, service string) func(*testing.T) (interface{}, string) {
  85. return func(c *testing.T) (interface{}, string) {
  86. tasks := d.GetServiceTasks(ctx, c, service)
  87. return len(tasks), ""
  88. }
  89. }
  90. // CheckRunningTaskNetworks returns the number of times each network is referenced from a task.
  91. func (d *Daemon) CheckRunningTaskNetworks(ctx context.Context) func(t *testing.T) (interface{}, string) {
  92. return func(t *testing.T) (interface{}, string) {
  93. cli := d.NewClientT(t)
  94. defer cli.Close()
  95. tasks, err := cli.TaskList(ctx, types.TaskListOptions{
  96. Filters: filters.NewArgs(filters.Arg("desired-state", "running")),
  97. })
  98. assert.NilError(t, err)
  99. result := make(map[string]int)
  100. for _, task := range tasks {
  101. for _, network := range task.Spec.Networks {
  102. result[network.Target]++
  103. }
  104. }
  105. return result, ""
  106. }
  107. }
  108. // CheckRunningTaskImages returns the times each image is running as a task.
  109. func (d *Daemon) CheckRunningTaskImages(ctx context.Context) func(t *testing.T) (interface{}, string) {
  110. return func(t *testing.T) (interface{}, string) {
  111. cli := d.NewClientT(t)
  112. defer cli.Close()
  113. tasks, err := cli.TaskList(ctx, types.TaskListOptions{
  114. Filters: filters.NewArgs(filters.Arg("desired-state", "running")),
  115. })
  116. assert.NilError(t, err)
  117. result := make(map[string]int)
  118. for _, task := range tasks {
  119. if task.Status.State == swarm.TaskStateRunning && task.Spec.ContainerSpec != nil {
  120. result[task.Spec.ContainerSpec.Image]++
  121. }
  122. }
  123. return result, ""
  124. }
  125. }
  126. // CheckNodeReadyCount returns the number of ready node on the swarm
  127. func (d *Daemon) CheckNodeReadyCount(ctx context.Context) func(t *testing.T) (interface{}, string) {
  128. return func(t *testing.T) (interface{}, string) {
  129. nodes := d.ListNodes(ctx, t)
  130. var readyCount int
  131. for _, node := range nodes {
  132. if node.Status.State == swarm.NodeStateReady {
  133. readyCount++
  134. }
  135. }
  136. return readyCount, ""
  137. }
  138. }
  139. // CheckLocalNodeState returns the current swarm node state
  140. func (d *Daemon) CheckLocalNodeState(ctx context.Context) func(t *testing.T) (interface{}, string) {
  141. return func(t *testing.T) (interface{}, string) {
  142. info := d.SwarmInfo(ctx, t)
  143. return info.LocalNodeState, ""
  144. }
  145. }
  146. // CheckControlAvailable returns the current swarm control available
  147. func (d *Daemon) CheckControlAvailable(ctx context.Context) func(t *testing.T) (interface{}, string) {
  148. return func(t *testing.T) (interface{}, string) {
  149. info := d.SwarmInfo(ctx, t)
  150. assert.Equal(t, info.LocalNodeState, swarm.LocalNodeStateActive)
  151. return info.ControlAvailable, ""
  152. }
  153. }
  154. // CheckLeader returns whether there is a leader on the swarm or not
  155. func (d *Daemon) CheckLeader(ctx context.Context) func(t *testing.T) (interface{}, string) {
  156. return func(t *testing.T) (interface{}, string) {
  157. cli := d.NewClientT(t)
  158. defer cli.Close()
  159. errList := "could not get node list"
  160. ls, err := cli.NodeList(ctx, types.NodeListOptions{})
  161. if err != nil {
  162. return err, errList
  163. }
  164. for _, node := range ls {
  165. if node.ManagerStatus != nil && node.ManagerStatus.Leader {
  166. return nil, ""
  167. }
  168. }
  169. return fmt.Errorf("no leader"), "could not find leader"
  170. }
  171. }
  172. // CmdRetryOutOfSequence tries the specified command against the current daemon
  173. // up to 10 times, retrying if it encounters an "update out of sequence" error.
  174. func (d *Daemon) CmdRetryOutOfSequence(args ...string) (string, error) {
  175. var (
  176. output string
  177. err error
  178. )
  179. for i := 0; i < 10; i++ {
  180. output, err = d.Cmd(args...)
  181. // error, no error, whatever. if we don't have "update out of
  182. // sequence", we don't retry, we just return.
  183. if !strings.Contains(output, "update out of sequence") {
  184. return output, err
  185. }
  186. }
  187. // otherwise, once all of our attempts have been exhausted, just return
  188. // whatever the last values were.
  189. return output, err
  190. }