@@ -30,6 +30,7 @@ import (
3030
3131 "github.com/NVIDIA/nvidia-container-toolkit/internal/config"
3232 "github.com/NVIDIA/nvidia-container-toolkit/internal/lookup/root"
33+ "github.com/NVIDIA/nvidia-container-toolkit/internal/oci"
3334 "github.com/NVIDIA/nvidia-container-toolkit/internal/test"
3435)
3536
@@ -165,3 +166,181 @@ func TestFactoryMethod(t *testing.T) {
165166 })
166167 }
167168}
169+
170+ func TestNewSpecModifier (t * testing.T ) {
171+ logger , _ := testlog .NewNullLogger ()
172+ driver := root .New (
173+ root .WithDriverRoot ("/nvidia/driver/root" ),
174+ )
175+ testCases := []struct {
176+ description string
177+ config * config.Config
178+ spec * specs.Spec
179+ expectedSpec * specs.Spec
180+ }{
181+ {
182+ description : "csv mode removes nvidia-container-runtime-hook" ,
183+ config : & config.Config {
184+ NVIDIAContainerRuntimeConfig : config.RuntimeConfig {
185+ Mode : "csv" ,
186+ },
187+ },
188+ spec : & specs.Spec {
189+ Hooks : & specs.Hooks {
190+ Prestart : []specs.Hook {
191+ {
192+ Path : "/path/to/nvidia-container-runtime-hook" ,
193+ Args : []string {"/path/to/nvidia-container-runtime-hook" , "prestart" },
194+ },
195+ },
196+ },
197+ },
198+ expectedSpec : & specs.Spec {
199+ Hooks : & specs.Hooks {
200+ Prestart : nil ,
201+ },
202+ },
203+ },
204+ {
205+ description : "csv mode removes nvidia-container-toolkit" ,
206+ config : & config.Config {
207+ NVIDIAContainerRuntimeConfig : config.RuntimeConfig {
208+ Mode : "csv" ,
209+ },
210+ },
211+ spec : & specs.Spec {
212+ Hooks : & specs.Hooks {
213+ Prestart : []specs.Hook {
214+ {
215+ Path : "/path/to/nvidia-container-toolkit" ,
216+ Args : []string {"/path/to/nvidia-container-toolkit" , "prestart" },
217+ },
218+ },
219+ },
220+ },
221+ expectedSpec : & specs.Spec {
222+ Hooks : & specs.Hooks {
223+ Prestart : nil ,
224+ },
225+ },
226+ },
227+ {
228+ description : "cdi mode removes nvidia-container-runtime-hook" ,
229+ config : & config.Config {
230+ NVIDIAContainerRuntimeConfig : config.RuntimeConfig {
231+ Mode : "cdi" ,
232+ },
233+ },
234+ spec : & specs.Spec {
235+ Hooks : & specs.Hooks {
236+ Prestart : []specs.Hook {
237+ {
238+ Path : "/path/to/nvidia-container-runtime-hook" ,
239+ Args : []string {"/path/to/nvidia-container-runtime-hook" , "prestart" },
240+ },
241+ },
242+ },
243+ },
244+ expectedSpec : & specs.Spec {
245+ Hooks : & specs.Hooks {
246+ Prestart : nil ,
247+ },
248+ },
249+ },
250+ {
251+ description : "cdi mode removes nvidia-container-toolkit" ,
252+ config : & config.Config {
253+ NVIDIAContainerRuntimeConfig : config.RuntimeConfig {
254+ Mode : "cdi" ,
255+ },
256+ },
257+ spec : & specs.Spec {
258+ Hooks : & specs.Hooks {
259+ Prestart : []specs.Hook {
260+ {
261+ Path : "/path/to/nvidia-container-toolkit" ,
262+ Args : []string {"/path/to/nvidia-container-toolkit" , "prestart" },
263+ },
264+ },
265+ },
266+ },
267+ expectedSpec : & specs.Spec {
268+ Hooks : & specs.Hooks {
269+ Prestart : nil ,
270+ },
271+ },
272+ },
273+ {
274+ description : "legacy mode keeps nvidia-container-runtime-hook" ,
275+ config : & config.Config {
276+ NVIDIAContainerRuntimeConfig : config.RuntimeConfig {
277+ Mode : "legacy" ,
278+ },
279+ },
280+ spec : & specs.Spec {
281+ Hooks : & specs.Hooks {
282+ Prestart : []specs.Hook {
283+ {
284+ Path : "/path/to/nvidia-container-runtime-hook" ,
285+ Args : []string {"/path/to/nvidia-container-runtime-hook" , "prestart" },
286+ },
287+ },
288+ },
289+ },
290+ expectedSpec : & specs.Spec {
291+ Hooks : & specs.Hooks {
292+ Prestart : []specs.Hook {
293+ {
294+ Path : "/path/to/nvidia-container-runtime-hook" ,
295+ Args : []string {"/path/to/nvidia-container-runtime-hook" , "prestart" },
296+ },
297+ },
298+ },
299+ },
300+ },
301+ {
302+ description : "legacy mode keeps nvidia-container-toolkit" ,
303+ config : & config.Config {
304+ NVIDIAContainerRuntimeConfig : config.RuntimeConfig {
305+ Mode : "legacy" ,
306+ },
307+ },
308+ spec : & specs.Spec {
309+ Hooks : & specs.Hooks {
310+ Prestart : []specs.Hook {
311+ {
312+ Path : "/path/to/nvidia-container-toolkit" ,
313+ Args : []string {"/path/to/nvidia-container-toolkit" , "prestart" },
314+ },
315+ },
316+ },
317+ },
318+ expectedSpec : & specs.Spec {
319+ Hooks : & specs.Hooks {
320+ Prestart : []specs.Hook {
321+ {
322+ Path : "/path/to/nvidia-container-toolkit" ,
323+ Args : []string {"/path/to/nvidia-container-toolkit" , "prestart" },
324+ },
325+ },
326+ },
327+ },
328+ },
329+ }
330+
331+ for _ , tc := range testCases {
332+ t .Run (tc .description , func (t * testing.T ) {
333+ spec := & oci.SpecMock {
334+ LoadFunc : func () (* specs.Spec , error ) {
335+ return tc .spec , nil
336+ },
337+ }
338+ m , err := newSpecModifier (logger , tc .config , spec , driver )
339+ require .NoError (t , err )
340+
341+ err = m .Modify (tc .spec )
342+ require .NoError (t , err )
343+ require .EqualValues (t , tc .expectedSpec , tc .spec )
344+ })
345+ }
346+ }
0 commit comments