-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathnvidia_smi_test.go
More file actions
133 lines (124 loc) · 2.35 KB
/
nvidia_smi_test.go
File metadata and controls
133 lines (124 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Copyright (c) Meta Platforms, Inc. and affiliates.
// All rights reserved.
package shelper
import (
"bufio"
"fmt"
"reflect"
"strings"
"testing"
)
func TestParseNvidiaSmiGetPidsCommand(t *testing.T) {
tests := []struct {
name string
input string
expected map[string]string
}{
{
name: "Normal output with PIDs and dashes",
input: `123
-
456
-
-
-
-
-`,
expected: map[string]string{
"0": "123",
"1": "",
"2": "456",
"3": "",
"4": "",
"5": "",
"6": "",
"7": "",
},
},
{
name: "Output with all PIDs",
input: `123
456
789
101
202
303
404
505`,
expected: map[string]string{
"0": "123",
"1": "456",
"2": "789",
"3": "101",
"4": "202",
"5": "303",
"6": "404",
"7": "505",
},
},
{
name: "Output with all dashes",
input: `-
-
-
-`,
expected: map[string]string{
"0": "",
"1": "",
"2": "",
"3": "",
},
},
{
name: "Output with non-numerical characters",
input: `123abc
-def
456ghi
-jkl`,
expected: map[string]string{
"0": "123",
"1": "",
"2": "456",
"3": "",
},
},
{
name: "Empty output",
input: "",
expected: map[string]string{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Create a modified version of parseNvidiaSmiGetPidsCommand that doesn't call executeGpuPidsCommand
result := parseNvidiaSmiGetPidsCommandForTest(tt.input)
if !reflect.DeepEqual(result, tt.expected) {
t.Errorf("parseNvidiaSmiGetPidsCommand() = %v, want %v", result, tt.expected)
}
})
}
}
// parseNvidiaSmiGetPidsCommandForTest is a test-friendly version of parseNvidiaSmiGetPidsCommand
// that doesn't call executeGpuPidsCommand and instead uses the provided input
func parseNvidiaSmiGetPidsCommandForTest(output string) map[string]string {
gpuToPid := make(map[string]string)
// Parse the output line by line
scanner := bufio.NewScanner(strings.NewReader(output))
gpuID := 0
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
// Extract numerical values until a non-numerical character
var numStr string
for _, char := range line {
if char >= '0' && char <= '9' {
numStr += string(char)
} else {
break
}
}
// Add to map (empty string if no numerical value found)
gpuToPid[fmt.Sprintf("%d", gpuID)] = numStr
gpuID++
}
return gpuToPid
}