github.com/xiaobinqt/libcompose@v1.1.0/integration/api_event_test.go (about)

     1  package integration
     2  
     3  import (
     4  	"time"
     5  
     6  	"golang.org/x/net/context"
     7  	check "gopkg.in/check.v1"
     8  
     9  	"github.com/xiaobinqt/libcompose/docker"
    10  	"github.com/xiaobinqt/libcompose/docker/ctx"
    11  	"github.com/xiaobinqt/libcompose/project"
    12  	"github.com/xiaobinqt/libcompose/project/events"
    13  	"github.com/xiaobinqt/libcompose/project/options"
    14  )
    15  
    16  func (s *APISuite) TestEvents(c *check.C) {
    17  	testRequires(c, not(DaemonVersionIs("1.9")))
    18  	composeFile := `
    19  simple:
    20    image: busybox:latest
    21    command: top
    22  another:
    23    image: busybox:latest
    24    command: top
    25  `
    26  	project, err := docker.NewProject(&ctx.Context{
    27  		Context: project.Context{
    28  			ComposeBytes: [][]byte{[]byte(composeFile)},
    29  			ProjectName:  "test-api-events",
    30  		},
    31  	}, nil)
    32  	c.Assert(err, check.IsNil)
    33  
    34  	ctx, cancelFun := context.WithCancel(context.Background())
    35  
    36  	evts, err := project.Events(ctx)
    37  	c.Assert(err, check.IsNil)
    38  
    39  	go func() {
    40  		c.Assert(project.Up(ctx, options.Up{}), check.IsNil)
    41  		// Close after everything is done
    42  		time.Sleep(250 * time.Millisecond)
    43  		cancelFun()
    44  		close(evts)
    45  	}()
    46  
    47  	actual := []events.ContainerEvent{}
    48  	for event := range evts {
    49  		actual = append(actual, event)
    50  	}
    51  
    52  	// Should be 4 events (2 create, 2 start)
    53  	c.Assert(len(actual), check.Equals, 4, check.Commentf("%v", actual))
    54  }