events_test.go 2.2 KB

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