Skip to content

Commit

Permalink
io/ioutil is deprecated, refactor it
Browse files Browse the repository at this point in the history
  • Loading branch information
CMGS committed Oct 6, 2022
1 parent ed3a2e7 commit bb6f2c9
Show file tree
Hide file tree
Showing 29 changed files with 77 additions and 93 deletions.
3 changes: 1 addition & 2 deletions cluster/calcium/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"time"

Expand Down Expand Up @@ -151,7 +150,7 @@ func (c *Calcium) pushImageAndClean(ctx context.Context, resp io.ReadCloser, nod
lastMessage.Error = err.Error()
break
}
malformed, _ := ioutil.ReadAll(decoder.Buffered()) // TODO err check
malformed, _ := io.ReadAll(decoder.Buffered()) // TODO err check
logger.Errorf(ctx, "[BuildImage] Decode build image message failed %+v, buffered: %v", err, malformed)
return
}
Expand Down
8 changes: 4 additions & 4 deletions cluster/calcium/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
"io/ioutil"
"io"
"testing"

enginemocks "github.com/projecteru2/core/engine/mocks"
Expand Down Expand Up @@ -103,14 +103,14 @@ func TestBuild(t *testing.T) {
assert.NoError(t, err)
buildImageResp2, err := json.Marshal(buildImageMessage)
assert.NoError(t, err)
buildImageRespReader := ioutil.NopCloser(bytes.NewReader(buildImageResp))
buildImageRespReader2 := ioutil.NopCloser(bytes.NewReader(buildImageResp2))
buildImageRespReader := io.NopCloser(bytes.NewReader(buildImageResp))
buildImageRespReader2 := io.NopCloser(bytes.NewReader(buildImageResp2))
engine.On("BuildRefs", mock.Anything, mock.Anything, mock.Anything).Return([]string{"t1", "t2"})
// failed by build context
engine.On("BuildContent", mock.Anything, mock.Anything, mock.Anything).Return("", nil, types.ErrBadCount).Once()
ch, err = c.BuildImage(ctx, opts)
assert.Error(t, err)
b := ioutil.NopCloser(bytes.NewReader([]byte{}))
b := io.NopCloser(bytes.NewReader([]byte{}))
engine.On("BuildContent", mock.Anything, mock.Anything, mock.Anything).Return("", b, nil)
// failed by ImageBuild
opts.BuildMethod = types.BuildFromRaw
Expand Down
5 changes: 2 additions & 3 deletions cluster/calcium/calcium_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package calcium

import (
"context"
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand All @@ -21,7 +20,7 @@ import (
)

func NewTestCluster() *Calcium {
walDir, err := ioutil.TempDir(os.TempDir(), "core.wal.*")
walDir, err := os.MkdirTemp(os.TempDir(), "core.wal.*")
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -65,7 +64,7 @@ func TestNewCluster(t *testing.T) {
assert.NoError(t, err)

c.Finalizer()
privFile, err := ioutil.TempFile("", "priv")
privFile, err := os.CreateTemp("", "priv")
assert.NoError(t, err)
_, err = privFile.WriteString("privkey")
assert.NoError(t, err)
Expand Down
4 changes: 2 additions & 2 deletions cluster/calcium/control_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package calcium
import (
"bytes"
"context"
"io/ioutil"
"io"
"testing"

"github.com/projecteru2/core/cluster"
Expand Down Expand Up @@ -72,7 +72,7 @@ func TestControlStart(t *testing.T) {
assert.Error(t, r.Error)
assert.Equal(t, r.WorkloadID, "id1")
}
data := ioutil.NopCloser(bytes.NewBufferString("output"))
data := io.NopCloser(bytes.NewBufferString("output"))
engine.On("Execute", mock.Anything, mock.Anything, mock.Anything).Return("eid", data, nil, nil, nil).Times(4)
// failed by ExecExitCode
engine.On("ExecExitCode", mock.Anything, mock.Anything, mock.Anything).Return(-1, types.ErrNilEngine).Once()
Expand Down
4 changes: 2 additions & 2 deletions cluster/calcium/execute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
"io/ioutil"
"io"
"testing"

enginemocks "github.com/projecteru2/core/engine/mocks"
Expand Down Expand Up @@ -50,7 +50,7 @@ func TestExecuteWorkload(t *testing.T) {
for ac := range ch {
assert.Equal(t, ac.WorkloadID, ID)
}
buf := ioutil.NopCloser(bytes.NewBufferString(`echo 1\n`))
buf := io.NopCloser(bytes.NewBufferString(`echo 1\n`))
engine.On("Execute", mock.Anything, mock.Anything, mock.Anything).Return(result, buf, nil, nil, nil).Twice()

// failed by ExecExitCode
Expand Down
4 changes: 2 additions & 2 deletions cluster/calcium/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package calcium
import (
"bytes"
"context"
"io/ioutil"
"io"
"testing"

enginemocks "github.com/projecteru2/core/engine/mocks"
Expand Down Expand Up @@ -51,7 +51,7 @@ func TestCacheImage(t *testing.T) {
// succ
engine.On("ImageRemoteDigest", mock.Anything, mock.Anything).Return("yy", nil)
engine.On("ImageLocalDigests", mock.Anything, mock.Anything).Return([]string{"xx"}, nil)
engine.On("ImagePull", mock.Anything, mock.Anything, mock.Anything).Return(ioutil.NopCloser(bytes.NewReader([]byte{})), nil)
engine.On("ImagePull", mock.Anything, mock.Anything, mock.Anything).Return(io.NopCloser(bytes.NewReader([]byte{})), nil)
ch, err = c.CacheImage(ctx, &types.ImageOptions{Podname: "podname", Images: []string{"xx"}})
for c := range ch {
assert.True(t, c.Success)
Expand Down
5 changes: 2 additions & 3 deletions cluster/calcium/lambda_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"strings"
"testing"

Expand Down Expand Up @@ -98,7 +97,7 @@ func TestLambdaWithWorkloadIDReturned(t *testing.T) {
w2.Write([]byte("stderr line2\n"))
w2.Close()
}()
engine.On("VirtualizationLogs", mock.Anything, mock.Anything).Return(ioutil.NopCloser(r1), ioutil.NopCloser(r2), nil)
engine.On("VirtualizationLogs", mock.Anything, mock.Anything).Return(io.NopCloser(r1), io.NopCloser(r2), nil)
engine.On("VirtualizationWait", mock.Anything, mock.Anything, mock.Anything).Return(&enginetypes.VirtualizationWaitResult{Code: 0}, nil)

ids, ch, err := c.RunAndWait(context.Background(), opts, make(chan []byte))
Expand Down Expand Up @@ -170,7 +169,7 @@ func TestLambdaWithError(t *testing.T) {
w2.Write([]byte("stderr line2\n"))
w2.Close()
}()
engine.On("VirtualizationLogs", mock.Anything, mock.Anything).Return(ioutil.NopCloser(r1), ioutil.NopCloser(r2), nil)
engine.On("VirtualizationLogs", mock.Anything, mock.Anything).Return(io.NopCloser(r1), io.NopCloser(r2), nil)

engine.On("VirtualizationWait", mock.Anything, mock.Anything, mock.Anything).Return(nil, fmt.Errorf("error"))
ids, ch2, err := c.RunAndWait(context.Background(), opts, make(chan []byte))
Expand Down
4 changes: 2 additions & 2 deletions cluster/calcium/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package calcium
import (
"bytes"
"context"
"io/ioutil"
"io"
"testing"

enginemocks "github.com/projecteru2/core/engine/mocks"
Expand Down Expand Up @@ -43,7 +43,7 @@ func TestLogStream(t *testing.T) {
assert.Empty(t, c.Data)
}
reader := bytes.NewBufferString("aaaa\nbbbb\n")
engine.On("VirtualizationLogs", mock.Anything, mock.Anything).Return(ioutil.NopCloser(reader), nil, nil)
engine.On("VirtualizationLogs", mock.Anything, mock.Anything).Return(io.NopCloser(reader), nil, nil)
// success
ch, err = c.LogStream(ctx, opts)
assert.NoError(t, err)
Expand Down
6 changes: 3 additions & 3 deletions cluster/calcium/send_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package calcium

import (
"context"
"io/ioutil"
"io"
"os"
"testing"

Expand All @@ -25,7 +25,7 @@ func TestSend(t *testing.T) {
_, err = c.Send(ctx, &types.SendOptions{IDs: []string{"id"}})
assert.Error(t, err)

tmpfile, err := ioutil.TempFile("", "example")
tmpfile, err := os.CreateTemp("", "example")
assert.NoError(t, err)
defer os.RemoveAll(tmpfile.Name())
defer tmpfile.Close()
Expand Down Expand Up @@ -55,7 +55,7 @@ func TestSend(t *testing.T) {
[]*types.Workload{{ID: "cid", Engine: engine}}, nil,
)
// failed by engine
content, _ := ioutil.ReadAll(tmpfile)
content, _ := io.ReadAll(tmpfile)
opts.Files[0].Content = content
engine.On("VirtualizationCopyTo",
mock.Anything, mock.Anything, mock.Anything,
Expand Down
3 changes: 1 addition & 2 deletions cluster/calcium/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"sync"

"github.com/pkg/errors"
Expand Down Expand Up @@ -151,7 +150,7 @@ func (c *Calcium) processBuildImageStream(ctx context.Context, reader io.ReadClo
err := decoder.Decode(message)
if err != nil {
if err != io.EOF {
malformed, _ := ioutil.ReadAll(decoder.Buffered()) // TODO err check
malformed, _ := io.ReadAll(decoder.Buffered()) // TODO err check
log.Errorf(ctx, "[processBuildImageStream] Decode image message failed %v, buffered: %s", err, string(malformed))
message.Error = err.Error()
ch <- message
Expand Down
7 changes: 6 additions & 1 deletion core.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os/signal"
"syscall"
"testing"
"time"

"github.com/prometheus/client_golang/prometheus/promhttp"

Expand Down Expand Up @@ -97,7 +98,11 @@ func serve(c *cli.Context) error {
if config.Profile != "" {
http.Handle("/metrics", metrics.Client.ResourceMiddleware(cluster)(promhttp.Handler()))
utils.SentryGo(func() {
if err := http.ListenAndServe(config.Profile, nil); err != nil {
server := &http.Server{
Addr: config.Profile,
ReadHeaderTimeout: 3 * time.Second,
}
if err := server.ListenAndServe(); err != nil {
log.Errorf(c.Context, "[main] start http failed %v", err) //nolint
}
})
Expand Down
7 changes: 3 additions & 4 deletions engine/docker/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -64,14 +63,14 @@ func (e *Engine) BuildRefs(ctx context.Context, opts *enginetypes.BuildRefOption
//
// build directory is like:
//
// buildDir ├─ :appname ├─ code
// ├─ Dockerfile
// buildDir ├─ :appname ├─ code
// ├─ Dockerfile
func (e *Engine) BuildContent(ctx context.Context, scm coresource.Source, opts *enginetypes.BuildContentOptions) (string, io.Reader, error) {
if opts.Builds == nil {
return "", nil, coretypes.ErrNoBuildsInSpec
}
// make build dir
buildDir, err := ioutil.TempDir(os.TempDir(), "corebuild-")
buildDir, err := os.MkdirTemp(os.TempDir(), "corebuild-")
if err != nil {
return "", nil, err
}
Expand Down
7 changes: 3 additions & 4 deletions engine/docker/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"math"
"os"
"path/filepath"
Expand Down Expand Up @@ -345,7 +344,7 @@ func (e *Engine) VirtualizationLogs(ctx context.Context, opts *enginetypes.Virtu
return nil, nil, err
}
if !opts.Stderr {
return ioutil.NopCloser(mergeStream(resp)), nil, nil
return io.NopCloser(mergeStream(resp)), nil, nil
}
stdout, stderr = e.demultiplexStdStream(ctx, resp)
return stdout, stderr, nil
Expand All @@ -365,7 +364,7 @@ func (e *Engine) VirtualizationAttach(ctx context.Context, ID string, stream, st
return nil, nil, nil, err
}
if stdin {
return ioutil.NopCloser(resp.Reader), nil, resp.Conn, nil
return io.NopCloser(resp.Reader), nil, resp.Conn, nil
}
stdout, stderr = e.demultiplexStdStream(ctx, resp.Reader)
return stdout, stderr, resp.Conn, nil
Expand Down Expand Up @@ -457,6 +456,6 @@ func (e *Engine) VirtualizationCopyFrom(ctx context.Context, ID, path string) (c
if err != nil {
return
}
content, err = ioutil.ReadAll(tarReader)
content, err = io.ReadAll(tarReader)
return content, header.Uid, header.Gid, header.Mode, err
}
3 changes: 1 addition & 2 deletions engine/docker/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package docker
import (
"context"
"io"
"io/ioutil"

enginetypes "github.com/projecteru2/core/engine/types"
"github.com/projecteru2/core/log"
Expand Down Expand Up @@ -44,7 +43,7 @@ func (e *Engine) execAttach(ctx context.Context, execID string, tty bool) (io.Re
if err != nil {
return nil, nil, err
}
return ioutil.NopCloser(resp.Reader), resp.Conn, nil
return io.NopCloser(resp.Reader), resp.Conn, nil
}

// Execute executes a workload
Expand Down
5 changes: 2 additions & 3 deletions engine/docker/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"math"
"net"
"net/http"
Expand Down Expand Up @@ -63,14 +62,14 @@ func mergeStream(stream io.ReadCloser) io.Reader {

// FuckDockerStream will copy docker stream to stdout and err
func FuckDockerStream(stream dockertypes.HijackedResponse) io.ReadCloser {
outr := mergeStream(ioutil.NopCloser(stream.Reader))
outr := mergeStream(io.NopCloser(stream.Reader))
return fuckDockerStream{stream.Conn, outr}
}

// make mount paths
// 使用volumes, 参数格式跟docker一样
// volumes:
// - "/foo-data:$SOMEENV/foodata:rw"
// - "/foo-data:$SOMEENV/foodata:rw"
func makeMountPaths(opts *enginetypes.VirtualizationCreateOptions) ([]string, map[string]struct{}) {
binds := []string{}
volumes := make(map[string]struct{})
Expand Down
4 changes: 2 additions & 2 deletions engine/docker/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package docker
import (
"bytes"
"context"
"io/ioutil"
"io"
"os"
"strings"
"testing"
Expand All @@ -15,7 +15,7 @@ import (

func TestCreateTarStream(t *testing.T) {
buff := bytes.NewBufferString("test")
rc := ioutil.NopCloser(buff)
rc := io.NopCloser(buff)
fname, err := coreutils.TempFile(rc)
assert.NoError(t, err)
_, err = CreateTarStream(fname)
Expand Down
3 changes: 1 addition & 2 deletions engine/docker/tarfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package docker
import (
"archive/tar"
"context"
"io/ioutil"
"os"
"path/filepath"

Expand All @@ -27,7 +26,7 @@ func withTarfileDump(ctx context.Context, target string, content []byte, uid, gi

func tempTarFile(path string, data []byte, uid, gid int, mode int64) (string, error) {
filename := filepath.Base(path)
f, err := ioutil.TempFile(os.TempDir(), filename)
f, err := os.CreateTemp(os.TempDir(), filename)
if err != nil {
return "", err
}
Expand Down
Loading

0 comments on commit bb6f2c9

Please sign in to comment.