Add test to make sure raw logs are properly activated.

Signed-off-by: David Calavera <david.calavera@gmail.com>
This commit is contained in:
David Calavera 2016-01-28 13:33:35 -05:00
parent 87a450a37f
commit 898599171e
3 changed files with 53 additions and 16 deletions

View file

@ -6,6 +6,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net"
"os"
@ -22,6 +23,7 @@ import (
"github.com/docker/libnetwork/iptables"
"github.com/docker/libtrust"
"github.com/go-check/check"
"github.com/kr/pty"
)
func (s *DockerDaemonSuite) TestDaemonRestartWithRunningContainersPorts(c *check.C) {
@ -584,7 +586,7 @@ func (s *DockerDaemonSuite) TestDaemonExitOnFailure(c *check.C) {
c.Fatalf("Expected daemon not to start, got %v", err)
}
// look in the log and make sure we got the message that daemon is shutting down
runCmd := exec.Command("grep", "Error starting daemon", s.d.LogfileName())
runCmd := exec.Command("grep", "Error starting daemon", s.d.LogFileName())
if out, _, err := runCommandWithOutput(runCmd); err != nil {
c.Fatalf("Expected 'Error starting daemon' message; but doesn't exist in log: %q, err: %v", out, err)
}
@ -1759,7 +1761,7 @@ func (s *DockerDaemonSuite) TestDaemonRestartLocalVolumes(c *check.C) {
func (s *DockerDaemonSuite) TestDaemonCorruptedLogDriverAddress(c *check.C) {
c.Assert(s.d.Start("--log-driver=syslog", "--log-opt", "syslog-address=corrupted:42"), check.NotNil)
expected := "Failed to set log opts: syslog-address should be in form proto://address"
runCmd := exec.Command("grep", expected, s.d.LogfileName())
runCmd := exec.Command("grep", expected, s.d.LogFileName())
if out, _, err := runCommandWithOutput(runCmd); err != nil {
c.Fatalf("Expected %q message; but doesn't exist in log: %q, err: %v", expected, out, err)
}
@ -1768,7 +1770,7 @@ func (s *DockerDaemonSuite) TestDaemonCorruptedLogDriverAddress(c *check.C) {
func (s *DockerDaemonSuite) TestDaemonCorruptedFluentdAddress(c *check.C) {
c.Assert(s.d.Start("--log-driver=fluentd", "--log-opt", "fluentd-address=corrupted:c"), check.NotNil)
expected := "Failed to set log opts: invalid fluentd-address corrupted:c: "
runCmd := exec.Command("grep", expected, s.d.LogfileName())
runCmd := exec.Command("grep", expected, s.d.LogFileName())
if out, _, err := runCommandWithOutput(runCmd); err != nil {
c.Fatalf("Expected %q message; but doesn't exist in log: %q, err: %v", expected, out, err)
}
@ -1922,7 +1924,7 @@ func (s *DockerDaemonSuite) TestDaemonRestartContainerLinksRestart(c *check.C) {
c.Assert(err, check.IsNil)
// clear the log file -- we don't need any of it but may for the next part
// can ignore the error here, this is just a cleanup
os.Truncate(d.LogfileName(), 0)
os.Truncate(d.LogFileName(), 0)
err = d.Start()
c.Assert(err, check.IsNil)
@ -1930,7 +1932,7 @@ func (s *DockerDaemonSuite) TestDaemonRestartContainerLinksRestart(c *check.C) {
out, err := d.Cmd("inspect", "-f", "{{ .State.Running }}", "parent"+num)
c.Assert(err, check.IsNil)
if strings.TrimSpace(out) != "true" {
log, _ := ioutil.ReadFile(d.LogfileName())
log, _ := ioutil.ReadFile(d.LogFileName())
c.Fatalf("parent container is not running\n%s", string(log))
}
}
@ -2064,3 +2066,32 @@ func (s *DockerDaemonSuite) TestRunLinksChanged(c *check.C) {
c.Assert(err, check.NotNil, check.Commentf(out))
c.Assert(out, check.Not(checker.Contains), "1 packets transmitted, 1 packets received")
}
func (s *DockerDaemonSuite) TestDaemonStartWithoutColors(c *check.C) {
testRequires(c, DaemonIsLinux)
newD := NewDaemon(c)
infoLog := "\x1b[34mINFO\x1b"
p, tty, err := pty.Open()
c.Assert(err, checker.IsNil)
defer func() {
tty.Close()
p.Close()
}()
b := bytes.NewBuffer(nil)
go io.Copy(b, p)
// Enable coloring explicitly
newD.StartWithLogFile(tty, "--raw-logs=false")
newD.Stop()
c.Assert(b.String(), checker.Contains, infoLog)
b.Reset()
// Disable coloring explicitly
newD.StartWithLogFile(tty, "--raw-logs=true")
newD.Stop()
c.Assert(b.String(), check.Not(checker.Contains), infoLog)
}

View file

@ -299,7 +299,7 @@ func (s *DockerExternalGraphdriverSuite) TearDownSuite(c *check.C) {
func (s *DockerExternalGraphdriverSuite) TestExternalGraphDriver(c *check.C) {
if err := s.d.StartWithBusybox("-s", "test-external-graph-driver"); err != nil {
b, _ := ioutil.ReadFile(s.d.LogfileName())
b, _ := ioutil.ReadFile(s.d.LogFileName())
c.Assert(err, check.IsNil, check.Commentf("\n%s", string(b)))
}

View file

@ -205,7 +205,15 @@ func (d *Daemon) getClientConfig() (*clientConfig, error) {
// Start will start the daemon and return once it is ready to receive requests.
// You can specify additional daemon flags.
func (d *Daemon) Start(arg ...string) error {
func (d *Daemon) Start(args ...string) error {
logFile, err := os.OpenFile(filepath.Join(d.folder, "docker.log"), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0600)
d.c.Assert(err, check.IsNil, check.Commentf("[%s] Could not create %s/docker.log", d.id, d.folder))
return d.StartWithLogFile(logFile, args...)
}
// StartWithLogFile will start the daemon and attach its streams to a given file.
func (d *Daemon) StartWithLogFile(out *os.File, providedArgs ...string) error {
dockerBinary, err := exec.LookPath(dockerBinary)
d.c.Assert(err, check.IsNil, check.Commentf("[%s] could not find docker binary in $PATH", d.id))
@ -226,7 +234,7 @@ func (d *Daemon) Start(arg ...string) error {
// turn on debug mode
foundLog := false
foundSd := false
for _, a := range arg {
for _, a := range providedArgs {
if strings.Contains(a, "--log-level") || strings.Contains(a, "-D") || strings.Contains(a, "--debug") {
foundLog = true
}
@ -241,14 +249,12 @@ func (d *Daemon) Start(arg ...string) error {
args = append(args, "--storage-driver", d.storageDriver)
}
args = append(args, arg...)
args = append(args, providedArgs...)
d.cmd = exec.Command(dockerBinary, args...)
d.logFile, err = os.OpenFile(filepath.Join(d.folder, "docker.log"), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0600)
d.c.Assert(err, check.IsNil, check.Commentf("[%s] Could not create %s/docker.log", d.id, d.folder))
d.cmd.Stdout = d.logFile
d.cmd.Stderr = d.logFile
d.cmd.Stdout = out
d.cmd.Stderr = out
d.logFile = out
if err := d.cmd.Start(); err != nil {
return fmt.Errorf("[%s] could not start daemon container: %v", d.id, err)
@ -472,8 +478,8 @@ func (d *Daemon) CmdWithArgs(daemonArgs []string, name string, arg ...string) (s
return string(b), err
}
// LogfileName returns the path the the daemon's log file
func (d *Daemon) LogfileName() string {
// LogFileName returns the path the the daemon's log file
func (d *Daemon) LogFileName() string {
return d.logFile.Name()
}