Skip to content

Commit 724eb82

Browse files
authored
Merge pull request #246 from kzys/no-ioutil
Don't use ioutil
2 parents e3fea41 + 98dc9d9 commit 724eb82

15 files changed

+59
-75
lines changed

cgroup_test.go

+16-17
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package cgroups
1818

1919
import (
2020
"fmt"
21-
"io/ioutil"
2221
"os"
2322
"path/filepath"
2423
"strconv"
@@ -30,7 +29,7 @@ import (
3029
// using t.Error in test were defers do cleanup on the filesystem
3130

3231
func TestCreate(t *testing.T) {
33-
mock, err := newMock()
32+
mock, err := newMock(t)
3433
if err != nil {
3534
t.Fatal(err)
3635
}
@@ -57,7 +56,7 @@ func TestCreate(t *testing.T) {
5756
}
5857

5958
func TestStat(t *testing.T) {
60-
mock, err := newMock()
59+
mock, err := newMock(t)
6160
if err != nil {
6261
t.Fatal(err)
6362
}
@@ -79,7 +78,7 @@ func TestStat(t *testing.T) {
7978
}
8079

8180
func TestAdd(t *testing.T) {
82-
mock, err := newMock()
81+
mock, err := newMock(t)
8382
if err != nil {
8483
t.Fatal(err)
8584
}
@@ -102,7 +101,7 @@ func TestAdd(t *testing.T) {
102101
}
103102

104103
func TestAddFilteredSubsystems(t *testing.T) {
105-
mock, err := newMock()
104+
mock, err := newMock(t)
106105
if err != nil {
107106
t.Fatal(err)
108107
}
@@ -159,7 +158,7 @@ func TestAddFilteredSubsystems(t *testing.T) {
159158
}
160159

161160
func TestAddTask(t *testing.T) {
162-
mock, err := newMock()
161+
mock, err := newMock(t)
163162
if err != nil {
164163
t.Fatal(err)
165164
}
@@ -182,7 +181,7 @@ func TestAddTask(t *testing.T) {
182181
}
183182

184183
func TestAddTaskFilteredSubsystems(t *testing.T) {
185-
mock, err := newMock()
184+
mock, err := newMock(t)
186185
if err != nil {
187186
t.Fatal(err)
188187
}
@@ -224,7 +223,7 @@ func TestAddTaskFilteredSubsystems(t *testing.T) {
224223
}
225224

226225
func TestListPids(t *testing.T) {
227-
mock, err := newMock()
226+
mock, err := newMock(t)
228227
if err != nil {
229228
t.Fatal(err)
230229
}
@@ -259,7 +258,7 @@ func TestListPids(t *testing.T) {
259258
}
260259

261260
func TestListTasksPids(t *testing.T) {
262-
mock, err := newMock()
261+
mock, err := newMock(t)
263262
if err != nil {
264263
t.Fatal(err)
265264
}
@@ -294,7 +293,7 @@ func TestListTasksPids(t *testing.T) {
294293
}
295294

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

348347
func TestLoad(t *testing.T) {
349-
mock, err := newMock()
348+
mock, err := newMock(t)
350349
if err != nil {
351350
t.Fatal(err)
352351
}
@@ -367,7 +366,7 @@ func TestLoad(t *testing.T) {
367366
}
368367

369368
func TestLoadWithMissingSubsystems(t *testing.T) {
370-
mock, err := newMock()
369+
mock, err := newMock(t)
371370
if err != nil {
372371
t.Fatal(err)
373372
}
@@ -401,7 +400,7 @@ func TestLoadWithMissingSubsystems(t *testing.T) {
401400
}
402401

403402
func TestDelete(t *testing.T) {
404-
mock, err := newMock()
403+
mock, err := newMock(t)
405404
if err != nil {
406405
t.Fatal(err)
407406
}
@@ -417,7 +416,7 @@ func TestDelete(t *testing.T) {
417416
}
418417

419418
func TestCreateSubCgroup(t *testing.T) {
420-
mock, err := newMock()
419+
mock, err := newMock(t)
421420
if err != nil {
422421
t.Fatal(err)
423422
}
@@ -455,7 +454,7 @@ func TestCreateSubCgroup(t *testing.T) {
455454
}
456455

457456
func TestFreezeThaw(t *testing.T) {
458-
mock, err := newMock()
457+
mock, err := newMock(t)
459458
if err != nil {
460459
t.Fatal(err)
461460
}
@@ -484,7 +483,7 @@ func TestFreezeThaw(t *testing.T) {
484483
}
485484

486485
func TestSubsystems(t *testing.T) {
487-
mock, err := newMock()
486+
mock, err := newMock(t)
488487
if err != nil {
489488
t.Fatal(err)
490489
}
@@ -507,7 +506,7 @@ func TestSubsystems(t *testing.T) {
507506

508507
func TestCpusetParent(t *testing.T) {
509508
const expected = "0-3"
510-
mock, err := newMock()
509+
mock, err := newMock(t)
511510
if err != nil {
512511
t.Fatal(err)
513512
}

cpuacct.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package cgroups
1919
import (
2020
"bufio"
2121
"fmt"
22-
"io/ioutil"
2322
"os"
2423
"path/filepath"
2524
"strconv"
@@ -72,7 +71,7 @@ func (c *cpuacctController) Stat(path string, stats *v1.Metrics) error {
7271

7372
func (c *cpuacctController) percpuUsage(path string) ([]uint64, error) {
7473
var usage []uint64
75-
data, err := ioutil.ReadFile(filepath.Join(c.Path(path), "cpuacct.usage_percpu"))
74+
data, err := os.ReadFile(filepath.Join(c.Path(path), "cpuacct.usage_percpu"))
7675
if err != nil {
7776
return nil, err
7877
}

cpuacct_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package cgroups
1818

1919
import (
20-
"io/ioutil"
2120
"os"
2221
"path/filepath"
2322
"testing"
@@ -29,7 +28,7 @@ sched_delay 3
2928
`
3029

3130
func TestGetUsage(t *testing.T) {
32-
mock, err := newMock()
31+
mock, err := newMock(t)
3332
if err != nil {
3433
t.Fatal(err)
3534
}
@@ -43,7 +42,7 @@ func TestGetUsage(t *testing.T) {
4342
t.Fatal(err)
4443
}
4544
current := filepath.Join(mock.root, string(Cpuacct), "test", "cpuacct.stat")
46-
if err = ioutil.WriteFile(
45+
if err = os.WriteFile(
4746
current,
4847
[]byte(cpuacctStatData),
4948
defaultFilePerm,

cpuset.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package cgroups
1919
import (
2020
"bytes"
2121
"fmt"
22-
"io/ioutil"
2322
"os"
2423
"path/filepath"
2524

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

8988
func (c *cpusetController) getValues(path string) (cpus []byte, mems []byte, err error) {
90-
if cpus, err = ioutil.ReadFile(filepath.Join(path, "cpuset.cpus")); err != nil && !os.IsNotExist(err) {
89+
if cpus, err = os.ReadFile(filepath.Join(path, "cpuset.cpus")); err != nil && !os.IsNotExist(err) {
9190
return
9291
}
93-
if mems, err = ioutil.ReadFile(filepath.Join(path, "cpuset.mems")); err != nil && !os.IsNotExist(err) {
92+
if mems, err = os.ReadFile(filepath.Join(path, "cpuset.mems")); err != nil && !os.IsNotExist(err) {
9493
return
9594
}
9695
return cpus, mems, nil

freezer.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
package cgroups
1818

1919
import (
20-
"io/ioutil"
20+
"os"
2121
"path/filepath"
2222
"strings"
2323
"time"
@@ -58,7 +58,7 @@ func (f *freezerController) changeState(path string, state State) error {
5858
}
5959

6060
func (f *freezerController) state(path string) (State, error) {
61-
current, err := ioutil.ReadFile(filepath.Join(f.root, path, "freezer.state"))
61+
current, err := os.ReadFile(filepath.Join(f.root, path, "freezer.state"))
6262
if err != nil {
6363
return "", err
6464
}

memory_test.go

+4-8
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package cgroups
1818

1919
import (
2020
"fmt"
21-
"io/ioutil"
2221
"os"
2322
"path"
2423
"strings"
@@ -259,18 +258,15 @@ func checkMemoryStatHasNoSwap(t *testing.T, mem *v1.MemoryStat) {
259258

260259
// buildMemoryMetrics creates fake cgroups memory entries in a temporary dir. Returns the fake cgroups root
261260
func buildMemoryMetrics(t *testing.T, modules []string, metrics []string) string {
262-
tmpRoot, err := ioutil.TempDir("", "memtests")
263-
if err != nil {
264-
t.Fatal(err)
265-
}
261+
tmpRoot := t.TempDir()
266262
tmpDir := path.Join(tmpRoot, string(Memory))
267263
if err := os.MkdirAll(tmpDir, defaultDirPerm); err != nil {
268264
t.Fatal(err)
269265
}
270-
if err := ioutil.WriteFile(path.Join(tmpDir, "memory.stat"), []byte(memoryData), defaultFilePerm); err != nil {
266+
if err := os.WriteFile(path.Join(tmpDir, "memory.stat"), []byte(memoryData), defaultFilePerm); err != nil {
271267
t.Fatal(err)
272268
}
273-
if err := ioutil.WriteFile(path.Join(tmpDir, "memory.oom_control"), []byte(memoryOomControlData), defaultFilePerm); err != nil {
269+
if err := os.WriteFile(path.Join(tmpDir, "memory.oom_control"), []byte(memoryOomControlData), defaultFilePerm); err != nil {
274270
t.Fatal(err)
275271
}
276272
cnt := 0
@@ -282,7 +278,7 @@ func buildMemoryMetrics(t *testing.T, modules []string, metrics []string) string
282278
} else {
283279
fileName = path.Join(tmpDir, strings.Join([]string{"memory", mod, metric}, "."))
284280
}
285-
if err := ioutil.WriteFile(fileName, []byte(fmt.Sprintln(cnt)), defaultFilePerm); err != nil {
281+
if err := os.WriteFile(fileName, []byte(fmt.Sprintln(cnt)), defaultFilePerm); err != nil {
286282
t.Fatal(err)
287283
}
288284
cnt++

mock_test.go

+4-7
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,17 @@
1717
package cgroups
1818

1919
import (
20-
"io/ioutil"
2120
"os"
2221
"path/filepath"
22+
"testing"
2323
)
2424

2525
func init() {
2626
defaultFilePerm = 0666
2727
}
2828

29-
func newMock() (*mockCgroup, error) {
30-
root, err := ioutil.TempDir("", "cgroups")
31-
if err != nil {
32-
return nil, err
33-
}
29+
func newMock(tb testing.TB) (*mockCgroup, error) {
30+
root := tb.TempDir()
3431
subsystems, err := defaults(root)
3532
if err != nil {
3633
return nil, err
@@ -54,7 +51,7 @@ func newMock() (*mockCgroup, error) {
5451
value: []byte("0-3"),
5552
},
5653
} {
57-
if err := ioutil.WriteFile(filepath.Join(root, "cpuset", v.name), v.value, defaultFilePerm); err != nil {
54+
if err := os.WriteFile(filepath.Join(root, "cpuset", v.name), v.value, defaultFilePerm); err != nil {
5855
return nil, err
5956
}
6057
}

pids.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package cgroups
1818

1919
import (
20-
"io/ioutil"
2120
"os"
2221
"path/filepath"
2322
"strconv"
@@ -69,7 +68,7 @@ func (p *pidsController) Stat(path string, stats *v1.Metrics) error {
6968
return err
7069
}
7170
var max uint64
72-
maxData, err := ioutil.ReadFile(filepath.Join(p.Path(path), "pids.max"))
71+
maxData, err := os.ReadFile(filepath.Join(p.Path(path), "pids.max"))
7372
if err != nil {
7473
return err
7574
}

0 commit comments

Comments
 (0)