From 043474bf042b34ac4db25d1a308541c3d747bd43 Mon Sep 17 00:00:00 2001 From: Evan Lezar Date: Mon, 10 Mar 2025 09:24:53 +0200 Subject: [PATCH] [no-relnote] Address stricter validation Signed-off-by: Evan Lezar --- pkg/nvcdi/spec/spec_test.go | 114 ++++++++++++++++++++++++------------ 1 file changed, 75 insertions(+), 39 deletions(-) diff --git a/pkg/nvcdi/spec/spec_test.go b/pkg/nvcdi/spec/spec_test.go index 15c84070..f5d50608 100644 --- a/pkg/nvcdi/spec/spec_test.go +++ b/pkg/nvcdi/spec/spec_test.go @@ -28,6 +28,18 @@ import ( ) func TestSpec(t *testing.T) { + minimalSpec := &specs.Spec{ + Kind: "nvidia.com/gpu", + Devices: []specs.Device{ + { + Name: "one", + ContainerEdits: specs.ContainerEdits{ + Env: []string{"DEVICE_FOO=bar"}, + }, + }, + }, + } + testCases := []struct { description string options []Option @@ -35,23 +47,17 @@ func TestSpec(t *testing.T) { transform transform.Transformer expectedSpec string }{ - { - description: "default options return empty spec", - expectedSpec: `--- -cdiVersion: 0.3.0 -containerEdits: {} -devices: null -kind: nvidia.com/gpu -`, - }, { description: "version is overridden", - options: []Option{WithVersion("0.5.0")}, + options: []Option{WithVersion("0.8.0"), WithRawSpec(minimalSpec)}, expectedSpec: `--- -cdiVersion: 0.5.0 -containerEdits: {} -devices: null +cdiVersion: 0.8.0 kind: nvidia.com/gpu +devices: +- name: one + containerEdits: + env: + - DEVICE_FOO=bar `, }, { @@ -60,18 +66,24 @@ kind: nvidia.com/gpu &specs.Spec{ Version: "0.5.0", Kind: "nvidia.com/gpu", - ContainerEdits: specs.ContainerEdits{ - Env: []string{"FOO=bar"}, + Devices: []specs.Device{ + { + Name: "one", + ContainerEdits: specs.ContainerEdits{ + Env: []string{"DEVICE_FOO=bar"}, + }, + }, }, }, )}, expectedSpec: `--- cdiVersion: 0.5.0 -containerEdits: - env: - - FOO=bar -devices: null kind: nvidia.com/gpu +devices: +- name: one + containerEdits: + env: + - DEVICE_FOO=bar `, }, { @@ -79,18 +91,24 @@ kind: nvidia.com/gpu options: []Option{WithRawSpec( &specs.Spec{ Kind: "nvidia.com/gpu", - ContainerEdits: specs.ContainerEdits{ - Env: []string{"FOO=bar"}, + Devices: []specs.Device{ + { + Name: "one", + ContainerEdits: specs.ContainerEdits{ + Env: []string{"DEVICE_FOO=bar"}, + }, + }, }, }, )}, expectedSpec: `--- cdiVersion: 0.3.0 -containerEdits: - env: - - FOO=bar -devices: null kind: nvidia.com/gpu +devices: +- name: one + containerEdits: + env: + - DEVICE_FOO=bar `, }, { @@ -98,8 +116,15 @@ kind: nvidia.com/gpu options: []Option{WithRawSpec( &specs.Spec{ Kind: "nvidia.com/gpu", + Devices: []specs.Device{ + { + Name: "one", + ContainerEdits: specs.ContainerEdits{ + Env: []string{"DEVICE_FOO=bar"}, + }, + }, + }, ContainerEdits: specs.ContainerEdits{ - Env: []string{"FOO=bar"}, DeviceNodes: []*specs.DeviceNode{ { HostPath: "/some/dev/dev0", @@ -111,14 +136,16 @@ kind: nvidia.com/gpu )}, expectedSpec: `--- cdiVersion: 0.5.0 +kind: nvidia.com/gpu +devices: +- name: one + containerEdits: + env: + - DEVICE_FOO=bar containerEdits: deviceNodes: - - hostPath: /some/dev/dev0 - path: /dev/dev0 - env: - - FOO=bar -devices: null -kind: nvidia.com/gpu + - path: /dev/dev0 + hostPath: /some/dev/dev0 `, }, { @@ -126,8 +153,15 @@ kind: nvidia.com/gpu options: []Option{WithRawSpec( &specs.Spec{ Kind: "nvidia.com/gpu", + Devices: []specs.Device{ + { + Name: "one", + ContainerEdits: specs.ContainerEdits{ + Env: []string{"DEVICE_FOO=bar"}, + }, + }, + }, ContainerEdits: specs.ContainerEdits{ - Env: []string{"FOO=bar"}, DeviceNodes: []*specs.DeviceNode{ { HostPath: "/some/dev/dev0", @@ -147,14 +181,16 @@ kind: nvidia.com/gpu ), expectedSpec: `--- cdiVersion: 0.5.0 +kind: nvidia.com/gpu +devices: +- name: one + containerEdits: + env: + - DEVICE_FOO=bar containerEdits: deviceNodes: - - hostPath: /dev/dev0 - path: /dev/dev0 - env: - - FOO=bar -devices: null -kind: nvidia.com/gpu + - path: /dev/dev0 + hostPath: /dev/dev0 `, }, }