1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Move docker ps test to integration cli

Docker-DCO-1.1-Signed-off-by: Fabio Falci <fabiofalci@gmail.com> (github: fabiofalci)
This commit is contained in:
Fabio Falci 2014-07-17 09:29:52 +01:00
parent c11660169a
commit 261dc5e2f6
2 changed files with 201 additions and 115 deletions

View file

@ -0,0 +1,201 @@
package main
import (
"os/exec"
"strings"
"testing"
)
func TestListContainers(t *testing.T) {
runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "top")
out, _, err := runCommandWithOutput(runCmd)
errorOut(err, t, out)
firstID := stripTrailingCharacters(out)
runCmd = exec.Command(dockerBinary, "run", "-d", "busybox", "top")
out, _, err = runCommandWithOutput(runCmd)
errorOut(err, t, out)
secondID := stripTrailingCharacters(out)
// not long running
runCmd = exec.Command(dockerBinary, "run", "-d", "busybox", "true")
out, _, err = runCommandWithOutput(runCmd)
errorOut(err, t, out)
thirdID := stripTrailingCharacters(out)
runCmd = exec.Command(dockerBinary, "run", "-d", "busybox", "top")
out, _, err = runCommandWithOutput(runCmd)
errorOut(err, t, out)
fourthID := stripTrailingCharacters(out)
// make sure third one is not running
runCmd = exec.Command(dockerBinary, "wait", thirdID)
out, _, err = runCommandWithOutput(runCmd)
errorOut(err, t, out)
// all
runCmd = exec.Command(dockerBinary, "ps", "-a")
out, _, err = runCommandWithOutput(runCmd)
errorOut(err, t, out)
if !assertContainerList(out, []string{fourthID, thirdID, secondID, firstID}) {
t.Error("Container list is not in the correct order")
}
// running
runCmd = exec.Command(dockerBinary, "ps")
out, _, err = runCommandWithOutput(runCmd)
errorOut(err, t, out)
if !assertContainerList(out, []string{fourthID, secondID, firstID}) {
t.Error("Container list is not in the correct order")
}
// from here all flag '-a' is ignored
// limit
runCmd = exec.Command(dockerBinary, "ps", "-n=2", "-a")
out, _, err = runCommandWithOutput(runCmd)
errorOut(err, t, out)
expected := []string{fourthID, thirdID}
if !assertContainerList(out, expected) {
t.Error("Container list is not in the correct order")
}
runCmd = exec.Command(dockerBinary, "ps", "-n=2")
out, _, err = runCommandWithOutput(runCmd)
errorOut(err, t, out)
if !assertContainerList(out, expected) {
t.Error("Container list is not in the correct order")
}
// since
runCmd = exec.Command(dockerBinary, "ps", "--since", firstID, "-a")
out, _, err = runCommandWithOutput(runCmd)
errorOut(err, t, out)
expected = []string{fourthID, thirdID, secondID}
if !assertContainerList(out, expected) {
t.Error("Container list is not in the correct order")
}
runCmd = exec.Command(dockerBinary, "ps", "--since", firstID)
out, _, err = runCommandWithOutput(runCmd)
errorOut(err, t, out)
if !assertContainerList(out, expected) {
t.Error("Container list is not in the correct order")
}
// before
runCmd = exec.Command(dockerBinary, "ps", "--before", thirdID, "-a")
out, _, err = runCommandWithOutput(runCmd)
errorOut(err, t, out)
expected = []string{secondID, firstID}
if !assertContainerList(out, expected) {
t.Error("Container list is not in the correct order")
}
runCmd = exec.Command(dockerBinary, "ps", "--before", thirdID)
out, _, err = runCommandWithOutput(runCmd)
errorOut(err, t, out)
if !assertContainerList(out, expected) {
t.Error("Container list is not in the correct order")
}
// since & before
runCmd = exec.Command(dockerBinary, "ps", "--since", firstID, "--before", fourthID, "-a")
out, _, err = runCommandWithOutput(runCmd)
errorOut(err, t, out)
expected = []string{thirdID, secondID}
if !assertContainerList(out, expected) {
t.Error("Container list is not in the correct order")
}
runCmd = exec.Command(dockerBinary, "ps", "--since", firstID, "--before", fourthID)
out, _, err = runCommandWithOutput(runCmd)
errorOut(err, t, out)
if !assertContainerList(out, expected) {
t.Error("Container list is not in the correct order")
}
// since & limit
runCmd = exec.Command(dockerBinary, "ps", "--since", firstID, "-n=2", "-a")
out, _, err = runCommandWithOutput(runCmd)
errorOut(err, t, out)
expected = []string{fourthID, thirdID}
if !assertContainerList(out, expected) {
t.Error("Container list is not in the correct order")
}
runCmd = exec.Command(dockerBinary, "ps", "--since", firstID, "-n=2")
out, _, err = runCommandWithOutput(runCmd)
errorOut(err, t, out)
if !assertContainerList(out, expected) {
t.Error("Container list is not in the correct order")
}
// before & limit
runCmd = exec.Command(dockerBinary, "ps", "--before", fourthID, "-n=1", "-a")
out, _, err = runCommandWithOutput(runCmd)
errorOut(err, t, out)
expected = []string{thirdID}
if !assertContainerList(out, expected) {
t.Error("Container list is not in the correct order")
}
runCmd = exec.Command(dockerBinary, "ps", "--before", fourthID, "-n=1")
out, _, err = runCommandWithOutput(runCmd)
errorOut(err, t, out)
if !assertContainerList(out, expected) {
t.Error("Container list is not in the correct order")
}
// since & before & limit
runCmd = exec.Command(dockerBinary, "ps", "--since", firstID, "--before", fourthID, "-n=1", "-a")
out, _, err = runCommandWithOutput(runCmd)
errorOut(err, t, out)
expected = []string{thirdID}
if !assertContainerList(out, expected) {
t.Error("Container list is not in the correct order")
}
runCmd = exec.Command(dockerBinary, "ps", "--since", firstID, "--before", fourthID, "-n=1")
out, _, err = runCommandWithOutput(runCmd)
errorOut(err, t, out)
if !assertContainerList(out, expected) {
t.Error("Container list is not in the correct order")
}
deleteAllContainers()
logDone("ps - test ps options")
}
func assertContainerList(out string, expected []string) bool {
lines := strings.Split(strings.Trim(out, "\n "), "\n")
if len(lines)-1 != len(expected) {
return false
}
containerIdIndex := strings.Index(lines[0], "CONTAINER ID")
for i := 0; i < len(expected); i++ {
foundID := lines[i+1][containerIdIndex : containerIdIndex+12]
if foundID != expected[i][:12] {
return false
}
}
return true
}

View file

@ -7,7 +7,6 @@ import (
"github.com/dotcloud/docker/engine"
"github.com/dotcloud/docker/runconfig"
"github.com/dotcloud/docker/server"
)
func TestCreateNumberHostname(t *testing.T) {
@ -299,120 +298,6 @@ func TestImagesFilter(t *testing.T) {
}
}
func TestListContainers(t *testing.T) {
eng := NewTestEngine(t)
srv := mkServerFromEngine(eng, t)
defer mkDaemonFromEngine(eng, t).Nuke()
config := runconfig.Config{
Image: unitTestImageID,
Cmd: []string{"/bin/sh", "-c", "cat"},
OpenStdin: true,
}
firstID := createTestContainer(eng, &config, t)
secondID := createTestContainer(eng, &config, t)
thirdID := createTestContainer(eng, &config, t)
fourthID := createTestContainer(eng, &config, t)
defer func() {
containerKill(eng, firstID, t)
containerKill(eng, secondID, t)
containerKill(eng, fourthID, t)
containerWait(eng, firstID, t)
containerWait(eng, secondID, t)
containerWait(eng, fourthID, t)
}()
startContainer(eng, firstID, t)
startContainer(eng, secondID, t)
startContainer(eng, fourthID, t)
// all
if !assertContainerList(srv, true, -1, "", "", []string{fourthID, thirdID, secondID, firstID}) {
t.Error("Container list is not in the correct order")
}
// running
if !assertContainerList(srv, false, -1, "", "", []string{fourthID, secondID, firstID}) {
t.Error("Container list is not in the correct order")
}
// from here 'all' flag is ignored
// limit
expected := []string{fourthID, thirdID}
if !assertContainerList(srv, true, 2, "", "", expected) ||
!assertContainerList(srv, false, 2, "", "", expected) {
t.Error("Container list is not in the correct order")
}
// since
expected = []string{fourthID, thirdID, secondID}
if !assertContainerList(srv, true, -1, firstID, "", expected) ||
!assertContainerList(srv, false, -1, firstID, "", expected) {
t.Error("Container list is not in the correct order")
}
// before
expected = []string{secondID, firstID}
if !assertContainerList(srv, true, -1, "", thirdID, expected) ||
!assertContainerList(srv, false, -1, "", thirdID, expected) {
t.Error("Container list is not in the correct order")
}
// since & before
expected = []string{thirdID, secondID}
if !assertContainerList(srv, true, -1, firstID, fourthID, expected) ||
!assertContainerList(srv, false, -1, firstID, fourthID, expected) {
t.Error("Container list is not in the correct order")
}
// since & limit
expected = []string{fourthID, thirdID}
if !assertContainerList(srv, true, 2, firstID, "", expected) ||
!assertContainerList(srv, false, 2, firstID, "", expected) {
t.Error("Container list is not in the correct order")
}
// before & limit
expected = []string{thirdID}
if !assertContainerList(srv, true, 1, "", fourthID, expected) ||
!assertContainerList(srv, false, 1, "", fourthID, expected) {
t.Error("Container list is not in the correct order")
}
// since & before & limit
expected = []string{thirdID}
if !assertContainerList(srv, true, 1, firstID, fourthID, expected) ||
!assertContainerList(srv, false, 1, firstID, fourthID, expected) {
t.Error("Container list is not in the correct order")
}
}
func assertContainerList(srv *server.Server, all bool, limit int, since, before string, expected []string) bool {
job := srv.Eng.Job("containers")
job.SetenvBool("all", all)
job.SetenvInt("limit", limit)
job.Setenv("since", since)
job.Setenv("before", before)
outs, err := job.Stdout.AddListTable()
if err != nil {
return false
}
if err := job.Run(); err != nil {
return false
}
if len(outs.Data) != len(expected) {
return false
}
for i := 0; i < len(outs.Data); i++ {
if outs.Data[i].Get("Id") != expected[i] {
return false
}
}
return true
}
// Regression test for being able to untag an image with an existing
// container
func TestDeleteTagWithExistingContainers(t *testing.T) {