Skip to content

Commit

Permalink
Merge pull request #442 from XiangRongLin/device-count-string-int
Browse files Browse the repository at this point in the history
  • Loading branch information
ndeloof committed Aug 4, 2023
2 parents 95ac1be + ecd8478 commit 08d8d55
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 08d8d55

Please sign in to comment.