return proper error for inspect and proxy on unhealthy node

Signed-off-by: Victor Vieux <victorvieux@gmail.com>
This commit is contained in:
Victor Vieux 2015-12-10 14:21:04 -08:00 committed by Victor Vieux
parent 744e3a3925
commit 4b33656bb4
3 changed files with 30 additions and 0 deletions

View File

@ -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")

View File

@ -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")
}

View File

@ -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"* ]]
}