diff --git a/api/api.go b/api/api.go index 74203f4a0f..4179fca68c 100644 --- a/api/api.go +++ b/api/api.go @@ -10,6 +10,7 @@ import ( "net/http" "runtime" "sort" + "strconv" "strings" log "github.com/Sirupsen/logrus" @@ -117,12 +118,13 @@ func getContainersJSON(c *context, w http.ResponseWriter, r *http.Request) { } all := r.Form.Get("all") == "1" + limit, _ := strconv.Atoi(r.Form.Get("limit")) out := []*dockerclient.Container{} for _, container := range c.cluster.Containers() { tmp := (*container).Container // Skip stopped containers unless -a was specified. - if !strings.Contains(tmp.Status, "Up") && !all { + if !strings.Contains(tmp.Status, "Up") && !all && limit <= 0 { continue } // Skip swarm containers unless -a was specified. @@ -151,7 +153,11 @@ func getContainersJSON(c *context, w http.ResponseWriter, r *http.Request) { sort.Sort(sort.Reverse(ContainerSorter(out))) w.Header().Set("Content-Type", "application/json") - json.NewEncoder(w).Encode(out) + if limit > 0 && limit < len(out) { + json.NewEncoder(w).Encode(out[:limit]) + } else { + json.NewEncoder(w).Encode(out) + } } // GET /containers/{name:.*}/json diff --git a/test/integration/api.bats b/test/integration/api.bats index 3f74952379..713b6cf44e 100644 --- a/test/integration/api.bats +++ b/test/integration/api.bats @@ -14,3 +14,26 @@ function teardown() { [ "$status" -eq 0 ] [[ "${lines[1]}" == *"Nodes: 3" ]] } + +@test "docker ps -n 3 should return the 3 last containers, including non running one" { +skip + start_docker 1 + start_manager + run docker_swarm run -d busybox sleep 42 + run docker_swarm run -d busybox false + run docker_swarm run -d busybox true + run docker_swarm ps -a -n 3 + [ "${#lines[@]}" -eq 3 ] +} + +@test "docker ps -l should return the last container, including non running one" { +skip + start_docker 1 + start_manager + run docker_swarm run -d busybox sleep 42 + run docker_swarm run -d busybox false + run docker_swarm run -d busybox true + run docker_swarm ps -l + [ "${#lines[@]}" -eq 2 ] + [[ "${lines[1]}" == *"true"* ]] +} diff --git a/test/integration/helpers.bash b/test/integration/helpers.bash index 1cdb160b75..8fc048a763 100644 --- a/test/integration/helpers.bash +++ b/test/integration/helpers.bash @@ -28,10 +28,12 @@ function swarm() { # Waits until the given docker engine API becomes reachable. function wait_until_reachable() { local attempts=0 - until docker -H $1 info &> /dev/null || [ $attempts -ge 10 ]; do + local max_attempts=5 + until docker -H $1 info || [ $attempts -ge $max_attempts ]; do echo "Attempt to connect to ${HOSTS[$i]} failed for the $((++attempts)) time" >&2 sleep 0.5 done + [[ $attempts -lt $max_attempts ]] } # Start the swarm manager in background.