2016-04-23 21:31:57 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-10-10 05:16:27 -04:00
|
|
|
"bytes"
|
2016-11-15 11:18:33 -05:00
|
|
|
"io/ioutil"
|
2016-04-23 21:31:57 -04:00
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli/command"
|
|
|
|
"github.com/docker/cli/cli/debug"
|
2017-08-07 05:52:40 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2018-06-08 12:24:26 -04:00
|
|
|
"gotest.tools/assert"
|
|
|
|
is "gotest.tools/assert/cmp"
|
2016-04-23 21:31:57 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestClientDebugEnabled(t *testing.T) {
|
2016-12-12 03:33:58 -05:00
|
|
|
defer debug.Disable()
|
2016-04-23 21:31:57 -04:00
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
cmd := newDockerCommand(&command.DockerCli{})
|
2016-06-22 18:36:51 -04:00
|
|
|
cmd.Flags().Set("debug", "true")
|
2016-06-22 13:08:04 -04:00
|
|
|
|
2016-11-15 11:18:33 -05:00
|
|
|
err := cmd.PersistentPreRunE(cmd, []string{})
|
2018-03-06 14:44:13 -05:00
|
|
|
assert.NilError(t, err)
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal("1", os.Getenv("DEBUG")))
|
|
|
|
assert.Check(t, is.Equal(logrus.DebugLevel, logrus.GetLevel()))
|
2016-11-15 11:18:33 -05:00
|
|
|
}
|
2016-04-23 21:31:57 -04:00
|
|
|
|
2019-01-28 08:52:58 -05:00
|
|
|
var discard = ioutil.NopCloser(bytes.NewBuffer(nil))
|
|
|
|
|
2016-11-15 11:18:33 -05:00
|
|
|
func TestExitStatusForInvalidSubcommandWithHelpFlag(t *testing.T) {
|
2019-01-28 08:52:58 -05:00
|
|
|
cli, err := command.NewDockerCli(command.WithInputStream(discard), command.WithCombinedStreams(ioutil.Discard))
|
|
|
|
assert.NilError(t, err)
|
|
|
|
cmd := newDockerCommand(cli)
|
2016-11-15 11:18:33 -05:00
|
|
|
cmd.SetArgs([]string{"help", "invalid"})
|
2019-01-28 08:52:58 -05:00
|
|
|
err = cmd.Execute()
|
2018-03-06 15:54:24 -05:00
|
|
|
assert.Error(t, err, "unknown help topic: invalid")
|
2016-04-23 21:31:57 -04:00
|
|
|
}
|
2018-10-10 05:16:27 -04:00
|
|
|
|
|
|
|
func TestExitStatusForInvalidSubcommand(t *testing.T) {
|
2019-01-28 08:52:58 -05:00
|
|
|
cli, err := command.NewDockerCli(command.WithInputStream(discard), command.WithCombinedStreams(ioutil.Discard))
|
|
|
|
assert.NilError(t, err)
|
|
|
|
cmd := newDockerCommand(cli)
|
2018-10-10 05:16:27 -04:00
|
|
|
cmd.SetArgs([]string{"invalid"})
|
2019-01-28 08:52:58 -05:00
|
|
|
err = cmd.Execute()
|
2018-10-10 05:16:27 -04:00
|
|
|
assert.Check(t, is.ErrorContains(err, "docker: 'invalid' is not a docker command."))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestVersion(t *testing.T) {
|
|
|
|
var b bytes.Buffer
|
2019-01-28 08:52:58 -05:00
|
|
|
cli, err := command.NewDockerCli(command.WithInputStream(discard), command.WithCombinedStreams(&b))
|
|
|
|
assert.NilError(t, err)
|
|
|
|
cmd := newDockerCommand(cli)
|
2018-10-10 05:16:27 -04:00
|
|
|
cmd.SetArgs([]string{"--version"})
|
2019-01-28 08:52:58 -05:00
|
|
|
err = cmd.Execute()
|
2018-10-10 05:16:27 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Contains(b.String(), "Docker version"))
|
|
|
|
}
|