*: move from io/ioutil to io and os packages

The io/ioutil package has been deprecated as of Go 1.16, see
https://golang.org/doc/go1.16#ioutil. This commit replaces the existing
io/ioutil functions with their new definitions in io and os packages.

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
This commit is contained in:
Eng Zer Jun
2021-10-28 00:00:49 +08:00
parent 0acbf2fc85
commit 2a151c8982
98 changed files with 267 additions and 319 deletions

View File

@@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
@@ -127,7 +126,7 @@ func TestHealthHandler(t *testing.T) {
func parseHealthOutput(body io.Reader) (Health, error) {
obj := Health{}
d, derr := ioutil.ReadAll(body)
d, derr := io.ReadAll(body)
if derr != nil {
return obj, derr
}

View File

@@ -18,7 +18,7 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"path"
@@ -98,9 +98,9 @@ func TestNewPeerHandlerOnRaftPrefix(t *testing.T) {
if err != nil {
t.Fatalf("unexpected http.Get error: %v", err)
}
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("unexpected ioutil.ReadAll error: %v", err)
t.Fatalf("unexpected io.ReadAll error: %v", err)
}
if w := "test data"; string(body) != w {
t.Errorf("#%d: body = %s, want %s", i, body, w)
@@ -267,10 +267,10 @@ func TestNewPeerHandlerOnMembersPromotePrefix(t *testing.T) {
if err != nil {
t.Fatalf("failed to get http response: %v", err)
}
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
t.Fatalf("unexpected ioutil.ReadAll error: %v", err)
t.Fatalf("unexpected io.ReadAll error: %v", err)
}
if resp.StatusCode != tt.wcode {
t.Fatalf("#%d: code = %d, want %d", i, resp.StatusCode, tt.wcode)

View File

@@ -18,7 +18,7 @@ import (
"context"
"errors"
"fmt"
"io/ioutil"
"io"
"net/http"
"path"
"strings"
@@ -111,7 +111,7 @@ func (h *pipelineHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Limit the data size that could be read from the request body, which ensures that read from
// connection will not time out accidentally due to possible blocking in underlying implementation.
limitedr := pioutil.NewLimitedBufferReader(r.Body, connReadLimitByte)
b, err := ioutil.ReadAll(limitedr)
b, err := io.ReadAll(limitedr)
if err != nil {
h.lg.Warn(
"failed to read Raft message",

View File

@@ -18,7 +18,7 @@ import (
"bytes"
"context"
"errors"
"io/ioutil"
"io"
"runtime"
"sync"
"time"
@@ -154,7 +154,7 @@ func (p *pipeline) post(data []byte) (err error) {
return err
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
b, err := io.ReadAll(resp.Body)
if err != nil {
p.picker.unreachable(u)
return err

View File

@@ -18,7 +18,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"sync"
"testing"
@@ -158,7 +157,7 @@ func TestPipelinePost(t *testing.T) {
if g := req.Header.Get("X-Etcd-Cluster-ID"); g != "1" {
t.Errorf("cluster id = %s, want %s", g, "1")
}
b, err := ioutil.ReadAll(req.Body)
b, err := io.ReadAll(req.Body)
if err != nil {
t.Fatalf("unexpected ReadAll error: %v", err)
}

View File

@@ -18,7 +18,6 @@ import (
"bytes"
"context"
"io"
"io/ioutil"
"net/http"
"time"
@@ -169,7 +168,7 @@ func (s *snapshotSender) post(req *http.Request) (err error) {
// prevents from reading the body forever when the other side dies right after
// successfully receives the request body.
time.AfterFunc(snapResponseReadTimeout, func() { httputil.GracefulClose(resp) })
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
result <- responseAndError{resp, body, err}
}()

View File

@@ -17,7 +17,6 @@ package rafthttp
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
@@ -94,8 +93,8 @@ func TestSnapshotSend(t *testing.T) {
}
}
func testSnapshotSend(t *testing.T, sm *snap.Message) (bool, []os.FileInfo) {
d, err := ioutil.TempDir(os.TempDir(), "snapdir")
func testSnapshotSend(t *testing.T, sm *snap.Message) (bool, []os.DirEntry) {
d, err := os.MkdirTemp(os.TempDir(), "snapdir")
if err != nil {
t.Fatal(err)
}
@@ -124,7 +123,7 @@ func testSnapshotSend(t *testing.T, sm *snap.Message) (bool, []os.FileInfo) {
// wait for handler to finish accepting snapshot
<-ch
files, rerr := ioutil.ReadDir(d)
files, rerr := os.ReadDir(d)
if rerr != nil {
t.Fatal(rerr)
}

View File

@@ -18,7 +18,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"net/http"
"path"
"strings"
@@ -629,7 +628,7 @@ func (cr *streamReader) dial(t streamType) (io.ReadCloser, error) {
return nil, fmt.Errorf("peer %s failed to find local node %s", cr.peerID, cr.tr.ID)
case http.StatusPreconditionFailed:
b, err := ioutil.ReadAll(resp.Body)
b, err := io.ReadAll(resp.Body)
if err != nil {
cr.picker.unreachable(u)
return nil, err

View File

@@ -18,7 +18,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"time"
@@ -36,7 +35,7 @@ var ErrNoDBSnapshot = errors.New("snap: snapshot file doesn't exist")
func (s *Snapshotter) SaveDBFrom(r io.Reader, id uint64) (int64, error) {
start := time.Now()
f, err := ioutil.TempFile(s.dir, "tmp")
f, err := os.CreateTemp(s.dir, "tmp")
if err != nil {
return 0, err
}

View File

@@ -18,7 +18,6 @@ import (
"errors"
"fmt"
"hash/crc32"
"io/ioutil"
"os"
"path/filepath"
"sort"
@@ -160,7 +159,7 @@ func loadSnap(lg *zap.Logger, dir, name string) (*raftpb.Snapshot, error) {
// Read reads the snapshot named by snapname and returns the snapshot.
func Read(lg *zap.Logger, snapname string) (*raftpb.Snapshot, error) {
b, err := ioutil.ReadFile(snapname)
b, err := os.ReadFile(snapname)
if err != nil {
if lg != nil {
lg.Warn("failed to read a snap file", zap.String("path", snapname), zap.Error(err))

View File

@@ -17,7 +17,6 @@ package snap
import (
"fmt"
"hash/crc32"
"io/ioutil"
"os"
"path/filepath"
"reflect"
@@ -94,7 +93,7 @@ func TestFailback(t *testing.T) {
defer os.RemoveAll(dir)
large := fmt.Sprintf("%016x-%016x-%016x.snap", 0xFFFF, 0xFFFF, 0xFFFF)
err = ioutil.WriteFile(filepath.Join(dir, large), []byte("bad data"), 0666)
err = os.WriteFile(filepath.Join(dir, large), []byte("bad data"), 0666)
if err != nil {
t.Fatal(err)
}
@@ -234,7 +233,7 @@ func TestEmptySnapshot(t *testing.T) {
}
defer os.RemoveAll(dir)
err = ioutil.WriteFile(filepath.Join(dir, "1.snap"), []byte(""), 0x700)
err = os.WriteFile(filepath.Join(dir, "1.snap"), []byte(""), 0x700)
if err != nil {
t.Fatal(err)
}
@@ -255,7 +254,7 @@ func TestAllSnapshotBroken(t *testing.T) {
}
defer os.RemoveAll(dir)
err = ioutil.WriteFile(filepath.Join(dir, "1.snap"), []byte("bad"), 0x700)
err = os.WriteFile(filepath.Join(dir, "1.snap"), []byte("bad"), 0x700)
if err != nil {
t.Fatal(err)
}
@@ -278,7 +277,7 @@ func TestReleaseSnapDBs(t *testing.T) {
snapIndices := []uint64{100, 200, 300, 400}
for _, index := range snapIndices {
filename := filepath.Join(dir, fmt.Sprintf("%016x.snap.db", index))
if err := ioutil.WriteFile(filename, []byte("snap file\n"), 0644); err != nil {
if err := os.WriteFile(filename, []byte("snap file\n"), 0644); err != nil {
t.Fatal(err)
}
}

View File

@@ -19,7 +19,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/url"
"path"
@@ -675,7 +675,7 @@ func unmarshalRequest(lg *zap.Logger, r *http.Request, req json.Unmarshaler, w h
writeError(lg, w, r, httptypes.NewHTTPError(http.StatusUnsupportedMediaType, fmt.Sprintf("Bad Content-Type %s, accept application/json", ctype)))
return false
}
b, err := ioutil.ReadAll(r.Body)
b, err := io.ReadAll(r.Body)
if err != nil {
writeError(lg, w, r, httptypes.NewHTTPError(http.StatusBadRequest, err.Error()))
return false

View File

@@ -21,10 +21,10 @@ import (
"encoding/pem"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"os"
"path"
"sort"
"strings"
@@ -456,7 +456,7 @@ func unauthedRequest() *http.Request {
}
func tlsAuthedRequest(req *http.Request, certname string) *http.Request {
bytes, err := ioutil.ReadFile(fmt.Sprintf("testdata/%s.pem", certname))
bytes, err := os.ReadFile(fmt.Sprintf("testdata/%s.pem", certname))
if err != nil {
panic(err)
}

View File

@@ -19,7 +19,7 @@ import (
"context"
"encoding/json"
"errors"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"net/url"
@@ -924,7 +924,7 @@ func TestServeMembersFail(t *testing.T) {
&http.Request{
URL: testutil.MustNewURL(t, membersPrefix),
Method: "POST",
Body: ioutil.NopCloser(strings.NewReader("bad json")),
Body: io.NopCloser(strings.NewReader("bad json")),
Header: map[string][]string{"Content-Type": {"application/json"}},
},
&resServer{},
@@ -936,7 +936,7 @@ func TestServeMembersFail(t *testing.T) {
&http.Request{
URL: testutil.MustNewURL(t, membersPrefix),
Method: "POST",
Body: ioutil.NopCloser(strings.NewReader(`{"PeerURLs": ["http://127.0.0.1:1"]}`)),
Body: io.NopCloser(strings.NewReader(`{"PeerURLs": ["http://127.0.0.1:1"]}`)),
Header: map[string][]string{"Content-Type": {"application/bad"}},
},
&errServer{},
@@ -948,7 +948,7 @@ func TestServeMembersFail(t *testing.T) {
&http.Request{
URL: testutil.MustNewURL(t, membersPrefix),
Method: "POST",
Body: ioutil.NopCloser(strings.NewReader(`{"PeerURLs": ["http://a"]}`)),
Body: io.NopCloser(strings.NewReader(`{"PeerURLs": ["http://a"]}`)),
Header: map[string][]string{"Content-Type": {"application/json"}},
},
&errServer{},
@@ -960,7 +960,7 @@ func TestServeMembersFail(t *testing.T) {
&http.Request{
URL: testutil.MustNewURL(t, membersPrefix),
Method: "POST",
Body: ioutil.NopCloser(strings.NewReader(`{"PeerURLs": ["http://127.0.0.1:1"]}`)),
Body: io.NopCloser(strings.NewReader(`{"PeerURLs": ["http://127.0.0.1:1"]}`)),
Header: map[string][]string{"Content-Type": {"application/json"}},
},
&errServer{
@@ -974,7 +974,7 @@ func TestServeMembersFail(t *testing.T) {
&http.Request{
URL: testutil.MustNewURL(t, membersPrefix),
Method: "POST",
Body: ioutil.NopCloser(strings.NewReader(`{"PeerURLs": ["http://127.0.0.1:1"]}`)),
Body: io.NopCloser(strings.NewReader(`{"PeerURLs": ["http://127.0.0.1:1"]}`)),
Header: map[string][]string{"Content-Type": {"application/json"}},
},
&errServer{
@@ -988,7 +988,7 @@ func TestServeMembersFail(t *testing.T) {
&http.Request{
URL: testutil.MustNewURL(t, membersPrefix),
Method: "POST",
Body: ioutil.NopCloser(strings.NewReader(`{"PeerURLs": ["http://127.0.0.1:1"]}`)),
Body: io.NopCloser(strings.NewReader(`{"PeerURLs": ["http://127.0.0.1:1"]}`)),
Header: map[string][]string{"Content-Type": {"application/json"}},
},
&errServer{
@@ -1058,7 +1058,7 @@ func TestServeMembersFail(t *testing.T) {
&http.Request{
URL: testutil.MustNewURL(t, path.Join(membersPrefix, "0")),
Method: "PUT",
Body: ioutil.NopCloser(strings.NewReader("bad json")),
Body: io.NopCloser(strings.NewReader("bad json")),
Header: map[string][]string{"Content-Type": {"application/json"}},
},
&resServer{},
@@ -1070,7 +1070,7 @@ func TestServeMembersFail(t *testing.T) {
&http.Request{
URL: testutil.MustNewURL(t, path.Join(membersPrefix, "0")),
Method: "PUT",
Body: ioutil.NopCloser(strings.NewReader(`{"PeerURLs": ["http://127.0.0.1:1"]}`)),
Body: io.NopCloser(strings.NewReader(`{"PeerURLs": ["http://127.0.0.1:1"]}`)),
Header: map[string][]string{"Content-Type": {"application/bad"}},
},
&errServer{},
@@ -1082,7 +1082,7 @@ func TestServeMembersFail(t *testing.T) {
&http.Request{
URL: testutil.MustNewURL(t, path.Join(membersPrefix, "0")),
Method: "PUT",
Body: ioutil.NopCloser(strings.NewReader(`{"PeerURLs": ["http://a"]}`)),
Body: io.NopCloser(strings.NewReader(`{"PeerURLs": ["http://a"]}`)),
Header: map[string][]string{"Content-Type": {"application/json"}},
},
&errServer{},
@@ -1094,7 +1094,7 @@ func TestServeMembersFail(t *testing.T) {
&http.Request{
URL: testutil.MustNewURL(t, path.Join(membersPrefix, "0")),
Method: "PUT",
Body: ioutil.NopCloser(strings.NewReader(`{"PeerURLs": ["http://127.0.0.1:1"]}`)),
Body: io.NopCloser(strings.NewReader(`{"PeerURLs": ["http://127.0.0.1:1"]}`)),
Header: map[string][]string{"Content-Type": {"application/json"}},
},
&errServer{
@@ -1108,7 +1108,7 @@ func TestServeMembersFail(t *testing.T) {
&http.Request{
URL: testutil.MustNewURL(t, path.Join(membersPrefix, "0")),
Method: "PUT",
Body: ioutil.NopCloser(strings.NewReader(`{"PeerURLs": ["http://127.0.0.1:1"]}`)),
Body: io.NopCloser(strings.NewReader(`{"PeerURLs": ["http://127.0.0.1:1"]}`)),
Header: map[string][]string{"Content-Type": {"application/json"}},
},
&errServer{
@@ -1122,7 +1122,7 @@ func TestServeMembersFail(t *testing.T) {
&http.Request{
URL: testutil.MustNewURL(t, path.Join(membersPrefix, "0")),
Method: "PUT",
Body: ioutil.NopCloser(strings.NewReader(`{"PeerURLs": ["http://127.0.0.1:1"]}`)),
Body: io.NopCloser(strings.NewReader(`{"PeerURLs": ["http://127.0.0.1:1"]}`)),
Header: map[string][]string{"Content-Type": {"application/json"}},
},
&errServer{