From 4b33656bb460ada714409ece90f8414eab3657c6 Mon Sep 17 00:00:00 2001 From: Victor Vieux Date: Thu, 10 Dec 2015 14:21:04 -0800 Subject: [PATCH] return proper error for inspect and proxy on unhealthy node Signed-off-by: Victor Vieux --- api/handlers.go | 6 ++++++ api/utils.go | 5 +++++ test/integration/api/logs.bats | 19 +++++++++++++++++++ 3 files changed, 30 insertions(+) diff --git a/api/handlers.go b/api/handlers.go index e31ee3967a..02ffed3965 100644 --- a/api/handlers.go +++ b/api/handlers.go @@ -376,6 +376,12 @@ func getContainerJSON(c *context, w http.ResponseWriter, r *http.Request) { httpError(w, fmt.Sprintf("No such container %s", name), http.StatusNotFound) return } + + if !container.Engine.IsHealthy() { + httpError(w, fmt.Sprintf("Container %s running on unhealthy node %s", name, container.Engine.Name), http.StatusNotFound) + return + } + client, scheme := newClientAndScheme(c.tlsConfig) resp, err := client.Get(scheme + "://" + container.Engine.Addr + "/containers/" + container.Id + "/json") diff --git a/api/utils.go b/api/utils.go index c65191ef5d..7d5a7efc57 100644 --- a/api/utils.go +++ b/api/utils.go @@ -64,10 +64,14 @@ func newClientAndScheme(tlsConfig *tls.Config) (*http.Client, string) { func getContainerFromVars(c *context, vars map[string]string) (string, *cluster.Container, error) { if name, ok := vars["name"]; ok { if container := c.cluster.Container(name); container != nil { + if !container.Engine.IsHealthy() { + return name, nil, fmt.Errorf("Container %s running on unhealthy node %s", name, container.Engine.Name) + } return name, container, nil } return name, nil, fmt.Errorf("No such container: %s", name) } + if ID, ok := vars["execid"]; ok { for _, container := range c.cluster.Containers() { for _, execID := range container.Info.ExecIDs { @@ -78,6 +82,7 @@ func getContainerFromVars(c *context, vars map[string]string) (string, *cluster. } return "", nil, fmt.Errorf("Exec %s not found", ID) } + return "", nil, errors.New("Not found") } diff --git a/test/integration/api/logs.bats b/test/integration/api/logs.bats index be54421ec9..38672c8b81 100644 --- a/test/integration/api/logs.bats +++ b/test/integration/api/logs.bats @@ -27,3 +27,22 @@ function teardown() { [[ "${lines[1]}" == *"hello docker"* ]] [[ "${lines[2]}" == *"hello swarm"* ]] } + +@test "docker logs unhealthy node" { + start_docker_with_busybox 1 + swarm_manage --engine-refresh-min-interval=1s --engine-refresh-max-interval=1s --engine-refresh-retry=1 ${HOSTS[0]} + + # run a container with echo command + docker_swarm run -d --name test_container busybox /bin/sh -c "echo hello world; echo hello docker; echo hello swarm" + + # Stop node-0 + docker_host stop ${DOCKER_CONTAINERS[0]} + + retry 5 1 eval "docker_swarm info | grep -q 'Unhealthy'" + + + # verify + run docker_swarm logs test_container + [ "$status" -eq 1 ] + [[ "${output}" == *" running on unhealthy node"* ]] +}