daemon_swarm.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. package daemon
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "strings"
  7. "time"
  8. "github.com/docker/docker/api/types"
  9. "github.com/docker/docker/api/types/filters"
  10. "github.com/docker/docker/api/types/swarm"
  11. "github.com/docker/docker/integration-cli/checker"
  12. "github.com/go-check/check"
  13. "github.com/pkg/errors"
  14. )
  15. // Swarm is a test daemon with helpers for participating in a swarm.
  16. type Swarm struct {
  17. *Daemon
  18. swarm.Info
  19. Port int
  20. ListenAddr string
  21. }
  22. // Init initializes a new swarm cluster.
  23. func (d *Swarm) Init(req swarm.InitRequest) error {
  24. if req.ListenAddr == "" {
  25. req.ListenAddr = d.ListenAddr
  26. }
  27. status, out, err := d.SockRequest("POST", "/swarm/init", req)
  28. if status != http.StatusOK {
  29. return fmt.Errorf("initializing swarm: invalid statuscode %v, %q", status, out)
  30. }
  31. if err != nil {
  32. return fmt.Errorf("initializing swarm: %v", err)
  33. }
  34. info, err := d.SwarmInfo()
  35. if err != nil {
  36. return err
  37. }
  38. d.Info = info
  39. return nil
  40. }
  41. // Join joins a daemon to an existing cluster.
  42. func (d *Swarm) Join(req swarm.JoinRequest) error {
  43. if req.ListenAddr == "" {
  44. req.ListenAddr = d.ListenAddr
  45. }
  46. status, out, err := d.SockRequest("POST", "/swarm/join", req)
  47. if status != http.StatusOK {
  48. return fmt.Errorf("joining swarm: invalid statuscode %v, %q", status, out)
  49. }
  50. if err != nil {
  51. return fmt.Errorf("joining swarm: %v", err)
  52. }
  53. info, err := d.SwarmInfo()
  54. if err != nil {
  55. return err
  56. }
  57. d.Info = info
  58. return nil
  59. }
  60. // Leave forces daemon to leave current cluster.
  61. func (d *Swarm) Leave(force bool) error {
  62. url := "/swarm/leave"
  63. if force {
  64. url += "?force=1"
  65. }
  66. status, out, err := d.SockRequest("POST", url, nil)
  67. if status != http.StatusOK {
  68. return fmt.Errorf("leaving swarm: invalid statuscode %v, %q", status, out)
  69. }
  70. if err != nil {
  71. err = fmt.Errorf("leaving swarm: %v", err)
  72. }
  73. return err
  74. }
  75. // SwarmInfo returns the swarm information of the daemon
  76. func (d *Swarm) SwarmInfo() (swarm.Info, error) {
  77. var info struct {
  78. Swarm swarm.Info
  79. }
  80. status, dt, err := d.SockRequest("GET", "/info", nil)
  81. if status != http.StatusOK {
  82. return info.Swarm, fmt.Errorf("get swarm info: invalid statuscode %v", status)
  83. }
  84. if err != nil {
  85. return info.Swarm, fmt.Errorf("get swarm info: %v", err)
  86. }
  87. if err := json.Unmarshal(dt, &info); err != nil {
  88. return info.Swarm, err
  89. }
  90. return info.Swarm, nil
  91. }
  92. // Unlock tries to unlock a locked swarm
  93. func (d *Swarm) Unlock(req swarm.UnlockRequest) error {
  94. status, out, err := d.SockRequest("POST", "/swarm/unlock", req)
  95. if status != http.StatusOK {
  96. return fmt.Errorf("unlocking swarm: invalid statuscode %v, %q", status, out)
  97. }
  98. if err != nil {
  99. err = errors.Wrap(err, "unlocking swarm")
  100. }
  101. return err
  102. }
  103. // ServiceConstructor defines a swarm service constructor function
  104. type ServiceConstructor func(*swarm.Service)
  105. // NodeConstructor defines a swarm node constructor
  106. type NodeConstructor func(*swarm.Node)
  107. // SecretConstructor defines a swarm secret constructor
  108. type SecretConstructor func(*swarm.Secret)
  109. // ConfigConstructor defines a swarm config constructor
  110. type ConfigConstructor func(*swarm.Config)
  111. // SpecConstructor defines a swarm spec constructor
  112. type SpecConstructor func(*swarm.Spec)
  113. // CreateService creates a swarm service given the specified service constructor
  114. func (d *Swarm) CreateService(c *check.C, f ...ServiceConstructor) string {
  115. var service swarm.Service
  116. for _, fn := range f {
  117. fn(&service)
  118. }
  119. status, out, err := d.SockRequest("POST", "/services/create", service.Spec)
  120. c.Assert(err, checker.IsNil, check.Commentf(string(out)))
  121. c.Assert(status, checker.Equals, http.StatusCreated, check.Commentf("output: %q", string(out)))
  122. var scr types.ServiceCreateResponse
  123. c.Assert(json.Unmarshal(out, &scr), checker.IsNil)
  124. return scr.ID
  125. }
  126. // GetService returns the swarm service corresponding to the specified id
  127. func (d *Swarm) GetService(c *check.C, id string) *swarm.Service {
  128. var service swarm.Service
  129. status, out, err := d.SockRequest("GET", "/services/"+id, nil)
  130. c.Assert(err, checker.IsNil, check.Commentf(string(out)))
  131. c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
  132. c.Assert(json.Unmarshal(out, &service), checker.IsNil)
  133. return &service
  134. }
  135. // GetServiceTasks returns the swarm tasks for the specified service
  136. func (d *Swarm) GetServiceTasks(c *check.C, service string) []swarm.Task {
  137. var tasks []swarm.Task
  138. filterArgs := filters.NewArgs()
  139. filterArgs.Add("desired-state", "running")
  140. filterArgs.Add("service", service)
  141. filters, err := filters.ToParam(filterArgs)
  142. c.Assert(err, checker.IsNil)
  143. status, out, err := d.SockRequest("GET", "/tasks?filters="+filters, nil)
  144. c.Assert(err, checker.IsNil, check.Commentf(string(out)))
  145. c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
  146. c.Assert(json.Unmarshal(out, &tasks), checker.IsNil)
  147. return tasks
  148. }
  149. // CheckServiceTasksInState returns the number of tasks with a matching state,
  150. // and optional message substring.
  151. func (d *Swarm) CheckServiceTasksInState(service string, state swarm.TaskState, message string) func(*check.C) (interface{}, check.CommentInterface) {
  152. return func(c *check.C) (interface{}, check.CommentInterface) {
  153. tasks := d.GetServiceTasks(c, service)
  154. var count int
  155. for _, task := range tasks {
  156. if task.Status.State == state {
  157. if message == "" || strings.Contains(task.Status.Message, message) {
  158. count++
  159. }
  160. }
  161. }
  162. return count, nil
  163. }
  164. }
  165. // CheckServiceRunningTasks returns the number of running tasks for the specified service
  166. func (d *Swarm) CheckServiceRunningTasks(service string) func(*check.C) (interface{}, check.CommentInterface) {
  167. return d.CheckServiceTasksInState(service, swarm.TaskStateRunning, "")
  168. }
  169. // CheckServiceUpdateState returns the current update state for the specified service
  170. func (d *Swarm) CheckServiceUpdateState(service string) func(*check.C) (interface{}, check.CommentInterface) {
  171. return func(c *check.C) (interface{}, check.CommentInterface) {
  172. service := d.GetService(c, service)
  173. if service.UpdateStatus == nil {
  174. return "", nil
  175. }
  176. return service.UpdateStatus.State, nil
  177. }
  178. }
  179. // CheckServiceTasks returns the number of tasks for the specified service
  180. func (d *Swarm) CheckServiceTasks(service string) func(*check.C) (interface{}, check.CommentInterface) {
  181. return func(c *check.C) (interface{}, check.CommentInterface) {
  182. tasks := d.GetServiceTasks(c, service)
  183. return len(tasks), nil
  184. }
  185. }
  186. // CheckRunningTaskNetworks returns the number of times each network is referenced from a task.
  187. func (d *Swarm) CheckRunningTaskNetworks(c *check.C) (interface{}, check.CommentInterface) {
  188. var tasks []swarm.Task
  189. filterArgs := filters.NewArgs()
  190. filterArgs.Add("desired-state", "running")
  191. filters, err := filters.ToParam(filterArgs)
  192. c.Assert(err, checker.IsNil)
  193. status, out, err := d.SockRequest("GET", "/tasks?filters="+filters, nil)
  194. c.Assert(err, checker.IsNil, check.Commentf(string(out)))
  195. c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
  196. c.Assert(json.Unmarshal(out, &tasks), checker.IsNil)
  197. result := make(map[string]int)
  198. for _, task := range tasks {
  199. for _, network := range task.Spec.Networks {
  200. result[network.Target]++
  201. }
  202. }
  203. return result, nil
  204. }
  205. // CheckRunningTaskImages returns the times each image is running as a task.
  206. func (d *Swarm) CheckRunningTaskImages(c *check.C) (interface{}, check.CommentInterface) {
  207. var tasks []swarm.Task
  208. filterArgs := filters.NewArgs()
  209. filterArgs.Add("desired-state", "running")
  210. filters, err := filters.ToParam(filterArgs)
  211. c.Assert(err, checker.IsNil)
  212. status, out, err := d.SockRequest("GET", "/tasks?filters="+filters, nil)
  213. c.Assert(err, checker.IsNil, check.Commentf(string(out)))
  214. c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
  215. c.Assert(json.Unmarshal(out, &tasks), checker.IsNil)
  216. result := make(map[string]int)
  217. for _, task := range tasks {
  218. if task.Status.State == swarm.TaskStateRunning {
  219. result[task.Spec.ContainerSpec.Image]++
  220. }
  221. }
  222. return result, nil
  223. }
  224. // CheckNodeReadyCount returns the number of ready node on the swarm
  225. func (d *Swarm) CheckNodeReadyCount(c *check.C) (interface{}, check.CommentInterface) {
  226. nodes := d.ListNodes(c)
  227. var readyCount int
  228. for _, node := range nodes {
  229. if node.Status.State == swarm.NodeStateReady {
  230. readyCount++
  231. }
  232. }
  233. return readyCount, nil
  234. }
  235. // GetTask returns the swarm task identified by the specified id
  236. func (d *Swarm) GetTask(c *check.C, id string) swarm.Task {
  237. var task swarm.Task
  238. status, out, err := d.SockRequest("GET", "/tasks/"+id, nil)
  239. c.Assert(err, checker.IsNil, check.Commentf(string(out)))
  240. c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
  241. c.Assert(json.Unmarshal(out, &task), checker.IsNil)
  242. return task
  243. }
  244. // UpdateService updates a swarm service with the specified service constructor
  245. func (d *Swarm) UpdateService(c *check.C, service *swarm.Service, f ...ServiceConstructor) {
  246. for _, fn := range f {
  247. fn(service)
  248. }
  249. url := fmt.Sprintf("/services/%s/update?version=%d", service.ID, service.Version.Index)
  250. status, out, err := d.SockRequest("POST", url, service.Spec)
  251. c.Assert(err, checker.IsNil, check.Commentf(string(out)))
  252. c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
  253. }
  254. // RemoveService removes the specified service
  255. func (d *Swarm) RemoveService(c *check.C, id string) {
  256. status, out, err := d.SockRequest("DELETE", "/services/"+id, nil)
  257. c.Assert(err, checker.IsNil, check.Commentf(string(out)))
  258. c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
  259. }
  260. // GetNode returns a swarm node identified by the specified id
  261. func (d *Swarm) GetNode(c *check.C, id string) *swarm.Node {
  262. var node swarm.Node
  263. status, out, err := d.SockRequest("GET", "/nodes/"+id, nil)
  264. c.Assert(err, checker.IsNil, check.Commentf(string(out)))
  265. c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
  266. c.Assert(json.Unmarshal(out, &node), checker.IsNil)
  267. c.Assert(node.ID, checker.Equals, id)
  268. return &node
  269. }
  270. // RemoveNode removes the specified node
  271. func (d *Swarm) RemoveNode(c *check.C, id string, force bool) {
  272. url := "/nodes/" + id
  273. if force {
  274. url += "?force=1"
  275. }
  276. status, out, err := d.SockRequest("DELETE", url, nil)
  277. c.Assert(err, checker.IsNil, check.Commentf(string(out)))
  278. c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
  279. }
  280. // UpdateNode updates a swarm node with the specified node constructor
  281. func (d *Swarm) UpdateNode(c *check.C, id string, f ...NodeConstructor) {
  282. for i := 0; ; i++ {
  283. node := d.GetNode(c, id)
  284. for _, fn := range f {
  285. fn(node)
  286. }
  287. url := fmt.Sprintf("/nodes/%s/update?version=%d", node.ID, node.Version.Index)
  288. status, out, err := d.SockRequest("POST", url, node.Spec)
  289. if i < 10 && strings.Contains(string(out), "update out of sequence") {
  290. time.Sleep(100 * time.Millisecond)
  291. continue
  292. }
  293. c.Assert(err, checker.IsNil, check.Commentf(string(out)))
  294. c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
  295. return
  296. }
  297. }
  298. // ListNodes returns the list of the current swarm nodes
  299. func (d *Swarm) ListNodes(c *check.C) []swarm.Node {
  300. status, out, err := d.SockRequest("GET", "/nodes", nil)
  301. c.Assert(err, checker.IsNil, check.Commentf(string(out)))
  302. c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
  303. nodes := []swarm.Node{}
  304. c.Assert(json.Unmarshal(out, &nodes), checker.IsNil)
  305. return nodes
  306. }
  307. // ListServices returns the list of the current swarm services
  308. func (d *Swarm) ListServices(c *check.C) []swarm.Service {
  309. status, out, err := d.SockRequest("GET", "/services", nil)
  310. c.Assert(err, checker.IsNil, check.Commentf(string(out)))
  311. c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
  312. services := []swarm.Service{}
  313. c.Assert(json.Unmarshal(out, &services), checker.IsNil)
  314. return services
  315. }
  316. // CreateSecret creates a secret given the specified spec
  317. func (d *Swarm) CreateSecret(c *check.C, secretSpec swarm.SecretSpec) string {
  318. status, out, err := d.SockRequest("POST", "/secrets/create", secretSpec)
  319. c.Assert(err, checker.IsNil, check.Commentf(string(out)))
  320. c.Assert(status, checker.Equals, http.StatusCreated, check.Commentf("output: %q", string(out)))
  321. var scr types.SecretCreateResponse
  322. c.Assert(json.Unmarshal(out, &scr), checker.IsNil)
  323. return scr.ID
  324. }
  325. // ListSecrets returns the list of the current swarm secrets
  326. func (d *Swarm) ListSecrets(c *check.C) []swarm.Secret {
  327. status, out, err := d.SockRequest("GET", "/secrets", nil)
  328. c.Assert(err, checker.IsNil, check.Commentf(string(out)))
  329. c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
  330. secrets := []swarm.Secret{}
  331. c.Assert(json.Unmarshal(out, &secrets), checker.IsNil)
  332. return secrets
  333. }
  334. // GetSecret returns a swarm secret identified by the specified id
  335. func (d *Swarm) GetSecret(c *check.C, id string) *swarm.Secret {
  336. var secret swarm.Secret
  337. status, out, err := d.SockRequest("GET", "/secrets/"+id, nil)
  338. c.Assert(err, checker.IsNil, check.Commentf(string(out)))
  339. c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
  340. c.Assert(json.Unmarshal(out, &secret), checker.IsNil)
  341. return &secret
  342. }
  343. // DeleteSecret removes the swarm secret identified by the specified id
  344. func (d *Swarm) DeleteSecret(c *check.C, id string) {
  345. status, out, err := d.SockRequest("DELETE", "/secrets/"+id, nil)
  346. c.Assert(err, checker.IsNil, check.Commentf(string(out)))
  347. c.Assert(status, checker.Equals, http.StatusNoContent, check.Commentf("output: %q", string(out)))
  348. }
  349. // UpdateSecret updates the swarm secret identified by the specified id
  350. // Currently, only label update is supported.
  351. func (d *Swarm) UpdateSecret(c *check.C, id string, f ...SecretConstructor) {
  352. secret := d.GetSecret(c, id)
  353. for _, fn := range f {
  354. fn(secret)
  355. }
  356. url := fmt.Sprintf("/secrets/%s/update?version=%d", secret.ID, secret.Version.Index)
  357. status, out, err := d.SockRequest("POST", url, secret.Spec)
  358. c.Assert(err, checker.IsNil, check.Commentf(string(out)))
  359. c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
  360. }
  361. // CreateConfig creates a config given the specified spec
  362. func (d *Swarm) CreateConfig(c *check.C, configSpec swarm.ConfigSpec) string {
  363. status, out, err := d.SockRequest("POST", "/configs/create", configSpec)
  364. c.Assert(err, checker.IsNil, check.Commentf(string(out)))
  365. c.Assert(status, checker.Equals, http.StatusCreated, check.Commentf("output: %q", string(out)))
  366. var scr types.ConfigCreateResponse
  367. c.Assert(json.Unmarshal(out, &scr), checker.IsNil)
  368. return scr.ID
  369. }
  370. // ListConfigs returns the list of the current swarm configs
  371. func (d *Swarm) ListConfigs(c *check.C) []swarm.Config {
  372. status, out, err := d.SockRequest("GET", "/configs", nil)
  373. c.Assert(err, checker.IsNil, check.Commentf(string(out)))
  374. c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
  375. configs := []swarm.Config{}
  376. c.Assert(json.Unmarshal(out, &configs), checker.IsNil)
  377. return configs
  378. }
  379. // GetConfig returns a swarm config identified by the specified id
  380. func (d *Swarm) GetConfig(c *check.C, id string) *swarm.Config {
  381. var config swarm.Config
  382. status, out, err := d.SockRequest("GET", "/configs/"+id, nil)
  383. c.Assert(err, checker.IsNil, check.Commentf(string(out)))
  384. c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
  385. c.Assert(json.Unmarshal(out, &config), checker.IsNil)
  386. return &config
  387. }
  388. // DeleteConfig removes the swarm config identified by the specified id
  389. func (d *Swarm) DeleteConfig(c *check.C, id string) {
  390. status, out, err := d.SockRequest("DELETE", "/configs/"+id, nil)
  391. c.Assert(err, checker.IsNil, check.Commentf(string(out)))
  392. c.Assert(status, checker.Equals, http.StatusNoContent, check.Commentf("output: %q", string(out)))
  393. }
  394. // UpdateConfig updates the swarm config identified by the specified id
  395. // Currently, only label update is supported.
  396. func (d *Swarm) UpdateConfig(c *check.C, id string, f ...ConfigConstructor) {
  397. config := d.GetConfig(c, id)
  398. for _, fn := range f {
  399. fn(config)
  400. }
  401. url := fmt.Sprintf("/configs/%s/update?version=%d", config.ID, config.Version.Index)
  402. status, out, err := d.SockRequest("POST", url, config.Spec)
  403. c.Assert(err, checker.IsNil, check.Commentf(string(out)))
  404. c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
  405. }
  406. // GetSwarm returns the current swarm object
  407. func (d *Swarm) GetSwarm(c *check.C) swarm.Swarm {
  408. var sw swarm.Swarm
  409. status, out, err := d.SockRequest("GET", "/swarm", nil)
  410. c.Assert(err, checker.IsNil, check.Commentf(string(out)))
  411. c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
  412. c.Assert(json.Unmarshal(out, &sw), checker.IsNil)
  413. return sw
  414. }
  415. // UpdateSwarm updates the current swarm object with the specified spec constructors
  416. func (d *Swarm) UpdateSwarm(c *check.C, f ...SpecConstructor) {
  417. sw := d.GetSwarm(c)
  418. for _, fn := range f {
  419. fn(&sw.Spec)
  420. }
  421. url := fmt.Sprintf("/swarm/update?version=%d", sw.Version.Index)
  422. status, out, err := d.SockRequest("POST", url, sw.Spec)
  423. c.Assert(err, checker.IsNil, check.Commentf(string(out)))
  424. c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
  425. }
  426. // RotateTokens update the swarm to rotate tokens
  427. func (d *Swarm) RotateTokens(c *check.C) {
  428. var sw swarm.Swarm
  429. status, out, err := d.SockRequest("GET", "/swarm", nil)
  430. c.Assert(err, checker.IsNil, check.Commentf(string(out)))
  431. c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
  432. c.Assert(json.Unmarshal(out, &sw), checker.IsNil)
  433. url := fmt.Sprintf("/swarm/update?version=%d&rotateWorkerToken=true&rotateManagerToken=true", sw.Version.Index)
  434. status, out, err = d.SockRequest("POST", url, sw.Spec)
  435. c.Assert(err, checker.IsNil, check.Commentf(string(out)))
  436. c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
  437. }
  438. // JoinTokens returns the current swarm join tokens
  439. func (d *Swarm) JoinTokens(c *check.C) swarm.JoinTokens {
  440. var sw swarm.Swarm
  441. status, out, err := d.SockRequest("GET", "/swarm", nil)
  442. c.Assert(err, checker.IsNil, check.Commentf(string(out)))
  443. c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
  444. c.Assert(json.Unmarshal(out, &sw), checker.IsNil)
  445. return sw.JoinTokens
  446. }
  447. // CheckLocalNodeState returns the current swarm node state
  448. func (d *Swarm) CheckLocalNodeState(c *check.C) (interface{}, check.CommentInterface) {
  449. info, err := d.SwarmInfo()
  450. c.Assert(err, checker.IsNil)
  451. return info.LocalNodeState, nil
  452. }
  453. // CheckControlAvailable returns the current swarm control available
  454. func (d *Swarm) CheckControlAvailable(c *check.C) (interface{}, check.CommentInterface) {
  455. info, err := d.SwarmInfo()
  456. c.Assert(err, checker.IsNil)
  457. c.Assert(info.LocalNodeState, checker.Equals, swarm.LocalNodeStateActive)
  458. return info.ControlAvailable, nil
  459. }
  460. // CheckLeader returns whether there is a leader on the swarm or not
  461. func (d *Swarm) CheckLeader(c *check.C) (interface{}, check.CommentInterface) {
  462. errList := check.Commentf("could not get node list")
  463. status, out, err := d.SockRequest("GET", "/nodes", nil)
  464. if err != nil {
  465. return err, errList
  466. }
  467. if status != http.StatusOK {
  468. return fmt.Errorf("expected http status OK, got: %d", status), errList
  469. }
  470. var ls []swarm.Node
  471. if err := json.Unmarshal(out, &ls); err != nil {
  472. return err, errList
  473. }
  474. for _, node := range ls {
  475. if node.ManagerStatus != nil && node.ManagerStatus.Leader {
  476. return nil, nil
  477. }
  478. }
  479. return fmt.Errorf("no leader"), check.Commentf("could not find leader")
  480. }
  481. // CmdRetryOutOfSequence tries the specified command against the current daemon for 10 times
  482. func (d *Swarm) CmdRetryOutOfSequence(args ...string) (string, error) {
  483. for i := 0; ; i++ {
  484. out, err := d.Cmd(args...)
  485. if err != nil {
  486. if strings.Contains(out, "update out of sequence") {
  487. if i < 10 {
  488. continue
  489. }
  490. }
  491. }
  492. return out, err
  493. }
  494. }