An initial start to migration of the API tests from integration to
the integration-cli model.
Docker-DCO-1.1-Signed-off-by: Phil Estes <estesp@linux.vnet.ibm.com>
(github: estesp)
| 1 | 1 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,122 @@ |
| 0 |
+package main |
|
| 1 |
+ |
|
| 2 |
+import ( |
|
| 3 |
+ "bytes" |
|
| 4 |
+ "encoding/json" |
|
| 5 |
+ "io" |
|
| 6 |
+ "os/exec" |
|
| 7 |
+ "testing" |
|
| 8 |
+ |
|
| 9 |
+ "github.com/docker/docker/vendor/src/code.google.com/p/go/src/pkg/archive/tar" |
|
| 10 |
+) |
|
| 11 |
+ |
|
| 12 |
+func TestContainerApiGetAll(t *testing.T) {
|
|
| 13 |
+ startCount, err := getContainerCount() |
|
| 14 |
+ if err != nil {
|
|
| 15 |
+ t.Fatalf("Cannot query container count: %v", err)
|
|
| 16 |
+ } |
|
| 17 |
+ |
|
| 18 |
+ runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "true") |
|
| 19 |
+ out, _, err := runCommandWithOutput(runCmd) |
|
| 20 |
+ if err != nil {
|
|
| 21 |
+ t.Fatalf("Error on container creation: %v, output: %q", err, out)
|
|
| 22 |
+ } |
|
| 23 |
+ |
|
| 24 |
+ testContainerId := stripTrailingCharacters(out) |
|
| 25 |
+ |
|
| 26 |
+ body, err := sockRequest("GET", "/containers/json?all=1")
|
|
| 27 |
+ if err != nil {
|
|
| 28 |
+ t.Fatalf("GET all containers sockRequest failed: %v", err)
|
|
| 29 |
+ } |
|
| 30 |
+ |
|
| 31 |
+ var inspectJSON []map[string]interface{}
|
|
| 32 |
+ if err = json.Unmarshal(body, &inspectJSON); err != nil {
|
|
| 33 |
+ t.Fatalf("unable to unmarshal response body: %v", err)
|
|
| 34 |
+ } |
|
| 35 |
+ |
|
| 36 |
+ if len(inspectJSON) != startCount+1 {
|
|
| 37 |
+ t.Fatalf("Expected %d container(s), %d found (started with: %d)", startCount+1, len(inspectJSON), startCount)
|
|
| 38 |
+ } |
|
| 39 |
+ if id, _ := inspectJSON[0]["Id"]; id != testContainerId {
|
|
| 40 |
+ t.Fatalf("Container ID mismatch. Expected: %s, received: %s\n", testContainerId, id)
|
|
| 41 |
+ } |
|
| 42 |
+ |
|
| 43 |
+ deleteAllContainers() |
|
| 44 |
+ |
|
| 45 |
+ logDone("container REST API - check GET json/all=1")
|
|
| 46 |
+} |
|
| 47 |
+ |
|
| 48 |
+func TestContainerApiGetExport(t *testing.T) {
|
|
| 49 |
+ runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "touch", "/test") |
|
| 50 |
+ out, _, err := runCommandWithOutput(runCmd) |
|
| 51 |
+ if err != nil {
|
|
| 52 |
+ t.Fatalf("Error on container creation: %v, output: %q", err, out)
|
|
| 53 |
+ } |
|
| 54 |
+ |
|
| 55 |
+ testContainerId := stripTrailingCharacters(out) |
|
| 56 |
+ |
|
| 57 |
+ body, err := sockRequest("GET", "/containers/"+testContainerId+"/export")
|
|
| 58 |
+ if err != nil {
|
|
| 59 |
+ t.Fatalf("GET containers/export sockRequest failed: %v", err)
|
|
| 60 |
+ } |
|
| 61 |
+ |
|
| 62 |
+ found := false |
|
| 63 |
+ for tarReader := tar.NewReader(bytes.NewReader(body)); ; {
|
|
| 64 |
+ h, err := tarReader.Next() |
|
| 65 |
+ if err != nil {
|
|
| 66 |
+ if err == io.EOF {
|
|
| 67 |
+ break |
|
| 68 |
+ } |
|
| 69 |
+ t.Fatal(err) |
|
| 70 |
+ } |
|
| 71 |
+ if h.Name == "test" {
|
|
| 72 |
+ found = true |
|
| 73 |
+ break |
|
| 74 |
+ } |
|
| 75 |
+ } |
|
| 76 |
+ |
|
| 77 |
+ if !found {
|
|
| 78 |
+ t.Fatalf("The created test file has not been found in the exported image")
|
|
| 79 |
+ } |
|
| 80 |
+ deleteAllContainers() |
|
| 81 |
+ |
|
| 82 |
+ logDone("container REST API - check GET containers/export")
|
|
| 83 |
+} |
|
| 84 |
+ |
|
| 85 |
+func TestContainerApiGetChanges(t *testing.T) {
|
|
| 86 |
+ runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "rm", "/etc/passwd") |
|
| 87 |
+ out, _, err := runCommandWithOutput(runCmd) |
|
| 88 |
+ if err != nil {
|
|
| 89 |
+ t.Fatalf("Error on container creation: %v, output: %q", err, out)
|
|
| 90 |
+ } |
|
| 91 |
+ |
|
| 92 |
+ testContainerId := stripTrailingCharacters(out) |
|
| 93 |
+ |
|
| 94 |
+ body, err := sockRequest("GET", "/containers/"+testContainerId+"/changes")
|
|
| 95 |
+ if err != nil {
|
|
| 96 |
+ t.Fatalf("GET containers/changes sockRequest failed: %v", err)
|
|
| 97 |
+ } |
|
| 98 |
+ |
|
| 99 |
+ changes := []struct {
|
|
| 100 |
+ Kind int |
|
| 101 |
+ Path string |
|
| 102 |
+ }{}
|
|
| 103 |
+ if err = json.Unmarshal(body, &changes); err != nil {
|
|
| 104 |
+ t.Fatalf("unable to unmarshal response body: %v", err)
|
|
| 105 |
+ } |
|
| 106 |
+ |
|
| 107 |
+ // Check the changelog for removal of /etc/passwd |
|
| 108 |
+ success := false |
|
| 109 |
+ for _, elem := range changes {
|
|
| 110 |
+ if elem.Path == "/etc/passwd" && elem.Kind == 2 {
|
|
| 111 |
+ success = true |
|
| 112 |
+ } |
|
| 113 |
+ } |
|
| 114 |
+ if !success {
|
|
| 115 |
+ t.Fatalf("/etc/passwd has been removed but is not present in the diff")
|
|
| 116 |
+ } |
|
| 117 |
+ |
|
| 118 |
+ deleteAllContainers() |
|
| 119 |
+ |
|
| 120 |
+ logDone("container REST API - check GET containers/changes")
|
|
| 121 |
+} |
| ... | ... |
@@ -21,100 +21,6 @@ import ( |
| 21 | 21 |
"github.com/docker/docker/vendor/src/code.google.com/p/go/src/pkg/archive/tar" |
| 22 | 22 |
) |
| 23 | 23 |
|
| 24 |
-func TestGetContainersJSON(t *testing.T) {
|
|
| 25 |
- eng := NewTestEngine(t) |
|
| 26 |
- defer mkDaemonFromEngine(eng, t).Nuke() |
|
| 27 |
- |
|
| 28 |
- job := eng.Job("containers")
|
|
| 29 |
- job.SetenvBool("all", true)
|
|
| 30 |
- outs, err := job.Stdout.AddTable() |
|
| 31 |
- if err != nil {
|
|
| 32 |
- t.Fatal(err) |
|
| 33 |
- } |
|
| 34 |
- if err := job.Run(); err != nil {
|
|
| 35 |
- t.Fatal(err) |
|
| 36 |
- } |
|
| 37 |
- beginLen := len(outs.Data) |
|
| 38 |
- |
|
| 39 |
- containerID := createTestContainer(eng, &runconfig.Config{
|
|
| 40 |
- Image: unitTestImageID, |
|
| 41 |
- Cmd: []string{"echo", "test"},
|
|
| 42 |
- }, t) |
|
| 43 |
- |
|
| 44 |
- if containerID == "" {
|
|
| 45 |
- t.Fatalf("Received empty container ID")
|
|
| 46 |
- } |
|
| 47 |
- |
|
| 48 |
- req, err := http.NewRequest("GET", "/containers/json?all=1", nil)
|
|
| 49 |
- if err != nil {
|
|
| 50 |
- t.Fatal(err) |
|
| 51 |
- } |
|
| 52 |
- |
|
| 53 |
- r := httptest.NewRecorder() |
|
| 54 |
- if err := server.ServeRequest(eng, api.APIVERSION, r, req); err != nil {
|
|
| 55 |
- t.Fatal(err) |
|
| 56 |
- } |
|
| 57 |
- assertHttpNotError(r, t) |
|
| 58 |
- containers := engine.NewTable("", 0)
|
|
| 59 |
- if _, err := containers.ReadListFrom(r.Body.Bytes()); err != nil {
|
|
| 60 |
- t.Fatal(err) |
|
| 61 |
- } |
|
| 62 |
- if len(containers.Data) != beginLen+1 {
|
|
| 63 |
- t.Fatalf("Expected %d container, %d found (started with: %d)", beginLen+1, len(containers.Data), beginLen)
|
|
| 64 |
- } |
|
| 65 |
- if id := containers.Data[0].Get("Id"); id != containerID {
|
|
| 66 |
- t.Fatalf("Container ID mismatch. Expected: %s, received: %s\n", containerID, id)
|
|
| 67 |
- } |
|
| 68 |
-} |
|
| 69 |
- |
|
| 70 |
-func TestGetContainersExport(t *testing.T) {
|
|
| 71 |
- eng := NewTestEngine(t) |
|
| 72 |
- defer mkDaemonFromEngine(eng, t).Nuke() |
|
| 73 |
- |
|
| 74 |
- // Create a container and remove a file |
|
| 75 |
- containerID := createTestContainer(eng, |
|
| 76 |
- &runconfig.Config{
|
|
| 77 |
- Image: unitTestImageID, |
|
| 78 |
- Cmd: []string{"touch", "/test"},
|
|
| 79 |
- }, |
|
| 80 |
- t, |
|
| 81 |
- ) |
|
| 82 |
- containerRun(eng, containerID, t) |
|
| 83 |
- |
|
| 84 |
- r := httptest.NewRecorder() |
|
| 85 |
- |
|
| 86 |
- req, err := http.NewRequest("GET", "/containers/"+containerID+"/export", nil)
|
|
| 87 |
- if err != nil {
|
|
| 88 |
- t.Fatal(err) |
|
| 89 |
- } |
|
| 90 |
- if err := server.ServeRequest(eng, api.APIVERSION, r, req); err != nil {
|
|
| 91 |
- t.Fatal(err) |
|
| 92 |
- } |
|
| 93 |
- assertHttpNotError(r, t) |
|
| 94 |
- |
|
| 95 |
- if r.Code != http.StatusOK {
|
|
| 96 |
- t.Fatalf("%d OK expected, received %d\n", http.StatusOK, r.Code)
|
|
| 97 |
- } |
|
| 98 |
- |
|
| 99 |
- found := false |
|
| 100 |
- for tarReader := tar.NewReader(r.Body); ; {
|
|
| 101 |
- h, err := tarReader.Next() |
|
| 102 |
- if err != nil {
|
|
| 103 |
- if err == io.EOF {
|
|
| 104 |
- break |
|
| 105 |
- } |
|
| 106 |
- t.Fatal(err) |
|
| 107 |
- } |
|
| 108 |
- if h.Name == "test" {
|
|
| 109 |
- found = true |
|
| 110 |
- break |
|
| 111 |
- } |
|
| 112 |
- } |
|
| 113 |
- if !found {
|
|
| 114 |
- t.Fatalf("The created test file has not been found in the exported image")
|
|
| 115 |
- } |
|
| 116 |
-} |
|
| 117 |
- |
|
| 118 | 24 |
func TestSaveImageAndThenLoad(t *testing.T) {
|
| 119 | 25 |
eng := NewTestEngine(t) |
| 120 | 26 |
defer mkDaemonFromEngine(eng, t).Nuke() |
| ... | ... |
@@ -186,46 +92,6 @@ func TestSaveImageAndThenLoad(t *testing.T) {
|
| 186 | 186 |
} |
| 187 | 187 |
} |
| 188 | 188 |
|
| 189 |
-func TestGetContainersChanges(t *testing.T) {
|
|
| 190 |
- eng := NewTestEngine(t) |
|
| 191 |
- defer mkDaemonFromEngine(eng, t).Nuke() |
|
| 192 |
- |
|
| 193 |
- // Create a container and remove a file |
|
| 194 |
- containerID := createTestContainer(eng, |
|
| 195 |
- &runconfig.Config{
|
|
| 196 |
- Image: unitTestImageID, |
|
| 197 |
- Cmd: []string{"/bin/rm", "/etc/passwd"},
|
|
| 198 |
- }, |
|
| 199 |
- t, |
|
| 200 |
- ) |
|
| 201 |
- containerRun(eng, containerID, t) |
|
| 202 |
- |
|
| 203 |
- r := httptest.NewRecorder() |
|
| 204 |
- req, err := http.NewRequest("GET", "/containers/"+containerID+"/changes", nil)
|
|
| 205 |
- if err != nil {
|
|
| 206 |
- t.Fatal(err) |
|
| 207 |
- } |
|
| 208 |
- if err := server.ServeRequest(eng, api.APIVERSION, r, req); err != nil {
|
|
| 209 |
- t.Fatal(err) |
|
| 210 |
- } |
|
| 211 |
- assertHttpNotError(r, t) |
|
| 212 |
- outs := engine.NewTable("", 0)
|
|
| 213 |
- if _, err := outs.ReadListFrom(r.Body.Bytes()); err != nil {
|
|
| 214 |
- t.Fatal(err) |
|
| 215 |
- } |
|
| 216 |
- |
|
| 217 |
- // Check the changelog |
|
| 218 |
- success := false |
|
| 219 |
- for _, elem := range outs.Data {
|
|
| 220 |
- if elem.Get("Path") == "/etc/passwd" && elem.GetInt("Kind") == 2 {
|
|
| 221 |
- success = true |
|
| 222 |
- } |
|
| 223 |
- } |
|
| 224 |
- if !success {
|
|
| 225 |
- t.Fatalf("/etc/passwd as been removed but is not present in the diff")
|
|
| 226 |
- } |
|
| 227 |
-} |
|
| 228 |
- |
|
| 229 | 189 |
func TestGetContainersTop(t *testing.T) {
|
| 230 | 190 |
eng := NewTestEngine(t) |
| 231 | 191 |
defer mkDaemonFromEngine(eng, t).Nuke() |