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

add support for docker ps -l / -n #468

Merged
merged 1 commit into from
Mar 10, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens when no limit is provided? Does it default to 0?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes


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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Maybe the limit could be checked outside the for loop since it doesn't make sense to iterate over all containers if limit is 0.

Then again, I think it would be a useless optimization: limit of 0 is a strange limit and it doesn't make much sense to optimize for this.

In short: up to you :) feel free to discard this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes it does, if limit is 0 it means no limit

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