Skip to content

Commit

Permalink
review processing of DeviceRequest due to Docker engine changes
Browse files Browse the repository at this point in the history
moby/moby#48483

Signed-off-by: Guillaume Lours <[email protected]>
  • Loading branch information
glours committed Oct 2, 2024
1 parent f8ea4c3 commit 173c5d6
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 1 deletion.
53 changes: 52 additions & 1 deletion loader/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2123,7 +2123,7 @@ services:
}

func TestServiceDeviceRequestCountStringType(t *testing.T) {
_, err := loadYAML(`
project, err := loadYAML(`
name: service-device-request-count
services:
hello-world:
Expand All @@ -2137,6 +2137,7 @@ services:
count: all
`)
assert.NilError(t, err)
assert.Equal(t, project.Services["hello-world"].Deploy.Resources.Reservations.Devices[0].Count, types.DeviceCount(-1), err)
}

func TestServiceDeviceRequestCountIntegerAsStringType(t *testing.T) {
Expand All @@ -2155,6 +2156,22 @@ services:
`)
assert.NilError(t, err)
}
func TestServiceDeviceRequestWithoutCountAndDeviceIdsType(t *testing.T) {
project, err := loadYAML(`
name: service-device-request-count-type
services:
hello-world:
image: redis:alpine
deploy:
resources:
reservations:
devices:
- driver: nvidia
capabilities: [gpu]
`)
assert.NilError(t, err)
assert.Equal(t, project.Services["hello-world"].Deploy.Resources.Reservations.Devices[0].Count, types.DeviceCount(-1), err)
}

func TestServiceDeviceRequestCountInvalidStringType(t *testing.T) {
_, err := loadYAML(`
Expand All @@ -2173,6 +2190,40 @@ services:
assert.ErrorContains(t, err, `invalid value "some_string", the only value allowed is 'all' or a number`)
}

func TestServiceDeviceRequestCountAndDeviceIdsExclusive(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: 2
device_ids: ["my-device-id"]
`)
assert.ErrorContains(t, err, `invalid "count" and "device_ids" are attributes are exclusive`)
}

func TestServiceDeviceRequestCapabilitiesMandatory(t *testing.T) {
_, err := loadYAML(`
name: service-device-request-count-type
services:
hello-world:
image: redis:alpine
deploy:
resources:
reservations:
devices:
- driver: nvidia
count: 2
`)
assert.ErrorContains(t, err, `"capabilities" attribute is mandatory for device request definition`)
}

func TestServicePullPolicy(t *testing.T) {
actual, err := loadYAML(`
name: service-pull-policy
Expand Down
45 changes: 45 additions & 0 deletions types/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,48 @@ func (c *DeviceCount) DecodeMapstructure(value interface{}) error {
}
return nil
}

func (d *DeviceRequest) DecodeMapstructure(value interface{}) error {
v, ok := value.(map[string]any)
if !ok {
return fmt.Errorf("invalid device request type %T", value)
}
if _, okCaps := v["capabilities"]; !okCaps {
return fmt.Errorf(`"capabilities" attribute is mandatory for device request definition`)
}
if _, okCount := v["count"]; okCount {
if _, okDeviceIds := v["device_ids"]; okDeviceIds {
return fmt.Errorf(`invalid "count" and "device_ids" are attributes are exclusive`)
}
}
d.Count = DeviceCount(-1)

capabilities := v["capabilities"]
caps := StringList{}
if err := caps.DecodeMapstructure(capabilities); err != nil {
return err
}
d.Capabilities = caps
if driver, ok := v["driver"]; ok {
if val, ok := driver.(string); ok {
d.Driver = val
} else {
return fmt.Errorf("invalid type for driver value: %T", driver)
}
}
if count, ok := v["count"]; ok {
if err := d.Count.DecodeMapstructure(count); err != nil {
return err
}
}
if deviceIDs, ok := v["device_ids"]; ok {
ids := StringList{}
if err := ids.DecodeMapstructure(deviceIDs); err != nil {
return err
}
d.IDs = ids
d.Count = DeviceCount(len(ids))
}
return nil

}

0 comments on commit 173c5d6

Please sign in to comment.