events_test.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package daemon // import "github.com/docker/docker/daemon"
  2. import (
  3. "testing"
  4. "time"
  5. containertypes "github.com/docker/docker/api/types/container"
  6. eventtypes "github.com/docker/docker/api/types/events"
  7. "github.com/docker/docker/container"
  8. "github.com/docker/docker/daemon/events"
  9. )
  10. func TestLogContainerEventCopyLabels(t *testing.T) {
  11. e := events.New()
  12. _, l, _ := e.Subscribe()
  13. defer e.Evict(l)
  14. ctr := &container.Container{
  15. ID: "container_id",
  16. Name: "container_name",
  17. Config: &containertypes.Config{
  18. Image: "image_name",
  19. Labels: map[string]string{
  20. "node": "1",
  21. "os": "alpine",
  22. },
  23. },
  24. }
  25. daemon := &Daemon{
  26. EventsService: e,
  27. }
  28. daemon.LogContainerEvent(ctr, eventtypes.ActionCreate)
  29. if _, mutated := ctr.Config.Labels["image"]; mutated {
  30. t.Fatalf("Expected to not mutate the container labels, got %q", ctr.Config.Labels)
  31. }
  32. validateTestAttributes(t, l, map[string]string{
  33. "node": "1",
  34. "os": "alpine",
  35. })
  36. }
  37. func TestLogContainerEventWithAttributes(t *testing.T) {
  38. e := events.New()
  39. _, l, _ := e.Subscribe()
  40. defer e.Evict(l)
  41. ctr := &container.Container{
  42. ID: "container_id",
  43. Name: "container_name",
  44. Config: &containertypes.Config{
  45. Labels: map[string]string{
  46. "node": "1",
  47. "os": "alpine",
  48. },
  49. },
  50. }
  51. daemon := &Daemon{
  52. EventsService: e,
  53. }
  54. daemon.LogContainerEventWithAttributes(ctr, eventtypes.ActionCreate, map[string]string{
  55. "node": "2",
  56. "foo": "bar",
  57. })
  58. validateTestAttributes(t, l, map[string]string{
  59. "node": "1",
  60. "foo": "bar",
  61. })
  62. }
  63. func validateTestAttributes(t *testing.T, l chan interface{}, expectedAttributesToTest map[string]string) {
  64. select {
  65. case ev := <-l:
  66. event, ok := ev.(eventtypes.Message)
  67. if !ok {
  68. t.Fatalf("Unexpected event message: %q", ev)
  69. }
  70. for key, expected := range expectedAttributesToTest {
  71. actual, ok := event.Actor.Attributes[key]
  72. if !ok || actual != expected {
  73. t.Fatalf("Expected value for key %s to be %s, but was %s (event:%v)", key, expected, actual, event)
  74. }
  75. }
  76. case <-time.After(10 * time.Second):
  77. t.Fatal("LogEvent test timed out")
  78. }
  79. }