docker_api_containers_test.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. package main
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "io"
  6. "io/ioutil"
  7. "os"
  8. "os/exec"
  9. "strings"
  10. "testing"
  11. "github.com/docker/docker/vendor/src/code.google.com/p/go/src/pkg/archive/tar"
  12. )
  13. func TestContainerApiGetAll(t *testing.T) {
  14. startCount, err := getContainerCount()
  15. if err != nil {
  16. t.Fatalf("Cannot query container count: %v", err)
  17. }
  18. name := "getall"
  19. runCmd := exec.Command(dockerBinary, "run", "--name", name, "busybox", "true")
  20. out, _, err := runCommandWithOutput(runCmd)
  21. if err != nil {
  22. t.Fatalf("Error on container creation: %v, output: %q", err, out)
  23. }
  24. body, err := sockRequest("GET", "/containers/json?all=1", nil)
  25. if err != nil {
  26. t.Fatalf("GET all containers sockRequest failed: %v", err)
  27. }
  28. var inspectJSON []struct {
  29. Names []string
  30. }
  31. if err = json.Unmarshal(body, &inspectJSON); err != nil {
  32. t.Fatalf("unable to unmarshal response body: %v", err)
  33. }
  34. if len(inspectJSON) != startCount+1 {
  35. t.Fatalf("Expected %d container(s), %d found (started with: %d)", startCount+1, len(inspectJSON), startCount)
  36. }
  37. if actual := inspectJSON[0].Names[0]; actual != "/"+name {
  38. t.Fatalf("Container Name mismatch. Expected: %q, received: %q\n", "/"+name, actual)
  39. }
  40. deleteAllContainers()
  41. logDone("container REST API - check GET json/all=1")
  42. }
  43. func TestContainerApiGetExport(t *testing.T) {
  44. name := "exportcontainer"
  45. runCmd := exec.Command(dockerBinary, "run", "--name", name, "busybox", "touch", "/test")
  46. out, _, err := runCommandWithOutput(runCmd)
  47. if err != nil {
  48. t.Fatalf("Error on container creation: %v, output: %q", err, out)
  49. }
  50. body, err := sockRequest("GET", "/containers/"+name+"/export", nil)
  51. if err != nil {
  52. t.Fatalf("GET containers/export sockRequest failed: %v", err)
  53. }
  54. found := false
  55. for tarReader := tar.NewReader(bytes.NewReader(body)); ; {
  56. h, err := tarReader.Next()
  57. if err != nil {
  58. if err == io.EOF {
  59. break
  60. }
  61. t.Fatal(err)
  62. }
  63. if h.Name == "test" {
  64. found = true
  65. break
  66. }
  67. }
  68. if !found {
  69. t.Fatalf("The created test file has not been found in the exported image")
  70. }
  71. deleteAllContainers()
  72. logDone("container REST API - check GET containers/export")
  73. }
  74. func TestContainerApiGetChanges(t *testing.T) {
  75. name := "changescontainer"
  76. runCmd := exec.Command(dockerBinary, "run", "--name", name, "busybox", "rm", "/etc/passwd")
  77. out, _, err := runCommandWithOutput(runCmd)
  78. if err != nil {
  79. t.Fatalf("Error on container creation: %v, output: %q", err, out)
  80. }
  81. body, err := sockRequest("GET", "/containers/"+name+"/changes", nil)
  82. if err != nil {
  83. t.Fatalf("GET containers/changes sockRequest failed: %v", err)
  84. }
  85. changes := []struct {
  86. Kind int
  87. Path string
  88. }{}
  89. if err = json.Unmarshal(body, &changes); err != nil {
  90. t.Fatalf("unable to unmarshal response body: %v", err)
  91. }
  92. // Check the changelog for removal of /etc/passwd
  93. success := false
  94. for _, elem := range changes {
  95. if elem.Path == "/etc/passwd" && elem.Kind == 2 {
  96. success = true
  97. }
  98. }
  99. if !success {
  100. t.Fatalf("/etc/passwd has been removed but is not present in the diff")
  101. }
  102. deleteAllContainers()
  103. logDone("container REST API - check GET containers/changes")
  104. }
  105. func TestContainerApiStartVolumeBinds(t *testing.T) {
  106. defer deleteAllContainers()
  107. name := "testing"
  108. config := map[string]interface{}{
  109. "Image": "busybox",
  110. "Volumes": map[string]struct{}{"/tmp": {}},
  111. }
  112. if _, err := sockRequest("POST", "/containers/create?name="+name, config); err != nil && !strings.Contains(err.Error(), "201 Created") {
  113. t.Fatal(err)
  114. }
  115. bindPath, err := ioutil.TempDir(os.TempDir(), "test")
  116. if err != nil {
  117. t.Fatal(err)
  118. }
  119. config = map[string]interface{}{
  120. "Binds": []string{bindPath + ":/tmp"},
  121. }
  122. if _, err := sockRequest("POST", "/containers/"+name+"/start", config); err != nil && !strings.Contains(err.Error(), "204 No Content") {
  123. t.Fatal(err)
  124. }
  125. pth, err := inspectFieldMap(name, "Volumes", "/tmp")
  126. if err != nil {
  127. t.Fatal(err)
  128. }
  129. if pth != bindPath {
  130. t.Fatalf("expected volume host path to be %s, got %s", bindPath, pth)
  131. }
  132. logDone("container REST API - check volume binds on start")
  133. }
  134. func TestContainerApiStartVolumesFrom(t *testing.T) {
  135. defer deleteAllContainers()
  136. volName := "voltst"
  137. volPath := "/tmp"
  138. if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "-d", "--name", volName, "-v", volPath, "busybox")); err != nil {
  139. t.Fatal(out, err)
  140. }
  141. name := "testing"
  142. config := map[string]interface{}{
  143. "Image": "busybox",
  144. "Volumes": map[string]struct{}{volPath: {}},
  145. }
  146. if _, err := sockRequest("POST", "/containers/create?name="+name, config); err != nil && !strings.Contains(err.Error(), "201 Created") {
  147. t.Fatal(err)
  148. }
  149. config = map[string]interface{}{
  150. "VolumesFrom": []string{volName},
  151. }
  152. if _, err := sockRequest("POST", "/containers/"+name+"/start", config); err != nil && !strings.Contains(err.Error(), "204 No Content") {
  153. t.Fatal(err)
  154. }
  155. pth, err := inspectFieldMap(name, "Volumes", volPath)
  156. if err != nil {
  157. t.Fatal(err)
  158. }
  159. pth2, err := inspectFieldMap(volName, "Volumes", volPath)
  160. if err != nil {
  161. t.Fatal(err)
  162. }
  163. if pth != pth2 {
  164. t.Fatalf("expected volume host path to be %s, got %s", pth, pth2)
  165. }
  166. logDone("container REST API - check VolumesFrom on start")
  167. }
  168. // Ensure that volumes-from has priority over binds/anything else
  169. // This is pretty much the same as TestRunApplyVolumesFromBeforeVolumes, except with passing the VolumesFrom and the bind on start
  170. func TestVolumesFromHasPriority(t *testing.T) {
  171. defer deleteAllContainers()
  172. volName := "voltst"
  173. volPath := "/tmp"
  174. if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "-d", "--name", volName, "-v", volPath, "busybox")); err != nil {
  175. t.Fatal(out, err)
  176. }
  177. name := "testing"
  178. config := map[string]interface{}{
  179. "Image": "busybox",
  180. "Volumes": map[string]struct{}{volPath: {}},
  181. }
  182. if _, err := sockRequest("POST", "/containers/create?name="+name, config); err != nil && !strings.Contains(err.Error(), "201 Created") {
  183. t.Fatal(err)
  184. }
  185. bindPath, err := ioutil.TempDir(os.TempDir(), "test")
  186. if err != nil {
  187. t.Fatal(err)
  188. }
  189. config = map[string]interface{}{
  190. "VolumesFrom": []string{volName},
  191. "Binds": []string{bindPath + ":/tmp"},
  192. }
  193. if _, err := sockRequest("POST", "/containers/"+name+"/start", config); err != nil && !strings.Contains(err.Error(), "204 No Content") {
  194. t.Fatal(err)
  195. }
  196. pth, err := inspectFieldMap(name, "Volumes", volPath)
  197. if err != nil {
  198. t.Fatal(err)
  199. }
  200. pth2, err := inspectFieldMap(volName, "Volumes", volPath)
  201. if err != nil {
  202. t.Fatal(err)
  203. }
  204. if pth != pth2 {
  205. t.Fatalf("expected volume host path to be %s, got %s", pth, pth2)
  206. }
  207. logDone("container REST API - check VolumesFrom has priority")
  208. }