File: use_test.go

package info (click to toggle)
docker.io 27.5.1%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 67,552 kB
  • sloc: sh: 5,847; makefile: 1,011; ansic: 664; python: 162; asm: 133
file content (159 lines) | stat: -rw-r--r-- 5,089 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package context

import (
	"bytes"
	"errors"
	"io"
	"os"
	"path/filepath"
	"runtime"
	"testing"

	"github.com/docker/cli/cli/command"
	"github.com/docker/cli/cli/config"
	"github.com/docker/cli/cli/config/configfile"
	"github.com/docker/cli/cli/flags"
	"github.com/docker/docker/errdefs"
	"gotest.tools/v3/assert"
	is "gotest.tools/v3/assert/cmp"
)

func TestUse(t *testing.T) {
	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)
	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) {
	cli := makeFakeCli(t)
	err := newUseCommand(cli).RunE(nil, []string{"test"})
	assert.Check(t, is.ErrorType(err, errdefs.IsNotFound))
}

// 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()
	if runtime.GOOS == "windows" {
		t.Setenv("USERPROFILE", tmpHomeDir)
	} else {
		t.Setenv("HOME", tmpHomeDir)
	}
	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))
}

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},
	})
	if err != nil {
		t.Skipf("Setup failed: %v", 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)
}