opts: Reject duplicate shorthand GPU counts

`--gpus count=1,2` and `--gpus 1,2` silently replace the first count, while `--gpus 1,count=2` correctly reports a duplicate.

Normalize shorthand counts to the `count` key before checking duplicates. Tests cover both orders, multiple shorthand values, and `all`; rejected options do not append a device request.

Signed-off-by: Alexandre Rodrigues <alexandre3ylf@gmail.com>
This commit is contained in:
Alexandre Rodrigues
2026-09-17 13:19:10 -03:00
parent 4a63305d74
commit a999520f67
2 changed files with 13 additions and 11 deletions
+3 -11
View File
@@ -33,8 +33,6 @@ func parseCount(s string) (int, error) {
}
// Set a new mount value
//
//nolint:gocyclo
func (o *GpuOpts) Set(value string) error {
csvReader := csv.NewReader(strings.NewReader(value))
fields, err := csvReader.Read()
@@ -48,20 +46,14 @@ func (o *GpuOpts) Set(value string) error {
// Set writable as the default
for _, field := range fields {
key, val, withValue := strings.Cut(field, "=")
if !withValue {
key, val = "count", key
}
if _, ok := seen[key]; ok {
return fmt.Errorf("gpu request key '%s' can be specified only once", key)
}
seen[key] = struct{}{}
if !withValue {
seen["count"] = struct{}{}
req.Count, err = parseCount(key)
if err != nil {
return err
}
continue
}
switch key {
case "driver":
req.Driver = val
+10
View File
@@ -52,3 +52,13 @@ func TestGpusOpts(t *testing.T) {
}))
}
}
func TestGpusOptDuplicateCount(t *testing.T) {
for _, value := range []string{"count=1,2", "1,2", "all,1", "count=1,all", "1,count=2", "count=1,count=2"} {
t.Run(value, func(t *testing.T) {
var gpus GpuOpts
assert.Error(t, gpus.Set(value), "gpu request key 'count' can be specified only once")
assert.Assert(t, is.Len(gpus.Value(), 0))
})
}
}