2018-11-09 09:10:41 -05:00
|
|
|
package context
|
|
|
|
|
|
|
|
import (
|
2022-06-07 17:56:11 -04:00
|
|
|
"bytes"
|
2022-07-29 05:58:37 -04:00
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"os"
|
2018-11-09 09:10:41 -05:00
|
|
|
"path/filepath"
|
2023-06-26 07:35:42 -04:00
|
|
|
"runtime"
|
2018-11-09 09:10:41 -05:00
|
|
|
"testing"
|
|
|
|
|
2022-06-07 17:56:11 -04:00
|
|
|
"github.com/docker/cli/cli/command"
|
2018-11-09 09:10:41 -05:00
|
|
|
"github.com/docker/cli/cli/config"
|
|
|
|
"github.com/docker/cli/cli/config/configfile"
|
2022-06-07 17:56:11 -04:00
|
|
|
"github.com/docker/cli/cli/flags"
|
2022-09-28 16:18:51 -04:00
|
|
|
"github.com/docker/docker/errdefs"
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/assert"
|
2022-06-07 17:56:11 -04:00
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2018-11-09 09:10:41 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestUse(t *testing.T) {
|
2022-02-25 07:07:16 -05:00
|
|
|
configDir := t.TempDir()
|
2018-11-09 09:10:41 -05:00
|
|
|
configFilePath := filepath.Join(configDir, "config.json")
|
|
|
|
testCfg := configfile.New(configFilePath)
|
2022-02-25 07:07:16 -05:00
|
|
|
cli := makeFakeCli(t, withCliConfig(testCfg))
|
|
|
|
err := RunCreate(cli, &CreateOptions{
|
2019-01-21 03:37:20 -05:00
|
|
|
Name: "test",
|
|
|
|
Docker: map[string]string{},
|
2018-11-09 09:10:41 -05:00
|
|
|
})
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.NilError(t, newUseCommand(cli).RunE(nil, []string{"test"}))
|
|
|
|
reloadedConfig, err := config.Load(configDir)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Equal(t, "test", reloadedConfig.CurrentContext)
|
|
|
|
|
|
|
|
// switch back to default
|
|
|
|
cli.OutBuffer().Reset()
|
|
|
|
cli.ErrBuffer().Reset()
|
|
|
|
assert.NilError(t, newUseCommand(cli).RunE(nil, []string{"default"}))
|
|
|
|
reloadedConfig, err = config.Load(configDir)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Equal(t, "", reloadedConfig.CurrentContext)
|
|
|
|
assert.Equal(t, "default\n", cli.OutBuffer().String())
|
|
|
|
assert.Equal(t, "Current context is now \"default\"\n", cli.ErrBuffer().String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUseNoExist(t *testing.T) {
|
2022-02-25 07:07:16 -05:00
|
|
|
cli := makeFakeCli(t)
|
2018-11-09 09:10:41 -05:00
|
|
|
err := newUseCommand(cli).RunE(nil, []string{"test"})
|
2022-09-28 16:18:51 -04:00
|
|
|
assert.Check(t, is.ErrorType(err, errdefs.IsNotFound))
|
2018-11-09 09:10:41 -05:00
|
|
|
}
|
2022-06-07 17:56:11 -04:00
|
|
|
|
2022-07-29 05:58:37 -04:00
|
|
|
// TestUseDefaultWithoutConfigFile verifies that the CLI does not create
|
|
|
|
// the default config file and directory when using the default context.
|
|
|
|
func TestUseDefaultWithoutConfigFile(t *testing.T) {
|
|
|
|
// We must use a temporary home-directory, because this test covers
|
|
|
|
// the _default_ configuration file. If we specify a custom configuration
|
|
|
|
// file, the CLI produces an error if the file doesn't exist.
|
|
|
|
tmpHomeDir := t.TempDir()
|
2023-06-26 07:35:42 -04:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
t.Setenv("USERPROFILE", tmpHomeDir)
|
|
|
|
} else {
|
|
|
|
t.Setenv("HOME", tmpHomeDir)
|
|
|
|
}
|
2022-07-29 05:58:37 -04:00
|
|
|
configDir := filepath.Join(tmpHomeDir, ".docker")
|
|
|
|
configFilePath := filepath.Join(configDir, "config.json")
|
|
|
|
|
|
|
|
// Verify config-dir and -file don't exist before
|
|
|
|
_, err := os.Stat(configDir)
|
|
|
|
assert.Check(t, errors.Is(err, os.ErrNotExist))
|
|
|
|
_, err = os.Stat(configFilePath)
|
|
|
|
assert.Check(t, errors.Is(err, os.ErrNotExist))
|
|
|
|
|
|
|
|
cli, err := command.NewDockerCli(command.WithCombinedStreams(io.Discard))
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.NilError(t, newUseCommand(cli).RunE(nil, []string{"default"}))
|
|
|
|
|
|
|
|
// Verify config-dir and -file don't exist after
|
|
|
|
_, err = os.Stat(configDir)
|
|
|
|
assert.Check(t, errors.Is(err, os.ErrNotExist))
|
|
|
|
_, err = os.Stat(configFilePath)
|
|
|
|
assert.Check(t, errors.Is(err, os.ErrNotExist))
|
|
|
|
}
|
|
|
|
|
2022-06-07 17:56:11 -04:00
|
|
|
func TestUseHostOverride(t *testing.T) {
|
|
|
|
t.Setenv("DOCKER_HOST", "tcp://ed:2375/")
|
|
|
|
|
|
|
|
configDir := t.TempDir()
|
|
|
|
configFilePath := filepath.Join(configDir, "config.json")
|
|
|
|
testCfg := configfile.New(configFilePath)
|
|
|
|
cli := makeFakeCli(t, withCliConfig(testCfg))
|
|
|
|
err := RunCreate(cli, &CreateOptions{
|
|
|
|
Name: "test",
|
|
|
|
Docker: map[string]string{},
|
|
|
|
})
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
cli.ResetOutputBuffers()
|
|
|
|
err = newUseCommand(cli).RunE(nil, []string{"test"})
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Assert(t, is.Contains(
|
|
|
|
cli.ErrBuffer().String(),
|
|
|
|
`Warning: DOCKER_HOST environment variable overrides the active context.`,
|
|
|
|
))
|
|
|
|
assert.Assert(t, is.Contains(cli.ErrBuffer().String(), `Current context is now "test"`))
|
|
|
|
assert.Equal(t, cli.OutBuffer().String(), "test\n")
|
|
|
|
|
|
|
|
// setting DOCKER_HOST with the default context should not print a warning
|
|
|
|
cli.ResetOutputBuffers()
|
|
|
|
err = newUseCommand(cli).RunE(nil, []string{"default"})
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Assert(t, is.Contains(cli.ErrBuffer().String(), `Current context is now "default"`))
|
|
|
|
assert.Equal(t, cli.OutBuffer().String(), "default\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
// An empty DOCKER_HOST used to break the 'context use' flow.
|
|
|
|
// So we have a test with fewer fakes that tests this flow holistically.
|
|
|
|
// https://github.com/docker/cli/issues/3667
|
|
|
|
func TestUseHostOverrideEmpty(t *testing.T) {
|
|
|
|
t.Setenv("DOCKER_HOST", "")
|
|
|
|
|
|
|
|
configDir := t.TempDir()
|
|
|
|
config.SetDir(configDir)
|
|
|
|
|
|
|
|
socketPath := "unix://" + filepath.Join(configDir, "docker.sock")
|
|
|
|
|
|
|
|
var out *bytes.Buffer
|
|
|
|
var cli *command.DockerCli
|
|
|
|
|
|
|
|
loadCli := func() {
|
|
|
|
out = bytes.NewBuffer(nil)
|
|
|
|
|
|
|
|
var err error
|
|
|
|
cli, err = command.NewDockerCli(command.WithCombinedStreams(out))
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.NilError(t, cli.Initialize(flags.NewClientOptions()))
|
|
|
|
}
|
|
|
|
loadCli()
|
|
|
|
err := RunCreate(cli, &CreateOptions{
|
|
|
|
Name: "test",
|
|
|
|
Docker: map[string]string{"host": socketPath},
|
|
|
|
})
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
err = newUseCommand(cli).RunE(nil, []string{"test"})
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Assert(t, !is.Contains(out.String(), "Warning")().Success())
|
|
|
|
assert.Assert(t, is.Contains(out.String(), `Current context is now "test"`))
|
|
|
|
|
|
|
|
loadCli()
|
|
|
|
err = newShowCommand(cli).RunE(nil, nil)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Assert(t, is.Contains(out.String(), "test"))
|
|
|
|
|
|
|
|
apiclient := cli.Client()
|
|
|
|
assert.Equal(t, apiclient.DaemonHost(), socketPath)
|
|
|
|
}
|