Skip to content

Commit ecd8478

Browse files
committed
Parse service device count to int if possible
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>
1 parent 95ac1be commit ecd8478

2 files changed

Lines changed: 43 additions & 4 deletions

File tree

loader/loader.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1038,7 +1038,12 @@ var transformServiceDeviceRequest TransformerFunc = func(data interface{}) (inte
10381038
value["count"] = -1
10391039
return value, nil
10401040
}
1041-
return data, errors.Errorf("invalid string value for 'count' (the only value allowed is 'all')")
1041+
i, err := strconv.ParseInt(val, 10, 64)
1042+
if err == nil {
1043+
value["count"] = i
1044+
return value, nil
1045+
}
1046+
return data, errors.Errorf("invalid string value for 'count' (the only value allowed is 'all' or a number)")
10421047
default:
10431048
return data, errors.Errorf("invalid type %T for device count", val)
10441049
}

loader/loader_test.go

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2044,7 +2044,24 @@ func TestLoadWithExtendsWithContextUrl(t *testing.T) {
20442044
assert.Check(t, is.DeepEqual(expServices, actual.Services))
20452045
}
20462046

2047-
func TestServiceDeviceRequestCount(t *testing.T) {
2047+
func TestServiceDeviceRequestCountIntegerType(t *testing.T) {
2048+
_, err := loadYAML(`
2049+
name: service-device-request-count
2050+
services:
2051+
hello-world:
2052+
image: redis:alpine
2053+
deploy:
2054+
resources:
2055+
reservations:
2056+
devices:
2057+
- driver: nvidia
2058+
capabilities: [gpu]
2059+
count: 1
2060+
`)
2061+
assert.NilError(t, err)
2062+
}
2063+
2064+
func TestServiceDeviceRequestCountStringType(t *testing.T) {
20482065
_, err := loadYAML(`
20492066
name: service-device-request-count
20502067
services:
@@ -2061,7 +2078,24 @@ services:
20612078
assert.NilError(t, err)
20622079
}
20632080

2064-
func TestServiceDeviceRequestCountType(t *testing.T) {
2081+
func TestServiceDeviceRequestCountIntegerAsStringType(t *testing.T) {
2082+
_, err := loadYAML(`
2083+
name: service-device-request-count-type
2084+
services:
2085+
hello-world:
2086+
image: redis:alpine
2087+
deploy:
2088+
resources:
2089+
reservations:
2090+
devices:
2091+
- driver: nvidia
2092+
capabilities: [gpu]
2093+
count: "1"
2094+
`)
2095+
assert.NilError(t, err)
2096+
}
2097+
2098+
func TestServiceDeviceRequestCountInvalidStringType(t *testing.T) {
20652099
_, err := loadYAML(`
20662100
name: service-device-request-count-type
20672101
services:
@@ -2075,7 +2109,7 @@ services:
20752109
capabilities: [gpu]
20762110
count: somestring
20772111
`)
2078-
assert.ErrorContains(t, err, "invalid string value for 'count' (the only value allowed is 'all')")
2112+
assert.ErrorContains(t, err, "invalid string value for 'count' (the only value allowed is 'all' or a number)")
20792113
}
20802114

20812115
func TestServicePullPolicy(t *testing.T) {

0 commit comments

Comments
 (0)