From 2082ff82b581dfbe252338829c1ce7c31797f66c Mon Sep 17 00:00:00 2001 From: HuKeping Date: Thu, 8 Jan 2015 17:15:55 +0800 Subject: [PATCH 1/2] log: Add restart policy name to the inspect information of container Under the restart policy "--restart=no", there is no record about it in the information from docker inspect. To keep it consistent around the three(maybe more in the future) restart policies and distinguish with no restart policy specified cases, it's worth to record it even though it is the default restart policy which will not restart the container. Signed-off-by: Hu Keping --- runconfig/parse.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/runconfig/parse.go b/runconfig/parse.go index 3502270b2..20510660f 100644 --- a/runconfig/parse.go +++ b/runconfig/parse.go @@ -336,18 +336,15 @@ func parseRestartPolicy(policy string) (RestartPolicy, error) { name = parts[0] ) + p.Name = name switch name { case "always": - p.Name = name - if len(parts) == 2 { return p, fmt.Errorf("maximum restart count not valid with restart policy of \"always\"") } case "no": // do nothing case "on-failure": - p.Name = name - if len(parts) == 2 { count, err := strconv.Atoi(parts[1]) if err != nil { From c3ed49dcdb2d835bf4fbdebe3f07318c945282c8 Mon Sep 17 00:00:00 2001 From: HuKeping Date: Fri, 16 Jan 2015 17:58:26 +0800 Subject: [PATCH 2/2] restart: add test for recording restart policy name Add test for recording restart policy name on - restart=no - restart=always - restart=on-failure Signed-off-by: Hu Keping --- integration-cli/docker_cli_restart_test.go | 69 ++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/integration-cli/docker_cli_restart_test.go b/integration-cli/docker_cli_restart_test.go index 3a390ef2c..93821f726 100644 --- a/integration-cli/docker_cli_restart_test.go +++ b/integration-cli/docker_cli_restart_test.go @@ -151,3 +151,72 @@ func TestRestartWithVolumes(t *testing.T) { logDone("restart - does not create a new volume on restart") } + +func TestRecordRestartPolicyNO(t *testing.T) { + defer deleteAllContainers() + + cmd := exec.Command(dockerBinary, "run", "-d", "--restart=no", "busybox", "false") + out, _, err := runCommandWithOutput(cmd) + if err != nil { + t.Fatal(err, out) + } + + id := strings.TrimSpace(string(out)) + name, err := inspectField(id, "HostConfig.RestartPolicy.Name") + if err != nil { + t.Fatal(err, out) + } + if name != "no" { + t.Fatalf("Container restart policy name is %s, expected %s", name, "no") + } + + logDone("restart - recording restart policy name for --restart=no") +} + +func TestRecordRestartPolicyAlways(t *testing.T) { + defer deleteAllContainers() + + cmd := exec.Command(dockerBinary, "run", "-d", "--restart=always", "busybox", "false") + out, _, err := runCommandWithOutput(cmd) + if err != nil { + t.Fatal(err, out) + } + + id := strings.TrimSpace(string(out)) + name, err := inspectField(id, "HostConfig.RestartPolicy.Name") + if err != nil { + t.Fatal(err, out) + } + if name != "always" { + t.Fatalf("Container restart policy name is %s, expected %s", name, "always") + } + + cmd = exec.Command(dockerBinary, "stop", id) + out, _, err = runCommandWithOutput(cmd) + if err != nil { + t.Fatal(err, out) + } + + logDone("restart - recording restart policy name for --restart=always") +} + +func TestRecordRestartPolicyOnFailure(t *testing.T) { + defer deleteAllContainers() + + cmd := exec.Command(dockerBinary, "run", "-d", "--restart=on-failure:1", "busybox", "false") + out, _, err := runCommandWithOutput(cmd) + if err != nil { + t.Fatal(err, out) + } + + id := strings.TrimSpace(string(out)) + name, err := inspectField(id, "HostConfig.RestartPolicy.Name") + if err != nil { + t.Fatal(err, out) + } + if name != "on-failure" { + t.Fatalf("Container restart policy name is %s, expected %s", name, "on-failure") + } + + logDone("restart - recording restart policy name for --restart=on-failure") +}