Ver Fonte

Test for restarting count

This test is for #10058
Signed-off-by: Alexander Morozov <lk4d4@docker.com>
Alexander Morozov há 10 anos atrás
pai
commit
79d30364c9
2 ficheiros alterados com 29 adições e 5 exclusões
  1. 20 0
      integration-cli/docker_cli_run_test.go
  2. 9 5
      integration-cli/utils.go

+ 20 - 0
integration-cli/docker_cli_run_test.go

@@ -2935,3 +2935,23 @@ func TestRunOOMExitCode(t *testing.T) {
 
 	logDone("run - exit code on oom")
 }
+
+func TestRunRestartMaxRetries(t *testing.T) {
+	defer deleteAllContainers()
+	out, err := exec.Command(dockerBinary, "run", "-d", "--restart=on-failure:3", "busybox", "false").CombinedOutput()
+	if err != nil {
+		t.Fatal(string(out), err)
+	}
+	id := strings.TrimSpace(string(out))
+	if err := waitInspect(id, "{{ .State.Restarting }} {{ .State.Running }}", "false false", 5); err != nil {
+		t.Fatal(err)
+	}
+	count, err := inspectField(id, "RestartCount")
+	if err != nil {
+		t.Fatal(err)
+	}
+	if count != "3" {
+		t.Fatalf("Container was restarted %s times, expected %d", count, 3)
+	}
+	logDone("run - test max-retries for --restart")
+}

+ 9 - 5
integration-cli/utils.go

@@ -135,28 +135,32 @@ func waitForContainer(contID string, args ...string) error {
 }
 
 func waitRun(contID string) error {
-	after := time.After(5 * time.Second)
+	return waitInspect(contID, "{{.State.Running}}", "true", 5)
+}
+
+func waitInspect(name, expr, expected string, timeout int) error {
+	after := time.After(time.Duration(timeout) * time.Second)
 
 	for {
-		cmd := exec.Command(dockerBinary, "inspect", "-f", "{{.State.Running}}", contID)
+		cmd := exec.Command(dockerBinary, "inspect", "-f", expr, name)
 		out, _, err := runCommandWithOutput(cmd)
 		if err != nil {
 			return fmt.Errorf("error executing docker inspect: %v", err)
 		}
 
-		if strings.Contains(out, "true") {
+		out = strings.TrimSpace(out)
+		if out == expected {
 			break
 		}
 
 		select {
 		case <-after:
-			return fmt.Errorf("container did not come up in time")
+			return fmt.Errorf("condition \"%q == %q\" not true in time", out, expected)
 		default:
 		}
 
 		time.Sleep(100 * time.Millisecond)
 	}
-
 	return nil
 }