github.com/pwn-term/docker@v0.0.0-20210616085119-6e977cce2565/cli/cmd/docker/docker_test.go (about) 1 package main 2 3 import ( 4 "bytes" 5 "io" 6 "io/ioutil" 7 "os" 8 "testing" 9 10 "github.com/docker/cli/cli/command" 11 "github.com/docker/cli/cli/debug" 12 "github.com/sirupsen/logrus" 13 "gotest.tools/v3/assert" 14 is "gotest.tools/v3/assert/cmp" 15 ) 16 17 func TestClientDebugEnabled(t *testing.T) { 18 defer debug.Disable() 19 20 tcmd := newDockerCommand(&command.DockerCli{}) 21 tcmd.SetFlag("debug", "true") 22 cmd, _, err := tcmd.HandleGlobalFlags() 23 assert.NilError(t, err) 24 assert.NilError(t, tcmd.Initialize()) 25 err = cmd.PersistentPreRunE(cmd, []string{}) 26 assert.NilError(t, err) 27 assert.Check(t, is.Equal("1", os.Getenv("DEBUG"))) 28 assert.Check(t, is.Equal(logrus.DebugLevel, logrus.GetLevel())) 29 } 30 31 var discard = ioutil.NopCloser(bytes.NewBuffer(nil)) 32 33 func runCliCommand(t *testing.T, r io.ReadCloser, w io.Writer, args ...string) error { 34 t.Helper() 35 if r == nil { 36 r = discard 37 } 38 if w == nil { 39 w = ioutil.Discard 40 } 41 cli, err := command.NewDockerCli(command.WithInputStream(r), command.WithCombinedStreams(w)) 42 assert.NilError(t, err) 43 tcmd := newDockerCommand(cli) 44 tcmd.SetArgs(args) 45 cmd, _, err := tcmd.HandleGlobalFlags() 46 assert.NilError(t, err) 47 assert.NilError(t, tcmd.Initialize()) 48 return cmd.Execute() 49 } 50 51 func TestExitStatusForInvalidSubcommandWithHelpFlag(t *testing.T) { 52 err := runCliCommand(t, nil, nil, "help", "invalid") 53 assert.Error(t, err, "unknown help topic: invalid") 54 } 55 56 func TestExitStatusForInvalidSubcommand(t *testing.T) { 57 err := runCliCommand(t, nil, nil, "invalid") 58 assert.Check(t, is.ErrorContains(err, "docker: 'invalid' is not a docker command.")) 59 } 60 61 func TestVersion(t *testing.T) { 62 var b bytes.Buffer 63 err := runCliCommand(t, nil, &b, "--version") 64 assert.NilError(t, err) 65 assert.Check(t, is.Contains(b.String(), "Docker version")) 66 }