1717package modifier
1818
1919import (
20- "fmt"
2120 "testing"
2221
2322 "github.com/opencontainers/runtime-spec/specs-go"
@@ -27,122 +26,116 @@ import (
2726 "github.com/NVIDIA/nvidia-container-toolkit/internal/config/image"
2827)
2928
30- func TestGetAnnotationDevices (t * testing.T ) {
29+ func TestDeviceRequests (t * testing.T ) {
30+ logger , _ := testlog .NewNullLogger ()
31+
3132 testCases := []struct {
3233 description string
34+ input cdiDeviceRequestor
35+ spec * specs.Spec
3336 prefixes []string
34- annotations map [string ]string
3537 expectedDevices []string
36- expectedError error
3738 }{
3839 {
39- description : "no annotations" ,
40+ description : "empty spec yields no devices" ,
41+ },
42+ {
43+ description : "cdi devices from mounts" ,
44+ input : cdiDeviceRequestor {
45+ defaultKind : "nvidia.com/gpu" ,
46+ },
47+ spec : & specs.Spec {
48+ Mounts : []specs.Mount {
49+ {
50+ Destination : "/var/run/nvidia-container-devices/cdi/nvidia.com/gpu/0" ,
51+ Source : "/dev/null" ,
52+ },
53+ {
54+ Destination : "/var/run/nvidia-container-devices/cdi/nvidia.com/gpu/1" ,
55+ Source : "/dev/null" ,
56+ },
57+ },
58+ },
59+ expectedDevices : []string {"nvidia.com/gpu=0" , "nvidia.com/gpu=1" },
60+ },
61+ {
62+ description : "cdi devices from envvar" ,
63+ input : cdiDeviceRequestor {
64+ defaultKind : "nvidia.com/gpu" ,
65+ },
66+ spec : & specs.Spec {
67+ Process : & specs.Process {
68+ Env : []string {"NVIDIA_VISIBLE_DEVICES=0,example.com/class=device" },
69+ },
70+ },
71+ expectedDevices : []string {"nvidia.com/gpu=0" , "example.com/class=device" },
4072 },
4173 {
4274 description : "no matching annotations" ,
4375 prefixes : []string {"not-prefix/" },
44- annotations : map [string ]string {
45- "prefix/foo" : "example.com/device=bar" ,
76+ spec : & specs.Spec {
77+ Annotations : map [string ]string {
78+ "prefix/foo" : "example.com/device=bar" ,
79+ },
4680 },
4781 },
4882 {
4983 description : "single matching annotation" ,
5084 prefixes : []string {"prefix/" },
51- annotations : map [string ]string {
52- "prefix/foo" : "example.com/device=bar" ,
85+ spec : & specs.Spec {
86+ Annotations : map [string ]string {
87+ "prefix/foo" : "example.com/device=bar" ,
88+ },
5389 },
5490 expectedDevices : []string {"example.com/device=bar" },
5591 },
5692 {
5793 description : "multiple matching annotations" ,
5894 prefixes : []string {"prefix/" , "another-prefix/" },
59- annotations : map [string ]string {
60- "prefix/foo" : "example.com/device=bar" ,
61- "another-prefix/bar" : "example.com/device=baz" ,
95+ spec : & specs.Spec {
96+ Annotations : map [string ]string {
97+ "prefix/foo" : "example.com/device=bar" ,
98+ "another-prefix/bar" : "example.com/device=baz" ,
99+ },
62100 },
63101 expectedDevices : []string {"example.com/device=bar" , "example.com/device=baz" },
64102 },
65103 {
66104 description : "multiple matching annotations with duplicate devices" ,
67105 prefixes : []string {"prefix/" , "another-prefix/" },
68- annotations : map [string ]string {
69- "prefix/foo" : "example.com/device=bar" ,
70- "another-prefix/bar" : "example.com/device=bar" ,
106+ spec : & specs.Spec {
107+ Annotations : map [string ]string {
108+ "prefix/foo" : "example.com/device=bar" ,
109+ "another-prefix/bar" : "example.com/device=bar" ,
110+ },
71111 },
72112 expectedDevices : []string {"example.com/device=bar" , "example.com/device=bar" },
73113 },
74114 {
75- description : "invalid devices" ,
76- prefixes : []string {"prefix/" },
77- annotations : map [string ]string {
78- "prefix/foo" : "example.com/device" ,
79- },
80- expectedError : fmt .Errorf ("invalid device %q" , "example.com/device" ),
81- },
82- }
83-
84- for _ , tc := range testCases {
85- t .Run (tc .description , func (t * testing.T ) {
86- image , err := image .New (
87- image .WithAnnotations (tc .annotations ),
88- image .WithAnnotationsPrefixes (tc .prefixes ),
89- )
90- require .NoError (t , err )
91-
92- devices , err := getAnnotationDevices (image )
93- if tc .expectedError != nil {
94- require .Error (t , err )
95- return
96- }
97-
98- require .NoError (t , err )
99- require .ElementsMatch (t , tc .expectedDevices , devices )
100- })
101- }
102- }
103-
104- func TestDeviceRequests (t * testing.T ) {
105- logger , _ := testlog .NewNullLogger ()
106-
107- testCases := []struct {
108- description string
109- input cdiDeviceRequestor
110- spec * specs.Spec
111- expectedDevices []string
112- }{
113- {
114- description : "empty spec yields no devices" ,
115- },
116- {
117- description : "cdi devices from mounts" ,
115+ description : "devices in annotations are expanded" ,
118116 input : cdiDeviceRequestor {
119117 defaultKind : "nvidia.com/gpu" ,
120118 },
119+ prefixes : []string {"prefix/" },
121120 spec : & specs.Spec {
122- Mounts : []specs.Mount {
123- {
124- Destination : "/var/run/nvidia-container-devices/cdi/nvidia.com/gpu/0" ,
125- Source : "/dev/null" ,
126- },
127- {
128- Destination : "/var/run/nvidia-container-devices/cdi/nvidia.com/gpu/1" ,
129- Source : "/dev/null" ,
130- },
121+ Annotations : map [string ]string {
122+ "prefix/foo" : "device" ,
131123 },
132124 },
133- expectedDevices : []string {"nvidia.com/gpu=0" , "nvidia.com/gpu=1 " },
125+ expectedDevices : []string {"nvidia.com/gpu=device " },
134126 },
135127 {
136- description : "cdi devices from envvar " ,
128+ description : "invalid devices in annotations are treated as strings " ,
137129 input : cdiDeviceRequestor {
138130 defaultKind : "nvidia.com/gpu" ,
139131 },
132+ prefixes : []string {"prefix/" },
140133 spec : & specs.Spec {
141- Process : & specs. Process {
142- Env : [] string { "NVIDIA_VISIBLE_DEVICES=0, example.com/class= device"} ,
134+ Annotations : map [ string ] string {
135+ "prefix/foo" : " example.com/device" ,
143136 },
144137 },
145- expectedDevices : []string {"nvidia.com/gpu=0" , " example.com/class= device" },
138+ expectedDevices : []string {"nvidia.com/gpu=example.com/device" },
146139 },
147140 }
148141
@@ -153,6 +146,7 @@ func TestDeviceRequests(t *testing.T) {
153146 tc .spec ,
154147 image .WithAcceptDeviceListAsVolumeMounts (true ),
155148 image .WithAcceptEnvvarUnprivileged (true ),
149+ image .WithAnnotationsPrefixes (tc .prefixes ),
156150 )
157151 require .NoError (t , err )
158152 tc .input .image = image
0 commit comments