cli/command/context: remove deprecated io/ioutil and use t.TempDir()

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
(cherry picked from commit 76b47359cb)
Signed-off-by: Cory Snider <csnider@mirantis.com>
This commit is contained in:
Sebastiaan van Stijn 2022-02-25 13:07:16 +01:00 committed by Cory Snider
parent b4d152f7ad
commit 8d0c8ef081
7 changed files with 39 additions and 86 deletions

View File

@ -2,8 +2,6 @@ package context
import (
"fmt"
"io/ioutil"
"os"
"testing"
"github.com/docker/cli/cli/command"
@ -16,9 +14,9 @@ import (
"gotest.tools/v3/env"
)
func makeFakeCli(t *testing.T, opts ...func(*test.FakeCli)) (*test.FakeCli, func()) {
dir, err := ioutil.TempDir("", t.Name())
assert.NilError(t, err)
func makeFakeCli(t *testing.T, opts ...func(*test.FakeCli)) *test.FakeCli {
t.Helper()
dir := t.TempDir()
storeConfig := store.NewConfig(
func() interface{} { return &command.DockerContext{} },
store.EndpointTypeGetter(docker.DockerEndpoint, func() interface{} { return &docker.EndpointMeta{} }),
@ -44,15 +42,12 @@ func makeFakeCli(t *testing.T, opts ...func(*test.FakeCli)) (*test.FakeCli, func
}, nil
},
}
cleanup := func() {
os.RemoveAll(dir)
}
result := test.NewFakeCli(nil, opts...)
for _, o := range opts {
o(result)
}
result.SetContextStore(store)
return result, cleanup
return result
}
func withCliConfig(configFile *configfile.ConfigFile) func(*test.FakeCli) {
@ -62,8 +57,7 @@ func withCliConfig(configFile *configfile.ConfigFile) func(*test.FakeCli) {
}
func TestCreateInvalids(t *testing.T) {
cli, cleanup := makeFakeCli(t)
defer cleanup()
cli := makeFakeCli(t)
assert.NilError(t, cli.ContextStore().CreateOrUpdate(store.Metadata{Name: "existing-context"}))
tests := []struct {
options CreateOptions
@ -138,8 +132,7 @@ func assertContextCreateLogging(t *testing.T, cli *test.FakeCli, n string) {
}
func TestCreateOrchestratorSwarm(t *testing.T) {
cli, cleanup := makeFakeCli(t)
defer cleanup()
cli := makeFakeCli(t)
err := RunCreate(cli, &CreateOptions{
Name: "test",
@ -151,8 +144,7 @@ func TestCreateOrchestratorSwarm(t *testing.T) {
}
func TestCreateOrchestratorEmpty(t *testing.T) {
cli, cleanup := makeFakeCli(t)
defer cleanup()
cli := makeFakeCli(t)
err := RunCreate(cli, &CreateOptions{
Name: "test",
@ -192,8 +184,7 @@ func createTestContextWithKube(t *testing.T, cli command.Cli) {
}
func TestCreateOrchestratorAllKubernetesEndpointFromCurrent(t *testing.T) {
cli, cleanup := makeFakeCli(t)
defer cleanup()
cli := makeFakeCli(t)
createTestContextWithKube(t, cli)
assertContextCreateLogging(t, cli, "test")
validateTestKubeEndpoint(t, cli.ContextStore(), "test")
@ -228,8 +219,7 @@ func TestCreateFromContext(t *testing.T) {
},
}
cli, cleanup := makeFakeCli(t)
defer cleanup()
cli := makeFakeCli(t)
revert := env.Patch(t, "KUBECONFIG", "./testdata/test-kubeconfig")
defer revert()
cli.ResetOutputBuffers()
@ -319,8 +309,7 @@ func TestCreateFromCurrent(t *testing.T) {
},
}
cli, cleanup := makeFakeCli(t)
defer cleanup()
cli := makeFakeCli(t)
revert := env.Patch(t, "KUBECONFIG", "./testdata/test-kubeconfig")
defer revert()
cli.ResetOutputBuffers()

View File

@ -3,7 +3,7 @@ package context
import (
"bytes"
"fmt"
"io/ioutil"
"io"
"os"
"path/filepath"
"testing"
@ -13,12 +13,8 @@ import (
)
func TestExportImportWithFile(t *testing.T) {
contextDir, err := ioutil.TempDir("", t.Name()+"context")
assert.NilError(t, err)
defer os.RemoveAll(contextDir)
contextFile := filepath.Join(contextDir, "exported")
cli, cleanup := makeFakeCli(t)
defer cleanup()
contextFile := filepath.Join(t.TempDir(), "exported")
cli := makeFakeCli(t)
createTestContextWithKube(t, cli)
cli.ErrBuffer().Reset()
assert.NilError(t, RunExport(cli, &ExportOptions{
@ -43,8 +39,7 @@ func TestExportImportWithFile(t *testing.T) {
}
func TestExportImportPipe(t *testing.T) {
cli, cleanup := makeFakeCli(t)
defer cleanup()
cli := makeFakeCli(t)
createTestContextWithKube(t, cli)
cli.ErrBuffer().Reset()
cli.OutBuffer().Reset()
@ -53,7 +48,7 @@ func TestExportImportPipe(t *testing.T) {
Dest: "-",
}))
assert.Equal(t, cli.ErrBuffer().String(), "")
cli.SetIn(streams.NewIn(ioutil.NopCloser(bytes.NewBuffer(cli.OutBuffer().Bytes()))))
cli.SetIn(streams.NewIn(io.NopCloser(bytes.NewBuffer(cli.OutBuffer().Bytes()))))
cli.OutBuffer().Reset()
cli.ErrBuffer().Reset()
assert.NilError(t, RunImport(cli, "test2", "-"))
@ -71,12 +66,8 @@ func TestExportImportPipe(t *testing.T) {
}
func TestExportKubeconfig(t *testing.T) {
contextDir, err := ioutil.TempDir("", t.Name()+"context")
assert.NilError(t, err)
defer os.RemoveAll(contextDir)
contextFile := filepath.Join(contextDir, "exported")
cli, cleanup := makeFakeCli(t)
defer cleanup()
contextFile := filepath.Join(t.TempDir(), "exported")
cli := makeFakeCli(t)
createTestContextWithKube(t, cli)
cli.ErrBuffer().Reset()
assert.NilError(t, RunExport(cli, &ExportOptions{
@ -96,15 +87,11 @@ func TestExportKubeconfig(t *testing.T) {
}
func TestExportExistingFile(t *testing.T) {
contextDir, err := ioutil.TempDir("", t.Name()+"context")
assert.NilError(t, err)
defer os.RemoveAll(contextDir)
contextFile := filepath.Join(contextDir, "exported")
cli, cleanup := makeFakeCli(t)
defer cleanup()
contextFile := filepath.Join(t.TempDir(), "exported")
cli := makeFakeCli(t)
createTestContextWithKube(t, cli)
cli.ErrBuffer().Reset()
assert.NilError(t, ioutil.WriteFile(contextFile, []byte{}, 0644))
err = RunExport(cli, &ExportOptions{ContextName: "test", Dest: contextFile})
assert.NilError(t, os.WriteFile(contextFile, []byte{}, 0644))
err := RunExport(cli, &ExportOptions{ContextName: "test", Dest: contextFile})
assert.Assert(t, os.IsExist(err))
}

View File

@ -9,8 +9,7 @@ import (
)
func TestInspect(t *testing.T) {
cli, cleanup := makeFakeCli(t)
defer cleanup()
cli := makeFakeCli(t)
createTestContextWithKubeAndSwarm(t, cli, "current", "all")
cli.OutBuffer().Reset()
assert.NilError(t, runInspect(cli, inspectOptions{

View File

@ -24,8 +24,7 @@ func createTestContextWithKubeAndSwarm(t *testing.T, cli command.Cli, name strin
}
func TestList(t *testing.T) {
cli, cleanup := makeFakeCli(t)
defer cleanup()
cli := makeFakeCli(t)
createTestContextWithKubeAndSwarm(t, cli, "current", "all")
createTestContextWithKubeAndSwarm(t, cli, "other", "all")
createTestContextWithKubeAndSwarm(t, cli, "unset", "unset")
@ -36,8 +35,7 @@ func TestList(t *testing.T) {
}
func TestListQuiet(t *testing.T) {
cli, cleanup := makeFakeCli(t)
defer cleanup()
cli := makeFakeCli(t)
createTestContextWithKubeAndSwarm(t, cli, "current", "all")
createTestContextWithKubeAndSwarm(t, cli, "other", "all")
cli.SetCurrentContext("current")

View File

@ -1,8 +1,6 @@
package context
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
@ -13,8 +11,7 @@ import (
)
func TestRemove(t *testing.T) {
cli, cleanup := makeFakeCli(t)
defer cleanup()
cli := makeFakeCli(t)
createTestContextWithKubeAndSwarm(t, cli, "current", "all")
createTestContextWithKubeAndSwarm(t, cli, "other", "all")
assert.NilError(t, RunRemove(cli, RemoveOptions{}, []string{"other"}))
@ -25,8 +22,7 @@ func TestRemove(t *testing.T) {
}
func TestRemoveNotAContext(t *testing.T) {
cli, cleanup := makeFakeCli(t)
defer cleanup()
cli := makeFakeCli(t)
createTestContextWithKubeAndSwarm(t, cli, "current", "all")
createTestContextWithKubeAndSwarm(t, cli, "other", "all")
err := RunRemove(cli, RemoveOptions{}, []string{"not-a-context"})
@ -34,8 +30,7 @@ func TestRemoveNotAContext(t *testing.T) {
}
func TestRemoveCurrent(t *testing.T) {
cli, cleanup := makeFakeCli(t)
defer cleanup()
cli := makeFakeCli(t)
createTestContextWithKubeAndSwarm(t, cli, "current", "all")
createTestContextWithKubeAndSwarm(t, cli, "other", "all")
cli.SetCurrentContext("current")
@ -44,16 +39,13 @@ func TestRemoveCurrent(t *testing.T) {
}
func TestRemoveCurrentForce(t *testing.T) {
configDir, err := ioutil.TempDir("", t.Name()+"config")
assert.NilError(t, err)
defer os.RemoveAll(configDir)
configDir := t.TempDir()
configFilePath := filepath.Join(configDir, "config.json")
testCfg := configfile.New(configFilePath)
testCfg.CurrentContext = "current"
assert.NilError(t, testCfg.Save())
cli, cleanup := makeFakeCli(t, withCliConfig(testCfg))
defer cleanup()
cli := makeFakeCli(t, withCliConfig(testCfg))
createTestContextWithKubeAndSwarm(t, cli, "current", "all")
createTestContextWithKubeAndSwarm(t, cli, "other", "all")
cli.SetCurrentContext("current")
@ -64,8 +56,7 @@ func TestRemoveCurrentForce(t *testing.T) {
}
func TestRemoveDefault(t *testing.T) {
cli, cleanup := makeFakeCli(t)
defer cleanup()
cli := makeFakeCli(t)
createTestContextWithKubeAndSwarm(t, cli, "other", "all")
cli.SetCurrentContext("current")
err := RunRemove(cli, RemoveOptions{}, []string{"default"})

View File

@ -11,8 +11,7 @@ import (
)
func TestUpdateDescriptionOnly(t *testing.T) {
cli, cleanup := makeFakeCli(t)
defer cleanup()
cli := makeFakeCli(t)
err := RunCreate(cli, &CreateOptions{
Name: "test",
DefaultStackOrchestrator: "swarm",
@ -37,8 +36,7 @@ func TestUpdateDescriptionOnly(t *testing.T) {
}
func TestUpdateDockerOnly(t *testing.T) {
cli, cleanup := makeFakeCli(t)
defer cleanup()
cli := makeFakeCli(t)
createTestContextWithKubeAndSwarm(t, cli, "test", "swarm")
assert.NilError(t, RunUpdate(cli, &UpdateOptions{
Name: "test",
@ -58,8 +56,7 @@ func TestUpdateDockerOnly(t *testing.T) {
}
func TestUpdateStackOrchestratorStrategy(t *testing.T) {
cli, cleanup := makeFakeCli(t)
defer cleanup()
cli := makeFakeCli(t)
err := RunCreate(cli, &CreateOptions{
Name: "test",
DefaultStackOrchestrator: "swarm",
@ -74,8 +71,7 @@ func TestUpdateStackOrchestratorStrategy(t *testing.T) {
}
func TestUpdateStackOrchestratorStrategyRemoveKubeEndpoint(t *testing.T) {
cli, cleanup := makeFakeCli(t)
defer cleanup()
cli := makeFakeCli(t)
createTestContextWithKubeAndSwarm(t, cli, "test", "kubernetes")
err := RunUpdate(cli, &UpdateOptions{
Name: "test",
@ -85,8 +81,7 @@ func TestUpdateStackOrchestratorStrategyRemoveKubeEndpoint(t *testing.T) {
}
func TestUpdateInvalidDockerHost(t *testing.T) {
cli, cleanup := makeFakeCli(t)
defer cleanup()
cli := makeFakeCli(t)
err := RunCreate(cli, &CreateOptions{
Name: "test",
Docker: map[string]string{},

View File

@ -1,8 +1,6 @@
package context
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
@ -13,14 +11,11 @@ import (
)
func TestUse(t *testing.T) {
configDir, err := ioutil.TempDir("", t.Name()+"config")
assert.NilError(t, err)
defer os.RemoveAll(configDir)
configDir := t.TempDir()
configFilePath := filepath.Join(configDir, "config.json")
testCfg := configfile.New(configFilePath)
cli, cleanup := makeFakeCli(t, withCliConfig(testCfg))
defer cleanup()
err = RunCreate(cli, &CreateOptions{
cli := makeFakeCli(t, withCliConfig(testCfg))
err := RunCreate(cli, &CreateOptions{
Name: "test",
Docker: map[string]string{},
})
@ -42,8 +37,7 @@ func TestUse(t *testing.T) {
}
func TestUseNoExist(t *testing.T) {
cli, cleanup := makeFakeCli(t)
defer cleanup()
cli := makeFakeCli(t)
err := newUseCommand(cli).RunE(nil, []string{"test"})
assert.Check(t, store.IsErrContextDoesNotExist(err))
}