Signed-off-by: David Calavera <david.calavera@gmail.com>
| ... | ... |
@@ -6,6 +6,7 @@ import ( |
| 6 | 6 |
"bytes" |
| 7 | 7 |
"encoding/json" |
| 8 | 8 |
"fmt" |
| 9 |
+ "io" |
|
| 9 | 10 |
"io/ioutil" |
| 10 | 11 |
"net" |
| 11 | 12 |
"os" |
| ... | ... |
@@ -22,6 +23,7 @@ import ( |
| 22 | 22 |
"github.com/docker/libnetwork/iptables" |
| 23 | 23 |
"github.com/docker/libtrust" |
| 24 | 24 |
"github.com/go-check/check" |
| 25 |
+ "github.com/kr/pty" |
|
| 25 | 26 |
) |
| 26 | 27 |
|
| 27 | 28 |
func (s *DockerDaemonSuite) TestDaemonRestartWithRunningContainersPorts(c *check.C) {
|
| ... | ... |
@@ -584,7 +586,7 @@ func (s *DockerDaemonSuite) TestDaemonExitOnFailure(c *check.C) {
|
| 584 | 584 |
c.Fatalf("Expected daemon not to start, got %v", err)
|
| 585 | 585 |
} |
| 586 | 586 |
// look in the log and make sure we got the message that daemon is shutting down |
| 587 |
- runCmd := exec.Command("grep", "Error starting daemon", s.d.LogfileName())
|
|
| 587 |
+ runCmd := exec.Command("grep", "Error starting daemon", s.d.LogFileName())
|
|
| 588 | 588 |
if out, _, err := runCommandWithOutput(runCmd); err != nil {
|
| 589 | 589 |
c.Fatalf("Expected 'Error starting daemon' message; but doesn't exist in log: %q, err: %v", out, err)
|
| 590 | 590 |
} |
| ... | ... |
@@ -1759,7 +1761,7 @@ func (s *DockerDaemonSuite) TestDaemonRestartLocalVolumes(c *check.C) {
|
| 1759 | 1759 |
func (s *DockerDaemonSuite) TestDaemonCorruptedLogDriverAddress(c *check.C) {
|
| 1760 | 1760 |
c.Assert(s.d.Start("--log-driver=syslog", "--log-opt", "syslog-address=corrupted:42"), check.NotNil)
|
| 1761 | 1761 |
expected := "Failed to set log opts: syslog-address should be in form proto://address" |
| 1762 |
- runCmd := exec.Command("grep", expected, s.d.LogfileName())
|
|
| 1762 |
+ runCmd := exec.Command("grep", expected, s.d.LogFileName())
|
|
| 1763 | 1763 |
if out, _, err := runCommandWithOutput(runCmd); err != nil {
|
| 1764 | 1764 |
c.Fatalf("Expected %q message; but doesn't exist in log: %q, err: %v", expected, out, err)
|
| 1765 | 1765 |
} |
| ... | ... |
@@ -1768,7 +1770,7 @@ func (s *DockerDaemonSuite) TestDaemonCorruptedLogDriverAddress(c *check.C) {
|
| 1768 | 1768 |
func (s *DockerDaemonSuite) TestDaemonCorruptedFluentdAddress(c *check.C) {
|
| 1769 | 1769 |
c.Assert(s.d.Start("--log-driver=fluentd", "--log-opt", "fluentd-address=corrupted:c"), check.NotNil)
|
| 1770 | 1770 |
expected := "Failed to set log opts: invalid fluentd-address corrupted:c: " |
| 1771 |
- runCmd := exec.Command("grep", expected, s.d.LogfileName())
|
|
| 1771 |
+ runCmd := exec.Command("grep", expected, s.d.LogFileName())
|
|
| 1772 | 1772 |
if out, _, err := runCommandWithOutput(runCmd); err != nil {
|
| 1773 | 1773 |
c.Fatalf("Expected %q message; but doesn't exist in log: %q, err: %v", expected, out, err)
|
| 1774 | 1774 |
} |
| ... | ... |
@@ -1922,7 +1924,7 @@ func (s *DockerDaemonSuite) TestDaemonRestartContainerLinksRestart(c *check.C) {
|
| 1922 | 1922 |
c.Assert(err, check.IsNil) |
| 1923 | 1923 |
// clear the log file -- we don't need any of it but may for the next part |
| 1924 | 1924 |
// can ignore the error here, this is just a cleanup |
| 1925 |
- os.Truncate(d.LogfileName(), 0) |
|
| 1925 |
+ os.Truncate(d.LogFileName(), 0) |
|
| 1926 | 1926 |
err = d.Start() |
| 1927 | 1927 |
c.Assert(err, check.IsNil) |
| 1928 | 1928 |
|
| ... | ... |
@@ -1930,7 +1932,7 @@ func (s *DockerDaemonSuite) TestDaemonRestartContainerLinksRestart(c *check.C) {
|
| 1930 | 1930 |
out, err := d.Cmd("inspect", "-f", "{{ .State.Running }}", "parent"+num)
|
| 1931 | 1931 |
c.Assert(err, check.IsNil) |
| 1932 | 1932 |
if strings.TrimSpace(out) != "true" {
|
| 1933 |
- log, _ := ioutil.ReadFile(d.LogfileName()) |
|
| 1933 |
+ log, _ := ioutil.ReadFile(d.LogFileName()) |
|
| 1934 | 1934 |
c.Fatalf("parent container is not running\n%s", string(log))
|
| 1935 | 1935 |
} |
| 1936 | 1936 |
} |
| ... | ... |
@@ -2064,3 +2066,32 @@ func (s *DockerDaemonSuite) TestRunLinksChanged(c *check.C) {
|
| 2064 | 2064 |
c.Assert(err, check.NotNil, check.Commentf(out)) |
| 2065 | 2065 |
c.Assert(out, check.Not(checker.Contains), "1 packets transmitted, 1 packets received") |
| 2066 | 2066 |
} |
| 2067 |
+ |
|
| 2068 |
+func (s *DockerDaemonSuite) TestDaemonStartWithoutColors(c *check.C) {
|
|
| 2069 |
+ testRequires(c, DaemonIsLinux) |
|
| 2070 |
+ newD := NewDaemon(c) |
|
| 2071 |
+ |
|
| 2072 |
+ infoLog := "\x1b[34mINFO\x1b" |
|
| 2073 |
+ |
|
| 2074 |
+ p, tty, err := pty.Open() |
|
| 2075 |
+ c.Assert(err, checker.IsNil) |
|
| 2076 |
+ defer func() {
|
|
| 2077 |
+ tty.Close() |
|
| 2078 |
+ p.Close() |
|
| 2079 |
+ }() |
|
| 2080 |
+ |
|
| 2081 |
+ b := bytes.NewBuffer(nil) |
|
| 2082 |
+ go io.Copy(b, p) |
|
| 2083 |
+ |
|
| 2084 |
+ // Enable coloring explicitly |
|
| 2085 |
+ newD.StartWithLogFile(tty, "--raw-logs=false") |
|
| 2086 |
+ newD.Stop() |
|
| 2087 |
+ c.Assert(b.String(), checker.Contains, infoLog) |
|
| 2088 |
+ |
|
| 2089 |
+ b.Reset() |
|
| 2090 |
+ |
|
| 2091 |
+ // Disable coloring explicitly |
|
| 2092 |
+ newD.StartWithLogFile(tty, "--raw-logs=true") |
|
| 2093 |
+ newD.Stop() |
|
| 2094 |
+ c.Assert(b.String(), check.Not(checker.Contains), infoLog) |
|
| 2095 |
+} |
| ... | ... |
@@ -299,7 +299,7 @@ func (s *DockerExternalGraphdriverSuite) TearDownSuite(c *check.C) {
|
| 299 | 299 |
|
| 300 | 300 |
func (s *DockerExternalGraphdriverSuite) TestExternalGraphDriver(c *check.C) {
|
| 301 | 301 |
if err := s.d.StartWithBusybox("-s", "test-external-graph-driver"); err != nil {
|
| 302 |
- b, _ := ioutil.ReadFile(s.d.LogfileName()) |
|
| 302 |
+ b, _ := ioutil.ReadFile(s.d.LogFileName()) |
|
| 303 | 303 |
c.Assert(err, check.IsNil, check.Commentf("\n%s", string(b)))
|
| 304 | 304 |
} |
| 305 | 305 |
|
| ... | ... |
@@ -205,7 +205,15 @@ func (d *Daemon) getClientConfig() (*clientConfig, error) {
|
| 205 | 205 |
|
| 206 | 206 |
// Start will start the daemon and return once it is ready to receive requests. |
| 207 | 207 |
// You can specify additional daemon flags. |
| 208 |
-func (d *Daemon) Start(arg ...string) error {
|
|
| 208 |
+func (d *Daemon) Start(args ...string) error {
|
|
| 209 |
+ logFile, err := os.OpenFile(filepath.Join(d.folder, "docker.log"), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0600) |
|
| 210 |
+ d.c.Assert(err, check.IsNil, check.Commentf("[%s] Could not create %s/docker.log", d.id, d.folder))
|
|
| 211 |
+ |
|
| 212 |
+ return d.StartWithLogFile(logFile, args...) |
|
| 213 |
+} |
|
| 214 |
+ |
|
| 215 |
+// StartWithLogFile will start the daemon and attach its streams to a given file. |
|
| 216 |
+func (d *Daemon) StartWithLogFile(out *os.File, providedArgs ...string) error {
|
|
| 209 | 217 |
dockerBinary, err := exec.LookPath(dockerBinary) |
| 210 | 218 |
d.c.Assert(err, check.IsNil, check.Commentf("[%s] could not find docker binary in $PATH", d.id))
|
| 211 | 219 |
|
| ... | ... |
@@ -226,7 +234,7 @@ func (d *Daemon) Start(arg ...string) error {
|
| 226 | 226 |
// turn on debug mode |
| 227 | 227 |
foundLog := false |
| 228 | 228 |
foundSd := false |
| 229 |
- for _, a := range arg {
|
|
| 229 |
+ for _, a := range providedArgs {
|
|
| 230 | 230 |
if strings.Contains(a, "--log-level") || strings.Contains(a, "-D") || strings.Contains(a, "--debug") {
|
| 231 | 231 |
foundLog = true |
| 232 | 232 |
} |
| ... | ... |
@@ -241,14 +249,12 @@ func (d *Daemon) Start(arg ...string) error {
|
| 241 | 241 |
args = append(args, "--storage-driver", d.storageDriver) |
| 242 | 242 |
} |
| 243 | 243 |
|
| 244 |
- args = append(args, arg...) |
|
| 244 |
+ args = append(args, providedArgs...) |
|
| 245 | 245 |
d.cmd = exec.Command(dockerBinary, args...) |
| 246 | 246 |
|
| 247 |
- d.logFile, err = os.OpenFile(filepath.Join(d.folder, "docker.log"), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0600) |
|
| 248 |
- d.c.Assert(err, check.IsNil, check.Commentf("[%s] Could not create %s/docker.log", d.id, d.folder))
|
|
| 249 |
- |
|
| 250 |
- d.cmd.Stdout = d.logFile |
|
| 251 |
- d.cmd.Stderr = d.logFile |
|
| 247 |
+ d.cmd.Stdout = out |
|
| 248 |
+ d.cmd.Stderr = out |
|
| 249 |
+ d.logFile = out |
|
| 252 | 250 |
|
| 253 | 251 |
if err := d.cmd.Start(); err != nil {
|
| 254 | 252 |
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 |
| 472 | 472 |
return string(b), err |
| 473 | 473 |
} |
| 474 | 474 |
|
| 475 |
-// LogfileName returns the path the the daemon's log file |
|
| 476 |
-func (d *Daemon) LogfileName() string {
|
|
| 475 |
+// LogFileName returns the path the the daemon's log file |
|
| 476 |
+func (d *Daemon) LogFileName() string {
|
|
| 477 | 477 |
return d.logFile.Name() |
| 478 | 478 |
} |
| 479 | 479 |
|