|
| 1 | +package worker |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + |
| 9 | + common "github.com/beam-cloud/beta9/pkg/common" |
| 10 | + "gvisor.dev/gvisor/pkg/sync" |
| 11 | +) |
| 12 | + |
| 13 | +func TestIntegrationGPUIsolation(t *testing.T) { |
| 14 | + if os.Getenv("GPU_INTEGRATION") != "1" { |
| 15 | + t.Skip("set GPU_INTEGRATION=1 to run on a real GPU node") |
| 16 | + } |
| 17 | + |
| 18 | + // Step 1: Read PID 1's env (what os.Getenv sees — the broken path) |
| 19 | + pid1Env := "(unknown)" |
| 20 | + data, err := os.ReadFile("/proc/1/environ") |
| 21 | + if err == nil { |
| 22 | + for _, entry := range strings.Split(string(data), "\x00") { |
| 23 | + if strings.HasPrefix(entry, "NVIDIA_VISIBLE_DEVICES=") { |
| 24 | + pid1Env = strings.TrimPrefix(entry, "NVIDIA_VISIBLE_DEVICES=") |
| 25 | + break |
| 26 | + } |
| 27 | + } |
| 28 | + } |
| 29 | + t.Logf("PID 1 NVIDIA_VISIBLE_DEVICES = %q", pid1Env) |
| 30 | + |
| 31 | + // Step 2: Call the REAL resolveVisibleDevices() from gpu_info.go |
| 32 | + resolved := resolveVisibleDevices() |
| 33 | + t.Logf("resolveVisibleDevices() = %q", resolved) |
| 34 | + |
| 35 | + if resolved == "void" || resolved == "" { |
| 36 | + t.Fatalf("resolveVisibleDevices() returned %q — void bug NOT fixed", resolved) |
| 37 | + } |
| 38 | + if !strings.HasPrefix(resolved, "GPU-") { |
| 39 | + t.Fatalf("resolveVisibleDevices() returned %q — expected GPU UUID", resolved) |
| 40 | + } |
| 41 | + |
| 42 | + // Step 3: Create the REAL NvidiaInfoClient with the resolved value (same as NewContainerNvidiaManager) |
| 43 | + client := &NvidiaInfoClient{visibleDevices: resolved} |
| 44 | + |
| 45 | + // Step 4: Call the REAL AvailableGPUDevices() |
| 46 | + devices, err := client.AvailableGPUDevices() |
| 47 | + if err != nil { |
| 48 | + t.Fatalf("AvailableGPUDevices() error: %v", err) |
| 49 | + } |
| 50 | + t.Logf("AvailableGPUDevices() = %v", devices) |
| 51 | + |
| 52 | + if len(devices) == 0 { |
| 53 | + t.Fatal("AvailableGPUDevices() returned empty — GPU not visible") |
| 54 | + } |
| 55 | + if len(devices) != 1 { |
| 56 | + t.Fatalf("AvailableGPUDevices() returned %d GPUs, expected exactly 1 for per-worker isolation", len(devices)) |
| 57 | + } |
| 58 | + |
| 59 | + // Step 5: Verify the OLD path (void) would have failed |
| 60 | + oldClient := &NvidiaInfoClient{visibleDevices: pid1Env} |
| 61 | + oldDevices, err := oldClient.AvailableGPUDevices() |
| 62 | + if err != nil { |
| 63 | + t.Logf("Old path error (expected): %v", err) |
| 64 | + } |
| 65 | + t.Logf("Old path (PID 1 env=%q) -> AvailableGPUDevices() = %v", pid1Env, oldDevices) |
| 66 | + |
| 67 | + if pid1Env == "void" && len(oldDevices) > 0 { |
| 68 | + t.Error("Old code path with void should return empty, but got devices — test logic wrong") |
| 69 | + } |
| 70 | + |
| 71 | + // Step 6: Exercise the REAL ContainerNvidiaManager.AssignGPUDevices (chooseDevices) |
| 72 | + manager := &ContainerNvidiaManager{ |
| 73 | + gpuAllocationMap: common.NewSafeMap[[]int](), |
| 74 | + gpuCount: 1, |
| 75 | + mu: sync.Mutex{}, |
| 76 | + infoClient: client, |
| 77 | + resolvedVisibleDevices: resolved, |
| 78 | + } |
| 79 | + |
| 80 | + assigned, err := manager.AssignGPUDevices("test-container-1", 1) |
| 81 | + if err != nil { |
| 82 | + t.Fatalf("AssignGPUDevices() failed: %v", err) |
| 83 | + } |
| 84 | + t.Logf("AssignGPUDevices(\"test-container-1\", 1) = %v", assigned) |
| 85 | + |
| 86 | + if len(assigned) != 1 { |
| 87 | + t.Fatalf("Expected 1 assigned GPU, got %d", len(assigned)) |
| 88 | + } |
| 89 | + if assigned[0] != devices[0] { |
| 90 | + t.Fatalf("Assigned GPU %d doesn't match available GPU %d", assigned[0], devices[0]) |
| 91 | + } |
| 92 | + |
| 93 | + // Step 7: Verify second allocation to same worker FAILS (only 1 GPU available) |
| 94 | + _, err = manager.AssignGPUDevices("test-container-2", 1) |
| 95 | + if err == nil { |
| 96 | + t.Fatal("Second allocation should fail — only 1 GPU per worker") |
| 97 | + } |
| 98 | + t.Logf("Second allocation correctly failed: %v", err) |
| 99 | + |
| 100 | + fmt.Printf("\nRESULT: resolved=%s gpu_index=%d old_path_would_fail=%v PASS\n", |
| 101 | + resolved, assigned[0], pid1Env == "void" && len(oldDevices) == 0) |
| 102 | +} |
0 commit comments