events_test.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. package events
  2. import (
  3. "fmt"
  4. "testing"
  5. "time"
  6. "github.com/docker/docker/api/types/events"
  7. timetypes "github.com/docker/docker/api/types/time"
  8. eventstestutils "github.com/docker/docker/daemon/events/testutils"
  9. )
  10. func TestEventsLog(t *testing.T) {
  11. e := New()
  12. _, l1, _ := e.Subscribe()
  13. _, l2, _ := e.Subscribe()
  14. defer e.Evict(l1)
  15. defer e.Evict(l2)
  16. count := e.SubscribersCount()
  17. if count != 2 {
  18. t.Fatalf("Must be 2 subscribers, got %d", count)
  19. }
  20. actor := events.Actor{
  21. ID: "cont",
  22. Attributes: map[string]string{"image": "image"},
  23. }
  24. e.Log("test", events.ContainerEventType, actor)
  25. select {
  26. case msg := <-l1:
  27. jmsg, ok := msg.(events.Message)
  28. if !ok {
  29. t.Fatalf("Unexpected type %T", msg)
  30. }
  31. if len(e.events) != 1 {
  32. t.Fatalf("Must be only one event, got %d", len(e.events))
  33. }
  34. if jmsg.Status != "test" {
  35. t.Fatalf("Status should be test, got %s", jmsg.Status)
  36. }
  37. if jmsg.ID != "cont" {
  38. t.Fatalf("ID should be cont, got %s", jmsg.ID)
  39. }
  40. if jmsg.From != "image" {
  41. t.Fatalf("From should be image, got %s", jmsg.From)
  42. }
  43. case <-time.After(1 * time.Second):
  44. t.Fatal("Timeout waiting for broadcasted message")
  45. }
  46. select {
  47. case msg := <-l2:
  48. jmsg, ok := msg.(events.Message)
  49. if !ok {
  50. t.Fatalf("Unexpected type %T", msg)
  51. }
  52. if len(e.events) != 1 {
  53. t.Fatalf("Must be only one event, got %d", len(e.events))
  54. }
  55. if jmsg.Status != "test" {
  56. t.Fatalf("Status should be test, got %s", jmsg.Status)
  57. }
  58. if jmsg.ID != "cont" {
  59. t.Fatalf("ID should be cont, got %s", jmsg.ID)
  60. }
  61. if jmsg.From != "image" {
  62. t.Fatalf("From should be image, got %s", jmsg.From)
  63. }
  64. case <-time.After(1 * time.Second):
  65. t.Fatal("Timeout waiting for broadcasted message")
  66. }
  67. }
  68. func TestEventsLogTimeout(t *testing.T) {
  69. e := New()
  70. _, l, _ := e.Subscribe()
  71. defer e.Evict(l)
  72. c := make(chan struct{})
  73. go func() {
  74. actor := events.Actor{
  75. ID: "image",
  76. }
  77. e.Log("test", events.ImageEventType, actor)
  78. close(c)
  79. }()
  80. select {
  81. case <-c:
  82. case <-time.After(time.Second):
  83. t.Fatal("Timeout publishing message")
  84. }
  85. }
  86. func TestLogEvents(t *testing.T) {
  87. e := New()
  88. for i := 0; i < eventsLimit+16; i++ {
  89. action := fmt.Sprintf("action_%d", i)
  90. id := fmt.Sprintf("cont_%d", i)
  91. from := fmt.Sprintf("image_%d", i)
  92. actor := events.Actor{
  93. ID: id,
  94. Attributes: map[string]string{"image": from},
  95. }
  96. e.Log(action, events.ContainerEventType, actor)
  97. }
  98. time.Sleep(50 * time.Millisecond)
  99. current, l, _ := e.Subscribe()
  100. for i := 0; i < 10; i++ {
  101. num := i + eventsLimit + 16
  102. action := fmt.Sprintf("action_%d", num)
  103. id := fmt.Sprintf("cont_%d", num)
  104. from := fmt.Sprintf("image_%d", num)
  105. actor := events.Actor{
  106. ID: id,
  107. Attributes: map[string]string{"image": from},
  108. }
  109. e.Log(action, events.ContainerEventType, actor)
  110. }
  111. if len(e.events) != eventsLimit {
  112. t.Fatalf("Must be %d events, got %d", eventsLimit, len(e.events))
  113. }
  114. var msgs []events.Message
  115. for len(msgs) < 10 {
  116. m := <-l
  117. jm, ok := (m).(events.Message)
  118. if !ok {
  119. t.Fatalf("Unexpected type %T", m)
  120. }
  121. msgs = append(msgs, jm)
  122. }
  123. if len(current) != eventsLimit {
  124. t.Fatalf("Must be %d events, got %d", eventsLimit, len(current))
  125. }
  126. first := current[0]
  127. // TODO remove this once we removed the deprecated `ID`, `Status`, and `From` fields
  128. if first.Action != first.Status {
  129. // Verify that the (deprecated) Status is set to the expected value
  130. t.Fatalf("Action (%s) does not match Status (%s)", first.Action, first.Status)
  131. }
  132. if first.Action != "action_16" {
  133. t.Fatalf("First action is %s, must be action_16", first.Action)
  134. }
  135. last := current[len(current)-1]
  136. if last.Action != "action_271" {
  137. t.Fatalf("Last action is %s, must be action_271", last.Action)
  138. }
  139. firstC := msgs[0]
  140. if firstC.Action != "action_272" {
  141. t.Fatalf("First action is %s, must be action_272", firstC.Action)
  142. }
  143. lastC := msgs[len(msgs)-1]
  144. if lastC.Action != "action_281" {
  145. t.Fatalf("Last action is %s, must be action_281", lastC.Action)
  146. }
  147. }
  148. // https://github.com/docker/docker/issues/20999
  149. // Fixtures:
  150. //
  151. //2016-03-07T17:28:03.022433271+02:00 container die 0b863f2a26c18557fc6cdadda007c459f9ec81b874780808138aea78a3595079 (image=ubuntu, name=small_hoover)
  152. //2016-03-07T17:28:03.091719377+02:00 network disconnect 19c5ed41acb798f26b751e0035cd7821741ab79e2bbd59a66b5fd8abf954eaa0 (type=bridge, container=0b863f2a26c18557fc6cdadda007c459f9ec81b874780808138aea78a3595079, name=bridge)
  153. //2016-03-07T17:28:03.129014751+02:00 container destroy 0b863f2a26c18557fc6cdadda007c459f9ec81b874780808138aea78a3595079 (image=ubuntu, name=small_hoover)
  154. func TestLoadBufferedEvents(t *testing.T) {
  155. now := time.Now()
  156. f, err := timetypes.GetTimestamp("2016-03-07T17:28:03.100000000+02:00", now)
  157. if err != nil {
  158. t.Fatal(err)
  159. }
  160. s, sNano, err := timetypes.ParseTimestamps(f, -1)
  161. if err != nil {
  162. t.Fatal(err)
  163. }
  164. m1, err := eventstestutils.Scan("2016-03-07T17:28:03.022433271+02:00 container die 0b863f2a26c18557fc6cdadda007c459f9ec81b874780808138aea78a3595079 (image=ubuntu, name=small_hoover)")
  165. if err != nil {
  166. t.Fatal(err)
  167. }
  168. m2, err := eventstestutils.Scan("2016-03-07T17:28:03.091719377+02:00 network disconnect 19c5ed41acb798f26b751e0035cd7821741ab79e2bbd59a66b5fd8abf954eaa0 (type=bridge, container=0b863f2a26c18557fc6cdadda007c459f9ec81b874780808138aea78a3595079, name=bridge)")
  169. if err != nil {
  170. t.Fatal(err)
  171. }
  172. m3, err := eventstestutils.Scan("2016-03-07T17:28:03.129014751+02:00 container destroy 0b863f2a26c18557fc6cdadda007c459f9ec81b874780808138aea78a3595079 (image=ubuntu, name=small_hoover)")
  173. if err != nil {
  174. t.Fatal(err)
  175. }
  176. events := &Events{
  177. events: []events.Message{*m1, *m2, *m3},
  178. }
  179. since := time.Unix(s, sNano)
  180. until := time.Time{}
  181. out := events.loadBufferedEvents(since, until, nil)
  182. if len(out) != 1 {
  183. t.Fatalf("expected 1 message, got %d: %v", len(out), out)
  184. }
  185. }
  186. func TestLoadBufferedEventsOnlyFromPast(t *testing.T) {
  187. now := time.Now()
  188. f, err := timetypes.GetTimestamp("2016-03-07T17:28:03.090000000+02:00", now)
  189. if err != nil {
  190. t.Fatal(err)
  191. }
  192. s, sNano, err := timetypes.ParseTimestamps(f, 0)
  193. if err != nil {
  194. t.Fatal(err)
  195. }
  196. f, err = timetypes.GetTimestamp("2016-03-07T17:28:03.100000000+02:00", now)
  197. if err != nil {
  198. t.Fatal(err)
  199. }
  200. u, uNano, err := timetypes.ParseTimestamps(f, 0)
  201. if err != nil {
  202. t.Fatal(err)
  203. }
  204. m1, err := eventstestutils.Scan("2016-03-07T17:28:03.022433271+02:00 container die 0b863f2a26c18557fc6cdadda007c459f9ec81b874780808138aea78a3595079 (image=ubuntu, name=small_hoover)")
  205. if err != nil {
  206. t.Fatal(err)
  207. }
  208. m2, err := eventstestutils.Scan("2016-03-07T17:28:03.091719377+02:00 network disconnect 19c5ed41acb798f26b751e0035cd7821741ab79e2bbd59a66b5fd8abf954eaa0 (type=bridge, container=0b863f2a26c18557fc6cdadda007c459f9ec81b874780808138aea78a3595079, name=bridge)")
  209. if err != nil {
  210. t.Fatal(err)
  211. }
  212. m3, err := eventstestutils.Scan("2016-03-07T17:28:03.129014751+02:00 container destroy 0b863f2a26c18557fc6cdadda007c459f9ec81b874780808138aea78a3595079 (image=ubuntu, name=small_hoover)")
  213. if err != nil {
  214. t.Fatal(err)
  215. }
  216. events := &Events{
  217. events: []events.Message{*m1, *m2, *m3},
  218. }
  219. since := time.Unix(s, sNano)
  220. until := time.Unix(u, uNano)
  221. out := events.loadBufferedEvents(since, until, nil)
  222. if len(out) != 1 {
  223. t.Fatalf("expected 1 message, got %d: %v", len(out), out)
  224. }
  225. if out[0].Type != "network" {
  226. t.Fatalf("expected network event, got %s", out[0].Type)
  227. }
  228. }
  229. // #13753
  230. func TestIgnoreBufferedWhenNoTimes(t *testing.T) {
  231. m1, err := eventstestutils.Scan("2016-03-07T17:28:03.022433271+02:00 container die 0b863f2a26c18557fc6cdadda007c459f9ec81b874780808138aea78a3595079 (image=ubuntu, name=small_hoover)")
  232. if err != nil {
  233. t.Fatal(err)
  234. }
  235. m2, err := eventstestutils.Scan("2016-03-07T17:28:03.091719377+02:00 network disconnect 19c5ed41acb798f26b751e0035cd7821741ab79e2bbd59a66b5fd8abf954eaa0 (type=bridge, container=0b863f2a26c18557fc6cdadda007c459f9ec81b874780808138aea78a3595079, name=bridge)")
  236. if err != nil {
  237. t.Fatal(err)
  238. }
  239. m3, err := eventstestutils.Scan("2016-03-07T17:28:03.129014751+02:00 container destroy 0b863f2a26c18557fc6cdadda007c459f9ec81b874780808138aea78a3595079 (image=ubuntu, name=small_hoover)")
  240. if err != nil {
  241. t.Fatal(err)
  242. }
  243. events := &Events{
  244. events: []events.Message{*m1, *m2, *m3},
  245. }
  246. since := time.Time{}
  247. until := time.Time{}
  248. out := events.loadBufferedEvents(since, until, nil)
  249. if len(out) != 0 {
  250. t.Fatalf("expected 0 buffered events, got %q", out)
  251. }
  252. }