 err!= nil {
		return "", err
	}
	err = dockerClient.PullImage(docker.PullImageOptions{Repository: "myimg"}, docker.AuthConfiguration{})
	if err!= nil {
		return "", err
	}
	if labels == nil {
		labels = make(map[string]string)
	}
	config := docker.Config{
		Image:  "myimg",
		Cmd:    []string{"mycmd"},
		Labels: labels,
	}
	opts := docker.CreateContainerOptions{Name: name, Config: &config}
	cont, err := dockerClient.CreateContainer(opts)
	if err!= nil {
		return "", err
	}
	return cont.ID, nil
}

func (s *S) SetUpTest(c *check.C) {
	var err error
	s.dockerServer, s.id, err = serverWithContainer()
	s.idShort = s.id[:12]
	c.Assert(err, check.IsNil)
	for _, env := range os.Environ() {
		if strings.HasPrefix(env, "LOG_") || strings.HasPrefix(env, "TSURU_") {
			os.Unsetenv(strings.SplitN(env, "=", 2)[0])
		}
	}
}

func (s *S) TearDownTest(c *check.C) {
	s.dockerServer.Stop()
}

func (s *S) TestLogForwarderStart(c *check.C) {
	addr, err := net.ResolveUDPAddr("udp", "127.0.0.1:0")
	c.Assert(err, check.IsNil)
	udpConn, err := net.ListenUDP("udp", addr)
	c.Assert(err, check.IsNil)
	os.Setenv("LOG_SYSLOG_FORWARD_ADDRESSES", "udp://"+udpConn.LocalAddr().String())
	lf := LogForwarder{
		BindAddress:     "udp://127.0.0.1:59317",
		DockerEndpoint:  s.dockerServer.URL(),
		EnabledBackends: []string{"syslog"},
	}
	err = lf.Start()
	c.Assert(err, check.IsNil)
	defer lf.stopWait()
	conn, err := net.Dial("udp", "127.0.0.1:59317")
	c.Assert(err, check.IsNil)
	defer conn.Close()
	msg := []byte(fmt.Sprintf("<30>2015-06-05T16:13:47Z myhost docker/%s: mymsg\n", s.id))
	_, err = conn.Write(msg)
	c.Assert(err, check.IsNil)
	buffer := make([]byte, 1024)
	udpConn.SetReadDeadline(time.Now().Add(2 * time.Second))
	n, err := udpConn.Read(buffer)
	c.Assert(err, check.IsNil)
	c.Assert(string(buffer[:n]), check.Equals, fmt.Sprintf("<30>Jun  5 13:13:47 %s coolappname[procx]: mymsg\n", s.idShort))
}

func (s *S) TestLogForwarderStartNoneBackend(c *check.C) {
	lf := LogForwarder{
		BindAddress:     "udp://127.0.0.1:59317",
		DockerEndpoint:  s.dockerServer.URL(),
		EnabledBackends: []string{"none"},
	}
	err := lf.Start()
	c.Assert(err, check.IsNil)
	defer lf.stopWait()
	conn, err := net.Dial("udp", "127.0.0.1:59317")
	c.Assert(err, check.IsNil)
	defer conn.Close()
	msg := []byte(fmt.Sprintf("<30>2015-06-05T16:13:47Z myhost docker/%s: mymsg\n", s.id))
	_, err = conn.Write(msg)
	c.Assert(err, check.IsNil)
}

func (s *S) TestLogForwarderStartWithTimezone(c *check.C) {
	os.Setenv("LOG_SYSLOG_TIMEZONE", "America/Grenada")
	addr, err := net.ResolveUDPAddr("udp", "127.0.0.1:0")
	c.Assert(err, check.IsNil)
	udpConn, err := net.ListenUDP("udp", addr)
	c.Assert(err, check.IsNil)
	os.Setenv("LOG_SYSLOG_FORWARD_ADDRESSES", "udp://"+udpConn.LocalAddr().String())
	lf := LogForwarder{
		BindAddress:     "udp://127.0.0.1:59317",
		DockerEndpoint:  s.dockerServer.URL(),
		EnabledBackends: []string{"syslog"},
	}
	err = lf.Start()
	c.Assert(err, check.IsNil)
	defer lf.stopWait()
	conn, err := net.Dial("udp", "127.0.0.1:59317")
	c.Assert(err, check.IsNil)
	defer conn.Close()
	msg := []byte(fmt.Sprintf("<30>2015-06-05T16:13:47Z myhost docker/%s: mymsg\n", s.id))
	_, err = conn.Write(msg)
	c.Assert(err, check.IsNil)
	buffer := make([]byte, 1024)
	udpConn.SetReadDeadline(time.Now().Add(2 * time.Second))
	n, err := udpConn.Read(buffer)
	c.Assert(err, check.IsNil)
	c.Assert(string(buffer[:n]), check.Equals, fmt.Sprintf("<30>Jun  5 12:13:47 %s coolappname[procx]: mymsg\n", s.idShort))
}

func (s *S) TestLogForwarderWSForwarderHTTP(c *check.C) {
	testLogForwarderWSForwarder(s, c, httptest.NewServer)
}

func (s *S) TestLogForwarderWSForwarderHTTPS(c *check.C) {
	testLogForwarderWSForwarder(s, c, httptest.NewTLSServer)
}

func recvTimeout(c *check.C, ch chan string) string {
	select {
	case data := <-ch:
		return data
	case <-time.After(5 * time.Second):
		c.Fatal("timeout waiting for channel")
	}
	return ""
}

func testLogForwarderWSForwarder(
	s *S, c *check.C,
	serverFunc func(handler http.Handler) *httptest.Server,
) {
	reqCh := make(chan *http.Request, 1)
	bodyCh := make(chan string, 1)
	srv := serverFunc(websocket.Handler(func(ws *websocket.Conn) {
		reqCh <- ws.Request()
		scanner := bufio.NewScanner(ws)
		for scanner.Scan() {
			bodyCh <- scanner.Text()
		}
	}))
	defer srv.Close()
	srvCerts := x509.NewCertPool()
	if srv.TLS!= nil {
		for _, c := range srv.TLS.Certificates {
			roots, _ := x509.ParseCertificates(c.Certificate[len(c.Certificate)-1])
			for _, root := range roots {
				srvCerts.AddCert(root)
			}
		}
	}
	os.Setenv("TSURU_ENDPOINT", srv.URL)
	os.Setenv("TSURU_TOKEN", "mytoken")
	os.Setenv("LOG_TSURU_BUFFER_SIZE", "100")
	os.Setenv("LOG_TSURU_PING_INTERVAL", "0.1")
	os.Setenv("LOG_TSURU_PONG_INTERVAL", "2.0")
	testTlsConfig = &tls.Config{RootCAs: srvCerts}
	lf := LogForwarder{
		EnabledBackends: []string{"tsuru"},
		BindAddress:     "udp://127.0.0.1:59317",
		DockerEndpoint:  s.dockerServer