Browse code

Test for restarting count

This test is for #10058
Signed-off-by: Alexander Morozov <lk4d4@docker.com>

Alexander Morozov authored on 2015/01/14 08:19:12
Showing 2 changed files
... ...
@@ -2935,3 +2935,23 @@ func TestRunOOMExitCode(t *testing.T) {
2935 2935
 
2936 2936
 	logDone("run - exit code on oom")
2937 2937
 }
2938
+
2939
+func TestRunRestartMaxRetries(t *testing.T) {
2940
+	defer deleteAllContainers()
2941
+	out, err := exec.Command(dockerBinary, "run", "-d", "--restart=on-failure:3", "busybox", "false").CombinedOutput()
2942
+	if err != nil {
2943
+		t.Fatal(string(out), err)
2944
+	}
2945
+	id := strings.TrimSpace(string(out))
2946
+	if err := waitInspect(id, "{{ .State.Restarting }} {{ .State.Running }}", "false false", 5); err != nil {
2947
+		t.Fatal(err)
2948
+	}
2949
+	count, err := inspectField(id, "RestartCount")
2950
+	if err != nil {
2951
+		t.Fatal(err)
2952
+	}
2953
+	if count != "3" {
2954
+		t.Fatalf("Container was restarted %s times, expected %d", count, 3)
2955
+	}
2956
+	logDone("run - test max-retries for --restart")
2957
+}
... ...
@@ -135,28 +135,32 @@ func waitForContainer(contID string, args ...string) error {
135 135
 }
136 136
 
137 137
 func waitRun(contID string) error {
138
-	after := time.After(5 * time.Second)
138
+	return waitInspect(contID, "{{.State.Running}}", "true", 5)
139
+}
140
+
141
+func waitInspect(name, expr, expected string, timeout int) error {
142
+	after := time.After(time.Duration(timeout) * time.Second)
139 143
 
140 144
 	for {
141
-		cmd := exec.Command(dockerBinary, "inspect", "-f", "{{.State.Running}}", contID)
145
+		cmd := exec.Command(dockerBinary, "inspect", "-f", expr, name)
142 146
 		out, _, err := runCommandWithOutput(cmd)
143 147
 		if err != nil {
144 148
 			return fmt.Errorf("error executing docker inspect: %v", err)
145 149
 		}
146 150
 
147
-		if strings.Contains(out, "true") {
151
+		out = strings.TrimSpace(out)
152
+		if out == expected {
148 153
 			break
149 154
 		}
150 155
 
151 156
 		select {
152 157
 		case <-after:
153
-			return fmt.Errorf("container did not come up in time")
158
+			return fmt.Errorf("condition \"%q == %q\" not true in time", out, expected)
154 159
 		default:
155 160
 		}
156 161
 
157 162
 		time.Sleep(100 * time.Millisecond)
158 163
 	}
159
-
160 164
 	return nil
161 165
 }
162 166