|
| 1 | +package host |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "reflect" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/dstackai/dstack/runner/internal/common" |
| 10 | +) |
| 11 | + |
| 12 | +func loadTestData(filename string) ([]byte, error) { |
| 13 | + path := filepath.Join("testdata", filename) |
| 14 | + return os.ReadFile(path) |
| 15 | +} |
| 16 | + |
| 17 | +func TestUnmarshalTtSmiSnapshot(t *testing.T) { |
| 18 | + tests := []struct { |
| 19 | + name string |
| 20 | + filename string |
| 21 | + want *ttSmiSnapshot |
| 22 | + wantErr bool |
| 23 | + }{ |
| 24 | + { |
| 25 | + name: "valid single device", |
| 26 | + filename: "valid_single_device.json", |
| 27 | + want: &ttSmiSnapshot{ |
| 28 | + DeviceInfo: []ttDeviceInfo{ |
| 29 | + { |
| 30 | + BoardInfo: ttBoardInfo{ |
| 31 | + BoardType: "n150 L", |
| 32 | + BoardID: "100018611902010", |
| 33 | + }, |
| 34 | + }, |
| 35 | + }, |
| 36 | + }, |
| 37 | + wantErr: false, |
| 38 | + }, |
| 39 | + { |
| 40 | + name: "valid multiple devices", |
| 41 | + filename: "valid_multiple_devices.json", |
| 42 | + want: &ttSmiSnapshot{ |
| 43 | + DeviceInfo: []ttDeviceInfo{ |
| 44 | + { |
| 45 | + BoardInfo: ttBoardInfo{ |
| 46 | + BoardType: "n300 L", |
| 47 | + BoardID: "10001451172208f", |
| 48 | + }, |
| 49 | + }, |
| 50 | + { |
| 51 | + BoardInfo: ttBoardInfo{ |
| 52 | + BoardType: "n300 L", |
| 53 | + BoardID: "100014511722053", |
| 54 | + }, |
| 55 | + }, |
| 56 | + { |
| 57 | + BoardInfo: ttBoardInfo{ |
| 58 | + BoardType: "n300 L", |
| 59 | + BoardID: "10001451172209c", |
| 60 | + }, |
| 61 | + }, |
| 62 | + { |
| 63 | + BoardInfo: ttBoardInfo{ |
| 64 | + BoardType: "n300 L", |
| 65 | + BoardID: "100014511722058", |
| 66 | + }, |
| 67 | + }, |
| 68 | + { |
| 69 | + BoardInfo: ttBoardInfo{ |
| 70 | + BoardType: "n300 R", |
| 71 | + BoardID: "10001451172208f", |
| 72 | + }, |
| 73 | + }, |
| 74 | + { |
| 75 | + BoardInfo: ttBoardInfo{ |
| 76 | + BoardType: "n300 R", |
| 77 | + BoardID: "100014511722053", |
| 78 | + }, |
| 79 | + }, |
| 80 | + { |
| 81 | + BoardInfo: ttBoardInfo{ |
| 82 | + BoardType: "n300 R", |
| 83 | + BoardID: "10001451172209c", |
| 84 | + }, |
| 85 | + }, |
| 86 | + { |
| 87 | + BoardInfo: ttBoardInfo{ |
| 88 | + BoardType: "n300 R", |
| 89 | + BoardID: "100014511722058", |
| 90 | + }, |
| 91 | + }, |
| 92 | + }, |
| 93 | + }, |
| 94 | + wantErr: false, |
| 95 | + }, |
| 96 | + { |
| 97 | + name: "empty device info", |
| 98 | + filename: "empty_device_info.json", |
| 99 | + want: &ttSmiSnapshot{ |
| 100 | + DeviceInfo: []ttDeviceInfo{}, |
| 101 | + }, |
| 102 | + wantErr: false, |
| 103 | + }, |
| 104 | + { |
| 105 | + name: "invalid JSON", |
| 106 | + filename: "invalid_json.json", |
| 107 | + want: nil, |
| 108 | + wantErr: true, |
| 109 | + }, |
| 110 | + { |
| 111 | + name: "missing device_info field", |
| 112 | + filename: "missing_device_info.json", |
| 113 | + want: &ttSmiSnapshot{DeviceInfo: nil}, |
| 114 | + wantErr: false, |
| 115 | + }, |
| 116 | + { |
| 117 | + name: "empty JSON", |
| 118 | + filename: "empty_json.json", |
| 119 | + want: &ttSmiSnapshot{DeviceInfo: nil}, |
| 120 | + wantErr: false, |
| 121 | + }, |
| 122 | + } |
| 123 | + |
| 124 | + for _, tt := range tests { |
| 125 | + t.Run(tt.name, func(t *testing.T) { |
| 126 | + data, err := loadTestData(tt.filename) |
| 127 | + if err != nil { |
| 128 | + t.Fatalf("Failed to load test data from %s: %v", tt.filename, err) |
| 129 | + } |
| 130 | + |
| 131 | + got, err := unmarshalTtSmiSnapshot(data) |
| 132 | + if (err != nil) != tt.wantErr { |
| 133 | + t.Errorf("unmarshalTtSmiSnapshot() error = %v, wantErr %v", err, tt.wantErr) |
| 134 | + return |
| 135 | + } |
| 136 | + if !tt.wantErr { |
| 137 | + if got == nil { |
| 138 | + t.Errorf("unmarshalTtSmiSnapshot() returned nil, expected non-nil result") |
| 139 | + return |
| 140 | + } |
| 141 | + if len(got.DeviceInfo) != len(tt.want.DeviceInfo) { |
| 142 | + t.Errorf("unmarshalTtSmiSnapshot() device count = %v, want %v", len(got.DeviceInfo), len(tt.want.DeviceInfo)) |
| 143 | + return |
| 144 | + } |
| 145 | + for i, device := range got.DeviceInfo { |
| 146 | + if i >= len(tt.want.DeviceInfo) { |
| 147 | + break |
| 148 | + } |
| 149 | + expected := tt.want.DeviceInfo[i] |
| 150 | + if device.BoardInfo.BoardType != expected.BoardInfo.BoardType { |
| 151 | + t.Errorf("unmarshalTtSmiSnapshot() device[%d].BoardInfo.BoardType = %v, want %v", i, device.BoardInfo.BoardType, expected.BoardInfo.BoardType) |
| 152 | + } |
| 153 | + if device.BoardInfo.BoardID != expected.BoardInfo.BoardID { |
| 154 | + t.Errorf("unmarshalTtSmiSnapshot() device[%d].BoardInfo.BoardID = %v, want %v", i, device.BoardInfo.BoardID, expected.BoardInfo.BoardID) |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | + }) |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +func TestGetGpusFromTtSmiSnapshot(t *testing.T) { |
| 163 | + data, err := loadTestData("single_n150_gpu.json") |
| 164 | + if err != nil { |
| 165 | + t.Fatalf("Failed to load test data: %v", err) |
| 166 | + } |
| 167 | + snapshot, err := unmarshalTtSmiSnapshot(data) |
| 168 | + if err != nil { |
| 169 | + t.Fatalf("Failed to unmarshal snapshot: %v", err) |
| 170 | + } |
| 171 | + |
| 172 | + expectedGpus := []GpuInfo{ |
| 173 | + { |
| 174 | + Vendor: common.GpuVendorTenstorrent, |
| 175 | + Name: "n150", |
| 176 | + Vram: 12 * 1024, |
| 177 | + ID: "100018611902010", |
| 178 | + Index: "0", |
| 179 | + }, |
| 180 | + } |
| 181 | + |
| 182 | + gpus := getGpusFromTtSmiSnapshot(snapshot) |
| 183 | + |
| 184 | + if !reflect.DeepEqual(gpus, expectedGpus) { |
| 185 | + t.Errorf("getGpusFromTtSmiSnapshot() = %v, want %v", gpus, expectedGpus) |
| 186 | + } |
| 187 | +} |
| 188 | + |
| 189 | +func TestGetGpusFromTtSmiSnapshotMultipleDevices(t *testing.T) { |
| 190 | + data, err := loadTestData("valid_multiple_devices.json") |
| 191 | + if err != nil { |
| 192 | + t.Fatalf("Failed to load test data: %v", err) |
| 193 | + } |
| 194 | + snapshot, err := unmarshalTtSmiSnapshot(data) |
| 195 | + if err != nil { |
| 196 | + t.Fatalf("Failed to unmarshal snapshot: %v", err) |
| 197 | + } |
| 198 | + |
| 199 | + gpus := getGpusFromTtSmiSnapshot(snapshot) |
| 200 | + |
| 201 | + // Verify we have 4 unique GPUs (grouped by board_id) |
| 202 | + if len(gpus) != 4 { |
| 203 | + t.Errorf("getGpusFromTtSmiSnapshot() returned %d GPUs, want 4", len(gpus)) |
| 204 | + } |
| 205 | + |
| 206 | + // Create a map to check the results by board_id |
| 207 | + gpusByID := make(map[string]GpuInfo) |
| 208 | + for _, gpu := range gpus { |
| 209 | + gpusByID[gpu.ID] = gpu |
| 210 | + } |
| 211 | + |
| 212 | + // Verify specific GPUs and their aggregated VRAM |
| 213 | + expectedGpus := map[string]struct { |
| 214 | + name string |
| 215 | + vram int |
| 216 | + }{ |
| 217 | + "10001451172208f": {"n300", 24 * 1024}, // 12GB (n300 L) + 12GB (n300 R) = 24GB |
| 218 | + "100014511722053": {"n300", 24 * 1024}, // 12GB (n300 L) + 12GB (n300 R) = 24GB |
| 219 | + "10001451172209c": {"n300", 24 * 1024}, // 12GB (n300 L) + 12GB (n300 R) = 24GB |
| 220 | + "100014511722058": {"n300", 24 * 1024}, // 12GB (n300 L) + 12GB (n300 R) = 24GB |
| 221 | + } |
| 222 | + |
| 223 | + for boardID, expected := range expectedGpus { |
| 224 | + gpu, exists := gpusByID[boardID] |
| 225 | + if !exists { |
| 226 | + t.Errorf("Expected GPU with board_id %s not found", boardID) |
| 227 | + continue |
| 228 | + } |
| 229 | + if gpu.Name != expected.name { |
| 230 | + t.Errorf("GPU %s: name = %s, want %s", boardID, gpu.Name, expected.name) |
| 231 | + } |
| 232 | + if gpu.Vram != expected.vram { |
| 233 | + t.Errorf("GPU %s: VRAM = %d, want %d", boardID, gpu.Vram, expected.vram) |
| 234 | + } |
| 235 | + if gpu.Vendor != common.GpuVendorTenstorrent { |
| 236 | + t.Errorf("GPU %s: vendor = %v, want %v", boardID, gpu.Vendor, common.GpuVendorTenstorrent) |
| 237 | + } |
| 238 | + } |
| 239 | +} |
0 commit comments