Skip to content

Commit

Permalink
Add regression tests for client debug flag.
Browse files Browse the repository at this point in the history
- Add client debug info to the `docker info` command.

Signed-off-by: David Calavera <[email protected]>
  • Loading branch information
calavera committed Feb 2, 2016
1 parent b6a6ed7 commit 9f315dd
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 1 deletion.
5 changes: 4 additions & 1 deletion api/client/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
Cli "github.com/docker/docker/cli"
"github.com/docker/docker/pkg/ioutils"
flag "github.com/docker/docker/pkg/mflag"
"github.com/docker/docker/utils"
"github.com/docker/go-units"
)

Expand Down Expand Up @@ -73,8 +74,10 @@ func (cli *DockerCli) CmdInfo(args ...string) error {
ioutils.FprintfIfNotEmpty(cli.out, "Name: %s\n", info.Name)
ioutils.FprintfIfNotEmpty(cli.out, "ID: %s\n", info.ID)

fmt.Fprintf(cli.out, "Debug mode (client): %v\n", utils.IsDebugEnabled())
fmt.Fprintf(cli.out, "Debug mode (server): %v\n", info.Debug)

if info.Debug {
fmt.Fprintf(cli.out, "Debug mode (server): %v\n", info.Debug)
fmt.Fprintf(cli.out, " File Descriptors: %d\n", info.NFd)
fmt.Fprintf(cli.out, " Goroutines: %d\n", info.NGoroutines)
fmt.Fprintf(cli.out, " System Time: %s\n", info.SystemTime)
Expand Down
23 changes: 23 additions & 0 deletions docker/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main

import (
"os"
"testing"

"github.com/Sirupsen/logrus"
"github.com/docker/docker/utils"
)

func TestClientDebugEnabled(t *testing.T) {
defer utils.DisableDebug()

clientFlags.Common.FlagSet.Parse([]string{"-D"})
clientFlags.PostParse()

if os.Getenv("DEBUG") != "1" {
t.Fatal("expected debug enabled, got false")
}
if logrus.GetLevel() != logrus.DebugLevel {
t.Fatalf("expected logrus debug level, got %v", logrus.GetLevel())
}
}
21 changes: 21 additions & 0 deletions integration-cli/docker_cli_daemon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2095,3 +2095,24 @@ func (s *DockerDaemonSuite) TestDaemonStartWithoutColors(c *check.C) {
newD.Stop()
c.Assert(b.String(), check.Not(checker.Contains), infoLog)
}

func (s *DockerDaemonSuite) TestDaemonDebugLog(c *check.C) {
testRequires(c, DaemonIsLinux)
newD := NewDaemon(c)

debugLog := "\x1b[37mDEBU\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)

newD.StartWithLogFile(tty, "--debug")
newD.Stop()
c.Assert(b.String(), checker.Contains, debugLog)
}
19 changes: 19 additions & 0 deletions integration-cli/docker_cli_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,22 @@ func (s *DockerSuite) TestInfoDisplaysStoppedContainers(c *check.C) {
c.Assert(out, checker.Contains, fmt.Sprintf(" Paused: %d\n", 0))
c.Assert(out, checker.Contains, fmt.Sprintf(" Stopped: %d\n", 1))
}

func (s *DockerSuite) TestInfoDebug(c *check.C) {
testRequires(c, SameHostDaemon, DaemonIsLinux)

d := NewDaemon(c)
err := d.Start("--debug")
c.Assert(err, checker.IsNil)
defer d.Stop()

out, err := d.Cmd("--debug", "info")
c.Assert(err, checker.IsNil)
c.Assert(out, checker.Contains, "Debug mode (client): true\n")
c.Assert(out, checker.Contains, "Debug mode (server): true\n")
c.Assert(out, checker.Contains, "File Descriptors")
c.Assert(out, checker.Contains, "Goroutines")
c.Assert(out, checker.Contains, "System Time")
c.Assert(out, checker.Contains, "EventsListeners")
c.Assert(out, checker.Contains, "Docker Root Dir")
}
43 changes: 43 additions & 0 deletions utils/debug_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package utils

import (
"os"
"testing"

"github.com/Sirupsen/logrus"
)

func TestEnableDebug(t *testing.T) {
defer func() {
os.Setenv("DEBUG", "")
logrus.SetLevel(logrus.InfoLevel)
}()
EnableDebug()
if os.Getenv("DEBUG") != "1" {
t.Fatalf("expected DEBUG=1, got %s\n", os.Getenv("DEBUG"))
}
if logrus.GetLevel() != logrus.DebugLevel {
t.Fatalf("expected log level %v, got %v\n", logrus.DebugLevel, logrus.GetLevel())
}
}

func TestDisableDebug(t *testing.T) {
DisableDebug()
if os.Getenv("DEBUG") != "" {
t.Fatalf("expected DEBUG=\"\", got %s\n", os.Getenv("DEBUG"))
}
if logrus.GetLevel() != logrus.InfoLevel {
t.Fatalf("expected log level %v, got %v\n", logrus.InfoLevel, logrus.GetLevel())
}
}

func TestDebugEnabled(t *testing.T) {
EnableDebug()
if !IsDebugEnabled() {
t.Fatal("expected debug enabled, got false")
}
DisableDebug()
if IsDebugEnabled() {
t.Fatal("expected debug disabled, got true")
}
}

0 comments on commit 9f315dd

Please sign in to comment.