From cf1e3ce7c5705b16b7b25a75ef7ed3aa8c8aba2f Mon Sep 17 00:00:00 2001 From: unclejack Date: Wed, 13 Aug 2014 17:30:45 +0300 Subject: [PATCH] integcli: add util to fetch container status Docker-DCO-1.1-Signed-off-by: Cristian Staretu (github: unclejack) Upstream-commit: 5d1cb9d0051c5263e23f06ccb431481f8e5b3d57 Component: engine --- .../engine/integration-cli/docker_utils.go | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/components/engine/integration-cli/docker_utils.go b/components/engine/integration-cli/docker_utils.go index 8fb5074e93..e2bdc5d08d 100644 --- a/components/engine/integration-cli/docker_utils.go +++ b/components/engine/integration-cli/docker_utils.go @@ -486,6 +486,36 @@ func getIDByName(name string) (string, error) { return inspectField(name, "Id") } +// getContainerState returns the exit code of the container +// and true if it's running +// the exit code should be ignored if it's running +func getContainerState(t *testing.T, id string) (int, bool, error) { + var ( + exitStatus int + running bool + ) + out, exitCode, err := dockerCmd(t, "inspect", "--format={{.State.Running}} {{.State.ExitCode}}", id) + if err != nil || exitCode != 0 { + return 0, false, fmt.Errorf("'%s' doesn't exist: %s", id, err) + } + + out = strings.Trim(out, "\n") + splitOutput := strings.Split(out, " ") + if len(splitOutput) != 2 { + return 0, false, fmt.Errorf("failed to get container state: output is broken") + } + if splitOutput[0] == "true" { + running = true + } + if n, err := strconv.Atoi(splitOutput[1]); err == nil { + exitStatus = n + } else { + return 0, false, fmt.Errorf("failed to get container state: couldn't parse integer") + } + + return exitStatus, running, nil +} + func buildImageWithOut(name, dockerfile string, useCache bool) (string, string, error) { args := []string{"build", "-t", name} if !useCache {