diff --git a/cli/command/cli.go b/cli/command/cli.go index fe6444f42f..4e27a1457e 100644 --- a/cli/command/cli.go +++ b/cli/command/cli.go @@ -3,7 +3,6 @@ package command import ( "context" "io" - "io/ioutil" "os" "path/filepath" "runtime" @@ -279,7 +278,7 @@ func NewAPIClientFromFlags(opts *cliflags.CommonOptions, configFile *configfile. store := &ContextStoreWithDefault{ Store: store.New(cliconfig.ContextStoreDir(), storeConfig), Resolver: func() (*DefaultContext, error) { - return ResolveDefaultContext(opts, configFile, storeConfig, ioutil.Discard) + return ResolveDefaultContext(opts, configFile, storeConfig, io.Discard) }, } contextName, err := resolveContextName(opts, configFile, store) diff --git a/cli/command/cli_test.go b/cli/command/cli_test.go index 370ecec803..a497b8fd65 100644 --- a/cli/command/cli_test.go +++ b/cli/command/cli_test.go @@ -5,7 +5,7 @@ import ( "context" "crypto/x509" "fmt" - "io/ioutil" + "io" "os" "runtime" "testing" @@ -275,23 +275,23 @@ func TestNewDockerCliAndOperators(t *testing.T) { outbuf := bytes.NewBuffer(nil) errbuf := bytes.NewBuffer(nil) err = cli.Apply( - WithInputStream(ioutil.NopCloser(inbuf)), + WithInputStream(io.NopCloser(inbuf)), WithOutputStream(outbuf), WithErrorStream(errbuf), ) assert.NilError(t, err) // Check input stream - inputStream, err := ioutil.ReadAll(cli.In()) + inputStream, err := io.ReadAll(cli.In()) assert.NilError(t, err) assert.Equal(t, string(inputStream), "input") // Check output stream fmt.Fprintf(cli.Out(), "output") - outputStream, err := ioutil.ReadAll(outbuf) + outputStream, err := io.ReadAll(outbuf) assert.NilError(t, err) assert.Equal(t, string(outputStream), "output") // Check error stream fmt.Fprintf(cli.Err(), "error") - errStream, err := ioutil.ReadAll(errbuf) + errStream, err := io.ReadAll(errbuf) assert.NilError(t, err) assert.Equal(t, string(errStream), "error") } diff --git a/cli/command/defaultcontextstore_test.go b/cli/command/defaultcontextstore_test.go index 3c36dc0dcb..2f0371968f 100644 --- a/cli/command/defaultcontextstore_test.go +++ b/cli/command/defaultcontextstore_test.go @@ -2,8 +2,6 @@ package command import ( "crypto/rand" - "io/ioutil" - "os" "testing" "github.com/docker/cli/cli/config/configfile" @@ -39,11 +37,10 @@ func testDefaultMetadata() store.Metadata { } } -func testStore(t *testing.T, meta store.Metadata, tls store.ContextTLSData) (store.Store, func()) { - testDir, err := ioutil.TempDir("", t.Name()) - assert.NilError(t, err) - s := &ContextStoreWithDefault{ - Store: store.New(testDir, testCfg), +func testStore(t *testing.T, meta store.Metadata, tls store.ContextTLSData) store.Store { + t.Helper() + return &ContextStoreWithDefault{ + Store: store.New(t.TempDir(), testCfg), Resolver: func() (*DefaultContext, error) { return &DefaultContext{ Meta: meta, @@ -51,9 +48,6 @@ func testStore(t *testing.T, meta store.Metadata, tls store.ContextTLSData) (sto }, nil }, } - return s, func() { - _ = os.RemoveAll(testDir) - } } func TestDefaultContextInitializer(t *testing.T) { @@ -81,7 +75,7 @@ func TestExportDefaultImport(t *testing.T) { rand.Read(file1) file2 := make([]byte, 3700) rand.Read(file2) - s, cleanup := testStore(t, testDefaultMetadata(), store.ContextTLSData{ + s := testStore(t, testDefaultMetadata(), store.ContextTLSData{ Endpoints: map[string]store.EndpointTLSData{ "ep2": { Files: map[string][]byte{ @@ -91,7 +85,6 @@ func TestExportDefaultImport(t *testing.T) { }, }, }) - defer cleanup() r := store.Export("default", s) defer r.Close() err := store.Import("dest", s, r) @@ -130,8 +123,7 @@ func TestExportDefaultImport(t *testing.T) { func TestListDefaultContext(t *testing.T) { meta := testDefaultMetadata() - s, cleanup := testStore(t, meta, store.ContextTLSData{}) - defer cleanup() + s := testStore(t, meta, store.ContextTLSData{}) result, err := s.List() assert.NilError(t, err) assert.Equal(t, 1, len(result)) @@ -139,8 +131,7 @@ func TestListDefaultContext(t *testing.T) { } func TestGetDefaultContextStorageInfo(t *testing.T) { - s, cleanup := testStore(t, testDefaultMetadata(), store.ContextTLSData{}) - defer cleanup() + s := testStore(t, testDefaultMetadata(), store.ContextTLSData{}) result := s.GetStorageInfo(DefaultContextName) assert.Equal(t, "", result.MetadataPath) assert.Equal(t, "", result.TLSPath) @@ -148,8 +139,7 @@ func TestGetDefaultContextStorageInfo(t *testing.T) { func TestGetDefaultContextMetadata(t *testing.T) { meta := testDefaultMetadata() - s, cleanup := testStore(t, meta, store.ContextTLSData{}) - defer cleanup() + s := testStore(t, meta, store.ContextTLSData{}) result, err := s.GetMetadata(DefaultContextName) assert.NilError(t, err) assert.Equal(t, DefaultContextName, result.Name) @@ -159,8 +149,7 @@ func TestGetDefaultContextMetadata(t *testing.T) { func TestErrCreateDefault(t *testing.T) { meta := testDefaultMetadata() - s, cleanup := testStore(t, meta, store.ContextTLSData{}) - defer cleanup() + s := testStore(t, meta, store.ContextTLSData{}) err := s.CreateOrUpdate(store.Metadata{ Endpoints: map[string]interface{}{ "ep1": endpoint{Foo: "bar"}, @@ -173,16 +162,14 @@ func TestErrCreateDefault(t *testing.T) { func TestErrRemoveDefault(t *testing.T) { meta := testDefaultMetadata() - s, cleanup := testStore(t, meta, store.ContextTLSData{}) - defer cleanup() + s := testStore(t, meta, store.ContextTLSData{}) err := s.Remove("default") assert.Error(t, err, "default context cannot be removed") } func TestErrTLSDataError(t *testing.T) { meta := testDefaultMetadata() - s, cleanup := testStore(t, meta, store.ContextTLSData{}) - defer cleanup() + s := testStore(t, meta, store.ContextTLSData{}) _, err := s.GetTLSData("default", "noop", "noop") assert.Check(t, store.IsErrTLSDataDoesNotExist(err)) } diff --git a/cli/command/orchestrator_test.go b/cli/command/orchestrator_test.go index fbd9018eff..b2e7ebb7ac 100644 --- a/cli/command/orchestrator_test.go +++ b/cli/command/orchestrator_test.go @@ -1,7 +1,7 @@ package command import ( - "io/ioutil" + "io" "testing" "gotest.tools/v3/assert" @@ -91,7 +91,7 @@ func TestOrchestratorSwitch(t *testing.T) { if testcase.envOrchestrator != "" { defer env.Patch(t, "DOCKER_STACK_ORCHESTRATOR", testcase.envOrchestrator)() } - orchestrator, err := GetStackOrchestrator(testcase.flagOrchestrator, testcase.contextOrchestrator, testcase.globalOrchestrator, ioutil.Discard) + orchestrator, err := GetStackOrchestrator(testcase.flagOrchestrator, testcase.contextOrchestrator, testcase.globalOrchestrator, io.Discard) assert.NilError(t, err) assert.Check(t, is.Equal(testcase.expectedKubernetes, orchestrator.HasKubernetes())) assert.Check(t, is.Equal(testcase.expectedSwarm, orchestrator.HasSwarm())) diff --git a/cli/command/utils_test.go b/cli/command/utils_test.go index 566b932841..2af232d2e9 100644 --- a/cli/command/utils_test.go +++ b/cli/command/utils_test.go @@ -1,7 +1,6 @@ package command import ( - "io/ioutil" "os" "path/filepath" "testing" @@ -37,15 +36,13 @@ func TestStringSliceReplaceAt(t *testing.T) { } func TestValidateOutputPath(t *testing.T) { - basedir, err := ioutil.TempDir("", "TestValidateOutputPath") - assert.NilError(t, err) - defer os.RemoveAll(basedir) + basedir := t.TempDir() dir := filepath.Join(basedir, "dir") notexist := filepath.Join(basedir, "notexist") - err = os.MkdirAll(dir, 0755) + err := os.MkdirAll(dir, 0755) assert.NilError(t, err) file := filepath.Join(dir, "file") - err = ioutil.WriteFile(file, []byte("hi"), 0644) + err = os.WriteFile(file, []byte("hi"), 0644) assert.NilError(t, err) var testcases = []struct { path string