Skip to content

Commit

Permalink
Don't use ioutil
Browse files Browse the repository at this point in the history
io/ioutil has been deprecated since Go 1.16.

Signed-off-by: Kazuyoshi Kato <[email protected]>
  • Loading branch information
kzys committed Oct 17, 2022
1 parent 38b9b00 commit 98dc9d9
Show file tree
Hide file tree
Showing 15 changed files with 59 additions and 75 deletions.
33 changes: 16 additions & 17 deletions cgroup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package cgroups

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
Expand All @@ -30,7 +29,7 @@ import (
// using t.Error in test were defers do cleanup on the filesystem

func TestCreate(t *testing.T) {
mock, err := newMock()
mock, err := newMock(t)
if err != nil {
t.Fatal(err)
}
Expand All @@ -57,7 +56,7 @@ func TestCreate(t *testing.T) {
}

func TestStat(t *testing.T) {
mock, err := newMock()
mock, err := newMock(t)
if err != nil {
t.Fatal(err)
}
Expand All @@ -79,7 +78,7 @@ func TestStat(t *testing.T) {
}

func TestAdd(t *testing.T) {
mock, err := newMock()
mock, err := newMock(t)
if err != nil {
t.Fatal(err)
}
Expand All @@ -102,7 +101,7 @@ func TestAdd(t *testing.T) {
}

func TestAddFilteredSubsystems(t *testing.T) {
mock, err := newMock()
mock, err := newMock(t)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -159,7 +158,7 @@ func TestAddFilteredSubsystems(t *testing.T) {
}

func TestAddTask(t *testing.T) {
mock, err := newMock()
mock, err := newMock(t)
if err != nil {
t.Fatal(err)
}
Expand All @@ -182,7 +181,7 @@ func TestAddTask(t *testing.T) {
}

func TestAddTaskFilteredSubsystems(t *testing.T) {
mock, err := newMock()
mock, err := newMock(t)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -224,7 +223,7 @@ func TestAddTaskFilteredSubsystems(t *testing.T) {
}

func TestListPids(t *testing.T) {
mock, err := newMock()
mock, err := newMock(t)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -259,7 +258,7 @@ func TestListPids(t *testing.T) {
}

func TestListTasksPids(t *testing.T) {
mock, err := newMock()
mock, err := newMock(t)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -294,7 +293,7 @@ func TestListTasksPids(t *testing.T) {
}

func readValue(mock *mockCgroup, path string) (string, error) {
data, err := ioutil.ReadFile(filepath.Join(mock.root, path))
data, err := os.ReadFile(filepath.Join(mock.root, path))
if err != nil {
return "", err
}
Expand Down Expand Up @@ -346,7 +345,7 @@ func mockNewNotInRdma(subsystems []Subsystem, path Path, resources *specs.LinuxR
}

func TestLoad(t *testing.T) {
mock, err := newMock()
mock, err := newMock(t)
if err != nil {
t.Fatal(err)
}
Expand All @@ -367,7 +366,7 @@ func TestLoad(t *testing.T) {
}

func TestLoadWithMissingSubsystems(t *testing.T) {
mock, err := newMock()
mock, err := newMock(t)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -401,7 +400,7 @@ func TestLoadWithMissingSubsystems(t *testing.T) {
}

func TestDelete(t *testing.T) {
mock, err := newMock()
mock, err := newMock(t)
if err != nil {
t.Fatal(err)
}
Expand All @@ -417,7 +416,7 @@ func TestDelete(t *testing.T) {
}

func TestCreateSubCgroup(t *testing.T) {
mock, err := newMock()
mock, err := newMock(t)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -455,7 +454,7 @@ func TestCreateSubCgroup(t *testing.T) {
}

func TestFreezeThaw(t *testing.T) {
mock, err := newMock()
mock, err := newMock(t)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -484,7 +483,7 @@ func TestFreezeThaw(t *testing.T) {
}

func TestSubsystems(t *testing.T) {
mock, err := newMock()
mock, err := newMock(t)
if err != nil {
t.Fatal(err)
}
Expand All @@ -507,7 +506,7 @@ func TestSubsystems(t *testing.T) {

func TestCpusetParent(t *testing.T) {
const expected = "0-3"
mock, err := newMock()
mock, err := newMock(t)
if err != nil {
t.Fatal(err)
}
Expand Down
3 changes: 1 addition & 2 deletions cpuacct.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package cgroups
import (
"bufio"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -72,7 +71,7 @@ func (c *cpuacctController) Stat(path string, stats *v1.Metrics) error {

func (c *cpuacctController) percpuUsage(path string) ([]uint64, error) {
var usage []uint64
data, err := ioutil.ReadFile(filepath.Join(c.Path(path), "cpuacct.usage_percpu"))
data, err := os.ReadFile(filepath.Join(c.Path(path), "cpuacct.usage_percpu"))
if err != nil {
return nil, err
}
Expand Down
5 changes: 2 additions & 3 deletions cpuacct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package cgroups

import (
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand All @@ -29,7 +28,7 @@ sched_delay 3
`

func TestGetUsage(t *testing.T) {
mock, err := newMock()
mock, err := newMock(t)
if err != nil {
t.Fatal(err)
}
Expand All @@ -43,7 +42,7 @@ func TestGetUsage(t *testing.T) {
t.Fatal(err)
}
current := filepath.Join(mock.root, string(Cpuacct), "test", "cpuacct.stat")
if err = ioutil.WriteFile(
if err = os.WriteFile(
current,
[]byte(cpuacctStatData),
defaultFilePerm,
Expand Down
5 changes: 2 additions & 3 deletions cpuset.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package cgroups
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"

Expand Down Expand Up @@ -87,10 +86,10 @@ func (c *cpusetController) Update(path string, resources *specs.LinuxResources)
}

func (c *cpusetController) getValues(path string) (cpus []byte, mems []byte, err error) {
if cpus, err = ioutil.ReadFile(filepath.Join(path, "cpuset.cpus")); err != nil && !os.IsNotExist(err) {
if cpus, err = os.ReadFile(filepath.Join(path, "cpuset.cpus")); err != nil && !os.IsNotExist(err) {
return
}
if mems, err = ioutil.ReadFile(filepath.Join(path, "cpuset.mems")); err != nil && !os.IsNotExist(err) {
if mems, err = os.ReadFile(filepath.Join(path, "cpuset.mems")); err != nil && !os.IsNotExist(err) {
return
}
return cpus, mems, nil
Expand Down
4 changes: 2 additions & 2 deletions freezer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package cgroups

import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
Expand Down Expand Up @@ -58,7 +58,7 @@ func (f *freezerController) changeState(path string, state State) error {
}

func (f *freezerController) state(path string) (State, error) {
current, err := ioutil.ReadFile(filepath.Join(f.root, path, "freezer.state"))
current, err := os.ReadFile(filepath.Join(f.root, path, "freezer.state"))
if err != nil {
return "", err
}
Expand Down
12 changes: 4 additions & 8 deletions memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package cgroups

import (
"fmt"
"io/ioutil"
"os"
"path"
"strings"
Expand Down Expand Up @@ -259,18 +258,15 @@ func checkMemoryStatHasNoSwap(t *testing.T, mem *v1.MemoryStat) {

// buildMemoryMetrics creates fake cgroups memory entries in a temporary dir. Returns the fake cgroups root
func buildMemoryMetrics(t *testing.T, modules []string, metrics []string) string {
tmpRoot, err := ioutil.TempDir("", "memtests")
if err != nil {
t.Fatal(err)
}
tmpRoot := t.TempDir()
tmpDir := path.Join(tmpRoot, string(Memory))
if err := os.MkdirAll(tmpDir, defaultDirPerm); err != nil {
t.Fatal(err)
}
if err := ioutil.WriteFile(path.Join(tmpDir, "memory.stat"), []byte(memoryData), defaultFilePerm); err != nil {
if err := os.WriteFile(path.Join(tmpDir, "memory.stat"), []byte(memoryData), defaultFilePerm); err != nil {
t.Fatal(err)
}
if err := ioutil.WriteFile(path.Join(tmpDir, "memory.oom_control"), []byte(memoryOomControlData), defaultFilePerm); err != nil {
if err := os.WriteFile(path.Join(tmpDir, "memory.oom_control"), []byte(memoryOomControlData), defaultFilePerm); err != nil {
t.Fatal(err)
}
cnt := 0
Expand All @@ -282,7 +278,7 @@ func buildMemoryMetrics(t *testing.T, modules []string, metrics []string) string
} else {
fileName = path.Join(tmpDir, strings.Join([]string{"memory", mod, metric}, "."))
}
if err := ioutil.WriteFile(fileName, []byte(fmt.Sprintln(cnt)), defaultFilePerm); err != nil {
if err := os.WriteFile(fileName, []byte(fmt.Sprintln(cnt)), defaultFilePerm); err != nil {
t.Fatal(err)
}
cnt++
Expand Down
11 changes: 4 additions & 7 deletions mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,17 @@
package cgroups

import (
"io/ioutil"
"os"
"path/filepath"
"testing"
)

func init() {
defaultFilePerm = 0666
}

func newMock() (*mockCgroup, error) {
root, err := ioutil.TempDir("", "cgroups")
if err != nil {
return nil, err
}
func newMock(tb testing.TB) (*mockCgroup, error) {
root := tb.TempDir()
subsystems, err := defaults(root)
if err != nil {
return nil, err
Expand All @@ -54,7 +51,7 @@ func newMock() (*mockCgroup, error) {
value: []byte("0-3"),
},
} {
if err := ioutil.WriteFile(filepath.Join(root, "cpuset", v.name), v.value, defaultFilePerm); err != nil {
if err := os.WriteFile(filepath.Join(root, "cpuset", v.name), v.value, defaultFilePerm); err != nil {
return nil, err
}
}
Expand Down
3 changes: 1 addition & 2 deletions pids.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package cgroups

import (
"io/ioutil"
"os"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -69,7 +68,7 @@ func (p *pidsController) Stat(path string, stats *v1.Metrics) error {
return err
}
var max uint64
maxData, err := ioutil.ReadFile(filepath.Join(p.Path(path), "pids.max"))
maxData, err := os.ReadFile(filepath.Join(p.Path(path), "pids.max"))
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 98dc9d9

Please sign in to comment.