mirror of
https://github.com/docker/cli.git
synced 2026-11-12 15:53:52 -04:00
Merge pull request #7312 from a3ylf/fix/gpu-duplicate-count
opts: Reject duplicate shorthand GPU counts
This commit is contained in:
+3
-11
@@ -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
|
||||
|
||||
@@ -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))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user