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

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
(cherry picked from commit 3f7e7bf9d2)
Signed-off-by: Cory Snider <csnider@mirantis.com>
This commit is contained in:
Sebastiaan van Stijn 2022-02-25 14:35:28 +01:00 committed by Cory Snider
parent 03d93a93c3
commit 8efbfcce25
5 changed files with 22 additions and 39 deletions

View File

@ -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)

View File

@ -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")
}

View File

@ -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, "<IN MEMORY>", result.MetadataPath)
assert.Equal(t, "<IN MEMORY>", 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))
}

View File

@ -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()))

View File

@ -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