Skip to content

Commit 8f3477f

Browse files
Add tests for weight/throttle device opts validators
opts/weightdevice.go and opts/throttledevice.go had no tests, unlike the sibling opts. Add table-driven coverage for ValidateWeightDevice, ValidateThrottleBpsDevice, ValidateThrottleIOpsDevice and the Set/GetList paths, including boundary (weight 0/10/1000/overflow) and error cases. Signed-off-by: Nikolaus Schuetz <nikolauspschuetz@gmail.com>
1 parent 7a54334 commit 8f3477f

2 files changed

Lines changed: 141 additions & 0 deletions

File tree

opts/throttledevice_test.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package opts
2+
3+
import (
4+
"testing"
5+
6+
"gotest.tools/v3/assert"
7+
is "gotest.tools/v3/assert/cmp"
8+
)
9+
10+
func TestValidateThrottleBpsDevice(t *testing.T) {
11+
tests := []struct {
12+
doc string
13+
input string
14+
expectedErr string
15+
expectedPath string
16+
expectedRate uint64
17+
}{
18+
{doc: "plain integer", input: "/dev/sda:1000", expectedPath: "/dev/sda", expectedRate: 1000},
19+
{doc: "with unit", input: "/dev/sda:1mb", expectedPath: "/dev/sda", expectedRate: 1048576},
20+
{doc: "zero", input: "/dev/sda:0", expectedPath: "/dev/sda", expectedRate: 0},
21+
{doc: "missing colon", input: "/dev/sda", expectedErr: "bad format: /dev/sda"},
22+
{doc: "empty device", input: ":1mb", expectedErr: "bad format: :1mb"},
23+
{doc: "missing /dev/ prefix", input: "sda:1mb", expectedErr: "bad format for device path: sda:1mb"},
24+
{doc: "non-numeric rate", input: "/dev/sda:foo", expectedErr: "invalid rate for device"},
25+
}
26+
for _, tc := range tests {
27+
t.Run(tc.doc, func(t *testing.T) {
28+
v, err := ValidateThrottleBpsDevice(tc.input)
29+
if tc.expectedErr != "" {
30+
assert.Check(t, is.ErrorContains(err, tc.expectedErr))
31+
assert.Check(t, is.Nil(v))
32+
return
33+
}
34+
assert.NilError(t, err)
35+
assert.Check(t, is.Equal(v.Path, tc.expectedPath))
36+
assert.Check(t, is.Equal(v.Rate, tc.expectedRate))
37+
})
38+
}
39+
}
40+
41+
func TestValidateThrottleIOpsDevice(t *testing.T) {
42+
tests := []struct {
43+
doc string
44+
input string
45+
expectedErr string
46+
expectedPath string
47+
expectedRate uint64
48+
}{
49+
{doc: "valid integer", input: "/dev/sda:100", expectedPath: "/dev/sda", expectedRate: 100},
50+
{doc: "fractional rejected", input: "/dev/sda:1.5", expectedErr: "invalid rate for device"},
51+
{doc: "negative rejected", input: "/dev/sda:-5", expectedErr: "invalid rate for device"},
52+
{doc: "unit suffix rejected (iops are integers)", input: "/dev/sda:1mb", expectedErr: "invalid rate for device"},
53+
{doc: "missing /dev/ prefix", input: "sda:100", expectedErr: "bad format for device path: sda:100"},
54+
}
55+
for _, tc := range tests {
56+
t.Run(tc.doc, func(t *testing.T) {
57+
v, err := ValidateThrottleIOpsDevice(tc.input)
58+
if tc.expectedErr != "" {
59+
assert.Check(t, is.ErrorContains(err, tc.expectedErr))
60+
assert.Check(t, is.Nil(v))
61+
return
62+
}
63+
assert.NilError(t, err)
64+
assert.Check(t, is.Equal(v.Path, tc.expectedPath))
65+
assert.Check(t, is.Equal(v.Rate, tc.expectedRate))
66+
})
67+
}
68+
}
69+
70+
func TestThrottledeviceOptSetGetList(t *testing.T) {
71+
opt := NewThrottledeviceOpt(ValidateThrottleBpsDevice)
72+
assert.NilError(t, opt.Set("/dev/sda:1mb"))
73+
assert.NilError(t, opt.Set("/dev/sdb:2mb"))
74+
75+
list := opt.GetList()
76+
assert.Assert(t, is.Len(list, 2))
77+
assert.Check(t, is.Equal(list[0].Path, "/dev/sda"))
78+
assert.Check(t, is.Equal(list[0].Rate, uint64(1048576)))
79+
assert.Check(t, is.Equal(list[1].Path, "/dev/sdb"))
80+
assert.Check(t, is.Equal(list[1].Rate, uint64(2097152)))
81+
82+
assert.Check(t, is.ErrorContains(opt.Set("/dev/sdc:bad"), "invalid rate for device"))
83+
assert.Check(t, is.Equal(opt.Type(), "list"))
84+
}

opts/weightdevice_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package opts
2+
3+
import (
4+
"testing"
5+
6+
"gotest.tools/v3/assert"
7+
is "gotest.tools/v3/assert/cmp"
8+
)
9+
10+
func TestValidateWeightDevice(t *testing.T) {
11+
tests := []struct {
12+
doc string
13+
input string
14+
expectedErr string
15+
expectedPath string
16+
expectedWeight uint16
17+
}{
18+
{doc: "valid minimum", input: "/dev/sda:10", expectedPath: "/dev/sda", expectedWeight: 10},
19+
{doc: "valid maximum", input: "/dev/sda:1000", expectedPath: "/dev/sda", expectedWeight: 1000},
20+
{doc: "zero is accepted (unset)", input: "/dev/sda:0", expectedPath: "/dev/sda", expectedWeight: 0},
21+
{doc: "below minimum", input: "/dev/sda:9", expectedErr: "invalid weight for device: /dev/sda:9"},
22+
{doc: "above maximum", input: "/dev/sda:1001", expectedErr: "invalid weight for device: /dev/sda:1001"},
23+
{doc: "overflows uint16", input: "/dev/sda:70000", expectedErr: "invalid weight for device: /dev/sda:70000"},
24+
{doc: "missing colon", input: "/dev/sda", expectedErr: "bad format: /dev/sda"},
25+
{doc: "empty device", input: ":100", expectedErr: "bad format: :100"},
26+
{doc: "missing /dev/ prefix", input: "sda:100", expectedErr: "bad format for device path: sda:100"},
27+
}
28+
for _, tc := range tests {
29+
t.Run(tc.doc, func(t *testing.T) {
30+
v, err := ValidateWeightDevice(tc.input)
31+
if tc.expectedErr != "" {
32+
assert.Check(t, is.Error(err, tc.expectedErr))
33+
assert.Check(t, is.Nil(v))
34+
return
35+
}
36+
assert.NilError(t, err)
37+
assert.Check(t, is.Equal(v.Path, tc.expectedPath))
38+
assert.Check(t, is.Equal(v.Weight, tc.expectedWeight))
39+
})
40+
}
41+
}
42+
43+
func TestWeightdeviceOptSetGetList(t *testing.T) {
44+
opt := NewWeightdeviceOpt(ValidateWeightDevice)
45+
assert.NilError(t, opt.Set("/dev/sda:100"))
46+
assert.NilError(t, opt.Set("/dev/sdb:200"))
47+
48+
list := opt.GetList()
49+
assert.Assert(t, is.Len(list, 2))
50+
assert.Check(t, is.Equal(list[0].Path, "/dev/sda"))
51+
assert.Check(t, is.Equal(list[0].Weight, uint16(100)))
52+
assert.Check(t, is.Equal(list[1].Path, "/dev/sdb"))
53+
assert.Check(t, is.Equal(list[1].Weight, uint16(200)))
54+
55+
assert.Check(t, is.Error(opt.Set("/dev/sdc:1"), "invalid weight for device: /dev/sdc:1"))
56+
assert.Check(t, is.Equal(opt.Type(), "list"))
57+
}

0 commit comments

Comments
 (0)