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