Add --log-level support

Next steps, in another PR, would be:
- make all logging go through the logrus stuff
- I'd like to see if we can remove the env var stuff (like DEBUG) but we'll see

Closes #5198

Signed-off-by: Doug Davis <dug@us.ibm.com>
This commit is contained in:
Doug Davis
2014-11-15 09:00:48 -08:00
parent 8682bac309
commit 2facc04673
10 changed files with 108 additions and 15 deletions
+61
View File
@@ -2,6 +2,7 @@ package main
import (
"encoding/json"
"io/ioutil"
"os"
"os/exec"
"strings"
@@ -223,3 +224,63 @@ func TestDaemonIptablesCreate(t *testing.T) {
logDone("daemon - run,iptables - iptables rules for always restarted container created after daemon restart")
}
func TestDaemonLoggingLevel(t *testing.T) {
d := NewDaemon(t)
if err := d.Start("--log-level=bogus"); err == nil {
t.Fatal("Daemon should not have been able to start")
}
d = NewDaemon(t)
if err := d.Start("--log-level=debug"); err != nil {
t.Fatal(err)
}
d.Stop()
content, _ := ioutil.ReadFile(d.logFile.Name())
if !strings.Contains(string(content), `level="debug"`) {
t.Fatalf(`Missing level="debug" in log file:\n%s`, string(content))
}
d = NewDaemon(t)
if err := d.Start("--log-level=fatal"); err != nil {
t.Fatal(err)
}
d.Stop()
content, _ = ioutil.ReadFile(d.logFile.Name())
if strings.Contains(string(content), `level="debug"`) {
t.Fatalf(`Should not have level="debug" in log file:\n%s`, string(content))
}
d = NewDaemon(t)
if err := d.Start("-D"); err != nil {
t.Fatal(err)
}
d.Stop()
content, _ = ioutil.ReadFile(d.logFile.Name())
if !strings.Contains(string(content), `level="debug"`) {
t.Fatalf(`Missing level="debug" in log file using -D:\n%s`, string(content))
}
d = NewDaemon(t)
if err := d.Start("--debug"); err != nil {
t.Fatal(err)
}
d.Stop()
content, _ = ioutil.ReadFile(d.logFile.Name())
if !strings.Contains(string(content), `level="debug"`) {
t.Fatalf(`Missing level="debug" in log file using --debug:\n%s`, string(content))
}
d = NewDaemon(t)
if err := d.Start("--debug", "--log-level=fatal"); err != nil {
t.Fatal(err)
}
d.Stop()
content, _ = ioutil.ReadFile(d.logFile.Name())
if !strings.Contains(string(content), `level="debug"`) {
t.Fatalf(`Missing level="debug" in log file when using both --debug and --log-level=fatal:\n%s`, string(content))
}
logDone("daemon - Logging Level")
}
+1 -1
View File
@@ -1804,7 +1804,7 @@ func TestRunWithBadDevice(t *testing.T) {
if err == nil {
t.Fatal("Run should fail with bad device")
}
expected := `"/etc": not a device node`
expected := `\"/etc\": not a device node`
if !strings.Contains(out, expected) {
t.Fatalf("Output should contain %q, actual out: %q", expected, out)
}
+21 -3
View File
@@ -41,7 +41,7 @@ func NewDaemon(t *testing.T) *Daemon {
t.Fatal("Please set the DEST environment variable")
}
dir := filepath.Join(dest, fmt.Sprintf("daemon%d", time.Now().Unix()))
dir := filepath.Join(dest, fmt.Sprintf("daemon%d", time.Now().UnixNano()%100000000))
daemonFolder, err := filepath.Abs(dir)
if err != nil {
t.Fatalf("Could not make %q an absolute path: %v", dir, err)
@@ -69,10 +69,23 @@ func (d *Daemon) Start(arg ...string) error {
args := []string{
"--host", d.sock(),
"--daemon", "--debug",
"--daemon",
"--graph", fmt.Sprintf("%s/graph", d.folder),
"--pidfile", fmt.Sprintf("%s/docker.pid", d.folder),
}
// If we don't explicitly set the log-level or debug flag(-D) then
// turn on debug mode
foundIt := false
for _, a := range arg {
if strings.Contains(a, "--log-level") || strings.Contains(a, "-D") {
foundIt = true
}
}
if !foundIt {
args = append(args, "--debug")
}
if d.storageDriver != "" {
args = append(args, "--storage-driver", d.storageDriver)
}
@@ -83,7 +96,7 @@ func (d *Daemon) Start(arg ...string) error {
args = append(args, arg...)
d.cmd = exec.Command(dockerBinary, args...)
d.logFile, err = os.OpenFile(filepath.Join(d.folder, "docker.log"), os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0600)
d.logFile, err = os.OpenFile(filepath.Join(d.folder, "docker.log"), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0600)
if err != nil {
d.t.Fatalf("Could not create %s/docker.log: %v", d.folder, err)
}
@@ -107,8 +120,13 @@ func (d *Daemon) Start(arg ...string) error {
tick := time.Tick(500 * time.Millisecond)
// make sure daemon is ready to receive requests
startTime := time.Now().Unix()
for {
d.t.Log("waiting for daemon to start")
if time.Now().Unix()-startTime > 5 {
// After 5 seconds, give up
return errors.New("Daemon exited and never started")
}
select {
case <-time.After(2 * time.Second):
return errors.New("timeout: daemon does not respond")