@@ -170,3 +170,86 @@ func TestDeviceRequests(t *testing.T) {
170170 })
171171 }
172172}
173+
174+ func Test_cdiModeIdentfiersFromDevices (t * testing.T ) {
175+ testCases := []struct {
176+ description string
177+ devices []string
178+ expected * cdiModeIdentifiers
179+ }{
180+ {
181+ description : "empty device list" ,
182+ devices : []string {},
183+ expected : & cdiModeIdentifiers {
184+ modes : []string {"auto" },
185+ idsByMode : map [string ][]string {},
186+ deviceClassByMode : map [string ]string {"auto" : "gpu" },
187+ },
188+ },
189+ {
190+ description : "single automatic device" ,
191+ devices : []string {"0" },
192+ expected : & cdiModeIdentifiers {
193+ modes : []string {"auto" },
194+ idsByMode : map [string ][]string {"auto" : {"0" }},
195+ deviceClassByMode : map [string ]string {"auto" : "gpu" },
196+ },
197+ },
198+ {
199+ description : "multiple automatic devices" ,
200+ devices : []string {"0" , "1" },
201+ expected : & cdiModeIdentifiers {
202+ modes : []string {"auto" },
203+ idsByMode : map [string ][]string {"auto" : {"0" , "1" }},
204+ deviceClassByMode : map [string ]string {"auto" : "gpu" },
205+ },
206+ },
207+ {
208+ description : "device with explicit mode" ,
209+ devices : []string {"mode=gds,id=foo" },
210+ expected : & cdiModeIdentifiers {
211+ modes : []string {"auto" , "gds" },
212+ idsByMode : map [string ][]string {"gds" : {"foo" }},
213+ deviceClassByMode : map [string ]string {"auto" : "gpu" },
214+ },
215+ },
216+ {
217+ description : "mixed auto and explicit" ,
218+ devices : []string {"0" , "mode=gds,id=foo" , "mode=gdrcopy,id=bar" },
219+ expected : & cdiModeIdentifiers {
220+ modes : []string {"auto" , "gds" , "gdrcopy" },
221+ idsByMode : map [string ][]string {
222+ "auto" : {"0" },
223+ "gds" : {"foo" },
224+ "gdrcopy" : {"bar" },
225+ },
226+ deviceClassByMode : map [string ]string {"auto" : "gpu" },
227+ },
228+ },
229+ {
230+ description : "device with only mode, no id" ,
231+ devices : []string {"mode=nvswitch" },
232+ expected : & cdiModeIdentifiers {
233+ modes : []string {"nvswitch" },
234+ idsByMode : map [string ][]string {"nvswitch" : {}},
235+ deviceClassByMode : map [string ]string {"auto" : "gpu" },
236+ },
237+ },
238+ {
239+ description : "duplicate modes" ,
240+ devices : []string {"mode=gds,id=x" , "mode=gds,id=y" , "mode=gds" },
241+ expected : & cdiModeIdentifiers {
242+ modes : []string {"auto" , "gds" },
243+ idsByMode : map [string ][]string {"gds" : {"x" , "y" }},
244+ deviceClassByMode : map [string ]string {"auto" : "gpu" },
245+ },
246+ },
247+ }
248+
249+ for _ , tc := range testCases {
250+ t .Run (tc .description , func (t * testing.T ) {
251+ result := cdiModeIdentfiersFromDevices (tc .devices ... )
252+ require .EqualValues (t , tc .expected , result )
253+ })
254+ }
255+ }
0 commit comments