docker_cli_events_test.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "net/http"
  6. "os/exec"
  7. "regexp"
  8. "strconv"
  9. "strings"
  10. "sync"
  11. "time"
  12. "github.com/go-check/check"
  13. )
  14. func (s *DockerSuite) TestEventsTimestampFormats(c *check.C) {
  15. image := "busybox"
  16. // Start stopwatch, generate an event
  17. time.Sleep(time.Second) // so that we don't grab events from previous test occured in the same second
  18. start := daemonTime(c)
  19. time.Sleep(time.Second) // remote API precision is only a second, wait a while before creating an event
  20. dockerCmd(c, "tag", image, "timestamptest:1")
  21. dockerCmd(c, "rmi", "timestamptest:1")
  22. time.Sleep(time.Second) // so that until > since
  23. end := daemonTime(c)
  24. // List of available time formats to --since
  25. unixTs := func(t time.Time) string { return fmt.Sprintf("%v", t.Unix()) }
  26. rfc3339 := func(t time.Time) string { return t.Format(time.RFC3339) }
  27. duration := func(t time.Time) string { return time.Now().Sub(t).String() }
  28. // --since=$start must contain only the 'untag' event
  29. for _, f := range []func(time.Time) string{unixTs, rfc3339, duration} {
  30. since, until := f(start), f(end)
  31. cmd := exec.Command(dockerBinary, "events", "--since="+since, "--until="+until)
  32. out, _, err := runCommandWithOutput(cmd)
  33. if err != nil {
  34. c.Fatalf("docker events cmd failed: %v\nout=%s", err, out)
  35. }
  36. events := strings.Split(strings.TrimSpace(out), "\n")
  37. if len(events) != 2 {
  38. c.Fatalf("unexpected events, was expecting only 2 events tag/untag (since=%s, until=%s) out=%s", since, until, out)
  39. }
  40. if !strings.Contains(out, "untag") {
  41. c.Fatalf("expected 'untag' event not found (since=%s, until=%s) out=%s", since, until, out)
  42. }
  43. }
  44. }
  45. func (s *DockerSuite) TestEventsUntag(c *check.C) {
  46. image := "busybox"
  47. dockerCmd(c, "tag", image, "utest:tag1")
  48. dockerCmd(c, "tag", image, "utest:tag2")
  49. dockerCmd(c, "rmi", "utest:tag1")
  50. dockerCmd(c, "rmi", "utest:tag2")
  51. eventsCmd := exec.Command(dockerBinary, "events", "--since=1")
  52. out, exitCode, _, err := runCommandWithOutputForDuration(eventsCmd, time.Duration(time.Millisecond*200))
  53. if exitCode != 0 || err != nil {
  54. c.Fatalf("Failed to get events - exit code %d: %s", exitCode, err)
  55. }
  56. events := strings.Split(out, "\n")
  57. nEvents := len(events)
  58. // The last element after the split above will be an empty string, so we
  59. // get the two elements before the last, which are the untags we're
  60. // looking for.
  61. for _, v := range events[nEvents-3 : nEvents-1] {
  62. if !strings.Contains(v, "untag") {
  63. c.Fatalf("event should be untag, not %#v", v)
  64. }
  65. }
  66. }
  67. func (s *DockerSuite) TestEventsContainerFailStartDie(c *check.C) {
  68. out, _ := dockerCmd(c, "images", "-q")
  69. image := strings.Split(out, "\n")[0]
  70. if err := exec.Command(dockerBinary, "run", "--name", "testeventdie", image, "blerg").Run(); err == nil {
  71. c.Fatalf("Container run with command blerg should have failed, but it did not")
  72. }
  73. eventsCmd := exec.Command(dockerBinary, "events", "--since=0", fmt.Sprintf("--until=%d", daemonTime(c).Unix()))
  74. out, _, _ = runCommandWithOutput(eventsCmd)
  75. events := strings.Split(out, "\n")
  76. if len(events) <= 1 {
  77. c.Fatalf("Missing expected event")
  78. }
  79. startEvent := strings.Fields(events[len(events)-3])
  80. dieEvent := strings.Fields(events[len(events)-2])
  81. if startEvent[len(startEvent)-1] != "start" {
  82. c.Fatalf("event should be start, not %#v", startEvent)
  83. }
  84. if dieEvent[len(dieEvent)-1] != "die" {
  85. c.Fatalf("event should be die, not %#v", dieEvent)
  86. }
  87. }
  88. func (s *DockerSuite) TestEventsLimit(c *check.C) {
  89. var waitGroup sync.WaitGroup
  90. errChan := make(chan error, 17)
  91. args := []string{"run", "--rm", "busybox", "true"}
  92. for i := 0; i < 17; i++ {
  93. waitGroup.Add(1)
  94. go func() {
  95. defer waitGroup.Done()
  96. errChan <- exec.Command(dockerBinary, args...).Run()
  97. }()
  98. }
  99. waitGroup.Wait()
  100. close(errChan)
  101. for err := range errChan {
  102. if err != nil {
  103. c.Fatalf("%q failed with error: %v", strings.Join(args, " "), err)
  104. }
  105. }
  106. eventsCmd := exec.Command(dockerBinary, "events", "--since=0", fmt.Sprintf("--until=%d", daemonTime(c).Unix()))
  107. out, _, _ := runCommandWithOutput(eventsCmd)
  108. events := strings.Split(out, "\n")
  109. nEvents := len(events) - 1
  110. if nEvents != 64 {
  111. c.Fatalf("events should be limited to 64, but received %d", nEvents)
  112. }
  113. }
  114. func (s *DockerSuite) TestEventsContainerEvents(c *check.C) {
  115. dockerCmd(c, "run", "--rm", "busybox", "true")
  116. eventsCmd := exec.Command(dockerBinary, "events", "--since=0", fmt.Sprintf("--until=%d", daemonTime(c).Unix()))
  117. out, exitCode, err := runCommandWithOutput(eventsCmd)
  118. if exitCode != 0 || err != nil {
  119. c.Fatalf("Failed to get events with exit code %d: %s", exitCode, err)
  120. }
  121. events := strings.Split(out, "\n")
  122. events = events[:len(events)-1]
  123. if len(events) < 5 {
  124. c.Fatalf("Missing expected event")
  125. }
  126. createEvent := strings.Fields(events[len(events)-5])
  127. attachEvent := strings.Fields(events[len(events)-4])
  128. startEvent := strings.Fields(events[len(events)-3])
  129. dieEvent := strings.Fields(events[len(events)-2])
  130. destroyEvent := strings.Fields(events[len(events)-1])
  131. if createEvent[len(createEvent)-1] != "create" {
  132. c.Fatalf("event should be create, not %#v", createEvent)
  133. }
  134. if attachEvent[len(createEvent)-1] != "attach" {
  135. c.Fatalf("event should be attach, not %#v", attachEvent)
  136. }
  137. if startEvent[len(startEvent)-1] != "start" {
  138. c.Fatalf("event should be start, not %#v", startEvent)
  139. }
  140. if dieEvent[len(dieEvent)-1] != "die" {
  141. c.Fatalf("event should be die, not %#v", dieEvent)
  142. }
  143. if destroyEvent[len(destroyEvent)-1] != "destroy" {
  144. c.Fatalf("event should be destroy, not %#v", destroyEvent)
  145. }
  146. }
  147. func (s *DockerSuite) TestEventsContainerEventsSinceUnixEpoch(c *check.C) {
  148. dockerCmd(c, "run", "--rm", "busybox", "true")
  149. timeBeginning := time.Unix(0, 0).Format(time.RFC3339Nano)
  150. timeBeginning = strings.Replace(timeBeginning, "Z", ".000000000Z", -1)
  151. eventsCmd := exec.Command(dockerBinary, "events", fmt.Sprintf("--since='%s'", timeBeginning),
  152. fmt.Sprintf("--until=%d", daemonTime(c).Unix()))
  153. out, exitCode, err := runCommandWithOutput(eventsCmd)
  154. if exitCode != 0 || err != nil {
  155. c.Fatalf("Failed to get events with exit code %d: %s", exitCode, err)
  156. }
  157. events := strings.Split(out, "\n")
  158. events = events[:len(events)-1]
  159. if len(events) < 5 {
  160. c.Fatalf("Missing expected event")
  161. }
  162. createEvent := strings.Fields(events[len(events)-5])
  163. attachEvent := strings.Fields(events[len(events)-4])
  164. startEvent := strings.Fields(events[len(events)-3])
  165. dieEvent := strings.Fields(events[len(events)-2])
  166. destroyEvent := strings.Fields(events[len(events)-1])
  167. if createEvent[len(createEvent)-1] != "create" {
  168. c.Fatalf("event should be create, not %#v", createEvent)
  169. }
  170. if attachEvent[len(attachEvent)-1] != "attach" {
  171. c.Fatalf("event should be attach, not %#v", attachEvent)
  172. }
  173. if startEvent[len(startEvent)-1] != "start" {
  174. c.Fatalf("event should be start, not %#v", startEvent)
  175. }
  176. if dieEvent[len(dieEvent)-1] != "die" {
  177. c.Fatalf("event should be die, not %#v", dieEvent)
  178. }
  179. if destroyEvent[len(destroyEvent)-1] != "destroy" {
  180. c.Fatalf("event should be destroy, not %#v", destroyEvent)
  181. }
  182. }
  183. func (s *DockerSuite) TestEventsImageUntagDelete(c *check.C) {
  184. name := "testimageevents"
  185. _, err := buildImage(name,
  186. `FROM scratch
  187. MAINTAINER "docker"`,
  188. true)
  189. if err != nil {
  190. c.Fatal(err)
  191. }
  192. if err := deleteImages(name); err != nil {
  193. c.Fatal(err)
  194. }
  195. eventsCmd := exec.Command(dockerBinary, "events", "--since=0", fmt.Sprintf("--until=%d", daemonTime(c).Unix()))
  196. out, exitCode, err := runCommandWithOutput(eventsCmd)
  197. if exitCode != 0 || err != nil {
  198. c.Fatalf("Failed to get events with exit code %d: %s", exitCode, err)
  199. }
  200. events := strings.Split(out, "\n")
  201. events = events[:len(events)-1]
  202. if len(events) < 2 {
  203. c.Fatalf("Missing expected event")
  204. }
  205. untagEvent := strings.Fields(events[len(events)-2])
  206. deleteEvent := strings.Fields(events[len(events)-1])
  207. if untagEvent[len(untagEvent)-1] != "untag" {
  208. c.Fatalf("untag should be untag, not %#v", untagEvent)
  209. }
  210. if deleteEvent[len(deleteEvent)-1] != "delete" {
  211. c.Fatalf("delete should be delete, not %#v", deleteEvent)
  212. }
  213. }
  214. func (s *DockerSuite) TestEventsImageTag(c *check.C) {
  215. time.Sleep(time.Second * 2) // because API has seconds granularity
  216. since := daemonTime(c).Unix()
  217. image := "testimageevents:tag"
  218. dockerCmd(c, "tag", "busybox", image)
  219. eventsCmd := exec.Command(dockerBinary, "events",
  220. fmt.Sprintf("--since=%d", since),
  221. fmt.Sprintf("--until=%d", daemonTime(c).Unix()))
  222. out, _, err := runCommandWithOutput(eventsCmd)
  223. c.Assert(err, check.IsNil)
  224. events := strings.Split(strings.TrimSpace(out), "\n")
  225. if len(events) != 1 {
  226. c.Fatalf("was expecting 1 event. out=%s", out)
  227. }
  228. event := strings.TrimSpace(events[0])
  229. expectedStr := image + ": tag"
  230. if !strings.HasSuffix(event, expectedStr) {
  231. c.Fatalf("wrong event format. expected='%s' got=%s", expectedStr, event)
  232. }
  233. }
  234. func (s *DockerSuite) TestEventsImagePull(c *check.C) {
  235. since := daemonTime(c).Unix()
  236. testRequires(c, Network)
  237. pullCmd := exec.Command(dockerBinary, "pull", "hello-world")
  238. if out, _, err := runCommandWithOutput(pullCmd); err != nil {
  239. c.Fatalf("pulling the hello-world image from has failed: %s, %v", out, err)
  240. }
  241. eventsCmd := exec.Command(dockerBinary, "events",
  242. fmt.Sprintf("--since=%d", since),
  243. fmt.Sprintf("--until=%d", daemonTime(c).Unix()))
  244. out, _, _ := runCommandWithOutput(eventsCmd)
  245. events := strings.Split(strings.TrimSpace(out), "\n")
  246. event := strings.TrimSpace(events[len(events)-1])
  247. if !strings.HasSuffix(event, "hello-world:latest: pull") {
  248. c.Fatalf("Missing pull event - got:%q", event)
  249. }
  250. }
  251. func (s *DockerSuite) TestEventsImageImport(c *check.C) {
  252. since := daemonTime(c).Unix()
  253. id := make(chan string)
  254. eventImport := make(chan struct{})
  255. eventsCmd := exec.Command(dockerBinary, "events", "--since", strconv.FormatInt(since, 10))
  256. stdout, err := eventsCmd.StdoutPipe()
  257. if err != nil {
  258. c.Fatal(err)
  259. }
  260. if err := eventsCmd.Start(); err != nil {
  261. c.Fatal(err)
  262. }
  263. defer eventsCmd.Process.Kill()
  264. go func() {
  265. containerID := <-id
  266. matchImport := regexp.MustCompile(containerID + `: import$`)
  267. scanner := bufio.NewScanner(stdout)
  268. for scanner.Scan() {
  269. if matchImport.MatchString(scanner.Text()) {
  270. close(eventImport)
  271. }
  272. }
  273. }()
  274. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "true")
  275. out, _, err := runCommandWithOutput(runCmd)
  276. if err != nil {
  277. c.Fatal("failed to create a container", out, err)
  278. }
  279. cleanedContainerID := strings.TrimSpace(out)
  280. out, _, err = runCommandPipelineWithOutput(
  281. exec.Command(dockerBinary, "export", cleanedContainerID),
  282. exec.Command(dockerBinary, "import", "-"),
  283. )
  284. if err != nil {
  285. c.Errorf("import failed with errors: %v, output: %q", err, out)
  286. }
  287. newContainerID := strings.TrimSpace(out)
  288. id <- newContainerID
  289. select {
  290. case <-time.After(5 * time.Second):
  291. c.Fatal("failed to observe image import in timely fashion")
  292. case <-eventImport:
  293. // ignore, done
  294. }
  295. }
  296. func (s *DockerSuite) TestEventsFilters(c *check.C) {
  297. parseEvents := func(out, match string) {
  298. events := strings.Split(out, "\n")
  299. events = events[:len(events)-1]
  300. for _, event := range events {
  301. eventFields := strings.Fields(event)
  302. eventName := eventFields[len(eventFields)-1]
  303. if ok, err := regexp.MatchString(match, eventName); err != nil || !ok {
  304. c.Fatalf("event should match %s, got %#v, err: %v", match, eventFields, err)
  305. }
  306. }
  307. }
  308. since := daemonTime(c).Unix()
  309. out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "--rm", "busybox", "true"))
  310. if err != nil {
  311. c.Fatal(out, err)
  312. }
  313. out, _, err = runCommandWithOutput(exec.Command(dockerBinary, "run", "--rm", "busybox", "true"))
  314. if err != nil {
  315. c.Fatal(out, err)
  316. }
  317. out, _, err = runCommandWithOutput(exec.Command(dockerBinary, "events", fmt.Sprintf("--since=%d", since), fmt.Sprintf("--until=%d", daemonTime(c).Unix()), "--filter", "event=die"))
  318. if err != nil {
  319. c.Fatalf("Failed to get events: %s", err)
  320. }
  321. parseEvents(out, "die")
  322. out, _, err = runCommandWithOutput(exec.Command(dockerBinary, "events", fmt.Sprintf("--since=%d", since), fmt.Sprintf("--until=%d", daemonTime(c).Unix()), "--filter", "event=die", "--filter", "event=start"))
  323. if err != nil {
  324. c.Fatalf("Failed to get events: %s", err)
  325. }
  326. parseEvents(out, "((die)|(start))")
  327. // make sure we at least got 2 start events
  328. count := strings.Count(out, "start")
  329. if count < 2 {
  330. c.Fatalf("should have had 2 start events but had %d, out: %s", count, out)
  331. }
  332. }
  333. func (s *DockerSuite) TestEventsFilterImageName(c *check.C) {
  334. since := daemonTime(c).Unix()
  335. out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "--name", "container_1", "-d", "busybox:latest", "true"))
  336. if err != nil {
  337. c.Fatal(out, err)
  338. }
  339. container1 := strings.TrimSpace(out)
  340. out, _, err = runCommandWithOutput(exec.Command(dockerBinary, "run", "--name", "container_2", "-d", "busybox", "true"))
  341. if err != nil {
  342. c.Fatal(out, err)
  343. }
  344. container2 := strings.TrimSpace(out)
  345. name := "busybox"
  346. eventsCmd := exec.Command(dockerBinary, "events", fmt.Sprintf("--since=%d", since), fmt.Sprintf("--until=%d", daemonTime(c).Unix()), "--filter", fmt.Sprintf("image=%s", name))
  347. out, _, err = runCommandWithOutput(eventsCmd)
  348. if err != nil {
  349. c.Fatalf("Failed to get events, error: %s(%s)", err, out)
  350. }
  351. events := strings.Split(out, "\n")
  352. events = events[:len(events)-1]
  353. if len(events) == 0 {
  354. c.Fatalf("Expected events but found none for the image busybox:latest")
  355. }
  356. count1 := 0
  357. count2 := 0
  358. for _, e := range events {
  359. if strings.Contains(e, container1) {
  360. count1++
  361. } else if strings.Contains(e, container2) {
  362. count2++
  363. }
  364. }
  365. if count1 == 0 || count2 == 0 {
  366. c.Fatalf("Expected events from each container but got %d from %s and %d from %s", count1, container1, count2, container2)
  367. }
  368. }
  369. func (s *DockerSuite) TestEventsFilterContainer(c *check.C) {
  370. since := fmt.Sprintf("%d", daemonTime(c).Unix())
  371. nameID := make(map[string]string)
  372. for _, name := range []string{"container_1", "container_2"} {
  373. out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "--name", name, "busybox", "true"))
  374. if err != nil {
  375. c.Fatalf("Error: %v, Output: %s", err, out)
  376. }
  377. id, err := inspectField(name, "Id")
  378. if err != nil {
  379. c.Fatal(err)
  380. }
  381. nameID[name] = id
  382. }
  383. until := fmt.Sprintf("%d", daemonTime(c).Unix())
  384. checkEvents := func(id string, events []string) error {
  385. if len(events) != 4 { // create, attach, start, die
  386. return fmt.Errorf("expected 3 events, got %v", events)
  387. }
  388. for _, event := range events {
  389. e := strings.Fields(event)
  390. if len(e) < 3 {
  391. return fmt.Errorf("got malformed event: %s", event)
  392. }
  393. // Check the id
  394. parsedID := strings.TrimSuffix(e[1], ":")
  395. if parsedID != id {
  396. return fmt.Errorf("expected event for container id %s: %s - parsed container id: %s", id, event, parsedID)
  397. }
  398. }
  399. return nil
  400. }
  401. for name, ID := range nameID {
  402. // filter by names
  403. eventsCmd := exec.Command(dockerBinary, "events", "--since", since, "--until", until, "--filter", "container="+name)
  404. out, _, err := runCommandWithOutput(eventsCmd)
  405. if err != nil {
  406. c.Fatal(err)
  407. }
  408. events := strings.Split(strings.TrimSuffix(out, "\n"), "\n")
  409. if err := checkEvents(ID, events); err != nil {
  410. c.Fatal(err)
  411. }
  412. // filter by ID's
  413. eventsCmd = exec.Command(dockerBinary, "events", "--since", since, "--until", until, "--filter", "container="+ID)
  414. out, _, err = runCommandWithOutput(eventsCmd)
  415. if err != nil {
  416. c.Fatal(err)
  417. }
  418. events = strings.Split(strings.TrimSuffix(out, "\n"), "\n")
  419. if err := checkEvents(ID, events); err != nil {
  420. c.Fatal(err)
  421. }
  422. }
  423. }
  424. func (s *DockerSuite) TestEventsStreaming(c *check.C) {
  425. start := daemonTime(c).Unix()
  426. id := make(chan string)
  427. eventCreate := make(chan struct{})
  428. eventStart := make(chan struct{})
  429. eventDie := make(chan struct{})
  430. eventDestroy := make(chan struct{})
  431. eventsCmd := exec.Command(dockerBinary, "events", "--since", strconv.FormatInt(start, 10))
  432. stdout, err := eventsCmd.StdoutPipe()
  433. if err != nil {
  434. c.Fatal(err)
  435. }
  436. if err := eventsCmd.Start(); err != nil {
  437. c.Fatalf("failed to start 'docker events': %s", err)
  438. }
  439. defer eventsCmd.Process.Kill()
  440. go func() {
  441. containerID := <-id
  442. matchCreate := regexp.MustCompile(containerID + `: \(from busybox:latest\) create$`)
  443. matchStart := regexp.MustCompile(containerID + `: \(from busybox:latest\) start$`)
  444. matchDie := regexp.MustCompile(containerID + `: \(from busybox:latest\) die$`)
  445. matchDestroy := regexp.MustCompile(containerID + `: \(from busybox:latest\) destroy$`)
  446. scanner := bufio.NewScanner(stdout)
  447. for scanner.Scan() {
  448. switch {
  449. case matchCreate.MatchString(scanner.Text()):
  450. close(eventCreate)
  451. case matchStart.MatchString(scanner.Text()):
  452. close(eventStart)
  453. case matchDie.MatchString(scanner.Text()):
  454. close(eventDie)
  455. case matchDestroy.MatchString(scanner.Text()):
  456. close(eventDestroy)
  457. }
  458. }
  459. }()
  460. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox:latest", "true")
  461. out, _, err := runCommandWithOutput(runCmd)
  462. if err != nil {
  463. c.Fatal(out, err)
  464. }
  465. cleanedContainerID := strings.TrimSpace(out)
  466. id <- cleanedContainerID
  467. select {
  468. case <-time.After(5 * time.Second):
  469. c.Fatal("failed to observe container create in timely fashion")
  470. case <-eventCreate:
  471. // ignore, done
  472. }
  473. select {
  474. case <-time.After(5 * time.Second):
  475. c.Fatal("failed to observe container start in timely fashion")
  476. case <-eventStart:
  477. // ignore, done
  478. }
  479. select {
  480. case <-time.After(5 * time.Second):
  481. c.Fatal("failed to observe container die in timely fashion")
  482. case <-eventDie:
  483. // ignore, done
  484. }
  485. rmCmd := exec.Command(dockerBinary, "rm", cleanedContainerID)
  486. out, _, err = runCommandWithOutput(rmCmd)
  487. if err != nil {
  488. c.Fatal(out, err)
  489. }
  490. select {
  491. case <-time.After(5 * time.Second):
  492. c.Fatal("failed to observe container destroy in timely fashion")
  493. case <-eventDestroy:
  494. // ignore, done
  495. }
  496. }
  497. func (s *DockerSuite) TestEventsCommit(c *check.C) {
  498. since := daemonTime(c).Unix()
  499. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "top")
  500. out, _, err := runCommandWithOutput(runCmd)
  501. if err != nil {
  502. c.Fatalf("Couldn't run top: %s\n%q", out, err)
  503. }
  504. cID := strings.TrimSpace(out)
  505. c.Assert(waitRun(cID), check.IsNil)
  506. cmd := exec.Command(dockerBinary, "commit", "-m", "test", cID)
  507. out, _, err = runCommandWithOutput(cmd)
  508. if err != nil {
  509. c.Fatalf("Couldn't commit: %s\n%q", out, err)
  510. }
  511. cmd = exec.Command(dockerBinary, "stop", cID)
  512. out, _, err = runCommandWithOutput(cmd)
  513. if err != nil {
  514. c.Fatalf("Couldn't stop: %s\n%q", out, err)
  515. }
  516. cmd = exec.Command(dockerBinary, "events", "--since=0", "-f", "container="+cID, "--until="+strconv.Itoa(int(since)))
  517. out, _, err = runCommandWithOutput(cmd)
  518. if err != nil {
  519. c.Fatalf("Couldn't get events: %s\n%q", out, err)
  520. }
  521. if !strings.Contains(out, " commit\n") {
  522. c.Fatalf("Missing 'commit' log event\n%s", out)
  523. }
  524. }
  525. func (s *DockerSuite) TestEventsCopy(c *check.C) {
  526. since := daemonTime(c).Unix()
  527. id, err := buildImage("cpimg", `
  528. FROM busybox
  529. RUN echo HI > /tmp/file`, true)
  530. if err != nil {
  531. c.Fatalf("Couldn't create image: %q", err)
  532. }
  533. runCmd := exec.Command(dockerBinary, "run", "--name=cptest", id, "true")
  534. out, _, err := runCommandWithOutput(runCmd)
  535. if err != nil {
  536. c.Fatalf("Couldn't run top: %s\n%q", out, err)
  537. }
  538. cmd := exec.Command(dockerBinary, "cp", "cptest:/tmp/file", "-")
  539. out, _, err = runCommandWithOutput(cmd)
  540. if err != nil {
  541. c.Fatalf("Failed getting file:%q\n%q", out, err)
  542. }
  543. cmd = exec.Command(dockerBinary, "events", "--since=0", "-f", "container=cptest", "--until="+strconv.Itoa(int(since)))
  544. out, _, err = runCommandWithOutput(cmd)
  545. if err != nil {
  546. c.Fatalf("Couldn't get events: %s\n%q", out, err)
  547. }
  548. if !strings.Contains(out, " copy\n") {
  549. c.Fatalf("Missing 'copy' log event\n%s", out)
  550. }
  551. }
  552. func (s *DockerSuite) TestEventsResize(c *check.C) {
  553. since := daemonTime(c).Unix()
  554. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "top")
  555. out, _, err := runCommandWithOutput(runCmd)
  556. if err != nil {
  557. c.Fatalf("Couldn't run top: %s\n%q", out, err)
  558. }
  559. cID := strings.TrimSpace(out)
  560. c.Assert(waitRun(cID), check.IsNil)
  561. endpoint := "/containers/" + cID + "/resize?h=80&w=24"
  562. status, _, err := sockRequest("POST", endpoint, nil)
  563. c.Assert(status, check.Equals, http.StatusOK)
  564. c.Assert(err, check.IsNil)
  565. cmd := exec.Command(dockerBinary, "stop", cID)
  566. out, _, err = runCommandWithOutput(cmd)
  567. if err != nil {
  568. c.Fatalf("Couldn't stop: %s\n%q", out, err)
  569. }
  570. cmd = exec.Command(dockerBinary, "events", "--since=0", "-f", "container="+cID, "--until="+strconv.Itoa(int(since)))
  571. out, _, err = runCommandWithOutput(cmd)
  572. if err != nil {
  573. c.Fatalf("Couldn't get events: %s\n%q", out, err)
  574. }
  575. if !strings.Contains(out, " resize\n") {
  576. c.Fatalf("Missing 'resize' log event\n%s", out)
  577. }
  578. }
  579. func (s *DockerSuite) TestEventsAttach(c *check.C) {
  580. since := daemonTime(c).Unix()
  581. out, _ := dockerCmd(c, "run", "-di", "busybox", "/bin/cat")
  582. cID := strings.TrimSpace(out)
  583. cmd := exec.Command(dockerBinary, "attach", cID)
  584. stdin, err := cmd.StdinPipe()
  585. c.Assert(err, check.IsNil)
  586. defer stdin.Close()
  587. stdout, err := cmd.StdoutPipe()
  588. c.Assert(err, check.IsNil)
  589. defer stdout.Close()
  590. c.Assert(cmd.Start(), check.IsNil)
  591. defer cmd.Process.Kill()
  592. // Make sure we're done attaching by writing/reading some stuff
  593. if _, err := stdin.Write([]byte("hello\n")); err != nil {
  594. c.Fatal(err)
  595. }
  596. out, err = bufio.NewReader(stdout).ReadString('\n')
  597. c.Assert(err, check.IsNil)
  598. if strings.TrimSpace(out) != "hello" {
  599. c.Fatalf("expected 'hello', got %q", out)
  600. }
  601. c.Assert(stdin.Close(), check.IsNil)
  602. cmd = exec.Command(dockerBinary, "stop", cID)
  603. out, _, err = runCommandWithOutput(cmd)
  604. if err != nil {
  605. c.Fatalf("Couldn't stop: %s\n%q", out, err)
  606. }
  607. cmd = exec.Command(dockerBinary, "events", "--since=0", "-f", "container="+cID, "--until="+strconv.Itoa(int(since)))
  608. out, _, err = runCommandWithOutput(cmd)
  609. if err != nil {
  610. c.Fatalf("Couldn't get events: %s\n%q", out, err)
  611. }
  612. if !strings.Contains(out, " attach\n") {
  613. c.Fatalf("Missing 'attach' log event\n%s", out)
  614. }
  615. }
  616. func (s *DockerSuite) TestEventsRename(c *check.C) {
  617. since := daemonTime(c).Unix()
  618. runCmd := exec.Command(dockerBinary, "run", "--name", "oldName", "busybox", "true")
  619. out, _, err := runCommandWithOutput(runCmd)
  620. if err != nil {
  621. c.Fatalf("Couldn't run true: %s\n%q", out, err)
  622. }
  623. renameCmd := exec.Command(dockerBinary, "rename", "oldName", "newName")
  624. out, _, err = runCommandWithOutput(renameCmd)
  625. if err != nil {
  626. c.Fatalf("Couldn't rename: %s\n%q", out, err)
  627. }
  628. cmd := exec.Command(dockerBinary, "events", "--since=0", "-f", "container=newName", "--until="+strconv.Itoa(int(since)))
  629. out, _, err = runCommandWithOutput(cmd)
  630. if err != nil {
  631. c.Fatalf("Couldn't get events: %s\n%q", out, err)
  632. }
  633. if !strings.Contains(out, " rename\n") {
  634. c.Fatalf("Missing 'rename' log event\n%s", out)
  635. }
  636. }
  637. func (s *DockerSuite) TestEventsTop(c *check.C) {
  638. since := daemonTime(c).Unix()
  639. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "top")
  640. out, _, err := runCommandWithOutput(runCmd)
  641. if err != nil {
  642. c.Fatalf("Couldn't run true: %s\n%q", out, err)
  643. }
  644. cID := strings.TrimSpace(out)
  645. c.Assert(waitRun(cID), check.IsNil)
  646. cmd := exec.Command(dockerBinary, "top", cID)
  647. out, _, err = runCommandWithOutput(cmd)
  648. if err != nil {
  649. c.Fatalf("Couldn't run docker top: %s\n%q", out, err)
  650. }
  651. cmd = exec.Command(dockerBinary, "stop", cID)
  652. out, _, err = runCommandWithOutput(cmd)
  653. if err != nil {
  654. c.Fatalf("Couldn't stop: %s\n%q", out, err)
  655. }
  656. cmd = exec.Command(dockerBinary, "events", "--since=0", "-f", "container="+cID, "--until="+strconv.Itoa(int(since)))
  657. out, _, err = runCommandWithOutput(cmd)
  658. if err != nil {
  659. c.Fatalf("Couldn't get events: %s\n%q", out, err)
  660. }
  661. if !strings.Contains(out, " top\n") {
  662. c.Fatalf("Missing 'top' log event\n%s", out)
  663. }
  664. }
  665. // #13753
  666. func (s *DockerSuite) TestEventsDefaultEmpty(c *check.C) {
  667. dockerCmd(c, "run", "busybox")
  668. out, _ := dockerCmd(c, "events", fmt.Sprintf("--until=%d", daemonTime(c).Unix()))
  669. c.Assert(strings.TrimSpace(out), check.Equals, "")
  670. }