daemon_swarm.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "strings"
  7. "time"
  8. "github.com/docker/docker/pkg/integration/checker"
  9. "github.com/docker/engine-api/types"
  10. "github.com/docker/engine-api/types/filters"
  11. "github.com/docker/engine-api/types/swarm"
  12. "github.com/go-check/check"
  13. )
  14. // SwarmDaemon is a test daemon with helpers for participating in a swarm.
  15. type SwarmDaemon struct {
  16. *Daemon
  17. swarm.Info
  18. port int
  19. listenAddr string
  20. }
  21. // Init initializes a new swarm cluster.
  22. func (d *SwarmDaemon) Init(req swarm.InitRequest) error {
  23. if req.ListenAddr == "" {
  24. req.ListenAddr = d.listenAddr
  25. }
  26. status, out, err := d.SockRequest("POST", "/swarm/init", req)
  27. if status != http.StatusOK {
  28. return fmt.Errorf("initializing swarm: invalid statuscode %v, %q", status, out)
  29. }
  30. if err != nil {
  31. return fmt.Errorf("initializing swarm: %v", err)
  32. }
  33. info, err := d.info()
  34. if err != nil {
  35. return err
  36. }
  37. d.Info = info
  38. return nil
  39. }
  40. // Join joins a daemon to an existing cluster.
  41. func (d *SwarmDaemon) Join(req swarm.JoinRequest) error {
  42. if req.ListenAddr == "" {
  43. req.ListenAddr = d.listenAddr
  44. }
  45. status, out, err := d.SockRequest("POST", "/swarm/join", req)
  46. if status != http.StatusOK {
  47. return fmt.Errorf("joining swarm: invalid statuscode %v, %q", status, out)
  48. }
  49. if err != nil {
  50. return fmt.Errorf("joining swarm: %v", err)
  51. }
  52. info, err := d.info()
  53. if err != nil {
  54. return err
  55. }
  56. d.Info = info
  57. return nil
  58. }
  59. // Leave forces daemon to leave current cluster.
  60. func (d *SwarmDaemon) Leave(force bool) error {
  61. url := "/swarm/leave"
  62. if force {
  63. url += "?force=1"
  64. }
  65. status, out, err := d.SockRequest("POST", url, nil)
  66. if status != http.StatusOK {
  67. return fmt.Errorf("leaving swarm: invalid statuscode %v, %q", status, out)
  68. }
  69. if err != nil {
  70. err = fmt.Errorf("leaving swarm: %v", err)
  71. }
  72. return err
  73. }
  74. func (d *SwarmDaemon) info() (swarm.Info, error) {
  75. var info struct {
  76. Swarm swarm.Info
  77. }
  78. status, dt, err := d.SockRequest("GET", "/info", nil)
  79. if status != http.StatusOK {
  80. return info.Swarm, fmt.Errorf("get swarm info: invalid statuscode %v", status)
  81. }
  82. if err != nil {
  83. return info.Swarm, fmt.Errorf("get swarm info: %v", err)
  84. }
  85. if err := json.Unmarshal(dt, &info); err != nil {
  86. return info.Swarm, err
  87. }
  88. return info.Swarm, nil
  89. }
  90. type serviceConstructor func(*swarm.Service)
  91. type nodeConstructor func(*swarm.Node)
  92. type specConstructor func(*swarm.Spec)
  93. func (d *SwarmDaemon) createService(c *check.C, f ...serviceConstructor) string {
  94. var service swarm.Service
  95. for _, fn := range f {
  96. fn(&service)
  97. }
  98. status, out, err := d.SockRequest("POST", "/services/create", service.Spec)
  99. c.Assert(err, checker.IsNil)
  100. c.Assert(status, checker.Equals, http.StatusCreated, check.Commentf("output: %q", string(out)))
  101. var scr types.ServiceCreateResponse
  102. c.Assert(json.Unmarshal(out, &scr), checker.IsNil)
  103. return scr.ID
  104. }
  105. func (d *SwarmDaemon) getService(c *check.C, id string) *swarm.Service {
  106. var service swarm.Service
  107. status, out, err := d.SockRequest("GET", "/services/"+id, nil)
  108. c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
  109. c.Assert(err, checker.IsNil)
  110. c.Assert(json.Unmarshal(out, &service), checker.IsNil)
  111. return &service
  112. }
  113. func (d *SwarmDaemon) getServiceTasks(c *check.C, service string) []swarm.Task {
  114. var tasks []swarm.Task
  115. filterArgs := filters.NewArgs()
  116. filterArgs.Add("desired-state", "running")
  117. filterArgs.Add("service", service)
  118. filters, err := filters.ToParam(filterArgs)
  119. c.Assert(err, checker.IsNil)
  120. status, out, err := d.SockRequest("GET", "/tasks?filters="+filters, nil)
  121. c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
  122. c.Assert(err, checker.IsNil, check.Commentf(string(out)))
  123. c.Assert(json.Unmarshal(out, &tasks), checker.IsNil)
  124. return tasks
  125. }
  126. func (d *SwarmDaemon) checkRunningTaskImages(c *check.C) (interface{}, check.CommentInterface) {
  127. var tasks []swarm.Task
  128. filterArgs := filters.NewArgs()
  129. filterArgs.Add("desired-state", "running")
  130. filters, err := filters.ToParam(filterArgs)
  131. c.Assert(err, checker.IsNil)
  132. status, out, err := d.SockRequest("GET", "/tasks?filters="+filters, nil)
  133. c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
  134. c.Assert(err, checker.IsNil, check.Commentf(string(out)))
  135. c.Assert(json.Unmarshal(out, &tasks), checker.IsNil)
  136. result := make(map[string]int)
  137. for _, task := range tasks {
  138. if task.Status.State == swarm.TaskStateRunning {
  139. result[task.Spec.ContainerSpec.Image]++
  140. }
  141. }
  142. return result, nil
  143. }
  144. func (d *SwarmDaemon) checkNodeReadyCount(c *check.C) (interface{}, check.CommentInterface) {
  145. nodes := d.listNodes(c)
  146. var readyCount int
  147. for _, node := range nodes {
  148. if node.Status.State == swarm.NodeStateReady {
  149. readyCount++
  150. }
  151. }
  152. return readyCount, nil
  153. }
  154. func (d *SwarmDaemon) getTask(c *check.C, id string) swarm.Task {
  155. var task swarm.Task
  156. status, out, err := d.SockRequest("GET", "/tasks/"+id, nil)
  157. c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
  158. c.Assert(err, checker.IsNil, check.Commentf(string(out)))
  159. c.Assert(json.Unmarshal(out, &task), checker.IsNil)
  160. return task
  161. }
  162. func (d *SwarmDaemon) updateService(c *check.C, service *swarm.Service, f ...serviceConstructor) {
  163. for _, fn := range f {
  164. fn(service)
  165. }
  166. url := fmt.Sprintf("/services/%s/update?version=%d", service.ID, service.Version.Index)
  167. status, out, err := d.SockRequest("POST", url, service.Spec)
  168. c.Assert(err, checker.IsNil)
  169. c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
  170. }
  171. func (d *SwarmDaemon) removeService(c *check.C, id string) {
  172. status, out, err := d.SockRequest("DELETE", "/services/"+id, nil)
  173. c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
  174. c.Assert(err, checker.IsNil)
  175. }
  176. func (d *SwarmDaemon) getNode(c *check.C, id string) *swarm.Node {
  177. var node swarm.Node
  178. status, out, err := d.SockRequest("GET", "/nodes/"+id, nil)
  179. c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
  180. c.Assert(err, checker.IsNil)
  181. c.Assert(json.Unmarshal(out, &node), checker.IsNil)
  182. c.Assert(node.ID, checker.Equals, id)
  183. return &node
  184. }
  185. func (d *SwarmDaemon) updateNode(c *check.C, id string, f ...nodeConstructor) {
  186. for i := 0; ; i++ {
  187. node := d.getNode(c, id)
  188. for _, fn := range f {
  189. fn(node)
  190. }
  191. url := fmt.Sprintf("/nodes/%s/update?version=%d", node.ID, node.Version.Index)
  192. status, out, err := d.SockRequest("POST", url, node.Spec)
  193. if i < 10 && strings.Contains(string(out), "update out of sequence") {
  194. time.Sleep(100 * time.Millisecond)
  195. continue
  196. }
  197. c.Assert(err, checker.IsNil)
  198. c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
  199. return
  200. }
  201. }
  202. func (d *SwarmDaemon) listNodes(c *check.C) []swarm.Node {
  203. status, out, err := d.SockRequest("GET", "/nodes", nil)
  204. c.Assert(err, checker.IsNil)
  205. c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
  206. nodes := []swarm.Node{}
  207. c.Assert(json.Unmarshal(out, &nodes), checker.IsNil)
  208. return nodes
  209. }
  210. func (d *SwarmDaemon) listServices(c *check.C) []swarm.Service {
  211. status, out, err := d.SockRequest("GET", "/services", nil)
  212. c.Assert(err, checker.IsNil)
  213. c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
  214. services := []swarm.Service{}
  215. c.Assert(json.Unmarshal(out, &services), checker.IsNil)
  216. return services
  217. }
  218. func (d *SwarmDaemon) updateSwarm(c *check.C, f ...specConstructor) {
  219. var sw swarm.Swarm
  220. status, out, err := d.SockRequest("GET", "/swarm", nil)
  221. c.Assert(err, checker.IsNil)
  222. c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
  223. c.Assert(json.Unmarshal(out, &sw), checker.IsNil)
  224. for _, fn := range f {
  225. fn(&sw.Spec)
  226. }
  227. url := fmt.Sprintf("/swarm/update?version=%d", sw.Version.Index)
  228. status, out, err = d.SockRequest("POST", url, sw.Spec)
  229. c.Assert(err, checker.IsNil)
  230. c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
  231. }
  232. func (d *SwarmDaemon) rotateTokens(c *check.C) {
  233. var sw swarm.Swarm
  234. status, out, err := d.SockRequest("GET", "/swarm", nil)
  235. c.Assert(err, checker.IsNil)
  236. c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
  237. c.Assert(json.Unmarshal(out, &sw), checker.IsNil)
  238. url := fmt.Sprintf("/swarm/update?version=%d&rotateWorkerToken=true&rotateManagerToken=true", sw.Version.Index)
  239. status, out, err = d.SockRequest("POST", url, sw.Spec)
  240. c.Assert(err, checker.IsNil)
  241. c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
  242. }
  243. func (d *SwarmDaemon) joinTokens(c *check.C) swarm.JoinTokens {
  244. var sw swarm.Swarm
  245. status, out, err := d.SockRequest("GET", "/swarm", nil)
  246. c.Assert(err, checker.IsNil)
  247. c.Assert(status, checker.Equals, http.StatusOK, check.Commentf("output: %q", string(out)))
  248. c.Assert(json.Unmarshal(out, &sw), checker.IsNil)
  249. return sw.JoinTokens
  250. }
  251. func (d *SwarmDaemon) checkLocalNodeState(c *check.C) (interface{}, check.CommentInterface) {
  252. info, err := d.info()
  253. c.Assert(err, checker.IsNil)
  254. return info.LocalNodeState, nil
  255. }
  256. func (d *SwarmDaemon) checkControlAvailable(c *check.C) (interface{}, check.CommentInterface) {
  257. info, err := d.info()
  258. c.Assert(err, checker.IsNil)
  259. c.Assert(info.LocalNodeState, checker.Equals, swarm.LocalNodeStateActive)
  260. return info.ControlAvailable, nil
  261. }