@@ -429,7 +429,7 @@ func TestGetDevicesFromEnvvar(t *testing.T) {
429429 )
430430
431431 require .NoError (t , err )
432- devices := image .VisibleDevicesFromEnvVar ()
432+ devices := image .visibleDevicesFromEnvVar ()
433433 require .EqualValues (t , tc .expectedDevices , devices )
434434 })
435435 }
@@ -508,13 +508,15 @@ func TestGetVisibleDevicesFromMounts(t *testing.T) {
508508
509509func TestVisibleDevices (t * testing.T ) {
510510 var tests = []struct {
511- description string
512- mountDevices []specs.Mount
513- envvarDevices string
514- privileged bool
515- acceptUnprivileged bool
516- acceptMounts bool
517- expectedDevices []string
511+ description string
512+ mountDevices []specs.Mount
513+ envvarDevices string
514+ privileged bool
515+ acceptUnprivileged bool
516+ acceptMounts bool
517+ preferredVisibleDeviceEnvVars []string
518+ env map [string ]string
519+ expectedDevices []string
518520 }{
519521 {
520522 description : "Mount devices, unprivileged, no accept unprivileged" ,
@@ -597,20 +599,92 @@ func TestVisibleDevices(t *testing.T) {
597599 acceptMounts : false ,
598600 expectedDevices : nil ,
599601 },
602+ // New test cases for visibleEnvVars functionality
603+ {
604+ description : "preferred env var set and present in env, privileged" ,
605+ mountDevices : nil ,
606+ envvarDevices : "" ,
607+ privileged : true ,
608+ acceptUnprivileged : false ,
609+ acceptMounts : true ,
610+ preferredVisibleDeviceEnvVars : []string {"DOCKER_RESOURCE_GPUS" },
611+ env : map [string ]string {
612+ "DOCKER_RESOURCE_GPUS" : "GPU-12345" ,
613+ },
614+ expectedDevices : []string {"GPU-12345" },
615+ },
616+ {
617+ description : "preferred env var set and present in env, unprivileged but accepted" ,
618+ mountDevices : nil ,
619+ envvarDevices : "" ,
620+ privileged : false ,
621+ acceptUnprivileged : true ,
622+ acceptMounts : true ,
623+ preferredVisibleDeviceEnvVars : []string {"DOCKER_RESOURCE_GPUS" },
624+ env : map [string ]string {
625+ "DOCKER_RESOURCE_GPUS" : "GPU-12345" ,
626+ },
627+ expectedDevices : []string {"GPU-12345" },
628+ },
629+ {
630+ description : "preferred env var set and present in env, unprivileged and not accepted" ,
631+ mountDevices : nil ,
632+ envvarDevices : "" ,
633+ privileged : false ,
634+ acceptUnprivileged : false ,
635+ acceptMounts : true ,
636+ preferredVisibleDeviceEnvVars : []string {"DOCKER_RESOURCE_GPUS" },
637+ env : map [string ]string {
638+ "DOCKER_RESOURCE_GPUS" : "GPU-12345" ,
639+ },
640+ expectedDevices : nil ,
641+ },
642+ {
643+ description : "multiple preferred env vars, both present, privileged" ,
644+ mountDevices : nil ,
645+ envvarDevices : "" ,
646+ privileged : true ,
647+ acceptUnprivileged : false ,
648+ acceptMounts : true ,
649+ preferredVisibleDeviceEnvVars : []string {"DOCKER_RESOURCE_GPUS" , "DOCKER_RESOURCE_GPUS_ADDITIONAL" },
650+ env : map [string ]string {
651+ "DOCKER_RESOURCE_GPUS" : "GPU-12345" ,
652+ "DOCKER_RESOURCE_GPUS_ADDITIONAL" : "GPU-67890" ,
653+ },
654+ expectedDevices : []string {"GPU-12345" , "GPU-67890" },
655+ },
656+ {
657+ description : "preferred env var not present, fallback to NVIDIA_VISIBLE_DEVICES, privileged" ,
658+ mountDevices : nil ,
659+ envvarDevices : "GPU-12345" ,
660+ privileged : true ,
661+ acceptUnprivileged : false ,
662+ acceptMounts : true ,
663+ preferredVisibleDeviceEnvVars : []string {"DOCKER_RESOURCE_GPUS" },
664+ env : map [string ]string {
665+ EnvVarNvidiaVisibleDevices : "GPU-12345" ,
666+ },
667+ expectedDevices : []string {"GPU-12345" },
668+ },
600669 }
601670 for _ , tc := range tests {
602671 t .Run (tc .description , func (t * testing.T ) {
603- // Wrap the call to getDevices() in a closure.
672+ // Create env map with both NVIDIA_VISIBLE_DEVICES and any additional env vars
673+ env := make (map [string ]string )
674+ if tc .envvarDevices != "" {
675+ env [EnvVarNvidiaVisibleDevices ] = tc .envvarDevices
676+ }
677+ for k , v := range tc .env {
678+ env [k ] = v
679+ }
680+
604681 image , err := New (
605- WithEnvMap (
606- map [string ]string {
607- EnvVarNvidiaVisibleDevices : tc .envvarDevices ,
608- },
609- ),
682+ WithEnvMap (env ),
610683 WithMounts (tc .mountDevices ),
611684 WithPrivileged (tc .privileged ),
612685 WithAcceptDeviceListAsVolumeMounts (tc .acceptMounts ),
613686 WithAcceptEnvvarUnprivileged (tc .acceptUnprivileged ),
687+ WithPreferredVisibleDevicesEnvVars (tc .preferredVisibleDeviceEnvVars ... ),
614688 )
615689 require .NoError (t , err )
616690 require .Equal (t , tc .expectedDevices , image .VisibleDevices ())
0 commit comments