|
| 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 | +} |
0 commit comments