docker_api_containers_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package main
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "io"
  6. "os/exec"
  7. "testing"
  8. "github.com/docker/docker/vendor/src/code.google.com/p/go/src/pkg/archive/tar"
  9. )
  10. func TestContainerApiGetAll(t *testing.T) {
  11. startCount, err := getContainerCount()
  12. if err != nil {
  13. t.Fatalf("Cannot query container count: %v", err)
  14. }
  15. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "true")
  16. out, _, err := runCommandWithOutput(runCmd)
  17. if err != nil {
  18. t.Fatalf("Error on container creation: %v, output: %q", err, out)
  19. }
  20. testContainerId := stripTrailingCharacters(out)
  21. body, err := sockRequest("GET", "/containers/json?all=1")
  22. if err != nil {
  23. t.Fatalf("GET all containers sockRequest failed: %v", err)
  24. }
  25. var inspectJSON []map[string]interface{}
  26. if err = json.Unmarshal(body, &inspectJSON); err != nil {
  27. t.Fatalf("unable to unmarshal response body: %v", err)
  28. }
  29. if len(inspectJSON) != startCount+1 {
  30. t.Fatalf("Expected %d container(s), %d found (started with: %d)", startCount+1, len(inspectJSON), startCount)
  31. }
  32. if id, _ := inspectJSON[0]["Id"]; id != testContainerId {
  33. t.Fatalf("Container ID mismatch. Expected: %s, received: %s\n", testContainerId, id)
  34. }
  35. deleteAllContainers()
  36. logDone("container REST API - check GET json/all=1")
  37. }
  38. func TestContainerApiGetExport(t *testing.T) {
  39. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "touch", "/test")
  40. out, _, err := runCommandWithOutput(runCmd)
  41. if err != nil {
  42. t.Fatalf("Error on container creation: %v, output: %q", err, out)
  43. }
  44. testContainerId := stripTrailingCharacters(out)
  45. body, err := sockRequest("GET", "/containers/"+testContainerId+"/export")
  46. if err != nil {
  47. t.Fatalf("GET containers/export sockRequest failed: %v", err)
  48. }
  49. found := false
  50. for tarReader := tar.NewReader(bytes.NewReader(body)); ; {
  51. h, err := tarReader.Next()
  52. if err != nil {
  53. if err == io.EOF {
  54. break
  55. }
  56. t.Fatal(err)
  57. }
  58. if h.Name == "test" {
  59. found = true
  60. break
  61. }
  62. }
  63. if !found {
  64. t.Fatalf("The created test file has not been found in the exported image")
  65. }
  66. deleteAllContainers()
  67. logDone("container REST API - check GET containers/export")
  68. }
  69. func TestContainerApiGetChanges(t *testing.T) {
  70. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "rm", "/etc/passwd")
  71. out, _, err := runCommandWithOutput(runCmd)
  72. if err != nil {
  73. t.Fatalf("Error on container creation: %v, output: %q", err, out)
  74. }
  75. testContainerId := stripTrailingCharacters(out)
  76. body, err := sockRequest("GET", "/containers/"+testContainerId+"/changes")
  77. if err != nil {
  78. t.Fatalf("GET containers/changes sockRequest failed: %v", err)
  79. }
  80. changes := []struct {
  81. Kind int
  82. Path string
  83. }{}
  84. if err = json.Unmarshal(body, &changes); err != nil {
  85. t.Fatalf("unable to unmarshal response body: %v", err)
  86. }
  87. // Check the changelog for removal of /etc/passwd
  88. success := false
  89. for _, elem := range changes {
  90. if elem.Path == "/etc/passwd" && elem.Kind == 2 {
  91. success = true
  92. }
  93. }
  94. if !success {
  95. t.Fatalf("/etc/passwd has been removed but is not present in the diff")
  96. }
  97. deleteAllContainers()
  98. logDone("container REST API - check GET containers/changes")
  99. }