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