Skip to content

Commit

Permalink
Parse service device count to int if possible
Browse files Browse the repository at this point in the history
When set via an environment variable an integer will be represented by a string, resulting in an error.
fixes: docker/compose#10667

Signed-off-by: Xiang Rong Lin <41164160+XiangRongLin@users.noreply.github.com>
  • Loading branch information
XiangRongLin committed Aug 4, 2023
1 parent 95ac1be commit ecd8478
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
7 changes: 6 additions & 1 deletion loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -1038,7 +1038,12 @@ var transformServiceDeviceRequest TransformerFunc = func(data interface{}) (inte
value["count"] = -1
return value, nil
}
return data, errors.Errorf("invalid string value for 'count' (the only value allowed is 'all')")
i, err := strconv.ParseInt(val, 10, 64)
if err == nil {
value["count"] = i
return value, nil
}
return data, errors.Errorf("invalid string value for 'count' (the only value allowed is 'all' or a number)")
default:
return data, errors.Errorf("invalid type %T for device count", val)
}
Expand Down
40 changes: 37 additions & 3 deletions loader/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2044,7 +2044,24 @@ func TestLoadWithExtendsWithContextUrl(t *testing.T) {
assert.Check(t, is.DeepEqual(expServices, actual.Services))
}

func TestServiceDeviceRequestCount(t *testing.T) {
func TestServiceDeviceRequestCountIntegerType(t *testing.T) {
_, err := loadYAML(`
name: service-device-request-count
services:
hello-world:
image: redis:alpine
deploy:
resources:
reservations:
devices:
- driver: nvidia
capabilities: [gpu]
count: 1
`)
assert.NilError(t, err)
}

func TestServiceDeviceRequestCountStringType(t *testing.T) {
_, err := loadYAML(`
name: service-device-request-count
services:
Expand All @@ -2061,7 +2078,24 @@ services:
assert.NilError(t, err)
}

func TestServiceDeviceRequestCountType(t *testing.T) {
func TestServiceDeviceRequestCountIntegerAsStringType(t *testing.T) {
_, err := loadYAML(`
name: service-device-request-count-type
services:
hello-world:
image: redis:alpine
deploy:
resources:
reservations:
devices:
- driver: nvidia
capabilities: [gpu]
count: "1"
`)
assert.NilError(t, err)
}

func TestServiceDeviceRequestCountInvalidStringType(t *testing.T) {
_, err := loadYAML(`
name: service-device-request-count-type
services:
Expand All @@ -2075,7 +2109,7 @@ services:
capabilities: [gpu]
count: somestring
`)
assert.ErrorContains(t, err, "invalid string value for 'count' (the only value allowed is 'all')")
assert.ErrorContains(t, err, "invalid string value for 'count' (the only value allowed is 'all' or a number)")
}

func TestServicePullPolicy(t *testing.T) {
Expand Down

0 comments on commit ecd8478

Please sign in to comment.