Browse code

fix up Image-name related issues in docker ps and CI

This patch include the following fixs:
- fix image name error when docker ps
- fix docker events test failure: use the exact image name for filter
- fix docker build CI test failure due to "docker events" change

Because of change of daemon log behavior. Now we record
the exact Image name as you typed. So docker run -d busybux sh
and docker run -d busybox:latest are not the same in the log.
So it will affect the docker events. So change the related CI

Signed-off-by: Liu Hua <sdu.liu@huawei.com>

Liu Hua authored on 2015/04/01 23:08:00
Showing 4 changed files
... ...
@@ -7,12 +7,9 @@ import (
7 7
 	"strings"
8 8
 
9 9
 	"github.com/docker/docker/api/types"
10
-	"github.com/docker/docker/graph"
11 10
 	"github.com/docker/docker/nat"
12 11
 	"github.com/docker/docker/pkg/graphdb"
13
-	"github.com/docker/docker/pkg/parsers"
14 12
 	"github.com/docker/docker/pkg/parsers/filters"
15
-	"github.com/docker/docker/utils"
16 13
 )
17 14
 
18 15
 // List returns an array of all containers registered in the daemon.
... ...
@@ -136,12 +133,7 @@ func (daemon *Daemon) Containers(config *ContainersConfig) ([]*types.Container,
136 136
 			ID:    container.ID,
137 137
 			Names: names[container.ID],
138 138
 		}
139
-		img := container.Config.Image
140
-		_, tag := parsers.ParseRepositoryTag(container.Config.Image)
141
-		if tag == "" {
142
-			img = utils.ImageReference(img, graph.DEFAULTTAG)
143
-		}
144
-		newC.Image = img
139
+		newC.Image = container.Config.Image
145 140
 		if len(container.Args) > 0 {
146 141
 			args := []string{}
147 142
 			for _, arg := range container.Args {
... ...
@@ -2009,8 +2009,16 @@ func TestBuildCancelationKillsSleep(t *testing.T) {
2009 2009
 		}()
2010 2010
 
2011 2011
 		var started, died bool
2012
-		matchStart := regexp.MustCompile(" \\(from busybox\\:latest\\) start$")
2013
-		matchDie := regexp.MustCompile(" \\(from busybox\\:latest\\) die$")
2012
+		var imageID string
2013
+
2014
+		if out, err := exec.Command(dockerBinary, "inspect", "-f", "{{.Id}}", "busybox").CombinedOutput(); err != nil {
2015
+			t.Fatalf("failed to get the image ID of busybox: %s, %v", out, err)
2016
+		} else {
2017
+			imageID = strings.TrimSpace(string(out))
2018
+		}
2019
+
2020
+		matchStart := regexp.MustCompile(" \\(from " + imageID + "\\) start$")
2021
+		matchDie := regexp.MustCompile(" \\(from " + imageID + "\\) die$")
2014 2022
 
2015 2023
 		//
2016 2024
 		// Read lines of `docker events` looking for container start and stop.
... ...
@@ -290,7 +290,7 @@ func TestEventsFilterImageName(t *testing.T) {
290 290
 	since := daemonTime(t).Unix()
291 291
 	defer deleteAllContainers()
292 292
 
293
-	out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "--name", "container_1", "-d", "busybox", "true"))
293
+	out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "--name", "container_1", "-d", "busybox:latest", "true"))
294 294
 	if err != nil {
295 295
 		t.Fatal(out, err)
296 296
 	}
... ...
@@ -302,30 +302,30 @@ func TestEventsFilterImageName(t *testing.T) {
302 302
 	}
303 303
 	container2 := strings.TrimSpace(out)
304 304
 
305
-	for _, s := range []string{"busybox", "busybox:latest"} {
306
-		eventsCmd := exec.Command(dockerBinary, "events", fmt.Sprintf("--since=%d", since), fmt.Sprintf("--until=%d", daemonTime(t).Unix()), "--filter", fmt.Sprintf("image=%s", s))
307
-		out, _, err := runCommandWithOutput(eventsCmd)
308
-		if err != nil {
309
-			t.Fatalf("Failed to get events, error: %s(%s)", err, out)
310
-		}
311
-		events := strings.Split(out, "\n")
312
-		events = events[:len(events)-1]
313
-		if len(events) == 0 {
314
-			t.Fatalf("Expected events but found none for the image busybox:latest")
315
-		}
316
-		count1 := 0
317
-		count2 := 0
318
-		for _, e := range events {
319
-			if strings.Contains(e, container1) {
320
-				count1++
321
-			} else if strings.Contains(e, container2) {
322
-				count2++
323
-			}
324
-		}
325
-		if count1 == 0 || count2 == 0 {
326
-			t.Fatalf("Expected events from each container but got %d from %s and %d from %s", count1, container1, count2, container2)
305
+	s := "busybox"
306
+	eventsCmd := exec.Command(dockerBinary, "events", fmt.Sprintf("--since=%d", since), fmt.Sprintf("--until=%d", daemonTime(t).Unix()), "--filter", fmt.Sprintf("image=%s", s))
307
+	out, _, err = runCommandWithOutput(eventsCmd)
308
+	if err != nil {
309
+		t.Fatalf("Failed to get events, error: %s(%s)", err, out)
310
+	}
311
+	events := strings.Split(out, "\n")
312
+	events = events[:len(events)-1]
313
+	if len(events) == 0 {
314
+		t.Fatalf("Expected events but found none for the image busybox:latest")
315
+	}
316
+	count1 := 0
317
+	count2 := 0
318
+
319
+	for _, e := range events {
320
+		if strings.Contains(e, container1) {
321
+			count1++
322
+		} else if strings.Contains(e, container2) {
323
+			count2++
327 324
 		}
328 325
 	}
326
+	if count1 == 0 || count2 == 0 {
327
+		t.Fatalf("Expected events from each container but got %d from %s and %d from %s", count1, container1, count2, container2)
328
+	}
329 329
 
330 330
 	logDone("events - filters using image")
331 331
 }
... ...
@@ -467,7 +467,7 @@ func TestEventsStreaming(t *testing.T) {
467 467
 		}
468 468
 	}()
469 469
 
470
-	runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "true")
470
+	runCmd := exec.Command(dockerBinary, "run", "-d", "busybox:latest", "true")
471 471
 	out, _, err := runCommandWithOutput(runCmd)
472 472
 	if err != nil {
473 473
 		t.Fatal(out, err)
... ...
@@ -594,6 +594,21 @@ func TestPsRightTagName(t *testing.T) {
594 594
 	} else {
595 595
 		id2 = strings.TrimSpace(string(out))
596 596
 	}
597
+
598
+	var imageID string
599
+	if out, err := exec.Command(dockerBinary, "inspect", "-f", "{{.Id}}", "busybox").CombinedOutput(); err != nil {
600
+		t.Fatalf("failed to get the image ID of busybox: %s, %v", out, err)
601
+	} else {
602
+		imageID = strings.TrimSpace(string(out))
603
+	}
604
+
605
+	var id3 string
606
+	if out, err := exec.Command(dockerBinary, "run", "-d", imageID, "top").CombinedOutput(); err != nil {
607
+		t.Fatalf("Failed to run container: %s, out: %q", err, out)
608
+	} else {
609
+		id3 = strings.TrimSpace(string(out))
610
+	}
611
+
597 612
 	out, err := exec.Command(dockerBinary, "ps", "--no-trunc").CombinedOutput()
598 613
 	if err != nil {
599 614
 		t.Fatalf("Failed to run 'ps': %s, out: %q", err, out)
... ...
@@ -601,22 +616,26 @@ func TestPsRightTagName(t *testing.T) {
601 601
 	lines := strings.Split(strings.TrimSpace(string(out)), "\n")
602 602
 	// skip header
603 603
 	lines = lines[1:]
604
-	if len(lines) != 2 {
605
-		t.Fatalf("There should be 2 running container, got %d", len(lines))
604
+	if len(lines) != 3 {
605
+		t.Fatalf("There should be 3 running container, got %d", len(lines))
606 606
 	}
607 607
 	for _, line := range lines {
608 608
 		f := strings.Fields(line)
609 609
 		switch f[0] {
610 610
 		case id1:
611
-			if f[1] != "busybox:latest" {
611
+			if f[1] != "busybox" {
612 612
 				t.Fatalf("Expected %s tag for id %s, got %s", "busybox", id1, f[1])
613 613
 			}
614 614
 		case id2:
615 615
 			if f[1] != tag {
616
-				t.Fatalf("Expected %s tag for id %s, got %s", tag, id1, f[1])
616
+				t.Fatalf("Expected %s tag for id %s, got %s", tag, id2, f[1])
617
+			}
618
+		case id3:
619
+			if f[1] != imageID {
620
+				t.Fatalf("Expected %s imageID for id %s, got %s", tag, id3, f[1])
617 621
 			}
618 622
 		default:
619
-			t.Fatalf("Unexpected id %s, expected %s and %s", f[0], id1, id2)
623
+			t.Fatalf("Unexpected id %s, expected %s and %s and %s", f[0], id1, id2, id3)
620 624
 		}
621 625
 	}
622 626
 	logDone("ps - right tags for containers")