Browse code

Merge pull request #10675 from brahmaroutu/events_filterbyimage_10645

Allow use of just image name without the tag

Phil Estes authored on 2015/03/07 03:38:41
Showing 2 changed files
... ...
@@ -2,6 +2,7 @@ package events
2 2
 
3 3
 import (
4 4
 	"encoding/json"
5
+	"strings"
5 6
 	"sync"
6 7
 	"time"
7 8
 
... ...
@@ -112,6 +113,12 @@ func writeEvent(job *engine.Job, event *utils.JSONMessage, eventFilters filters.
112 112
 			if v == field {
113 113
 				return false
114 114
 			}
115
+			if strings.Contains(field, ":") {
116
+				image := strings.Split(field, ":")
117
+				if image[0] == v {
118
+					return false
119
+				}
120
+			}
115 121
 		}
116 122
 		return true
117 123
 	}
... ...
@@ -248,3 +248,47 @@ func TestEventsFilters(t *testing.T) {
248 248
 
249 249
 	logDone("events - filters")
250 250
 }
251
+
252
+func TestEventsFilterImageName(t *testing.T) {
253
+	since := time.Now().Unix()
254
+	defer deleteAllContainers()
255
+
256
+	out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "--name", "container_1", "-d", "busybox", "true"))
257
+	if err != nil {
258
+		t.Fatal(out, err)
259
+	}
260
+	container1 := stripTrailingCharacters(out)
261
+	out, _, err = runCommandWithOutput(exec.Command(dockerBinary, "run", "--name", "container_2", "-d", "busybox", "true"))
262
+	if err != nil {
263
+		t.Fatal(out, err)
264
+	}
265
+	container2 := stripTrailingCharacters(out)
266
+
267
+	for _, s := range []string{"busybox", "busybox:latest"} {
268
+		eventsCmd := exec.Command(dockerBinary, "events", fmt.Sprintf("--since=%d", since), fmt.Sprintf("--until=%d", time.Now().Unix()), "--filter", fmt.Sprintf("image=%s", s))
269
+		out, _, err := runCommandWithOutput(eventsCmd)
270
+		if err != nil {
271
+			t.Fatalf("Failed to get events, error: %s(%s)", err, out)
272
+		}
273
+		events := strings.Split(out, "\n")
274
+		events = events[:len(events)-1]
275
+		if len(events) == 0 {
276
+			t.Fatalf("Expected events but found none for the image busybox:latest")
277
+		}
278
+		count1 := 0
279
+		count2 := 0
280
+		for _, e := range events {
281
+			if strings.Contains(e, container1) {
282
+				count1++
283
+			} else if strings.Contains(e, container2) {
284
+				count2++
285
+			}
286
+		}
287
+		if count1 == 0 || count2 == 0 {
288
+			t.Fatalf("Expected events from each container but got %d from %s and %d from %s", count1, container1, count2, container2)
289
+		}
290
+	}
291
+
292
+	logDone("events - filters using image")
293
+
294
+}