Merge pull request #1123 from vieux/add_before_support

add support for --before in ps
This commit is contained in:
Andrea Luzzardi 2015-08-07 17:38:42 -07:00
commit c2efaadbea
2 changed files with 36 additions and 3 deletions

View File

@ -152,8 +152,18 @@ func getContainersJSON(c *context, w http.ResponseWriter, r *http.Request) {
}
// Parse flags.
all := boolValue(r, "all")
limit := intValueOrZero(r, "limit")
var (
all = boolValue(r, "all")
limit = intValueOrZero(r, "limit")
before *cluster.Container
)
if value := r.FormValue("before"); value != "" {
before = c.cluster.Container(value)
if before == nil {
httpError(w, fmt.Sprintf("No such container %s", value), http.StatusNotFound)
return
}
}
// Parse filters.
filters, err := dockerfilters.FromParam(r.Form.Get("filters"))
@ -184,7 +194,7 @@ func getContainersJSON(c *context, w http.ResponseWriter, r *http.Request) {
candidates := []*cluster.Container{}
for _, container := range c.cluster.Containers() {
// Skip stopped containers unless -a was specified.
if !container.Info.State.Running && !all && limit <= 0 {
if !container.Info.State.Running && !all && before == nil && limit <= 0 {
continue
}
@ -232,6 +242,12 @@ func getContainersJSON(c *context, w http.ResponseWriter, r *http.Request) {
// Convert cluster.Container back into dockerclient.Container.
out := []*dockerclient.Container{}
for _, container := range candidates {
if before != nil {
if container.Id == before.Id {
before = nil
}
continue
}
// Create a copy of the underlying dockerclient.Container so we can
// make changes without messing with cluster.Container.
tmp := (*container).Container

View File

@ -45,6 +45,23 @@ function teardown() {
[[ "${lines[1]}" == *"false"* ]]
}
@test "docker ps --before" {
start_docker_with_busybox 2
swarm_manage
docker_swarm run -d --name c1 busybox echo c1
docker_swarm run -d --name c2 busybox echo c2
run docker_swarm ps --before c1
[ "${#lines[@]}" -eq 1 ]
run docker_swarm ps --before c2
[ "${#lines[@]}" -eq 2 ]
run docker_swarm ps --before c3
[ "$status" -eq 1 ]
}
@test "docker ps --filter" {
start_docker_with_busybox 2
swarm_manage