Skip to content

Commit

Permalink
Add support for the health and is-task filters
Browse files Browse the repository at this point in the history
Signed-off-by: Wayne Song <[email protected]>
  • Loading branch information
Wayne Song committed Aug 31, 2017
1 parent b3cca37 commit 1ca94a0
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 1 deletion.
12 changes: 12 additions & 0 deletions api/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,18 @@ func getContainersJSON(c *context, w http.ResponseWriter, r *http.Request) {
if !filters.Match("node", container.Engine.Name) {
continue
}
if !filters.ExactMatch("health", cluster.HealthString(container.Info.State)) {
continue
}
if filters.Include("is-task") {
_, isTask := container.Config.Labels["com.docker.swarm.task"]
if filters.ExactMatch("is-task", "true") && !isTask {
continue
}
if filters.ExactMatch("is-task", "false") && isTask {
continue
}
}
if filters.Include("volume") {
volumeExist := fmt.Errorf("volume mounted in container")
err := filters.WalkValues("volume", func(value string) error {
Expand Down
10 changes: 9 additions & 1 deletion cluster/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

"github.com/docker/docker/api/types"
"github.com/docker/docker/pkg/stringid"
"github.com/docker/go-units"
units "github.com/docker/go-units"
)

// Container is exported
Expand Down Expand Up @@ -43,6 +43,14 @@ func StateString(state *types.ContainerState) string {
return "exited"
}

// HealthString returns a single string to describe health status.
func HealthString(state *types.ContainerState) string {
if state.Health == nil {
return types.NoHealthcheck
}
return state.Health.Status
}

// FullStateString returns human-readable description of the state
func FullStateString(state *types.ContainerState) string {
startedAt, _ := time.Parse(time.RFC3339Nano, state.StartedAt)
Expand Down
53 changes: 53 additions & 0 deletions test/integration/api/ps.bats
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,56 @@ function teardown() {
[[ "${output}" == *"node-1/c2"* ]]
[[ "${output}" != *"node-1/c3"* ]]
}

@test "docker ps --filter is-task" {
start_docker_with_busybox 2
swarm_manage

docker_swarm run --name c1 -e constraint:node==node-0 --label com.docker.swarm.task='' -d busybox:latest sleep 100
docker_swarm run --name c2 -e constraint:node==node-1 -d busybox:latest sleep 100

run docker_swarm ps --filter is-task=true
[ "$status" -eq 0 ]
[[ "${output}" == *"node-0/c1"* ]]
[[ "${output}" != *"node-1/c2"* ]]

run docker_swarm ps --filter is-task=false
[ "$status" -eq 0 ]
[[ "${output}" != *"node-0/c1"* ]]
[[ "${output}" == *"node-1/c2"* ]]
}

@test "docker ps --filter health" {
start_docker_with_busybox 2
swarm_manage

docker_swarm run --name c1 -e constraint:node==node-0 --health-cmd true -d busybox:latest sleep 100
docker_swarm run --name c2 -e constraint:node==node-1 --health-cmd false -d busybox:latest sleep 100
docker_swarm run --name c3 -e constraint:node==node-1 -d busybox:latest sleep 100

run docker_swarm ps --filter health=starting
[ "$status" -eq 0 ]
[[ "${output}" == *"node-0/c1"* ]]
[[ "${output}" == *"node-1/c2"* ]]
[[ "${output}" != *"node-1/c3"* ]]

retry 120 5 eval "docker_swarm ps | grep -q 'unhealthy'"

run docker_swarm ps --filter health=healthy
[ "$status" -eq 0 ]
[[ "${output}" == *"node-0/c1"* ]]
[[ "${output}" != *"node-1/c2"* ]]
[[ "${output}" != *"node-1/c3"* ]]

run docker_swarm ps --filter health=unhealthy
[ "$status" -eq 0 ]
[[ "${output}" != *"node-0/c1"* ]]
[[ "${output}" == *"node-1/c2"* ]]
[[ "${output}" != *"node-1/c3"* ]]

run docker_swarm ps --filter health=none
[ "$status" -eq 0 ]
[[ "${output}" != *"node-0/c1"* ]]
[[ "${output}" != *"node-1/c2"* ]]
[[ "${output}" == *"node-1/c3"* ]]
}

0 comments on commit 1ca94a0

Please sign in to comment.