2018-02-05 21:05:59 +00:00
|
|
|
package daemon // import "github.com/docker/docker/daemon"
|
2015-12-21 22:55:23 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2016-01-07 22:14:05 +00:00
|
|
|
"time"
|
2015-12-21 22:55:23 +00:00
|
|
|
|
2016-09-06 18:18:12 +00:00
|
|
|
containertypes "github.com/docker/docker/api/types/container"
|
|
|
|
eventtypes "github.com/docker/docker/api/types/events"
|
2015-12-21 22:55:23 +00:00
|
|
|
"github.com/docker/docker/container"
|
|
|
|
"github.com/docker/docker/daemon/events"
|
|
|
|
)
|
|
|
|
|
2016-01-07 22:14:05 +00:00
|
|
|
func TestLogContainerEventCopyLabels(t *testing.T) {
|
2015-12-21 22:55:23 +00:00
|
|
|
e := events.New()
|
|
|
|
_, l, _ := e.Subscribe()
|
|
|
|
defer e.Evict(l)
|
|
|
|
|
2019-08-09 12:10:07 +00:00
|
|
|
ctr := &container.Container{
|
2017-04-25 19:03:45 +00:00
|
|
|
ID: "container_id",
|
|
|
|
Name: "container_name",
|
|
|
|
Config: &containertypes.Config{
|
|
|
|
Image: "image_name",
|
|
|
|
Labels: map[string]string{
|
|
|
|
"node": "1",
|
|
|
|
"os": "alpine",
|
2015-12-21 22:55:23 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
daemon := &Daemon{
|
|
|
|
EventsService: e,
|
|
|
|
}
|
2019-08-09 12:10:07 +00:00
|
|
|
daemon.LogContainerEvent(ctr, "create")
|
2015-12-21 22:55:23 +00:00
|
|
|
|
2019-08-09 12:10:07 +00:00
|
|
|
if _, mutated := ctr.Config.Labels["image"]; mutated {
|
|
|
|
t.Fatalf("Expected to not mutate the container labels, got %q", ctr.Config.Labels)
|
2015-12-21 22:55:23 +00:00
|
|
|
}
|
2016-01-07 22:14:05 +00:00
|
|
|
|
|
|
|
validateTestAttributes(t, l, map[string]string{
|
|
|
|
"node": "1",
|
|
|
|
"os": "alpine",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLogContainerEventWithAttributes(t *testing.T) {
|
|
|
|
e := events.New()
|
|
|
|
_, l, _ := e.Subscribe()
|
|
|
|
defer e.Evict(l)
|
|
|
|
|
2019-08-09 12:10:07 +00:00
|
|
|
ctr := &container.Container{
|
2017-04-25 19:03:45 +00:00
|
|
|
ID: "container_id",
|
|
|
|
Name: "container_name",
|
|
|
|
Config: &containertypes.Config{
|
|
|
|
Labels: map[string]string{
|
|
|
|
"node": "1",
|
|
|
|
"os": "alpine",
|
2016-01-07 22:14:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
daemon := &Daemon{
|
|
|
|
EventsService: e,
|
|
|
|
}
|
2023-08-26 15:08:38 +00:00
|
|
|
daemon.LogContainerEventWithAttributes(ctr, "create", map[string]string{
|
2016-01-07 22:14:05 +00:00
|
|
|
"node": "2",
|
|
|
|
"foo": "bar",
|
2023-08-26 15:08:38 +00:00
|
|
|
})
|
2016-01-07 22:14:05 +00:00
|
|
|
|
|
|
|
validateTestAttributes(t, l, map[string]string{
|
|
|
|
"node": "1",
|
|
|
|
"foo": "bar",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateTestAttributes(t *testing.T, l chan interface{}, expectedAttributesToTest map[string]string) {
|
|
|
|
select {
|
|
|
|
case ev := <-l:
|
|
|
|
event, ok := ev.(eventtypes.Message)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Unexpected event message: %q", ev)
|
|
|
|
}
|
|
|
|
for key, expected := range expectedAttributesToTest {
|
|
|
|
actual, ok := event.Actor.Attributes[key]
|
|
|
|
if !ok || actual != expected {
|
|
|
|
t.Fatalf("Expected value for key %s to be %s, but was %s (event:%v)", key, expected, actual, event)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case <-time.After(10 * time.Second):
|
2017-02-21 08:53:29 +00:00
|
|
|
t.Fatal("LogEvent test timed out")
|
2016-01-07 22:14:05 +00:00
|
|
|
}
|
2015-12-21 22:55:23 +00:00
|
|
|
}
|