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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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