Skip to content
This repository has been archived by the owner on Feb 1, 2021. It is now read-only.

Commit

Permalink
Merge pull request #468 from vieux/ps_n_l
Browse files Browse the repository at this point in the history
add support for docker ps -l / -n
  • Loading branch information
vieux committed Mar 10, 2015
2 parents d27df69 + 328b9ae commit 7945e0a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
10 changes: 8 additions & 2 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net/http"
"runtime"
"sort"
"strconv"
"strings"

log "github.com/Sirupsen/logrus"
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions test/integration/api.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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"* ]]
}
4 changes: 3 additions & 1 deletion test/integration/helpers.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 7945e0a

Please sign in to comment.