Browse code

Move `docker ps` test to integration cli

Docker-DCO-1.1-Signed-off-by: Fabio Falci <fabiofalci@gmail.com> (github: fabiofalci)

Fabio Falci authored on 2014/07/17 17:29:52
Showing 2 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,201 @@
0
+package main
1
+
2
+import (
3
+	"os/exec"
4
+	"strings"
5
+	"testing"
6
+)
7
+
8
+func TestListContainers(t *testing.T) {
9
+	runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "top")
10
+	out, _, err := runCommandWithOutput(runCmd)
11
+	errorOut(err, t, out)
12
+	firstID := stripTrailingCharacters(out)
13
+
14
+	runCmd = exec.Command(dockerBinary, "run", "-d", "busybox", "top")
15
+	out, _, err = runCommandWithOutput(runCmd)
16
+	errorOut(err, t, out)
17
+	secondID := stripTrailingCharacters(out)
18
+
19
+	// not long running
20
+	runCmd = exec.Command(dockerBinary, "run", "-d", "busybox", "true")
21
+	out, _, err = runCommandWithOutput(runCmd)
22
+	errorOut(err, t, out)
23
+	thirdID := stripTrailingCharacters(out)
24
+
25
+	runCmd = exec.Command(dockerBinary, "run", "-d", "busybox", "top")
26
+	out, _, err = runCommandWithOutput(runCmd)
27
+	errorOut(err, t, out)
28
+	fourthID := stripTrailingCharacters(out)
29
+
30
+	// make sure third one is not running
31
+	runCmd = exec.Command(dockerBinary, "wait", thirdID)
32
+	out, _, err = runCommandWithOutput(runCmd)
33
+	errorOut(err, t, out)
34
+
35
+	// all
36
+	runCmd = exec.Command(dockerBinary, "ps", "-a")
37
+	out, _, err = runCommandWithOutput(runCmd)
38
+	errorOut(err, t, out)
39
+
40
+	if !assertContainerList(out, []string{fourthID, thirdID, secondID, firstID}) {
41
+		t.Error("Container list is not in the correct order")
42
+	}
43
+
44
+	// running
45
+	runCmd = exec.Command(dockerBinary, "ps")
46
+	out, _, err = runCommandWithOutput(runCmd)
47
+	errorOut(err, t, out)
48
+
49
+	if !assertContainerList(out, []string{fourthID, secondID, firstID}) {
50
+		t.Error("Container list is not in the correct order")
51
+	}
52
+
53
+	// from here all flag '-a' is ignored
54
+
55
+	// limit
56
+	runCmd = exec.Command(dockerBinary, "ps", "-n=2", "-a")
57
+	out, _, err = runCommandWithOutput(runCmd)
58
+	errorOut(err, t, out)
59
+	expected := []string{fourthID, thirdID}
60
+
61
+	if !assertContainerList(out, expected) {
62
+		t.Error("Container list is not in the correct order")
63
+	}
64
+
65
+	runCmd = exec.Command(dockerBinary, "ps", "-n=2")
66
+	out, _, err = runCommandWithOutput(runCmd)
67
+	errorOut(err, t, out)
68
+
69
+	if !assertContainerList(out, expected) {
70
+		t.Error("Container list is not in the correct order")
71
+	}
72
+
73
+	// since
74
+	runCmd = exec.Command(dockerBinary, "ps", "--since", firstID, "-a")
75
+	out, _, err = runCommandWithOutput(runCmd)
76
+	errorOut(err, t, out)
77
+	expected = []string{fourthID, thirdID, secondID}
78
+
79
+	if !assertContainerList(out, expected) {
80
+		t.Error("Container list is not in the correct order")
81
+	}
82
+
83
+	runCmd = exec.Command(dockerBinary, "ps", "--since", firstID)
84
+	out, _, err = runCommandWithOutput(runCmd)
85
+	errorOut(err, t, out)
86
+
87
+	if !assertContainerList(out, expected) {
88
+		t.Error("Container list is not in the correct order")
89
+	}
90
+
91
+	// before
92
+	runCmd = exec.Command(dockerBinary, "ps", "--before", thirdID, "-a")
93
+	out, _, err = runCommandWithOutput(runCmd)
94
+	errorOut(err, t, out)
95
+	expected = []string{secondID, firstID}
96
+
97
+	if !assertContainerList(out, expected) {
98
+		t.Error("Container list is not in the correct order")
99
+	}
100
+
101
+	runCmd = exec.Command(dockerBinary, "ps", "--before", thirdID)
102
+	out, _, err = runCommandWithOutput(runCmd)
103
+	errorOut(err, t, out)
104
+
105
+	if !assertContainerList(out, expected) {
106
+		t.Error("Container list is not in the correct order")
107
+	}
108
+
109
+	// since & before
110
+	runCmd = exec.Command(dockerBinary, "ps", "--since", firstID, "--before", fourthID, "-a")
111
+	out, _, err = runCommandWithOutput(runCmd)
112
+	errorOut(err, t, out)
113
+	expected = []string{thirdID, secondID}
114
+
115
+	if !assertContainerList(out, expected) {
116
+		t.Error("Container list is not in the correct order")
117
+	}
118
+
119
+	runCmd = exec.Command(dockerBinary, "ps", "--since", firstID, "--before", fourthID)
120
+	out, _, err = runCommandWithOutput(runCmd)
121
+	errorOut(err, t, out)
122
+	if !assertContainerList(out, expected) {
123
+		t.Error("Container list is not in the correct order")
124
+	}
125
+
126
+	// since & limit
127
+	runCmd = exec.Command(dockerBinary, "ps", "--since", firstID, "-n=2", "-a")
128
+	out, _, err = runCommandWithOutput(runCmd)
129
+	errorOut(err, t, out)
130
+	expected = []string{fourthID, thirdID}
131
+
132
+	if !assertContainerList(out, expected) {
133
+		t.Error("Container list is not in the correct order")
134
+	}
135
+
136
+	runCmd = exec.Command(dockerBinary, "ps", "--since", firstID, "-n=2")
137
+	out, _, err = runCommandWithOutput(runCmd)
138
+	errorOut(err, t, out)
139
+
140
+	if !assertContainerList(out, expected) {
141
+		t.Error("Container list is not in the correct order")
142
+	}
143
+
144
+	// before & limit
145
+	runCmd = exec.Command(dockerBinary, "ps", "--before", fourthID, "-n=1", "-a")
146
+	out, _, err = runCommandWithOutput(runCmd)
147
+	errorOut(err, t, out)
148
+	expected = []string{thirdID}
149
+
150
+	if !assertContainerList(out, expected) {
151
+		t.Error("Container list is not in the correct order")
152
+	}
153
+
154
+	runCmd = exec.Command(dockerBinary, "ps", "--before", fourthID, "-n=1")
155
+	out, _, err = runCommandWithOutput(runCmd)
156
+	errorOut(err, t, out)
157
+
158
+	if !assertContainerList(out, expected) {
159
+		t.Error("Container list is not in the correct order")
160
+	}
161
+
162
+	// since & before & limit
163
+	runCmd = exec.Command(dockerBinary, "ps", "--since", firstID, "--before", fourthID, "-n=1", "-a")
164
+	out, _, err = runCommandWithOutput(runCmd)
165
+	errorOut(err, t, out)
166
+	expected = []string{thirdID}
167
+
168
+	if !assertContainerList(out, expected) {
169
+		t.Error("Container list is not in the correct order")
170
+	}
171
+
172
+	runCmd = exec.Command(dockerBinary, "ps", "--since", firstID, "--before", fourthID, "-n=1")
173
+	out, _, err = runCommandWithOutput(runCmd)
174
+	errorOut(err, t, out)
175
+
176
+	if !assertContainerList(out, expected) {
177
+		t.Error("Container list is not in the correct order")
178
+	}
179
+
180
+	deleteAllContainers()
181
+
182
+	logDone("ps - test ps options")
183
+}
184
+
185
+func assertContainerList(out string, expected []string) bool {
186
+	lines := strings.Split(strings.Trim(out, "\n "), "\n")
187
+	if len(lines)-1 != len(expected) {
188
+		return false
189
+	}
190
+
191
+	containerIdIndex := strings.Index(lines[0], "CONTAINER ID")
192
+	for i := 0; i < len(expected); i++ {
193
+		foundID := lines[i+1][containerIdIndex : containerIdIndex+12]
194
+		if foundID != expected[i][:12] {
195
+			return false
196
+		}
197
+	}
198
+
199
+	return true
200
+}
... ...
@@ -7,7 +7,6 @@ import (
7 7
 
8 8
 	"github.com/dotcloud/docker/engine"
9 9
 	"github.com/dotcloud/docker/runconfig"
10
-	"github.com/dotcloud/docker/server"
11 10
 )
12 11
 
13 12
 func TestCreateNumberHostname(t *testing.T) {
... ...
@@ -299,120 +298,6 @@ func TestImagesFilter(t *testing.T) {
299 299
 	}
300 300
 }
301 301
 
302
-func TestListContainers(t *testing.T) {
303
-	eng := NewTestEngine(t)
304
-	srv := mkServerFromEngine(eng, t)
305
-	defer mkDaemonFromEngine(eng, t).Nuke()
306
-
307
-	config := runconfig.Config{
308
-		Image:     unitTestImageID,
309
-		Cmd:       []string{"/bin/sh", "-c", "cat"},
310
-		OpenStdin: true,
311
-	}
312
-
313
-	firstID := createTestContainer(eng, &config, t)
314
-	secondID := createTestContainer(eng, &config, t)
315
-	thirdID := createTestContainer(eng, &config, t)
316
-	fourthID := createTestContainer(eng, &config, t)
317
-	defer func() {
318
-		containerKill(eng, firstID, t)
319
-		containerKill(eng, secondID, t)
320
-		containerKill(eng, fourthID, t)
321
-		containerWait(eng, firstID, t)
322
-		containerWait(eng, secondID, t)
323
-		containerWait(eng, fourthID, t)
324
-	}()
325
-
326
-	startContainer(eng, firstID, t)
327
-	startContainer(eng, secondID, t)
328
-	startContainer(eng, fourthID, t)
329
-
330
-	// all
331
-	if !assertContainerList(srv, true, -1, "", "", []string{fourthID, thirdID, secondID, firstID}) {
332
-		t.Error("Container list is not in the correct order")
333
-	}
334
-
335
-	// running
336
-	if !assertContainerList(srv, false, -1, "", "", []string{fourthID, secondID, firstID}) {
337
-		t.Error("Container list is not in the correct order")
338
-	}
339
-
340
-	// from here 'all' flag is ignored
341
-
342
-	// limit
343
-	expected := []string{fourthID, thirdID}
344
-	if !assertContainerList(srv, true, 2, "", "", expected) ||
345
-		!assertContainerList(srv, false, 2, "", "", expected) {
346
-		t.Error("Container list is not in the correct order")
347
-	}
348
-
349
-	// since
350
-	expected = []string{fourthID, thirdID, secondID}
351
-	if !assertContainerList(srv, true, -1, firstID, "", expected) ||
352
-		!assertContainerList(srv, false, -1, firstID, "", expected) {
353
-		t.Error("Container list is not in the correct order")
354
-	}
355
-
356
-	// before
357
-	expected = []string{secondID, firstID}
358
-	if !assertContainerList(srv, true, -1, "", thirdID, expected) ||
359
-		!assertContainerList(srv, false, -1, "", thirdID, expected) {
360
-		t.Error("Container list is not in the correct order")
361
-	}
362
-
363
-	// since & before
364
-	expected = []string{thirdID, secondID}
365
-	if !assertContainerList(srv, true, -1, firstID, fourthID, expected) ||
366
-		!assertContainerList(srv, false, -1, firstID, fourthID, expected) {
367
-		t.Error("Container list is not in the correct order")
368
-	}
369
-
370
-	// since & limit
371
-	expected = []string{fourthID, thirdID}
372
-	if !assertContainerList(srv, true, 2, firstID, "", expected) ||
373
-		!assertContainerList(srv, false, 2, firstID, "", expected) {
374
-		t.Error("Container list is not in the correct order")
375
-	}
376
-
377
-	// before & limit
378
-	expected = []string{thirdID}
379
-	if !assertContainerList(srv, true, 1, "", fourthID, expected) ||
380
-		!assertContainerList(srv, false, 1, "", fourthID, expected) {
381
-		t.Error("Container list is not in the correct order")
382
-	}
383
-
384
-	// since & before & limit
385
-	expected = []string{thirdID}
386
-	if !assertContainerList(srv, true, 1, firstID, fourthID, expected) ||
387
-		!assertContainerList(srv, false, 1, firstID, fourthID, expected) {
388
-		t.Error("Container list is not in the correct order")
389
-	}
390
-}
391
-
392
-func assertContainerList(srv *server.Server, all bool, limit int, since, before string, expected []string) bool {
393
-	job := srv.Eng.Job("containers")
394
-	job.SetenvBool("all", all)
395
-	job.SetenvInt("limit", limit)
396
-	job.Setenv("since", since)
397
-	job.Setenv("before", before)
398
-	outs, err := job.Stdout.AddListTable()
399
-	if err != nil {
400
-		return false
401
-	}
402
-	if err := job.Run(); err != nil {
403
-		return false
404
-	}
405
-	if len(outs.Data) != len(expected) {
406
-		return false
407
-	}
408
-	for i := 0; i < len(outs.Data); i++ {
409
-		if outs.Data[i].Get("Id") != expected[i] {
410
-			return false
411
-		}
412
-	}
413
-	return true
414
-}
415
-
416 302
 // Regression test for being able to untag an image with an existing
417 303
 // container
418 304
 func TestDeleteTagWithExistingContainers(t *testing.T) {