Browse code

integ-cli: fix clock skew against remote test daemon

This fixes the `docker events`-related tests as they have been
failing due to clock skew between CI machine and test daemon on
some other machine (even 1-2 seconds of diff causes races as
we pass local time to --since/--until).

If we're running in same host, we keep using time.Now(), otherwise
we read the system time of the daemon from `/info` endpoint.

Fixes pretty much all events-related tests on windows CI.

Signed-off-by: Ahmet Alp Balkan <ahmetalpbalkan@gmail.com>

Ahmet Alp Balkan authored on 2015/03/11 06:19:32
Showing 4 changed files
... ...
@@ -45,7 +45,7 @@ func TestEventsContainerFailStartDie(t *testing.T) {
45 45
 		t.Fatalf("Container run with command blerg should have failed, but it did not")
46 46
 	}
47 47
 
48
-	eventsCmd = exec.Command(dockerBinary, "events", "--since=0", fmt.Sprintf("--until=%d", time.Now().Unix()))
48
+	eventsCmd = exec.Command(dockerBinary, "events", "--since=0", fmt.Sprintf("--until=%d", daemonTime(t).Unix()))
49 49
 	out, _, _ = runCommandWithOutput(eventsCmd)
50 50
 	events := strings.Split(out, "\n")
51 51
 	if len(events) <= 1 {
... ...
@@ -70,7 +70,7 @@ func TestEventsLimit(t *testing.T) {
70 70
 	for i := 0; i < 30; i++ {
71 71
 		dockerCmd(t, "run", "busybox", "echo", strconv.Itoa(i))
72 72
 	}
73
-	eventsCmd := exec.Command(dockerBinary, "events", "--since=0", fmt.Sprintf("--until=%d", time.Now().Unix()))
73
+	eventsCmd := exec.Command(dockerBinary, "events", "--since=0", fmt.Sprintf("--until=%d", daemonTime(t).Unix()))
74 74
 	out, _, _ := runCommandWithOutput(eventsCmd)
75 75
 	events := strings.Split(out, "\n")
76 76
 	nEvents := len(events) - 1
... ...
@@ -82,7 +82,7 @@ func TestEventsLimit(t *testing.T) {
82 82
 
83 83
 func TestEventsContainerEvents(t *testing.T) {
84 84
 	dockerCmd(t, "run", "--rm", "busybox", "true")
85
-	eventsCmd := exec.Command(dockerBinary, "events", "--since=0", fmt.Sprintf("--until=%d", time.Now().Unix()))
85
+	eventsCmd := exec.Command(dockerBinary, "events", "--since=0", fmt.Sprintf("--until=%d", daemonTime(t).Unix()))
86 86
 	out, exitCode, err := runCommandWithOutput(eventsCmd)
87 87
 	if exitCode != 0 || err != nil {
88 88
 		t.Fatalf("Failed to get events with exit code %d: %s", exitCode, err)
... ...
@@ -125,7 +125,7 @@ func TestEventsImageUntagDelete(t *testing.T) {
125 125
 	if err := deleteImages(name); err != nil {
126 126
 		t.Fatal(err)
127 127
 	}
128
-	eventsCmd := exec.Command(dockerBinary, "events", "--since=0", fmt.Sprintf("--until=%d", time.Now().Unix()))
128
+	eventsCmd := exec.Command(dockerBinary, "events", "--since=0", fmt.Sprintf("--until=%d", daemonTime(t).Unix()))
129 129
 	out, exitCode, err := runCommandWithOutput(eventsCmd)
130 130
 	if exitCode != 0 || err != nil {
131 131
 		t.Fatalf("Failed to get events with exit code %d: %s", exitCode, err)
... ...
@@ -148,7 +148,7 @@ func TestEventsImageUntagDelete(t *testing.T) {
148 148
 }
149 149
 
150 150
 func TestEventsImagePull(t *testing.T) {
151
-	since := time.Now().Unix()
151
+	since := daemonTime(t).Unix()
152 152
 
153 153
 	defer deleteImages("hello-world")
154 154
 
... ...
@@ -159,7 +159,7 @@ func TestEventsImagePull(t *testing.T) {
159 159
 
160 160
 	eventsCmd := exec.Command(dockerBinary, "events",
161 161
 		fmt.Sprintf("--since=%d", since),
162
-		fmt.Sprintf("--until=%d", time.Now().Unix()))
162
+		fmt.Sprintf("--until=%d", daemonTime(t).Unix()))
163 163
 	out, _, _ := runCommandWithOutput(eventsCmd)
164 164
 
165 165
 	events := strings.Split(strings.TrimSpace(out), "\n")
... ...
@@ -174,7 +174,7 @@ func TestEventsImagePull(t *testing.T) {
174 174
 
175 175
 func TestEventsImageImport(t *testing.T) {
176 176
 	defer deleteAllContainers()
177
-	since := time.Now().Unix()
177
+	since := daemonTime(t).Unix()
178 178
 
179 179
 	runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "true")
180 180
 	out, _, err := runCommandWithOutput(runCmd)
... ...
@@ -193,7 +193,7 @@ func TestEventsImageImport(t *testing.T) {
193 193
 
194 194
 	eventsCmd := exec.Command(dockerBinary, "events",
195 195
 		fmt.Sprintf("--since=%d", since),
196
-		fmt.Sprintf("--until=%d", time.Now().Unix()))
196
+		fmt.Sprintf("--until=%d", daemonTime(t).Unix()))
197 197
 	out, _, _ = runCommandWithOutput(eventsCmd)
198 198
 
199 199
 	events := strings.Split(strings.TrimSpace(out), "\n")
... ...
@@ -219,7 +219,7 @@ func TestEventsFilters(t *testing.T) {
219 219
 		}
220 220
 	}
221 221
 
222
-	since := time.Now().Unix()
222
+	since := daemonTime(t).Unix()
223 223
 	out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "--rm", "busybox", "true"))
224 224
 	if err != nil {
225 225
 		t.Fatal(out, err)
... ...
@@ -228,13 +228,13 @@ func TestEventsFilters(t *testing.T) {
228 228
 	if err != nil {
229 229
 		t.Fatal(out, err)
230 230
 	}
231
-	out, _, err = runCommandWithOutput(exec.Command(dockerBinary, "events", fmt.Sprintf("--since=%d", since), fmt.Sprintf("--until=%d", time.Now().Unix()), "--filter", "event=die"))
231
+	out, _, err = runCommandWithOutput(exec.Command(dockerBinary, "events", fmt.Sprintf("--since=%d", since), fmt.Sprintf("--until=%d", daemonTime(t).Unix()), "--filter", "event=die"))
232 232
 	if err != nil {
233 233
 		t.Fatalf("Failed to get events: %s", err)
234 234
 	}
235 235
 	parseEvents(out, "die")
236 236
 
237
-	out, _, err = runCommandWithOutput(exec.Command(dockerBinary, "events", fmt.Sprintf("--since=%d", since), fmt.Sprintf("--until=%d", time.Now().Unix()), "--filter", "event=die", "--filter", "event=start"))
237
+	out, _, err = runCommandWithOutput(exec.Command(dockerBinary, "events", fmt.Sprintf("--since=%d", since), fmt.Sprintf("--until=%d", daemonTime(t).Unix()), "--filter", "event=die", "--filter", "event=start"))
238 238
 	if err != nil {
239 239
 		t.Fatalf("Failed to get events: %s", err)
240 240
 	}
... ...
@@ -250,7 +250,7 @@ func TestEventsFilters(t *testing.T) {
250 250
 }
251 251
 
252 252
 func TestEventsFilterImageName(t *testing.T) {
253
-	since := time.Now().Unix()
253
+	since := daemonTime(t).Unix()
254 254
 	defer deleteAllContainers()
255 255
 
256 256
 	out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "--name", "container_1", "-d", "busybox", "true"))
... ...
@@ -266,7 +266,7 @@ func TestEventsFilterImageName(t *testing.T) {
266 266
 	container2 := stripTrailingCharacters(out)
267 267
 
268 268
 	for _, s := range []string{"busybox", "busybox:latest"} {
269
-		eventsCmd := exec.Command(dockerBinary, "events", fmt.Sprintf("--since=%d", since), fmt.Sprintf("--until=%d", time.Now().Unix()), "--filter", fmt.Sprintf("image=%s", s))
269
+		eventsCmd := exec.Command(dockerBinary, "events", fmt.Sprintf("--since=%d", since), fmt.Sprintf("--until=%d", daemonTime(t).Unix()), "--filter", fmt.Sprintf("image=%s", s))
270 270
 		out, _, err := runCommandWithOutput(eventsCmd)
271 271
 		if err != nil {
272 272
 			t.Fatalf("Failed to get events, error: %s(%s)", err, out)
... ...
@@ -294,7 +294,7 @@ func TestEventsFilterImageName(t *testing.T) {
294 294
 }
295 295
 
296 296
 func TestEventsFilterContainerID(t *testing.T) {
297
-	since := time.Now().Unix()
297
+	since := daemonTime(t).Unix()
298 298
 	defer deleteAllContainers()
299 299
 
300 300
 	out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "-d", "busybox", "true"))
... ...
@@ -310,7 +310,7 @@ func TestEventsFilterContainerID(t *testing.T) {
310 310
 	container2 := stripTrailingCharacters(out)
311 311
 
312 312
 	for _, s := range []string{container1, container2, container1[:12], container2[:12]} {
313
-		eventsCmd := exec.Command(dockerBinary, "events", fmt.Sprintf("--since=%d", since), fmt.Sprintf("--until=%d", time.Now().Unix()), "--filter", fmt.Sprintf("container=%s", s))
313
+		eventsCmd := exec.Command(dockerBinary, "events", fmt.Sprintf("--since=%d", since), fmt.Sprintf("--until=%d", daemonTime(t).Unix()), "--filter", fmt.Sprintf("container=%s", s))
314 314
 		out, _, err := runCommandWithOutput(eventsCmd)
315 315
 		if err != nil {
316 316
 			t.Fatalf("Failed to get events, error: %s(%s)", err, out)
... ...
@@ -342,7 +342,7 @@ func TestEventsFilterContainerID(t *testing.T) {
342 342
 }
343 343
 
344 344
 func TestEventsFilterContainerName(t *testing.T) {
345
-	since := time.Now().Unix()
345
+	since := daemonTime(t).Unix()
346 346
 	defer deleteAllContainers()
347 347
 
348 348
 	_, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "--name", "container_1", "busybox", "true"))
... ...
@@ -356,7 +356,7 @@ func TestEventsFilterContainerName(t *testing.T) {
356 356
 	}
357 357
 
358 358
 	for _, s := range []string{"container_1", "container_2"} {
359
-		eventsCmd := exec.Command(dockerBinary, "events", fmt.Sprintf("--since=%d", since), fmt.Sprintf("--until=%d", time.Now().Unix()), "--filter", fmt.Sprintf("container=%s", s))
359
+		eventsCmd := exec.Command(dockerBinary, "events", fmt.Sprintf("--since=%d", since), fmt.Sprintf("--until=%d", daemonTime(t).Unix()), "--filter", fmt.Sprintf("container=%s", s))
360 360
 		out, _, err := runCommandWithOutput(eventsCmd)
361 361
 		if err != nil {
362 362
 			t.Fatalf("Failed to get events, error : %s(%s)", err, out)
... ...
@@ -9,7 +9,6 @@ import (
9 9
 	"os"
10 10
 	"os/exec"
11 11
 	"testing"
12
-	"time"
13 12
 	"unicode"
14 13
 
15 14
 	"github.com/kr/pty"
... ...
@@ -17,11 +16,8 @@ import (
17 17
 
18 18
 // #5979
19 19
 func TestEventsRedirectStdout(t *testing.T) {
20
-
21
-	since := time.Now().Unix()
22
-
20
+	since := daemonTime(t).Unix()
23 21
 	dockerCmd(t, "run", "busybox", "true")
24
-
25 22
 	defer deleteAllContainers()
26 23
 
27 24
 	file, err := ioutil.TempFile("", "")
... ...
@@ -30,7 +26,7 @@ func TestEventsRedirectStdout(t *testing.T) {
30 30
 	}
31 31
 	defer os.Remove(file.Name())
32 32
 
33
-	command := fmt.Sprintf("%s events --since=%d --until=%d > %s", dockerBinary, since, time.Now().Unix(), file.Name())
33
+	command := fmt.Sprintf("%s events --since=%d --until=%d > %s", dockerBinary, since, daemonTime(t).Unix(), file.Name())
34 34
 	_, tty, err := pty.Open()
35 35
 	if err != nil {
36 36
 		t.Fatalf("Could not open pty: %v", err)
... ...
@@ -5,7 +5,6 @@ import (
5 5
 	"os/exec"
6 6
 	"strings"
7 7
 	"testing"
8
-	"time"
9 8
 )
10 9
 
11 10
 func TestPause(t *testing.T) {
... ...
@@ -28,7 +27,7 @@ func TestPause(t *testing.T) {
28 28
 
29 29
 	dockerCmd(t, "unpause", name)
30 30
 
31
-	eventsCmd := exec.Command(dockerBinary, "events", "--since=0", fmt.Sprintf("--until=%d", time.Now().Unix()))
31
+	eventsCmd := exec.Command(dockerBinary, "events", "--since=0", fmt.Sprintf("--until=%d", daemonTime(t).Unix()))
32 32
 	out, _, _ = runCommandWithOutput(eventsCmd)
33 33
 	events := strings.Split(out, "\n")
34 34
 	if len(events) <= 1 {
... ...
@@ -77,7 +76,7 @@ func TestPauseMultipleContainers(t *testing.T) {
77 77
 
78 78
 	dockerCmd(t, append([]string{"unpause"}, containers...)...)
79 79
 
80
-	eventsCmd := exec.Command(dockerBinary, "events", "--since=0", fmt.Sprintf("--until=%d", time.Now().Unix()))
80
+	eventsCmd := exec.Command(dockerBinary, "events", "--since=0", fmt.Sprintf("--until=%d", daemonTime(t).Unix()))
81 81
 	out, _, _ = runCommandWithOutput(eventsCmd)
82 82
 	events := strings.Split(out, "\n")
83 83
 	if len(events) <= len(containers)*3-2 {
... ...
@@ -981,6 +981,32 @@ func readContainerFileWithExec(containerId, filename string) ([]byte, error) {
981 981
 	return []byte(out), err
982 982
 }
983 983
 
984
+// daemonTime provides the current time on the daemon host
985
+func daemonTime(t *testing.T) time.Time {
986
+	if isLocalDaemon {
987
+		return time.Now()
988
+	}
989
+
990
+	body, err := sockRequest("GET", "/info", nil)
991
+	if err != nil {
992
+		t.Fatal("daemonTime: failed to get /info: %v", err)
993
+	}
994
+
995
+	type infoJSON struct {
996
+		SystemTime string
997
+	}
998
+	var info infoJSON
999
+	if err = json.Unmarshal(body, &info); err != nil {
1000
+		t.Fatalf("unable to unmarshal /info response: %v", err)
1001
+	}
1002
+
1003
+	dt, err := time.Parse(time.RFC3339Nano, info.SystemTime)
1004
+	if err != nil {
1005
+		t.Fatal(err)
1006
+	}
1007
+	return dt
1008
+}
1009
+
984 1010
 func setupRegistry(t *testing.T) func() {
985 1011
 	testRequires(t, RegistryHosting)
986 1012
 	reg, err := newTestRegistryV2(t)