github.com/noxiouz/docker@v0.7.3-0.20160629055221-3d231c78e8c5/integration-cli/docker_api_events_test.go (about)

     1  package main
     2  
     3  import (
     4  	"encoding/json"
     5  	"io"
     6  	"net/http"
     7  	"net/url"
     8  	"strconv"
     9  	"strings"
    10  	"time"
    11  
    12  	"github.com/docker/docker/pkg/integration/checker"
    13  	"github.com/docker/docker/pkg/jsonmessage"
    14  	"github.com/go-check/check"
    15  )
    16  
    17  func (s *DockerSuite) TestEventsApiEmptyOutput(c *check.C) {
    18  	type apiResp struct {
    19  		resp *http.Response
    20  		err  error
    21  	}
    22  	chResp := make(chan *apiResp)
    23  	go func() {
    24  		resp, body, err := sockRequestRaw("GET", "/events", nil, "")
    25  		body.Close()
    26  		chResp <- &apiResp{resp, err}
    27  	}()
    28  
    29  	select {
    30  	case r := <-chResp:
    31  		c.Assert(r.err, checker.IsNil)
    32  		c.Assert(r.resp.StatusCode, checker.Equals, http.StatusOK)
    33  	case <-time.After(3 * time.Second):
    34  		c.Fatal("timeout waiting for events api to respond, should have responded immediately")
    35  	}
    36  }
    37  
    38  func (s *DockerSuite) TestEventsApiBackwardsCompatible(c *check.C) {
    39  	since := daemonTime(c).Unix()
    40  	ts := strconv.FormatInt(since, 10)
    41  
    42  	out, _ := runSleepingContainer(c, "--name=foo", "-d")
    43  	containerID := strings.TrimSpace(out)
    44  	c.Assert(waitRun(containerID), checker.IsNil)
    45  
    46  	q := url.Values{}
    47  	q.Set("since", ts)
    48  
    49  	_, body, err := sockRequestRaw("GET", "/events?"+q.Encode(), nil, "")
    50  	c.Assert(err, checker.IsNil)
    51  	defer body.Close()
    52  
    53  	dec := json.NewDecoder(body)
    54  	var containerCreateEvent *jsonmessage.JSONMessage
    55  	for {
    56  		var event jsonmessage.JSONMessage
    57  		if err := dec.Decode(&event); err != nil {
    58  			if err == io.EOF {
    59  				break
    60  			}
    61  			c.Fatal(err)
    62  		}
    63  		if event.Status == "create" && event.ID == containerID {
    64  			containerCreateEvent = &event
    65  			break
    66  		}
    67  	}
    68  
    69  	c.Assert(containerCreateEvent, checker.Not(checker.IsNil))
    70  	c.Assert(containerCreateEvent.Status, checker.Equals, "create")
    71  	c.Assert(containerCreateEvent.ID, checker.Equals, containerID)
    72  	c.Assert(containerCreateEvent.From, checker.Equals, "busybox")
    73  }