Skip to content

Commit 479d74f

Browse files
authored
Merge pull request #13 from kolyshkin/modernize
Modernize code
2 parents 2a1597e + a473597 commit 479d74f

File tree

11 files changed

+18
-32
lines changed

11 files changed

+18
-32
lines changed

devices/systemd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ func systemdProperties(r *cgroups.Resources, sdVer int) ([]systemdDbus.Property,
166166
return properties, nil
167167
}
168168

169-
func newProp(name string, units interface{}) systemdDbus.Property {
169+
func newProp(name string, units any) systemdDbus.Property {
170170
return systemdDbus.Property{
171171
Name: name,
172172
Value: dbus.MakeVariant(units),

devices/systemd_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func TestPodSkipDevicesUpdate(t *testing.T) {
9595
}
9696

9797
// Now update the pod a few times.
98-
for i := 0; i < 42; i++ {
98+
for range 42 {
9999
podConfig.Resources.PidsLimit++
100100
podConfig.Resources.Memory += 1024 * 1024
101101
if err := pm.Set(podConfig.Resources); err != nil {

file_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func TestWriteCgroupFileHandlesInterrupt(t *testing.T) {
3232
t.Skip(err)
3333
}
3434

35-
for i := 0; i < 100000; i++ {
35+
for i := range 100000 {
3636
limit := 1024*1024 + i
3737
if err := WriteFile(cgroupPath, memoryLimit, strconv.Itoa(limit)); err != nil {
3838
t.Fatalf("Failed to write %d on attempt %d: %+v", limit, i, err)

fs/blkio_test.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package fs
22

33
import (
4+
"maps"
45
"strconv"
56
"testing"
67

@@ -465,10 +466,7 @@ func TestBlkioStatsNoFilesBFQDebug(t *testing.T) {
465466
for _, testCase := range testCases {
466467
path := tempDir(t, "cpuset")
467468

468-
tempBlkioTestFiles := map[string]string{}
469-
for i, v := range blkioBFQDebugStatsTestFiles {
470-
tempBlkioTestFiles[i] = v
471-
}
469+
tempBlkioTestFiles := maps.Clone(blkioBFQDebugStatsTestFiles)
472470
delete(tempBlkioTestFiles, testCase.filename)
473471

474472
writeFileContents(t, path, tempBlkioTestFiles)
@@ -579,10 +577,7 @@ func TestBlkioStatsNoFilesCFQ(t *testing.T) {
579577
for _, testCase := range testCases {
580578
path := tempDir(t, "cpuset")
581579

582-
tempBlkioTestFiles := map[string]string{}
583-
for i, v := range blkioCFQStatsTestFiles {
584-
tempBlkioTestFiles[i] = v
585-
}
580+
tempBlkioTestFiles := maps.Clone(blkioCFQStatsTestFiles)
586581
delete(tempBlkioTestFiles, testCase.filename)
587582

588583
writeFileContents(t, path, tempBlkioTestFiles)

fs/cpuset_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package fs
22

33
import (
4+
"maps"
45
"reflect"
56
"testing"
67

@@ -211,10 +212,7 @@ func TestCPUSetStatsMissingFiles(t *testing.T) {
211212
t.Run(testCase.desc, func(t *testing.T) {
212213
path := tempDir(t, "cpuset")
213214

214-
tempCpusetTestFiles := map[string]string{}
215-
for i, v := range cpusetTestFiles {
216-
tempCpusetTestFiles[i] = v
217-
}
215+
tempCpusetTestFiles := maps.Clone(cpusetTestFiles)
218216

219217
if testCase.removeFile {
220218
delete(tempCpusetTestFiles, testCase.filename)

fs/freezer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func (s *FreezerGroup) Set(path string, r *cgroups.Resources) (Err error) {
5757
// Alas, this is still a game of chances, since the real fix
5858
// belong to the kernel (cgroup v2 do not have this bug).
5959

60-
for i := 0; i < 1000; i++ {
60+
for i := range 1000 {
6161
if i%50 == 49 {
6262
// Occasional thaw and sleep improves
6363
// the chances to succeed in freezing

systemd/common.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func ExpandSlice(slice string) (string, error) {
8989
return path, nil
9090
}
9191

92-
func newProp(name string, units interface{}) systemdDbus.Property {
92+
func newProp(name string, units any) systemdDbus.Property {
9393
return systemdDbus.Property{
9494
Name: name,
9595
Value: dbus.MakeVariant(units),
@@ -266,7 +266,7 @@ func systemdVersionAtoi(str string) (int, error) {
266266
// Unconditionally remove the leading prefix ("v).
267267
str = strings.TrimLeft(str, `"v`)
268268
// Match on the first integer we can grab.
269-
for i := 0; i < len(str); i++ {
269+
for i := range len(str) {
270270
if str[i] < '0' || str[i] > '9' {
271271
// First non-digit: cut the tail.
272272
str = str[:i]

systemd/systemd_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func TestUnitExistsIgnored(t *testing.T) {
9090
pm := newManager(t, podConfig)
9191

9292
// create twice to make sure "UnitExists" error is ignored.
93-
for i := 0; i < 2; i++ {
93+
for range 2 {
9494
if err := pm.Apply(-1); err != nil {
9595
t.Fatal(err)
9696
}

utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ func WriteCgroupProc(dir string, pid int) error {
395395
}
396396
defer file.Close()
397397

398-
for i := 0; i < 5; i++ {
398+
for range 5 {
399399
_, err = file.WriteString(strconv.Itoa(pid))
400400
if err == nil {
401401
return nil

utils_test.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"errors"
66
"path/filepath"
77
"reflect"
8+
"slices"
89
"strings"
910
"testing"
1011

@@ -273,14 +274,7 @@ func TestGetCgroupMounts(t *testing.T) {
273274
if !strings.HasPrefix(m.Mountpoint, "/sys/fs/cgroup/") && !strings.Contains(m.Mountpoint, ss) {
274275
t.Fatalf("unexpected mountpoint for %s: %s", ss, m.Mountpoint)
275276
}
276-
var ssFound bool
277-
for _, mss := range m.Subsystems {
278-
if mss == ss {
279-
ssFound = true
280-
break
281-
}
282-
}
283-
if !ssFound {
277+
if !slices.Contains(m.Subsystems, ss) {
284278
t.Fatalf("subsystem %s not found in Subsystems field %v", ss, m.Subsystems)
285279
}
286280
}

0 commit comments

Comments
 (0)