Skip to content

Commit 3806d36

Browse files
Move from io/ioutil to io and os packages
Signed-off-by: Ilya Dmitrichenko <[email protected]>
1 parent 6303310 commit 3806d36

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+244
-270
lines changed

.circleci/config.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ jobs:
2929

3030
# /dev/shm in the container is tmpfs although files in /dev/shm are not executable.
3131
# If we specify TMPDIR=/dev/shm, /dev/shm will be used by our tests, which call
32-
# ioutil.TempDir/ioutil.TempFile, to write temporary files.
32+
# os.MkdirTemp/os.CreateTemp, to write temporary files.
3333
# We can also specify GOTMPDIR=/tmp or some other non-tmpfs directory
3434
# (see https://golang.org/doc/go1.10#goroot) - this is the directory in which the
3535
# go tool itself will put temporarily compiled test executables, etc.

agent/exec/dockerapi/controller_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"context"
66
"fmt"
77
"io"
8-
"io/ioutil"
98
"reflect"
109
"runtime"
1110
"testing"
@@ -38,7 +37,7 @@ func TestControllerPrepare(t *testing.T) {
3837

3938
client.ImagePullFn = func(_ context.Context, refStr string, options types.ImagePullOptions) (io.ReadCloser, error) {
4039
if refStr == config.image() {
41-
return ioutil.NopCloser(bytes.NewBuffer([]byte{})), nil
40+
return io.NopCloser(bytes.NewBuffer([]byte{})), nil
4241
}
4342
panic("unexpected call of ImagePull")
4443
}
@@ -68,7 +67,7 @@ func TestControllerPrepareAlreadyPrepared(t *testing.T) {
6867

6968
client.ImagePullFn = func(_ context.Context, refStr string, options types.ImagePullOptions) (io.ReadCloser, error) {
7069
if refStr == config.image() {
71-
return ioutil.NopCloser(bytes.NewBuffer([]byte{})), nil
70+
return io.NopCloser(bytes.NewBuffer([]byte{})), nil
7271
}
7372
panic("unexpected call of ImagePull")
7473
}

agent/storage_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package agent
22

33
import (
4-
"io/ioutil"
54
"math/rand"
65
"os"
76
"path/filepath"
@@ -176,11 +175,11 @@ func genTaskStatus() *api.TaskStatus {
176175
// tests.
177176
func storageTestEnv(t *testing.T) (*bolt.DB, func()) {
178177
var cleanup []func()
179-
dir, err := ioutil.TempDir("", "agent-TestStorage-")
178+
dir, err := os.MkdirTemp("", "agent-TestStorage-")
180179
assert.NoError(t, err)
181180

182181
dbpath := filepath.Join(dir, "tasks.db")
183-
assert.NoError(t, os.MkdirAll(dir, 0777))
182+
assert.NoError(t, os.MkdirAll(dir, 0o777))
184183
cleanup = append(cleanup, func() { os.RemoveAll(dir) })
185184

186185
db, err := bolt.Open(dbpath, 0666, nil)

agent/testutils/fakes.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package testutils
22

33
import (
44
"context"
5-
"io/ioutil"
65
"net"
76
"os"
87
"path/filepath"
@@ -235,7 +234,7 @@ func NewMockDispatcher(t *testing.T, secConfig *ca.SecurityConfig, local bool) (
235234
cleanup func()
236235
)
237236
if local {
238-
tempDir, err := ioutil.TempDir("", "local-dispatcher-socket")
237+
tempDir, err := os.MkdirTemp("", "local-dispatcher-socket")
239238
require.NoError(t, err)
240239
addr = filepath.Join(tempDir, "socket")
241240
l, err = net.Listen("unix", addr)

ca/certificates.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"encoding/pem"
1515
"fmt"
1616
"io"
17-
"io/ioutil"
1817
"os"
1918
"path/filepath"
2019
"time"
@@ -687,7 +686,7 @@ func ensureCertKeyMatch(cert *x509.Certificate, key crypto.PublicKey) error {
687686
// CA certificate, and returns the PEM-encoded Certificate if so
688687
func GetLocalRootCA(paths CertPaths) (RootCA, error) {
689688
// Check if we have a Certificate file
690-
cert, err := ioutil.ReadFile(paths.Cert)
689+
cert, err := os.ReadFile(paths.Cert)
691690
if err != nil {
692691
if os.IsNotExist(err) {
693692
err = ErrNoLocalRootCA
@@ -697,7 +696,7 @@ func GetLocalRootCA(paths CertPaths) (RootCA, error) {
697696
}
698697
signingCert := cert
699698

700-
key, err := ioutil.ReadFile(paths.Key)
699+
key, err := os.ReadFile(paths.Key)
701700
if err != nil {
702701
if !os.IsNotExist(err) {
703702
return RootCA{}, err
@@ -910,13 +909,13 @@ func readCertValidity(kr KeyReader) (time.Time, time.Time, error) {
910909
// SaveRootCA saves a RootCA object to disk
911910
func SaveRootCA(rootCA RootCA, paths CertPaths) error {
912911
// Make sure the necessary dirs exist and they are writable
913-
err := os.MkdirAll(filepath.Dir(paths.Cert), 0755)
912+
err := os.MkdirAll(filepath.Dir(paths.Cert), 0o755)
914913
if err != nil {
915914
return err
916915
}
917916

918917
// If the root certificate got returned successfully, save the rootCA to disk.
919-
return ioutils.AtomicWriteFile(paths.Cert, rootCA.Certs, 0644)
918+
return ioutils.AtomicWriteFile(paths.Cert, rootCA.Certs, 0o644)
920919
}
921920

922921
// GenerateNewCSR returns a newly generated key and CSR signed with said key

ca/certificates_test.go

+21-22
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"encoding/hex"
1212
"encoding/pem"
1313
"fmt"
14-
"io/ioutil"
1514
"net"
1615
"os"
1716
"sync"
@@ -79,7 +78,7 @@ func TestMain(m *testing.M) {
7978
}
8079

8180
func TestCreateRootCASaveRootCA(t *testing.T) {
82-
tempBaseDir, err := ioutil.TempDir("", "swarm-ca-test-")
81+
tempBaseDir, err := os.MkdirTemp("", "swarm-ca-test-")
8382
assert.NoError(t, err)
8483
defer os.RemoveAll(tempBaseDir)
8584

@@ -100,7 +99,7 @@ func TestCreateRootCASaveRootCA(t *testing.T) {
10099
assert.True(t, os.IsNotExist(err))
101100

102101
// ensure that the cert that was written is already normalized
103-
written, err := ioutil.ReadFile(paths.RootCA.Cert)
102+
written, err := os.ReadFile(paths.RootCA.Cert)
104103
assert.NoError(t, err)
105104
assert.Equal(t, written, ca.NormalizePEMs(written))
106105
}
@@ -118,7 +117,7 @@ func TestCreateRootCAExpiry(t *testing.T) {
118117
}
119118

120119
func TestGetLocalRootCA(t *testing.T) {
121-
tempBaseDir, err := ioutil.TempDir("", "swarm-ca-test-")
120+
tempBaseDir, err := os.MkdirTemp("", "swarm-ca-test-")
122121
assert.NoError(t, err)
123122
defer os.RemoveAll(tempBaseDir)
124123

@@ -144,7 +143,7 @@ func TestGetLocalRootCA(t *testing.T) {
144143
assert.Equal(t, err, ca.ErrNoValidSigner)
145144

146145
// write private key and assert we can load it and sign
147-
assert.NoError(t, ioutil.WriteFile(paths.RootCA.Key, s.Key, os.FileMode(0600)))
146+
assert.NoError(t, os.WriteFile(paths.RootCA.Key, s.Key, os.FileMode(0o600)))
148147
rootCA3, err := ca.GetLocalRootCA(paths.RootCA)
149148
assert.NoError(t, err)
150149
assert.Equal(t, rootCA.Certs, rootCA3.Certs)
@@ -160,30 +159,30 @@ func TestGetLocalRootCA(t *testing.T) {
160159
Type: "EC PRIVATE KEY",
161160
Bytes: privKeyBytes,
162161
})
163-
assert.NoError(t, ioutil.WriteFile(paths.RootCA.Key, privKeyPem, os.FileMode(0600)))
162+
assert.NoError(t, os.WriteFile(paths.RootCA.Key, privKeyPem, os.FileMode(0o600)))
164163

165164
_, err = ca.GetLocalRootCA(paths.RootCA)
166165
assert.EqualError(t, err, "certificate key mismatch")
167166
}
168167

169168
func TestGetLocalRootCAInvalidCert(t *testing.T) {
170-
tempBaseDir, err := ioutil.TempDir("", "swarm-ca-test-")
169+
tempBaseDir, err := os.MkdirTemp("", "swarm-ca-test-")
171170
assert.NoError(t, err)
172171
defer os.RemoveAll(tempBaseDir)
173172

174173
paths := ca.NewConfigPaths(tempBaseDir)
175174

176175
// Write some garbage to the CA cert
177-
require.NoError(t, ioutil.WriteFile(paths.RootCA.Cert, []byte(`-----BEGIN CERTIFICATE-----\n
176+
require.NoError(t, os.WriteFile(paths.RootCA.Cert, []byte(`-----BEGIN CERTIFICATE-----\n
178177
some random garbage\n
179-
-----END CERTIFICATE-----`), 0644))
178+
-----END CERTIFICATE-----`), 0o644))
180179

181180
_, err = ca.GetLocalRootCA(paths.RootCA)
182181
require.Error(t, err)
183182
}
184183

185184
func TestGetLocalRootCAInvalidKey(t *testing.T) {
186-
tempBaseDir, err := ioutil.TempDir("", "swarm-ca-test-")
185+
tempBaseDir, err := os.MkdirTemp("", "swarm-ca-test-")
187186
assert.NoError(t, err)
188187
defer os.RemoveAll(tempBaseDir)
189188

@@ -194,9 +193,9 @@ func TestGetLocalRootCAInvalidKey(t *testing.T) {
194193
require.NoError(t, ca.SaveRootCA(rootCA, paths.RootCA))
195194

196195
// Write some garbage to the root key - this will cause the loading to fail
197-
require.NoError(t, ioutil.WriteFile(paths.RootCA.Key, []byte(`-----BEGIN PRIVATE KEY-----\n
196+
require.NoError(t, os.WriteFile(paths.RootCA.Key, []byte(`-----BEGIN PRIVATE KEY-----\n
198197
some random garbage\n
199-
-----END PRIVATE KEY-----`), 0600))
198+
-----END PRIVATE KEY-----`), 0o600))
200199

201200
_, err = ca.GetLocalRootCA(paths.RootCA)
202201
require.Error(t, err)
@@ -261,7 +260,7 @@ func TestGetRemoteCA(t *testing.T) {
261260

262261
// update the test CA to include a multi-certificate bundle as the root - the digest
263262
// we use to verify with must be the digest of the whole bundle
264-
tmpDir, err := ioutil.TempDir("", "GetRemoteCA")
263+
tmpDir, err := os.MkdirTemp("", "GetRemoteCA")
265264
require.NoError(t, err)
266265
defer os.RemoveAll(tmpDir)
267266
paths := ca.NewConfigPaths(tmpDir)
@@ -338,7 +337,7 @@ func testRequestAndSaveNewCertificates(t *testing.T, tc *cautils.TestCA) (*ca.Is
338337
require.False(t, perms.GroupWrite())
339338
require.False(t, perms.OtherWrite())
340339

341-
certs, err := ioutil.ReadFile(tc.Paths.Node.Cert)
340+
certs, err := os.ReadFile(tc.Paths.Node.Cert)
342341
require.NoError(t, err)
343342
require.Equal(t, certs, ca.NormalizePEMs(certs))
344343

@@ -374,7 +373,7 @@ func TestRequestAndSaveNewCertificatesWithIntermediates(t *testing.T) {
374373
CrossSignedCACert: concat([]byte(" "), cautils.ECDSACertChain[1]),
375374
},
376375
}
377-
tempdir, err := ioutil.TempDir("", "test-request-and-save-new-certificates")
376+
tempdir, err := os.MkdirTemp("", "test-request-and-save-new-certificates")
378377
require.NoError(t, err)
379378
defer os.RemoveAll(tempdir)
380379

@@ -453,7 +452,7 @@ func TestRequestAndSaveNewCertificatesWithKEKUpdate(t *testing.T) {
453452

454453
// returns the issuer of the issued certificate and the parsed certs of the issued certificate
455454
func testIssueAndSaveNewCertificates(t *testing.T, rca *ca.RootCA) {
456-
tempdir, err := ioutil.TempDir("", "test-issue-and-save-new-certificates")
455+
tempdir, err := os.MkdirTemp("", "test-issue-and-save-new-certificates")
457456
require.NoError(t, err)
458457
defer os.RemoveAll(tempdir)
459458
paths := ca.NewConfigPaths(tempdir)
@@ -485,7 +484,7 @@ func testIssueAndSaveNewCertificates(t *testing.T, rca *ca.RootCA) {
485484
require.False(t, perms.GroupWrite())
486485
require.False(t, perms.OtherWrite())
487486

488-
certBytes, err := ioutil.ReadFile(paths.Node.Cert)
487+
certBytes, err := os.ReadFile(paths.Node.Cert)
489488
require.NoError(t, err)
490489
parsed := checkLeafCert(t, certBytes, issuer.Subject.CommonName, "CN", role, "org", additionalNames...)
491490
if len(rca.Intermediates) > 0 {
@@ -926,7 +925,7 @@ func TestNewRootCA(t *testing.T) {
926925
}
927926

928927
func TestNewRootCABundle(t *testing.T) {
929-
tempBaseDir, err := ioutil.TempDir("", "swarm-ca-test-")
928+
tempBaseDir, err := os.MkdirTemp("", "swarm-ca-test-")
930929
assert.NoError(t, err)
931930
defer os.RemoveAll(tempBaseDir)
932931

@@ -944,7 +943,7 @@ func TestNewRootCABundle(t *testing.T) {
944943

945944
// Overwrite the bytes of the second Root CA with the bundle, creating a valid 2 cert bundle
946945
bundle := append(firstRootCA.Certs, secondRootCA.Certs...)
947-
err = ioutil.WriteFile(paths.RootCA.Cert, bundle, 0644)
946+
err = os.WriteFile(paths.RootCA.Cert, bundle, 0o644)
948947
assert.NoError(t, err)
949948

950949
newRootCA, err := ca.NewRootCA(bundle, firstRootCA.Certs, s.Key, ca.DefaultNodeCertExpiration, nil)
@@ -957,7 +956,7 @@ func TestNewRootCABundle(t *testing.T) {
957956
_, _, err = newRootCA.IssueAndSaveNewCertificates(kw, "CN", "OU", "ORG")
958957
assert.NoError(t, err)
959958

960-
certBytes, err := ioutil.ReadFile(paths.Node.Cert)
959+
certBytes, err := os.ReadFile(paths.Node.Cert)
961960
assert.NoError(t, err)
962961
assert.Len(t, checkLeafCert(t, certBytes, "rootCN1", "CN", "OU", "ORG"), 1)
963962
}
@@ -1189,7 +1188,7 @@ func TestNewRootCAInvalidCertAndKeys(t *testing.T) {
11891188
}
11901189

11911190
func TestRootCAWithCrossSignedIntermediates(t *testing.T) {
1192-
tempdir, err := ioutil.TempDir("", "swarm-ca-test-")
1191+
tempdir, err := os.MkdirTemp("", "swarm-ca-test-")
11931192
require.NoError(t, err)
11941193
defer os.RemoveAll(tempdir)
11951194

@@ -1485,7 +1484,7 @@ func TestRootCACrossSignCACertificate(t *testing.T) {
14851484
},
14861485
}
14871486

1488-
tempdir, err := ioutil.TempDir("", "cross-sign-cert")
1487+
tempdir, err := os.MkdirTemp("", "cross-sign-cert")
14891488
require.NoError(t, err)
14901489
defer os.RemoveAll(tempdir)
14911490
paths := ca.NewConfigPaths(tempdir)

0 commit comments

Comments
 (0)