mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Merge pull request #10675 from brahmaroutu/events_filterbyimage_10645
Allow use of just image name without the tag
This commit is contained in:
commit
c94eb28af0
2 changed files with 51 additions and 0 deletions
|
@ -2,6 +2,7 @@ package events
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -112,6 +113,12 @@ func writeEvent(job *engine.Job, event *utils.JSONMessage, eventFilters filters.
|
||||||
if v == field {
|
if v == field {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
if strings.Contains(field, ":") {
|
||||||
|
image := strings.Split(field, ":")
|
||||||
|
if image[0] == v {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
|
@ -248,3 +248,47 @@ func TestEventsFilters(t *testing.T) {
|
||||||
|
|
||||||
logDone("events - filters")
|
logDone("events - filters")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestEventsFilterImageName(t *testing.T) {
|
||||||
|
since := time.Now().Unix()
|
||||||
|
defer deleteAllContainers()
|
||||||
|
|
||||||
|
out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "--name", "container_1", "-d", "busybox", "true"))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(out, err)
|
||||||
|
}
|
||||||
|
container1 := stripTrailingCharacters(out)
|
||||||
|
out, _, err = runCommandWithOutput(exec.Command(dockerBinary, "run", "--name", "container_2", "-d", "busybox", "true"))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(out, err)
|
||||||
|
}
|
||||||
|
container2 := stripTrailingCharacters(out)
|
||||||
|
|
||||||
|
for _, s := range []string{"busybox", "busybox:latest"} {
|
||||||
|
eventsCmd := exec.Command(dockerBinary, "events", fmt.Sprintf("--since=%d", since), fmt.Sprintf("--until=%d", time.Now().Unix()), "--filter", fmt.Sprintf("image=%s", s))
|
||||||
|
out, _, err := runCommandWithOutput(eventsCmd)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed to get events, error: %s(%s)", err, out)
|
||||||
|
}
|
||||||
|
events := strings.Split(out, "\n")
|
||||||
|
events = events[:len(events)-1]
|
||||||
|
if len(events) == 0 {
|
||||||
|
t.Fatalf("Expected events but found none for the image busybox:latest")
|
||||||
|
}
|
||||||
|
count1 := 0
|
||||||
|
count2 := 0
|
||||||
|
for _, e := range events {
|
||||||
|
if strings.Contains(e, container1) {
|
||||||
|
count1++
|
||||||
|
} else if strings.Contains(e, container2) {
|
||||||
|
count2++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if count1 == 0 || count2 == 0 {
|
||||||
|
t.Fatalf("Expected events from each container but got %d from %s and %d from %s", count1, container1, count2, container2)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
logDone("events - filters using image")
|
||||||
|
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue