|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/stretchr/testify/require" |
| 5 | + "sort" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | +) |
| 9 | + |
| 10 | +func TestMergeVisibleDevicesEnvvars(t *testing.T) { |
| 11 | + var tests = []struct { |
| 12 | + name string |
| 13 | + input []string |
| 14 | + expected string |
| 15 | + enableMerge bool |
| 16 | + }{ |
| 17 | + { |
| 18 | + "Simple Merge Enabled", |
| 19 | + []string{ |
| 20 | + "NVIDIA_VISIBLE_DEVICES_0=0,1", |
| 21 | + "NVIDIA_VISIBLE_DEVICES_1=2,3", |
| 22 | + "NVIDIA_VISIBLE_DEVICES_WHATEVER=4,5", |
| 23 | + }, |
| 24 | + "0,1,2,3,4,5", |
| 25 | + true, |
| 26 | + }, |
| 27 | + { |
| 28 | + "Simple Merge Disabled", |
| 29 | + []string{ |
| 30 | + "NVIDIA_VISIBLE_DEVICES_0=0,1", |
| 31 | + "NVIDIA_VISIBLE_DEVICES_1=2,3", |
| 32 | + "NVIDIA_VISIBLE_DEVICES_WHATEVER=4,5", |
| 33 | + }, |
| 34 | + "", |
| 35 | + false, |
| 36 | + }, |
| 37 | + { |
| 38 | + "Merge No Override (Enabled)", |
| 39 | + []string{ |
| 40 | + "NVIDIA_VISIBLE_DEVICES=all", |
| 41 | + }, |
| 42 | + "all", |
| 43 | + true, |
| 44 | + }, |
| 45 | + { |
| 46 | + "Merge No Override (Disabled)", |
| 47 | + []string{ |
| 48 | + "NVIDIA_VISIBLE_DEVICES=all", |
| 49 | + }, |
| 50 | + "all", |
| 51 | + false, |
| 52 | + }, |
| 53 | + { |
| 54 | + "Merge Override (Enabled, Before)", |
| 55 | + []string{ |
| 56 | + "NVIDIA_VISIBLE_DEVICES=all", |
| 57 | + "NVIDIA_VISIBLE_DEVICES_0=0,1", |
| 58 | + "NVIDIA_VISIBLE_DEVICES_1=2,3", |
| 59 | + "NVIDIA_VISIBLE_DEVICES_WHATEVER=4,5", |
| 60 | + }, |
| 61 | + "0,1,2,3,4,5", |
| 62 | + true, |
| 63 | + }, |
| 64 | + { |
| 65 | + "Merge Override (Enabled, After)", |
| 66 | + []string{ |
| 67 | + "NVIDIA_VISIBLE_DEVICES_0=0,1", |
| 68 | + "NVIDIA_VISIBLE_DEVICES_1=2,3", |
| 69 | + "NVIDIA_VISIBLE_DEVICES_WHATEVER=4,5", |
| 70 | + "NVIDIA_VISIBLE_DEVICES=all", |
| 71 | + }, |
| 72 | + "0,1,2,3,4,5", |
| 73 | + true, |
| 74 | + }, |
| 75 | + { |
| 76 | + "Merge Override (Enabled, In Between)", |
| 77 | + []string{ |
| 78 | + "NVIDIA_VISIBLE_DEVICES_0=0,1", |
| 79 | + "NVIDIA_VISIBLE_DEVICES_1=2,3", |
| 80 | + "NVIDIA_VISIBLE_DEVICES=all", |
| 81 | + "NVIDIA_VISIBLE_DEVICES_WHATEVER=4,5", |
| 82 | + }, |
| 83 | + "0,1,2,3,4,5", |
| 84 | + true, |
| 85 | + }, |
| 86 | + { |
| 87 | + "Merge Override (Disabled, Before)", |
| 88 | + []string{ |
| 89 | + "NVIDIA_VISIBLE_DEVICES=all", |
| 90 | + "NVIDIA_VISIBLE_DEVICES_0=0,1", |
| 91 | + "NVIDIA_VISIBLE_DEVICES_1=2,3", |
| 92 | + "NVIDIA_VISIBLE_DEVICES_WHATEVER=4,5", |
| 93 | + }, |
| 94 | + "all", |
| 95 | + false, |
| 96 | + }, |
| 97 | + { |
| 98 | + "Merge Override (Disabled, After)", |
| 99 | + []string{ |
| 100 | + "NVIDIA_VISIBLE_DEVICES_0=0,1", |
| 101 | + "NVIDIA_VISIBLE_DEVICES_1=2,3", |
| 102 | + "NVIDIA_VISIBLE_DEVICES_WHATEVER=4,5", |
| 103 | + "NVIDIA_VISIBLE_DEVICES=all", |
| 104 | + }, |
| 105 | + "all", |
| 106 | + false, |
| 107 | + }, |
| 108 | + { |
| 109 | + "Merge Override (Disabled, In Between)", |
| 110 | + []string{ |
| 111 | + "NVIDIA_VISIBLE_DEVICES_0=0,1", |
| 112 | + "NVIDIA_VISIBLE_DEVICES_1=2,3", |
| 113 | + "NVIDIA_VISIBLE_DEVICES=all", |
| 114 | + "NVIDIA_VISIBLE_DEVICES_WHATEVER=4,5", |
| 115 | + }, |
| 116 | + "all", |
| 117 | + false, |
| 118 | + }, |
| 119 | + } |
| 120 | + for _, tc := range tests { |
| 121 | + t.Run(tc.name, func(t *testing.T) { |
| 122 | + config := CLIConfig{ |
| 123 | + AlphaMergeVisibleDevicesEnvvars: tc.enableMerge, |
| 124 | + } |
| 125 | + envvars := getEnvMap(tc.input, config) |
| 126 | + devices := strings.Split(envvars[envNVVisibleDevices], ",") |
| 127 | + sort.Strings(devices) |
| 128 | + require.Equal(t, tc.expected, strings.Join(devices, ",")) |
| 129 | + }) |
| 130 | + } |
| 131 | +} |
0 commit comments