docker_api_containers_test.go 3.0 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. name := "getall"
  16. runCmd := exec.Command(dockerBinary, "run", "--name", name, "busybox", "true")
  17. out, _, err := runCommandWithOutput(runCmd)
  18. if err != nil {
  19. t.Fatalf("Error on container creation: %v, output: %q", err, out)
  20. }
  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 []struct {
  26. Names []string
  27. }
  28. if err = json.Unmarshal(body, &inspectJSON); err != nil {
  29. t.Fatalf("unable to unmarshal response body: %v", err)
  30. }
  31. if len(inspectJSON) != startCount+1 {
  32. t.Fatalf("Expected %d container(s), %d found (started with: %d)", startCount+1, len(inspectJSON), startCount)
  33. }
  34. if actual := inspectJSON[0].Names[0]; actual != "/"+name {
  35. t.Fatalf("Container Name mismatch. Expected: %q, received: %q\n", "/"+name, actual)
  36. }
  37. deleteAllContainers()
  38. logDone("container REST API - check GET json/all=1")
  39. }
  40. func TestContainerApiGetExport(t *testing.T) {
  41. name := "exportcontainer"
  42. runCmd := exec.Command(dockerBinary, "run", "--name", name, "busybox", "touch", "/test")
  43. out, _, err := runCommandWithOutput(runCmd)
  44. if err != nil {
  45. t.Fatalf("Error on container creation: %v, output: %q", err, out)
  46. }
  47. body, err := sockRequest("GET", "/containers/"+name+"/export")
  48. if err != nil {
  49. t.Fatalf("GET containers/export sockRequest failed: %v", err)
  50. }
  51. found := false
  52. for tarReader := tar.NewReader(bytes.NewReader(body)); ; {
  53. h, err := tarReader.Next()
  54. if err != nil {
  55. if err == io.EOF {
  56. break
  57. }
  58. t.Fatal(err)
  59. }
  60. if h.Name == "test" {
  61. found = true
  62. break
  63. }
  64. }
  65. if !found {
  66. t.Fatalf("The created test file has not been found in the exported image")
  67. }
  68. deleteAllContainers()
  69. logDone("container REST API - check GET containers/export")
  70. }
  71. func TestContainerApiGetChanges(t *testing.T) {
  72. name := "changescontainer"
  73. runCmd := exec.Command(dockerBinary, "run", "--name", name, "busybox", "rm", "/etc/passwd")
  74. out, _, err := runCommandWithOutput(runCmd)
  75. if err != nil {
  76. t.Fatalf("Error on container creation: %v, output: %q", err, out)
  77. }
  78. body, err := sockRequest("GET", "/containers/"+name+"/changes")
  79. if err != nil {
  80. t.Fatalf("GET containers/changes sockRequest failed: %v", err)
  81. }
  82. changes := []struct {
  83. Kind int
  84. Path string
  85. }{}
  86. if err = json.Unmarshal(body, &changes); err != nil {
  87. t.Fatalf("unable to unmarshal response body: %v", err)
  88. }
  89. // Check the changelog for removal of /etc/passwd
  90. success := false
  91. for _, elem := range changes {
  92. if elem.Path == "/etc/passwd" && elem.Kind == 2 {
  93. success = true
  94. }
  95. }
  96. if !success {
  97. t.Fatalf("/etc/passwd has been removed but is not present in the diff")
  98. }
  99. deleteAllContainers()
  100. logDone("container REST API - check GET containers/changes")
  101. }