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

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2022-02-25 13:07:16 +01:00
parent e946bf0804
commit 76b47359cb
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
7 changed files with 33 additions and 72 deletions

View File

@ -2,8 +2,6 @@ package context
import ( import (
"fmt" "fmt"
"io/ioutil"
"os"
"testing" "testing"
"github.com/docker/cli/cli/command" "github.com/docker/cli/cli/command"
@ -14,9 +12,9 @@ import (
"gotest.tools/v3/assert" "gotest.tools/v3/assert"
) )
func makeFakeCli(t *testing.T, opts ...func(*test.FakeCli)) (*test.FakeCli, func()) { func makeFakeCli(t *testing.T, opts ...func(*test.FakeCli)) *test.FakeCli {
dir, err := ioutil.TempDir("", t.Name()) t.Helper()
assert.NilError(t, err) dir := t.TempDir()
storeConfig := store.NewConfig( storeConfig := store.NewConfig(
func() interface{} { return &command.DockerContext{} }, func() interface{} { return &command.DockerContext{} },
store.EndpointTypeGetter(docker.DockerEndpoint, func() interface{} { return &docker.EndpointMeta{} }), store.EndpointTypeGetter(docker.DockerEndpoint, func() interface{} { return &docker.EndpointMeta{} }),
@ -40,15 +38,12 @@ func makeFakeCli(t *testing.T, opts ...func(*test.FakeCli)) (*test.FakeCli, func
}, nil }, nil
}, },
} }
cleanup := func() {
os.RemoveAll(dir)
}
result := test.NewFakeCli(nil, opts...) result := test.NewFakeCli(nil, opts...)
for _, o := range opts { for _, o := range opts {
o(result) o(result)
} }
result.SetContextStore(store) result.SetContextStore(store)
return result, cleanup return result
} }
func withCliConfig(configFile *configfile.ConfigFile) func(*test.FakeCli) { func withCliConfig(configFile *configfile.ConfigFile) func(*test.FakeCli) {
@ -58,8 +53,7 @@ func withCliConfig(configFile *configfile.ConfigFile) func(*test.FakeCli) {
} }
func TestCreate(t *testing.T) { func TestCreate(t *testing.T) {
cli, cleanup := makeFakeCli(t) cli := makeFakeCli(t)
defer cleanup()
assert.NilError(t, cli.ContextStore().CreateOrUpdate(store.Metadata{Name: "existing-context"})) assert.NilError(t, cli.ContextStore().CreateOrUpdate(store.Metadata{Name: "existing-context"}))
tests := []struct { tests := []struct {
options CreateOptions options CreateOptions
@ -115,8 +109,7 @@ func assertContextCreateLogging(t *testing.T, cli *test.FakeCli, n string) {
} }
func TestCreateOrchestratorEmpty(t *testing.T) { func TestCreateOrchestratorEmpty(t *testing.T) {
cli, cleanup := makeFakeCli(t) cli := makeFakeCli(t)
defer cleanup()
err := RunCreate(cli, &CreateOptions{ err := RunCreate(cli, &CreateOptions{
Name: "test", Name: "test",
@ -144,8 +137,7 @@ func TestCreateFromContext(t *testing.T) {
}, },
} }
cli, cleanup := makeFakeCli(t) cli := makeFakeCli(t)
defer cleanup()
cli.ResetOutputBuffers() cli.ResetOutputBuffers()
assert.NilError(t, RunCreate(cli, &CreateOptions{ assert.NilError(t, RunCreate(cli, &CreateOptions{
Name: "original", Name: "original",
@ -210,8 +202,7 @@ func TestCreateFromCurrent(t *testing.T) {
}, },
} }
cli, cleanup := makeFakeCli(t) cli := makeFakeCli(t)
defer cleanup()
cli.ResetOutputBuffers() cli.ResetOutputBuffers()
assert.NilError(t, RunCreate(cli, &CreateOptions{ assert.NilError(t, RunCreate(cli, &CreateOptions{
Name: "original", Name: "original",

View File

@ -3,7 +3,7 @@ package context
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"io/ioutil" "io"
"os" "os"
"path/filepath" "path/filepath"
"testing" "testing"
@ -13,12 +13,8 @@ import (
) )
func TestExportImportWithFile(t *testing.T) { func TestExportImportWithFile(t *testing.T) {
contextDir, err := ioutil.TempDir("", t.Name()+"context") contextFile := filepath.Join(t.TempDir(), "exported")
assert.NilError(t, err) cli := makeFakeCli(t)
defer os.RemoveAll(contextDir)
contextFile := filepath.Join(contextDir, "exported")
cli, cleanup := makeFakeCli(t)
defer cleanup()
createTestContext(t, cli, "test") createTestContext(t, cli, "test")
cli.ErrBuffer().Reset() cli.ErrBuffer().Reset()
assert.NilError(t, RunExport(cli, &ExportOptions{ assert.NilError(t, RunExport(cli, &ExportOptions{
@ -43,8 +39,7 @@ func TestExportImportWithFile(t *testing.T) {
} }
func TestExportImportPipe(t *testing.T) { func TestExportImportPipe(t *testing.T) {
cli, cleanup := makeFakeCli(t) cli := makeFakeCli(t)
defer cleanup()
createTestContext(t, cli, "test") createTestContext(t, cli, "test")
cli.ErrBuffer().Reset() cli.ErrBuffer().Reset()
cli.OutBuffer().Reset() cli.OutBuffer().Reset()
@ -53,7 +48,7 @@ func TestExportImportPipe(t *testing.T) {
Dest: "-", Dest: "-",
})) }))
assert.Equal(t, cli.ErrBuffer().String(), "") 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.OutBuffer().Reset()
cli.ErrBuffer().Reset() cli.ErrBuffer().Reset()
assert.NilError(t, RunImport(cli, "test2", "-")) assert.NilError(t, RunImport(cli, "test2", "-"))
@ -71,14 +66,10 @@ func TestExportImportPipe(t *testing.T) {
} }
func TestExportExistingFile(t *testing.T) { func TestExportExistingFile(t *testing.T) {
contextDir, err := ioutil.TempDir("", t.Name()+"context") contextFile := filepath.Join(t.TempDir(), "exported")
assert.NilError(t, err) cli := makeFakeCli(t)
defer os.RemoveAll(contextDir)
contextFile := filepath.Join(contextDir, "exported")
cli, cleanup := makeFakeCli(t)
defer cleanup()
cli.ErrBuffer().Reset() cli.ErrBuffer().Reset()
assert.NilError(t, ioutil.WriteFile(contextFile, []byte{}, 0644)) assert.NilError(t, os.WriteFile(contextFile, []byte{}, 0644))
err = RunExport(cli, &ExportOptions{ContextName: "test", Dest: contextFile}) err := RunExport(cli, &ExportOptions{ContextName: "test", Dest: contextFile})
assert.Assert(t, os.IsExist(err)) assert.Assert(t, os.IsExist(err))
} }

View File

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

View File

@ -20,8 +20,7 @@ func createTestContext(t *testing.T, cli command.Cli, name string) {
} }
func TestList(t *testing.T) { func TestList(t *testing.T) {
cli, cleanup := makeFakeCli(t) cli := makeFakeCli(t)
defer cleanup()
createTestContext(t, cli, "current") createTestContext(t, cli, "current")
createTestContext(t, cli, "other") createTestContext(t, cli, "other")
createTestContext(t, cli, "unset") createTestContext(t, cli, "unset")
@ -32,8 +31,7 @@ func TestList(t *testing.T) {
} }
func TestListQuiet(t *testing.T) { func TestListQuiet(t *testing.T) {
cli, cleanup := makeFakeCli(t) cli := makeFakeCli(t)
defer cleanup()
createTestContext(t, cli, "current") createTestContext(t, cli, "current")
createTestContext(t, cli, "other") createTestContext(t, cli, "other")
cli.SetCurrentContext("current") cli.SetCurrentContext("current")

View File

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

View File

@ -10,8 +10,7 @@ import (
) )
func TestUpdateDescriptionOnly(t *testing.T) { func TestUpdateDescriptionOnly(t *testing.T) {
cli, cleanup := makeFakeCli(t) cli := makeFakeCli(t)
defer cleanup()
err := RunCreate(cli, &CreateOptions{ err := RunCreate(cli, &CreateOptions{
Name: "test", Name: "test",
Docker: map[string]string{}, Docker: map[string]string{},
@ -34,8 +33,7 @@ func TestUpdateDescriptionOnly(t *testing.T) {
} }
func TestUpdateDockerOnly(t *testing.T) { func TestUpdateDockerOnly(t *testing.T) {
cli, cleanup := makeFakeCli(t) cli := makeFakeCli(t)
defer cleanup()
createTestContext(t, cli, "test") createTestContext(t, cli, "test")
assert.NilError(t, RunUpdate(cli, &UpdateOptions{ assert.NilError(t, RunUpdate(cli, &UpdateOptions{
Name: "test", Name: "test",
@ -53,8 +51,7 @@ func TestUpdateDockerOnly(t *testing.T) {
} }
func TestUpdateInvalidDockerHost(t *testing.T) { func TestUpdateInvalidDockerHost(t *testing.T) {
cli, cleanup := makeFakeCli(t) cli := makeFakeCli(t)
defer cleanup()
err := RunCreate(cli, &CreateOptions{ err := RunCreate(cli, &CreateOptions{
Name: "test", Name: "test",
Docker: map[string]string{}, Docker: map[string]string{},

View File

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