Remove ioutil from client-go

Signed-off-by: inosato <si17_21@yahoo.co.jp>

Kubernetes-commit: 88dfa51b6003c90e8f0a0508939a1d79950a40df
This commit is contained in:
inosato
2022-07-30 20:54:41 +09:00
committed by Kubernetes Publisher
parent 2e404084ad
commit 27de641f75
43 changed files with 248 additions and 275 deletions

View File

@@ -18,7 +18,7 @@ package disk
import (
"errors"
"io/ioutil"
"io"
"net/http"
"os"
"path/filepath"
@@ -157,7 +157,7 @@ func (d *CachedDiscoveryClient) getCachedFile(filename string) ([]byte, error) {
}
// the cache is present and its valid. Try to read and use it.
cachedBytes, err := ioutil.ReadAll(file)
cachedBytes, err := io.ReadAll(file)
if err != nil {
return nil, err
}
@@ -179,7 +179,7 @@ func (d *CachedDiscoveryClient) writeCachedFile(filename string, obj runtime.Obj
return err
}
f, err := ioutil.TempFile(filepath.Dir(filename), filepath.Base(filename)+".")
f, err := os.CreateTemp(filepath.Dir(filename), filepath.Base(filename)+".")
if err != nil {
return err
}

View File

@@ -17,7 +17,6 @@ limitations under the License.
package disk
import (
"io/ioutil"
"os"
"path/filepath"
"strings"
@@ -42,7 +41,7 @@ import (
func TestCachedDiscoveryClient_Fresh(t *testing.T) {
assert := assert.New(t)
d, err := ioutil.TempDir("", "")
d, err := os.MkdirTemp("", "")
assert.NoError(err)
defer os.RemoveAll(d)
@@ -86,7 +85,7 @@ func TestCachedDiscoveryClient_Fresh(t *testing.T) {
func TestNewCachedDiscoveryClient_TTL(t *testing.T) {
assert := assert.New(t)
d, err := ioutil.TempDir("", "")
d, err := os.MkdirTemp("", "")
assert.NoError(err)
defer os.RemoveAll(d)
@@ -104,7 +103,7 @@ func TestNewCachedDiscoveryClient_TTL(t *testing.T) {
func TestNewCachedDiscoveryClient_PathPerm(t *testing.T) {
assert := assert.New(t)
d, err := ioutil.TempDir("", "")
d, err := os.MkdirTemp("", "")
assert.NoError(err)
os.RemoveAll(d)
defer os.RemoveAll(d)
@@ -131,13 +130,13 @@ func TestNewCachedDiscoveryClient_PathPerm(t *testing.T) {
// successive calls
func TestOpenAPIDiskCache(t *testing.T) {
// Create discovery cache dir (unused)
discoCache, err := ioutil.TempDir("", "")
discoCache, err := os.MkdirTemp("", "")
require.NoError(t, err)
os.RemoveAll(discoCache)
defer os.RemoveAll(discoCache)
// Create http cache dir
httpCache, err := ioutil.TempDir("", "")
httpCache, err := os.MkdirTemp("", "")
require.NoError(t, err)
os.RemoveAll(httpCache)
defer os.RemoveAll(httpCache)

View File

@@ -19,7 +19,7 @@ package disk
import (
"bytes"
"crypto/sha256"
"io/ioutil"
"io"
"net/http"
"net/url"
"os"
@@ -43,7 +43,7 @@ func (rt *testRoundTripper) RoundTrip(req *http.Request) (*http.Response, error)
}
func BenchmarkDiskCache(b *testing.B) {
cacheDir, err := ioutil.TempDir("", "cache-rt")
cacheDir, err := os.MkdirTemp("", "cache-rt")
if err != nil {
b.Fatal(err)
}
@@ -57,7 +57,7 @@ func BenchmarkDiskCache(b *testing.B) {
})
k := "localhost:8080/apis/batch/v1.json"
v, err := ioutil.ReadFile("../../testdata/apis/batch/v1.json")
v, err := os.ReadFile("../../testdata/apis/batch/v1.json")
if err != nil {
b.Fatal(err)
}
@@ -73,7 +73,7 @@ func BenchmarkDiskCache(b *testing.B) {
func TestCacheRoundTripper(t *testing.T) {
rt := &testRoundTripper{}
cacheDir, err := ioutil.TempDir("", "cache-rt")
cacheDir, err := os.MkdirTemp("", "cache-rt")
defer os.RemoveAll(cacheDir)
if err != nil {
t.Fatal(err)
@@ -87,14 +87,14 @@ func TestCacheRoundTripper(t *testing.T) {
}
rt.Response = &http.Response{
Header: http.Header{"ETag": []string{`"123456"`}},
Body: ioutil.NopCloser(bytes.NewReader([]byte("Content"))),
Body: io.NopCloser(bytes.NewReader([]byte("Content"))),
StatusCode: http.StatusOK,
}
resp, err := cache.RoundTrip(req)
if err != nil {
t.Fatal(err)
}
content, err := ioutil.ReadAll(resp.Body)
content, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
@@ -109,7 +109,7 @@ func TestCacheRoundTripper(t *testing.T) {
}
rt.Response = &http.Response{
StatusCode: http.StatusNotModified,
Body: ioutil.NopCloser(bytes.NewReader([]byte("Other Content"))),
Body: io.NopCloser(bytes.NewReader([]byte("Other Content"))),
}
resp, err = cache.RoundTrip(req)
@@ -118,7 +118,7 @@ func TestCacheRoundTripper(t *testing.T) {
}
// Read body and make sure we have the initial content
content, err = ioutil.ReadAll(resp.Body)
content, err = io.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
t.Fatal(err)
@@ -132,7 +132,7 @@ func TestCacheRoundTripperPathPerm(t *testing.T) {
assert := assert.New(t)
rt := &testRoundTripper{}
cacheDir, err := ioutil.TempDir("", "cache-rt")
cacheDir, err := os.MkdirTemp("", "cache-rt")
os.RemoveAll(cacheDir)
defer os.RemoveAll(cacheDir)
@@ -148,14 +148,14 @@ func TestCacheRoundTripperPathPerm(t *testing.T) {
}
rt.Response = &http.Response{
Header: http.Header{"ETag": []string{`"123456"`}},
Body: ioutil.NopCloser(bytes.NewReader([]byte("Content"))),
Body: io.NopCloser(bytes.NewReader([]byte("Content"))),
StatusCode: http.StatusOK,
}
resp, err := cache.RoundTrip(req)
if err != nil {
t.Fatal(err)
}
content, err := ioutil.ReadAll(resp.Body)
content, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
@@ -182,7 +182,7 @@ func TestSumDiskCache(t *testing.T) {
// Ensure that we'll return a cache miss if the backing file doesn't exist.
t.Run("NoSuchKey", func(t *testing.T) {
cacheDir, err := ioutil.TempDir("", "cache-test")
cacheDir, err := os.MkdirTemp("", "cache-test")
if err != nil {
t.Fatal(err)
}
@@ -199,7 +199,7 @@ func TestSumDiskCache(t *testing.T) {
// Ensure that we'll return a cache miss if the backing file is empty.
t.Run("EmptyFile", func(t *testing.T) {
cacheDir, err := ioutil.TempDir("", "cache-test")
cacheDir, err := os.MkdirTemp("", "cache-test")
if err != nil {
t.Fatal(err)
}
@@ -223,7 +223,7 @@ func TestSumDiskCache(t *testing.T) {
// Ensure that we'll return a cache miss if the backing has an invalid
// checksum.
t.Run("InvalidChecksum", func(t *testing.T) {
cacheDir, err := ioutil.TempDir("", "cache-test")
cacheDir, err := os.MkdirTemp("", "cache-test")
if err != nil {
t.Fatal(err)
}
@@ -258,7 +258,7 @@ func TestSumDiskCache(t *testing.T) {
// This should cause httpcache to fall back to its underlying transport and
// to subsequently cache the new value, overwriting the corrupt one.
t.Run("OverwriteExistingKey", func(t *testing.T) {
cacheDir, err := ioutil.TempDir("", "cache-test")
cacheDir, err := os.MkdirTemp("", "cache-test")
if err != nil {
t.Fatal(err)
}
@@ -290,7 +290,7 @@ func TestSumDiskCache(t *testing.T) {
// Ensure that deleting a key does in fact delete it.
t.Run("DeleteKey", func(t *testing.T) {
cacheDir, err := ioutil.TempDir("", "cache-test")
cacheDir, err := os.MkdirTemp("", "cache-test")
if err != nil {
t.Fatal(err)
}