-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathoptions_test.go
More file actions
338 lines (302 loc) · 8.46 KB
/
Copy pathoptions_test.go
File metadata and controls
338 lines (302 loc) · 8.46 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
package config
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/leptonai/gpud/components"
)
func TestOpApplyOpts(t *testing.T) {
t.Run("default values", func(t *testing.T) {
op := &Op{}
err := op.ApplyOpts([]OpOption{})
assert.NoError(t, err)
})
}
func TestWithFailureInjector(t *testing.T) {
tests := []struct {
name string
injector *components.FailureInjector
}{
{
name: "nil injector",
injector: nil,
},
{
name: "empty injector",
injector: &components.FailureInjector{
GPUUUIDsWithRowRemappingPending: []string{},
GPUUUIDsWithRowRemappingFailed: []string{},
},
},
{
name: "injector with UUIDs",
injector: &components.FailureInjector{
GPUUUIDsWithRowRemappingPending: []string{"GPU-12345678-1234-1234-1234-123456789012"},
GPUUUIDsWithRowRemappingFailed: []string{"GPU-87654321-4321-4321-4321-210987654321"},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
op := &Op{}
option := WithFailureInjector(tt.injector)
option(op)
assert.Equal(t, tt.injector, op.FailureInjector)
})
}
}
func TestWithExcludedInfinibandDevices(t *testing.T) {
t.Parallel()
op := &Op{}
WithExcludedInfinibandDevices([]string{"mlx5_0", "mlx5_1"})(op)
assert.Equal(t, []string{"mlx5_0", "mlx5_1"}, op.ExcludedInfinibandDevices)
}
// TestWithSessionToken tests the WithSessionToken option.
// Note: SessionToken is the SESSION token returned by the control plane after login,
// NOT the registration token passed via --token flag.
// The registration token is used only for login.Login() authentication.
// The session token (loginResp.Token) is what gets stored in the DB and used for session keepalive.
func TestWithSessionToken(t *testing.T) {
tests := []struct {
name string
token string
}{
{
name: "empty token",
token: "",
},
{
name: "valid session token from control plane",
token: "session-token-from-login-response",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
op := &Op{}
option := WithSessionToken(tt.token)
option(op)
assert.Equal(t, tt.token, op.SessionToken)
})
}
}
// TestWithSessionMachineID tests the WithSessionMachineID option.
// Note: SessionMachineID is the machine ID ASSIGNED by the control plane after login,
// NOT the machine ID passed via --machine-id flag (which is optional for override).
// The assigned machine ID (loginResp.MachineID) is what gets stored in the DB.
func TestWithSessionMachineID(t *testing.T) {
tests := []struct {
name string
machineID string
}{
{
name: "empty machine ID",
machineID: "",
},
{
name: "valid assigned machine ID from control plane",
machineID: "assigned-machine-id-from-login-response",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
op := &Op{}
option := WithSessionMachineID(tt.machineID)
option(op)
assert.Equal(t, tt.machineID, op.SessionMachineID)
})
}
}
// TestDBInMemoryWithSessionCredentials tests the combination of
// --db-in-memory with session credentials.
//
// Token flow distinction:
// - Registration token (--token flag): Used only for login.Login() to authenticate with control plane
// - Session token (loginResp.Token): Returned by control plane, stored in DB, used for session keepalive
// - Assigned machine ID (loginResp.MachineID): Returned by control plane, stored in DB
// - Endpoint: Stored in DB, needed for session keepalive (server reads from DB, not config)
//
// This tests the scenario where:
// 1. login.Login() authenticates with registration token, receives session token + assigned machine ID
// 2. login.Login() writes SESSION token, ASSIGNED machine ID, and ENDPOINT to persistent file
// 3. When --db-in-memory, gpud run reads session credentials from persistent file
// 4. gpud run passes them via WithSessionToken, WithSessionMachineID, WithSessionEndpoint to config
// 5. server.New() seeds them into the in-memory database for session keepalive
func TestDBInMemoryWithSessionCredentials(t *testing.T) {
op := &Op{}
// Apply all options as gpud run would
// Note: These are SESSION credentials (from login response), not registration credentials
opts := []OpOption{
WithDBInMemory(true),
WithSessionToken("session-token-from-login-response"),
WithSessionMachineID("assigned-machine-id-from-login-response"),
WithSessionMachineProof("machine-proof-from-login-response"),
WithSessionEndpoint("https://api.example.com"),
}
err := op.ApplyOpts(opts)
assert.NoError(t, err)
assert.True(t, op.DBInMemory)
assert.Equal(t, "session-token-from-login-response", op.SessionToken)
assert.Equal(t, "assigned-machine-id-from-login-response", op.SessionMachineID)
assert.Equal(t, "machine-proof-from-login-response", op.SessionMachineProof)
assert.Equal(t, "https://api.example.com", op.SessionEndpoint)
}
func TestWithDBInMemory(t *testing.T) {
tests := []struct {
name string
value bool
expected bool
}{
{
name: "enabled",
value: true,
expected: true,
},
{
name: "disabled",
value: false,
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
op := &Op{}
option := WithDBInMemory(tt.value)
option(op)
assert.Equal(t, tt.expected, op.DBInMemory)
})
}
}
func TestWithRebootCommands(t *testing.T) {
tests := []struct {
name string
command string
expected string
}{
{
name: "empty reboot command",
command: "",
expected: "",
},
{
name: "custom reboot command",
command: "nsenter --target 1 --mount -- /usr/bin/systemctl reboot",
expected: "nsenter --target 1 --mount -- /usr/bin/systemctl reboot",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
op := &Op{}
option := WithRebootCommands(tt.command)
option(op)
assert.Equal(t, tt.expected, op.RebootCommands)
})
}
}
func TestWithDataDir(t *testing.T) {
tests := []struct {
name string
dataDir string
expected string
}{
{
name: "empty data dir",
dataDir: "",
expected: "",
},
{
name: "custom data dir",
dataDir: "/custom/path",
expected: "/custom/path",
},
{
name: "home directory",
dataDir: "/home/user/.gpud",
expected: "/home/user/.gpud",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
op := &Op{}
option := WithDataDir(tt.dataDir)
option(op)
assert.Equal(t, tt.expected, op.DataDir)
})
}
}
func TestWithInfinibandClassRootDir(t *testing.T) {
tests := []struct {
name string
dir string
expected string
}{
{
name: "empty dir",
dir: "",
expected: "",
},
{
name: "custom dir",
dir: "/sys/class/infiniband",
expected: "/sys/class/infiniband",
},
{
name: "alternate path",
dir: "/custom/infiniband/class",
expected: "/custom/infiniband/class",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
op := &Op{}
option := WithInfinibandClassRootDir(tt.dir)
option(op)
assert.Equal(t, tt.expected, op.InfinibandClassRootDir)
})
}
}
// TestWithSessionEndpoint tests the WithSessionEndpoint option.
// Note: SessionEndpoint is needed because the server reads the endpoint from
// the metadata DB (not from config) for session keepalive.
func TestWithSessionEndpoint(t *testing.T) {
tests := []struct {
name string
endpoint string
}{
{
name: "empty endpoint",
endpoint: "",
},
{
name: "valid endpoint",
endpoint: "https://api.example.com",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
op := &Op{}
option := WithSessionEndpoint(tt.endpoint)
option(op)
assert.Equal(t, tt.endpoint, op.SessionEndpoint)
})
}
}
func TestApplyOptsWithMultipleOptions(t *testing.T) {
op := &Op{}
opts := []OpOption{
WithDataDir("/custom/data"),
WithDBInMemory(true),
WithSessionToken("token123"),
WithSessionMachineID("machine456"),
WithSessionEndpoint("https://api.example.com"),
WithInfinibandClassRootDir("/sys/class/infiniband"),
WithExcludedInfinibandDevices([]string{"mlx5_0"}),
}
err := op.ApplyOpts(opts)
assert.NoError(t, err)
assert.Equal(t, "/custom/data", op.DataDir)
assert.True(t, op.DBInMemory)
assert.Equal(t, "token123", op.SessionToken)
assert.Equal(t, "machine456", op.SessionMachineID)
assert.Equal(t, "https://api.example.com", op.SessionEndpoint)
assert.Equal(t, "/sys/class/infiniband", op.InfinibandClassRootDir)
assert.Equal(t, []string{"mlx5_0"}, op.ExcludedInfinibandDevices)
}