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 (
"fmt"
"io/ioutil"
"os"
"testing"
"github.com/docker/cli/cli/command"
@ -14,9 +12,9 @@ import (
"gotest.tools/v3/assert"
)
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{} }),
@ -40,15 +38,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) {
@ -58,8 +53,7 @@ func withCliConfig(configFile *configfile.ConfigFile) func(*test.FakeCli) {
}
func TestCreate(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
@ -115,8 +109,7 @@ func assertContextCreateLogging(t *testing.T, cli *test.FakeCli, n string) {
}
func TestCreateOrchestratorEmpty(t *testing.T) {
cli, cleanup := makeFakeCli(t)
defer cleanup()
cli := makeFakeCli(t)
err := RunCreate(cli, &CreateOptions{
Name: "test",
@ -144,8 +137,7 @@ func TestCreateFromContext(t *testing.T) {
},
}
cli, cleanup := makeFakeCli(t)
defer cleanup()
cli := makeFakeCli(t)
cli.ResetOutputBuffers()
assert.NilError(t, RunCreate(cli, &CreateOptions{
Name: "original",
@ -210,8 +202,7 @@ func TestCreateFromCurrent(t *testing.T) {
},
}
cli, cleanup := makeFakeCli(t)
defer cleanup()
cli := makeFakeCli(t)
cli.ResetOutputBuffers()
assert.NilError(t, RunCreate(cli, &CreateOptions{
Name: "original",

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)
createTestContext(t, cli, "test")
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)
createTestContext(t, cli, "test")
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,14 +66,10 @@ func TestExportImportPipe(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)
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)
createTestContext(t, cli, "current")
cli.OutBuffer().Reset()
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) {
cli, cleanup := makeFakeCli(t)
defer cleanup()
cli := makeFakeCli(t)
createTestContext(t, cli, "current")
createTestContext(t, cli, "other")
createTestContext(t, cli, "unset")
@ -32,8 +31,7 @@ func TestList(t *testing.T) {
}
func TestListQuiet(t *testing.T) {
cli, cleanup := makeFakeCli(t)
defer cleanup()
cli := makeFakeCli(t)
createTestContext(t, cli, "current")
createTestContext(t, cli, "other")
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)
createTestContext(t, cli, "current")
createTestContext(t, cli, "other")
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)
createTestContext(t, cli, "current")
createTestContext(t, cli, "other")
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)
createTestContext(t, cli, "current")
createTestContext(t, cli, "other")
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))
createTestContext(t, cli, "current")
createTestContext(t, cli, "other")
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)
createTestContext(t, cli, "other")
cli.SetCurrentContext("current")
err := RunRemove(cli, RemoveOptions{}, []string{"default"})

View File

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