From 78cb61c61c073592c45f30f4e827841be341935a Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Fri, 25 Feb 2022 14:32:11 +0100 Subject: [PATCH] cli/command/swarm: remove deprecated io/ioutil and use t.TempDir() Signed-off-by: Sebastiaan van Stijn --- cli/command/swarm/ca.go | 3 +-- cli/command/swarm/ca_test.go | 11 +++++------ cli/command/swarm/init_test.go | 4 ++-- cli/command/swarm/join_test.go | 4 ++-- cli/command/swarm/join_token_test.go | 4 ++-- cli/command/swarm/leave_test.go | 4 ++-- cli/command/swarm/opts.go | 6 +++--- cli/command/swarm/unlock_key_test.go | 4 ++-- cli/command/swarm/unlock_test.go | 6 +++--- cli/command/swarm/update_test.go | 4 ++-- 10 files changed, 24 insertions(+), 26 deletions(-) diff --git a/cli/command/swarm/ca.go b/cli/command/swarm/ca.go index 961e7e8944..1c01f68460 100644 --- a/cli/command/swarm/ca.go +++ b/cli/command/swarm/ca.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "io" - "io/ioutil" "strings" "github.com/docker/cli/cli" @@ -113,7 +112,7 @@ func attach(ctx context.Context, dockerCli command.Cli, opts caOptions) error { }() if opts.quiet { - go io.Copy(ioutil.Discard, pipeReader) + go io.Copy(io.Discard, pipeReader) return <-errChan } diff --git a/cli/command/swarm/ca_test.go b/cli/command/swarm/ca_test.go index 1f65647c58..2758bfb121 100644 --- a/cli/command/swarm/ca_test.go +++ b/cli/command/swarm/ca_test.go @@ -2,7 +2,7 @@ package swarm import ( "bytes" - "io/ioutil" + "io" "os" "testing" "time" @@ -65,7 +65,7 @@ type invalidCATestCases struct { } func writeFile(data string) (string, error) { - tmpfile, err := ioutil.TempFile("", "testfile") + tmpfile, err := os.CreateTemp("", "testfile") if err != nil { return "", err } @@ -73,15 +73,14 @@ func writeFile(data string) (string, error) { if err != nil { return "", err } - tmpfile.Close() - return tmpfile.Name(), nil + return tmpfile.Name(), tmpfile.Close() } func TestDisplayTrustRootInvalidFlags(t *testing.T) { // we need an actual PEMfile to test tmpfile, err := writeFile(cert) assert.NilError(t, err) - defer os.Remove(tmpfile) + t.Cleanup(func() { _ = os.Remove(tmpfile) }) errorTestCases := []invalidCATestCases{ { @@ -145,7 +144,7 @@ func TestDisplayTrustRootInvalidFlags(t *testing.T) { }, })) assert.Check(t, cmd.Flags().Parse(testCase.args)) - cmd.SetOut(ioutil.Discard) + cmd.SetOut(io.Discard) assert.ErrorContains(t, cmd.Execute(), testCase.errorMsg) } } diff --git a/cli/command/swarm/init_test.go b/cli/command/swarm/init_test.go index 2614177269..e2153040e9 100644 --- a/cli/command/swarm/init_test.go +++ b/cli/command/swarm/init_test.go @@ -2,7 +2,7 @@ package swarm import ( "fmt" - "io/ioutil" + "io" "testing" "github.com/docker/cli/internal/test" @@ -73,7 +73,7 @@ func TestSwarmInitErrorOnAPIFailure(t *testing.T) { for key, value := range tc.flags { cmd.Flags().Set(key, value) } - cmd.SetOut(ioutil.Discard) + cmd.SetOut(io.Discard) assert.Error(t, cmd.Execute(), tc.expectedError) } } diff --git a/cli/command/swarm/join_test.go b/cli/command/swarm/join_test.go index 7fba3bc4d7..e8204f3913 100644 --- a/cli/command/swarm/join_test.go +++ b/cli/command/swarm/join_test.go @@ -1,7 +1,7 @@ package swarm import ( - "io/ioutil" + "io" "strings" "testing" @@ -54,7 +54,7 @@ func TestSwarmJoinErrors(t *testing.T) { infoFunc: tc.infoFunc, })) cmd.SetArgs(tc.args) - cmd.SetOut(ioutil.Discard) + cmd.SetOut(io.Discard) assert.ErrorContains(t, cmd.Execute(), tc.expectedError) } } diff --git a/cli/command/swarm/join_token_test.go b/cli/command/swarm/join_token_test.go index 3d79ae90ba..082b54852a 100644 --- a/cli/command/swarm/join_token_test.go +++ b/cli/command/swarm/join_token_test.go @@ -2,7 +2,7 @@ package swarm import ( "fmt" - "io/ioutil" + "io" "testing" "github.com/docker/cli/internal/test" @@ -98,7 +98,7 @@ func TestSwarmJoinTokenErrors(t *testing.T) { for key, value := range tc.flags { cmd.Flags().Set(key, value) } - cmd.SetOut(ioutil.Discard) + cmd.SetOut(io.Discard) assert.ErrorContains(t, cmd.Execute(), tc.expectedError) } } diff --git a/cli/command/swarm/leave_test.go b/cli/command/swarm/leave_test.go index 969bdbbfb4..e1005828d0 100644 --- a/cli/command/swarm/leave_test.go +++ b/cli/command/swarm/leave_test.go @@ -1,7 +1,7 @@ package swarm import ( - "io/ioutil" + "io" "strings" "testing" @@ -37,7 +37,7 @@ func TestSwarmLeaveErrors(t *testing.T) { swarmLeaveFunc: tc.swarmLeaveFunc, })) cmd.SetArgs(tc.args) - cmd.SetOut(ioutil.Discard) + cmd.SetOut(io.Discard) assert.ErrorContains(t, cmd.Execute(), tc.expectedError) } } diff --git a/cli/command/swarm/opts.go b/cli/command/swarm/opts.go index ea66bf3987..4db2e303b1 100644 --- a/cli/command/swarm/opts.go +++ b/cli/command/swarm/opts.go @@ -4,7 +4,7 @@ import ( "encoding/csv" "encoding/pem" "fmt" - "io/ioutil" + "os" "strings" "time" @@ -140,7 +140,7 @@ func (p *PEMFile) String() string { // Set parses a root rotation option func (p *PEMFile) Set(value string) error { - contents, err := ioutil.ReadFile(value) + contents, err := os.ReadFile(value) if err != nil { return err } @@ -195,7 +195,7 @@ func parseExternalCA(caSpec string) (*swarm.ExternalCA, error) { hasURL = true externalCA.URL = value case "cacert": - cacontents, err := ioutil.ReadFile(value) + cacontents, err := os.ReadFile(value) if err != nil { return nil, errors.Wrap(err, "unable to read CA cert for external CA") } diff --git a/cli/command/swarm/unlock_key_test.go b/cli/command/swarm/unlock_key_test.go index e51bd726ef..24ca164aa6 100644 --- a/cli/command/swarm/unlock_key_test.go +++ b/cli/command/swarm/unlock_key_test.go @@ -2,7 +2,7 @@ package swarm import ( "fmt" - "io/ioutil" + "io" "testing" "github.com/docker/cli/internal/test" @@ -90,7 +90,7 @@ func TestSwarmUnlockKeyErrors(t *testing.T) { for key, value := range tc.flags { cmd.Flags().Set(key, value) } - cmd.SetOut(ioutil.Discard) + cmd.SetOut(io.Discard) assert.ErrorContains(t, cmd.Execute(), tc.expectedError) } } diff --git a/cli/command/swarm/unlock_test.go b/cli/command/swarm/unlock_test.go index 6d40613619..c7e463a32a 100644 --- a/cli/command/swarm/unlock_test.go +++ b/cli/command/swarm/unlock_test.go @@ -1,7 +1,7 @@ package swarm import ( - "io/ioutil" + "io" "strings" "testing" @@ -70,7 +70,7 @@ func TestSwarmUnlockErrors(t *testing.T) { swarmUnlockFunc: tc.swarmUnlockFunc, })) cmd.SetArgs(tc.args) - cmd.SetOut(ioutil.Discard) + cmd.SetOut(io.Discard) assert.ErrorContains(t, cmd.Execute(), tc.expectedError) } } @@ -92,7 +92,7 @@ func TestSwarmUnlock(t *testing.T) { return nil }, }) - dockerCli.SetIn(streams.NewIn(ioutil.NopCloser(strings.NewReader(input)))) + dockerCli.SetIn(streams.NewIn(io.NopCloser(strings.NewReader(input)))) cmd := newUnlockCommand(dockerCli) assert.NilError(t, cmd.Execute()) } diff --git a/cli/command/swarm/update_test.go b/cli/command/swarm/update_test.go index ea695f2588..14605c033a 100644 --- a/cli/command/swarm/update_test.go +++ b/cli/command/swarm/update_test.go @@ -2,7 +2,7 @@ package swarm import ( "fmt" - "io/ioutil" + "io" "testing" "time" @@ -75,7 +75,7 @@ func TestSwarmUpdateErrors(t *testing.T) { for key, value := range tc.flags { cmd.Flags().Set(key, value) } - cmd.SetOut(ioutil.Discard) + cmd.SetOut(io.Discard) assert.ErrorContains(t, cmd.Execute(), tc.expectedError) } }