daemon_swarm.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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/client"
  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(service string, state swarm.TaskState, message string) func(*testing.T) (interface{}, string) {
  16. return func(c *testing.T) (interface{}, string) {
  17. tasks := d.GetServiceTasks(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(service string, state swarm.TaskState, errorMessage string) func(*testing.T) (interface{}, string) {
  32. return func(c *testing.T) (interface{}, string) {
  33. tasks := d.GetServiceTasks(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(service string) func(*testing.T) (interface{}, string) {
  47. return d.CheckServiceTasksInState(service, swarm.TaskStateRunning, "")
  48. }
  49. // CheckServiceUpdateState returns the current update state for the specified service
  50. func (d *Daemon) CheckServiceUpdateState(service string) func(*testing.T) (interface{}, string) {
  51. return func(c *testing.T) (interface{}, string) {
  52. service := d.GetService(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(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(context.Background(), plugin)
  64. if client.IsErrNotFound(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(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(context.Background(), plugin)
  76. if client.IsErrNotFound(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(service string) func(*testing.T) (interface{}, string) {
  85. return func(c *testing.T) (interface{}, string) {
  86. tasks := d.GetServiceTasks(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(c *testing.T) (interface{}, string) {
  92. cli := d.NewClientT(c)
  93. defer cli.Close()
  94. filterArgs := filters.NewArgs()
  95. filterArgs.Add("desired-state", "running")
  96. options := types.TaskListOptions{
  97. Filters: filterArgs,
  98. }
  99. tasks, err := cli.TaskList(context.Background(), options)
  100. assert.NilError(c, err)
  101. result := make(map[string]int)
  102. for _, task := range tasks {
  103. for _, network := range task.Spec.Networks {
  104. result[network.Target]++
  105. }
  106. }
  107. return result, ""
  108. }
  109. // CheckRunningTaskImages returns the times each image is running as a task.
  110. func (d *Daemon) CheckRunningTaskImages(c *testing.T) (interface{}, string) {
  111. cli := d.NewClientT(c)
  112. defer cli.Close()
  113. filterArgs := filters.NewArgs()
  114. filterArgs.Add("desired-state", "running")
  115. options := types.TaskListOptions{
  116. Filters: filterArgs,
  117. }
  118. tasks, err := cli.TaskList(context.Background(), options)
  119. assert.NilError(c, err)
  120. result := make(map[string]int)
  121. for _, task := range tasks {
  122. if task.Status.State == swarm.TaskStateRunning && task.Spec.ContainerSpec != nil {
  123. result[task.Spec.ContainerSpec.Image]++
  124. }
  125. }
  126. return result, ""
  127. }
  128. // CheckNodeReadyCount returns the number of ready node on the swarm
  129. func (d *Daemon) CheckNodeReadyCount(c *testing.T) (interface{}, string) {
  130. nodes := d.ListNodes(c)
  131. var readyCount int
  132. for _, node := range nodes {
  133. if node.Status.State == swarm.NodeStateReady {
  134. readyCount++
  135. }
  136. }
  137. return readyCount, ""
  138. }
  139. // CheckLocalNodeState returns the current swarm node state
  140. func (d *Daemon) CheckLocalNodeState(c *testing.T) (interface{}, string) {
  141. info := d.SwarmInfo(c)
  142. return info.LocalNodeState, ""
  143. }
  144. // CheckControlAvailable returns the current swarm control available
  145. func (d *Daemon) CheckControlAvailable(c *testing.T) (interface{}, string) {
  146. info := d.SwarmInfo(c)
  147. assert.Equal(c, info.LocalNodeState, swarm.LocalNodeStateActive)
  148. return info.ControlAvailable, ""
  149. }
  150. // CheckLeader returns whether there is a leader on the swarm or not
  151. func (d *Daemon) CheckLeader(c *testing.T) (interface{}, string) {
  152. cli := d.NewClientT(c)
  153. defer cli.Close()
  154. errList := "could not get node list"
  155. ls, err := cli.NodeList(context.Background(), types.NodeListOptions{})
  156. if err != nil {
  157. return err, errList
  158. }
  159. for _, node := range ls {
  160. if node.ManagerStatus != nil && node.ManagerStatus.Leader {
  161. return nil, ""
  162. }
  163. }
  164. return fmt.Errorf("no leader"), "could not find leader"
  165. }
  166. // CmdRetryOutOfSequence tries the specified command against the current daemon
  167. // up to 10 times, retrying if it encounters an "update out of sequence" error.
  168. func (d *Daemon) CmdRetryOutOfSequence(args ...string) (string, error) {
  169. var (
  170. output string
  171. err error
  172. )
  173. for i := 0; i < 10; i++ {
  174. output, err = d.Cmd(args...)
  175. // error, no error, whatever. if we don't have "update out of
  176. // sequence", we don't retry, we just return.
  177. if !strings.Contains(output, "update out of sequence") {
  178. return output, err
  179. }
  180. }
  181. // otherwise, once all of our attempts have been exhausted, just return
  182. // whatever the last values were.
  183. return output, err
  184. }