docker_api_network_test.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. package main
  2. import (
  3. "encoding/json"
  4. "net"
  5. "net/http"
  6. "net/url"
  7. "strings"
  8. "github.com/docker/docker/api/types"
  9. "github.com/docker/docker/pkg/parsers/filters"
  10. "github.com/go-check/check"
  11. )
  12. func (s *DockerSuite) TestApiNetworkGetDefaults(c *check.C) {
  13. // By default docker daemon creates 3 networks. check if they are present
  14. defaults := []string{"bridge", "host", "none"}
  15. for _, nn := range defaults {
  16. c.Assert(isNetworkAvailable(c, nn), check.Equals, true)
  17. }
  18. }
  19. func (s *DockerSuite) TestApiNetworkCreateDelete(c *check.C) {
  20. // Create a network
  21. name := "testnetwork"
  22. id := createNetwork(c, name, true)
  23. c.Assert(isNetworkAvailable(c, name), check.Equals, true)
  24. // POST another network with same name and CheckDuplicate must fail
  25. createNetwork(c, name, false)
  26. // delete the network and make sure it is deleted
  27. deleteNetwork(c, id, true)
  28. c.Assert(isNetworkAvailable(c, name), check.Equals, false)
  29. }
  30. func (s *DockerSuite) TestApiNetworkFilter(c *check.C) {
  31. nr := getNetworkResource(c, getNetworkIDByName(c, "bridge"))
  32. c.Assert(nr.Name, check.Equals, "bridge")
  33. }
  34. func (s *DockerSuite) TestApiNetworkInspect(c *check.C) {
  35. // Inspect default bridge network
  36. nr := getNetworkResource(c, "bridge")
  37. c.Assert(nr.Name, check.Equals, "bridge")
  38. // run a container and attach it to the default bridge network
  39. out, _ := dockerCmd(c, "run", "-d", "--name", "test", "busybox", "top")
  40. containerID := strings.TrimSpace(out)
  41. containerIP := findContainerIP(c, "test")
  42. // inspect default bridge network again and make sure the container is connected
  43. nr = getNetworkResource(c, nr.ID)
  44. c.Assert(len(nr.Containers), check.Equals, 1)
  45. c.Assert(nr.Containers[containerID], check.NotNil)
  46. ip, _, err := net.ParseCIDR(nr.Containers[containerID].IPv4Address)
  47. c.Assert(err, check.IsNil)
  48. c.Assert(ip.String(), check.Equals, containerIP)
  49. }
  50. func (s *DockerSuite) TestApiNetworkConnectDisconnect(c *check.C) {
  51. // Create test network
  52. name := "testnetwork"
  53. id := createNetwork(c, name, true)
  54. nr := getNetworkResource(c, id)
  55. c.Assert(nr.Name, check.Equals, name)
  56. c.Assert(nr.ID, check.Equals, id)
  57. c.Assert(len(nr.Containers), check.Equals, 0)
  58. // run a container
  59. out, _ := dockerCmd(c, "run", "-d", "--name", "test", "busybox", "top")
  60. containerID := strings.TrimSpace(out)
  61. // connect the container to the test network
  62. connectNetwork(c, nr.ID, containerID)
  63. // inspect the network to make sure container is connected
  64. nr = getNetworkResource(c, nr.ID)
  65. c.Assert(len(nr.Containers), check.Equals, 1)
  66. c.Assert(nr.Containers[containerID], check.NotNil)
  67. // check if container IP matches network inspect
  68. ip, _, err := net.ParseCIDR(nr.Containers[containerID].IPv4Address)
  69. c.Assert(err, check.IsNil)
  70. containerIP := findContainerIP(c, "test")
  71. c.Assert(ip.String(), check.Equals, containerIP)
  72. // disconnect container from the network
  73. disconnectNetwork(c, nr.ID, containerID)
  74. nr = getNetworkResource(c, nr.ID)
  75. c.Assert(nr.Name, check.Equals, name)
  76. c.Assert(len(nr.Containers), check.Equals, 0)
  77. // delete the network
  78. deleteNetwork(c, nr.ID, true)
  79. }
  80. func isNetworkAvailable(c *check.C, name string) bool {
  81. status, body, err := sockRequest("GET", "/networks", nil)
  82. c.Assert(status, check.Equals, http.StatusOK)
  83. c.Assert(err, check.IsNil)
  84. nJSON := []types.NetworkResource{}
  85. err = json.Unmarshal(body, &nJSON)
  86. c.Assert(err, check.IsNil)
  87. for _, n := range nJSON {
  88. if n.Name == name {
  89. return true
  90. }
  91. }
  92. return false
  93. }
  94. func getNetworkIDByName(c *check.C, name string) string {
  95. var (
  96. v = url.Values{}
  97. filterArgs = filters.Args{}
  98. )
  99. filterArgs["name"] = []string{name}
  100. filterJSON, err := filters.ToParam(filterArgs)
  101. c.Assert(err, check.IsNil)
  102. v.Set("filters", filterJSON)
  103. status, body, err := sockRequest("GET", "/networks?"+v.Encode(), nil)
  104. c.Assert(status, check.Equals, http.StatusOK)
  105. c.Assert(err, check.IsNil)
  106. nJSON := []types.NetworkResource{}
  107. err = json.Unmarshal(body, &nJSON)
  108. c.Assert(err, check.IsNil)
  109. c.Assert(len(nJSON), check.Equals, 1)
  110. return nJSON[0].ID
  111. }
  112. func getNetworkResource(c *check.C, id string) *types.NetworkResource {
  113. _, obj, err := sockRequest("GET", "/networks/"+id, nil)
  114. c.Assert(err, check.IsNil)
  115. nr := types.NetworkResource{}
  116. err = json.Unmarshal(obj, &nr)
  117. c.Assert(err, check.IsNil)
  118. return &nr
  119. }
  120. func createNetwork(c *check.C, name string, shouldSucceed bool) string {
  121. config := types.NetworkCreate{
  122. Name: name,
  123. Driver: "bridge",
  124. CheckDuplicate: true,
  125. }
  126. status, resp, err := sockRequest("POST", "/networks/create", config)
  127. if !shouldSucceed {
  128. c.Assert(status, check.Not(check.Equals), http.StatusCreated)
  129. return ""
  130. }
  131. c.Assert(status, check.Equals, http.StatusCreated)
  132. c.Assert(err, check.IsNil)
  133. var nr types.NetworkCreateResponse
  134. err = json.Unmarshal(resp, &nr)
  135. c.Assert(err, check.IsNil)
  136. return nr.ID
  137. }
  138. func connectNetwork(c *check.C, nid, cid string) {
  139. config := types.NetworkConnect{
  140. Container: cid,
  141. }
  142. status, _, err := sockRequest("POST", "/networks/"+nid+"/connect", config)
  143. c.Assert(status, check.Equals, http.StatusOK)
  144. c.Assert(err, check.IsNil)
  145. }
  146. func disconnectNetwork(c *check.C, nid, cid string) {
  147. config := types.NetworkConnect{
  148. Container: cid,
  149. }
  150. status, _, err := sockRequest("POST", "/networks/"+nid+"/disconnect", config)
  151. c.Assert(status, check.Equals, http.StatusOK)
  152. c.Assert(err, check.IsNil)
  153. }
  154. func deleteNetwork(c *check.C, id string, shouldSucceed bool) {
  155. status, _, err := sockRequest("DELETE", "/networks/"+id, nil)
  156. if !shouldSucceed {
  157. c.Assert(status, check.Not(check.Equals), http.StatusOK)
  158. c.Assert(err, check.NotNil)
  159. return
  160. }
  161. c.Assert(status, check.Equals, http.StatusOK)
  162. c.Assert(err, check.IsNil)
  163. }