cli/command/swarm: 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 14:32:11 +01:00
parent d59330f40d
commit 78cb61c61c
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
10 changed files with 24 additions and 26 deletions

View File

@ -4,7 +4,6 @@ import (
"context" "context"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"strings" "strings"
"github.com/docker/cli/cli" "github.com/docker/cli/cli"
@ -113,7 +112,7 @@ func attach(ctx context.Context, dockerCli command.Cli, opts caOptions) error {
}() }()
if opts.quiet { if opts.quiet {
go io.Copy(ioutil.Discard, pipeReader) go io.Copy(io.Discard, pipeReader)
return <-errChan return <-errChan
} }

View File

@ -2,7 +2,7 @@ package swarm
import ( import (
"bytes" "bytes"
"io/ioutil" "io"
"os" "os"
"testing" "testing"
"time" "time"
@ -65,7 +65,7 @@ type invalidCATestCases struct {
} }
func writeFile(data string) (string, error) { func writeFile(data string) (string, error) {
tmpfile, err := ioutil.TempFile("", "testfile") tmpfile, err := os.CreateTemp("", "testfile")
if err != nil { if err != nil {
return "", err return "", err
} }
@ -73,15 +73,14 @@ func writeFile(data string) (string, error) {
if err != nil { if err != nil {
return "", err return "", err
} }
tmpfile.Close() return tmpfile.Name(), tmpfile.Close()
return tmpfile.Name(), nil
} }
func TestDisplayTrustRootInvalidFlags(t *testing.T) { func TestDisplayTrustRootInvalidFlags(t *testing.T) {
// we need an actual PEMfile to test // we need an actual PEMfile to test
tmpfile, err := writeFile(cert) tmpfile, err := writeFile(cert)
assert.NilError(t, err) assert.NilError(t, err)
defer os.Remove(tmpfile) t.Cleanup(func() { _ = os.Remove(tmpfile) })
errorTestCases := []invalidCATestCases{ errorTestCases := []invalidCATestCases{
{ {
@ -145,7 +144,7 @@ func TestDisplayTrustRootInvalidFlags(t *testing.T) {
}, },
})) }))
assert.Check(t, cmd.Flags().Parse(testCase.args)) assert.Check(t, cmd.Flags().Parse(testCase.args))
cmd.SetOut(ioutil.Discard) cmd.SetOut(io.Discard)
assert.ErrorContains(t, cmd.Execute(), testCase.errorMsg) assert.ErrorContains(t, cmd.Execute(), testCase.errorMsg)
} }
} }

View File

@ -2,7 +2,7 @@ package swarm
import ( import (
"fmt" "fmt"
"io/ioutil" "io"
"testing" "testing"
"github.com/docker/cli/internal/test" "github.com/docker/cli/internal/test"
@ -73,7 +73,7 @@ func TestSwarmInitErrorOnAPIFailure(t *testing.T) {
for key, value := range tc.flags { for key, value := range tc.flags {
cmd.Flags().Set(key, value) cmd.Flags().Set(key, value)
} }
cmd.SetOut(ioutil.Discard) cmd.SetOut(io.Discard)
assert.Error(t, cmd.Execute(), tc.expectedError) assert.Error(t, cmd.Execute(), tc.expectedError)
} }
} }

View File

@ -1,7 +1,7 @@
package swarm package swarm
import ( import (
"io/ioutil" "io"
"strings" "strings"
"testing" "testing"
@ -54,7 +54,7 @@ func TestSwarmJoinErrors(t *testing.T) {
infoFunc: tc.infoFunc, infoFunc: tc.infoFunc,
})) }))
cmd.SetArgs(tc.args) cmd.SetArgs(tc.args)
cmd.SetOut(ioutil.Discard) cmd.SetOut(io.Discard)
assert.ErrorContains(t, cmd.Execute(), tc.expectedError) assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
} }
} }

View File

@ -2,7 +2,7 @@ package swarm
import ( import (
"fmt" "fmt"
"io/ioutil" "io"
"testing" "testing"
"github.com/docker/cli/internal/test" "github.com/docker/cli/internal/test"
@ -98,7 +98,7 @@ func TestSwarmJoinTokenErrors(t *testing.T) {
for key, value := range tc.flags { for key, value := range tc.flags {
cmd.Flags().Set(key, value) cmd.Flags().Set(key, value)
} }
cmd.SetOut(ioutil.Discard) cmd.SetOut(io.Discard)
assert.ErrorContains(t, cmd.Execute(), tc.expectedError) assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
} }
} }

View File

@ -1,7 +1,7 @@
package swarm package swarm
import ( import (
"io/ioutil" "io"
"strings" "strings"
"testing" "testing"
@ -37,7 +37,7 @@ func TestSwarmLeaveErrors(t *testing.T) {
swarmLeaveFunc: tc.swarmLeaveFunc, swarmLeaveFunc: tc.swarmLeaveFunc,
})) }))
cmd.SetArgs(tc.args) cmd.SetArgs(tc.args)
cmd.SetOut(ioutil.Discard) cmd.SetOut(io.Discard)
assert.ErrorContains(t, cmd.Execute(), tc.expectedError) assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
} }
} }

View File

@ -4,7 +4,7 @@ import (
"encoding/csv" "encoding/csv"
"encoding/pem" "encoding/pem"
"fmt" "fmt"
"io/ioutil" "os"
"strings" "strings"
"time" "time"
@ -140,7 +140,7 @@ func (p *PEMFile) String() string {
// Set parses a root rotation option // Set parses a root rotation option
func (p *PEMFile) Set(value string) error { func (p *PEMFile) Set(value string) error {
contents, err := ioutil.ReadFile(value) contents, err := os.ReadFile(value)
if err != nil { if err != nil {
return err return err
} }
@ -195,7 +195,7 @@ func parseExternalCA(caSpec string) (*swarm.ExternalCA, error) {
hasURL = true hasURL = true
externalCA.URL = value externalCA.URL = value
case "cacert": case "cacert":
cacontents, err := ioutil.ReadFile(value) cacontents, err := os.ReadFile(value)
if err != nil { if err != nil {
return nil, errors.Wrap(err, "unable to read CA cert for external CA") return nil, errors.Wrap(err, "unable to read CA cert for external CA")
} }

View File

@ -2,7 +2,7 @@ package swarm
import ( import (
"fmt" "fmt"
"io/ioutil" "io"
"testing" "testing"
"github.com/docker/cli/internal/test" "github.com/docker/cli/internal/test"
@ -90,7 +90,7 @@ func TestSwarmUnlockKeyErrors(t *testing.T) {
for key, value := range tc.flags { for key, value := range tc.flags {
cmd.Flags().Set(key, value) cmd.Flags().Set(key, value)
} }
cmd.SetOut(ioutil.Discard) cmd.SetOut(io.Discard)
assert.ErrorContains(t, cmd.Execute(), tc.expectedError) assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
} }
} }

View File

@ -1,7 +1,7 @@
package swarm package swarm
import ( import (
"io/ioutil" "io"
"strings" "strings"
"testing" "testing"
@ -70,7 +70,7 @@ func TestSwarmUnlockErrors(t *testing.T) {
swarmUnlockFunc: tc.swarmUnlockFunc, swarmUnlockFunc: tc.swarmUnlockFunc,
})) }))
cmd.SetArgs(tc.args) cmd.SetArgs(tc.args)
cmd.SetOut(ioutil.Discard) cmd.SetOut(io.Discard)
assert.ErrorContains(t, cmd.Execute(), tc.expectedError) assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
} }
} }
@ -92,7 +92,7 @@ func TestSwarmUnlock(t *testing.T) {
return nil return nil
}, },
}) })
dockerCli.SetIn(streams.NewIn(ioutil.NopCloser(strings.NewReader(input)))) dockerCli.SetIn(streams.NewIn(io.NopCloser(strings.NewReader(input))))
cmd := newUnlockCommand(dockerCli) cmd := newUnlockCommand(dockerCli)
assert.NilError(t, cmd.Execute()) assert.NilError(t, cmd.Execute())
} }

View File

@ -2,7 +2,7 @@ package swarm
import ( import (
"fmt" "fmt"
"io/ioutil" "io"
"testing" "testing"
"time" "time"
@ -75,7 +75,7 @@ func TestSwarmUpdateErrors(t *testing.T) {
for key, value := range tc.flags { for key, value := range tc.flags {
cmd.Flags().Set(key, value) cmd.Flags().Set(key, value)
} }
cmd.SetOut(ioutil.Discard) cmd.SetOut(io.Discard)
assert.ErrorContains(t, cmd.Execute(), tc.expectedError) assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
} }
} }