events_test.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package 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. container := &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(container, "create")
  29. if _, mutated := container.Config.Labels["image"]; mutated {
  30. t.Fatalf("Expected to not mutate the container labels, got %q", container.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. container := &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. attributes := map[string]string{
  55. "node": "2",
  56. "foo": "bar",
  57. }
  58. daemon.LogContainerEventWithAttributes(container, "create", attributes)
  59. validateTestAttributes(t, l, map[string]string{
  60. "node": "1",
  61. "foo": "bar",
  62. })
  63. }
  64. func validateTestAttributes(t *testing.T, l chan interface{}, expectedAttributesToTest map[string]string) {
  65. select {
  66. case ev := <-l:
  67. event, ok := ev.(eventtypes.Message)
  68. if !ok {
  69. t.Fatalf("Unexpected event message: %q", ev)
  70. }
  71. for key, expected := range expectedAttributesToTest {
  72. actual, ok := event.Actor.Attributes[key]
  73. if !ok || actual != expected {
  74. t.Fatalf("Expected value for key %s to be %s, but was %s (event:%v)", key, expected, actual, event)
  75. }
  76. }
  77. case <-time.After(10 * time.Second):
  78. t.Fatal("LogEvent test timed out")
  79. }
  80. }