-
Notifications
You must be signed in to change notification settings - Fork 141
Expand file tree
/
Copy pathconfigure_test.go
More file actions
525 lines (461 loc) · 13.9 KB
/
Copy pathconfigure_test.go
File metadata and controls
525 lines (461 loc) · 13.9 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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
package commands
import (
"testing"
"github.com/docker/model-runner/pkg/inference"
)
func TestConfigureCmdHfOverridesFlag(t *testing.T) {
// Create the configure command
cmd := newConfigureCmd()
// Verify the --hf_overrides flag exists
hfOverridesFlag := cmd.Flags().Lookup("hf_overrides")
if hfOverridesFlag == nil {
t.Fatal("--hf_overrides flag not found")
return // unreachable but satisfies staticcheck SA5011
}
// Get values to avoid potential nil dereference flagged by linter
defValue := hfOverridesFlag.DefValue
// Verify the default value is empty
if defValue != "" {
t.Errorf("Expected default hf_overrides value to be empty, got '%s'", defValue)
}
// Verify the flag type
if hfOverridesFlag.Value.Type() != "string" {
t.Errorf("Expected hf_overrides flag type to be 'string', got '%s'", hfOverridesFlag.Value.Type())
}
}
func TestConfigureCmdContextSizeFlag(t *testing.T) {
// Create the configure command
cmd := newConfigureCmd()
// Verify the --context-size flag exists
contextSizeFlag := cmd.Flags().Lookup("context-size")
if contextSizeFlag == nil {
t.Fatal("--context-size flag not found")
return // unreachable but satisfies staticcheck SA5011
}
// Get values to avoid potential nil dereference flagged by linter
defValue := contextSizeFlag.DefValue
// Verify the default value is empty (nil pointer)
if defValue != "" {
t.Errorf("Expected default context-size value to be '' (nil), got '%s'", defValue)
}
// Test setting the flag value
err := cmd.Flags().Set("context-size", "8192")
if err != nil {
t.Errorf("Failed to set context-size flag: %v", err)
}
// Verify the value was set using String() method
contextSizeValue := contextSizeFlag.Value.String()
if contextSizeValue != "8192" {
t.Errorf("Expected context-size flag value to be '8192', got '%s'", contextSizeValue)
}
}
func TestConfigureCmdSpeculativeFlags(t *testing.T) {
cmd := newConfigureCmd()
// Test speculative-draft-model flag
draftModelFlag := cmd.Flags().Lookup("speculative-draft-model")
if draftModelFlag == nil {
t.Fatal("--speculative-draft-model flag not found")
}
// Test speculative-num-tokens flag
numTokensFlag := cmd.Flags().Lookup("speculative-num-tokens")
if numTokensFlag == nil {
t.Fatal("--speculative-num-tokens flag not found")
}
// Test speculative-min-acceptance-rate flag
minAcceptanceRateFlag := cmd.Flags().Lookup("speculative-min-acceptance-rate")
if minAcceptanceRateFlag == nil {
t.Fatal("--speculative-min-acceptance-rate flag not found")
}
}
func TestConfigureCmdModeFlag(t *testing.T) {
// Create the configure command
cmd := newConfigureCmd()
// Verify the --mode flag exists
modeFlag := cmd.Flags().Lookup("mode")
if modeFlag == nil {
t.Fatal("--mode flag not found")
return // unreachable but satisfies staticcheck SA5011
}
// Get values to avoid potential nil dereference flagged by linter
defValue := modeFlag.DefValue
// Verify the default value is empty
if defValue != "" {
t.Errorf("Expected default mode value to be empty, got '%s'", defValue)
}
// Verify the flag type
if modeFlag.Value.Type() != "string" {
t.Errorf("Expected mode flag type to be 'string', got '%s'", modeFlag.Value.Type())
}
}
func TestConfigureCmdThinkFlag(t *testing.T) {
// Create the configure command
cmd := newConfigureCmd()
// Verify the --think flag exists
thinkFlag := cmd.Flags().Lookup("think")
if thinkFlag == nil {
t.Fatal("--think flag not found")
return // unreachable but satisfies staticcheck SA5011
}
// Get values to avoid potential nil dereference flagged by linter
defValue := thinkFlag.DefValue
// Verify the default value is empty
if defValue != "" {
t.Errorf("Expected default think value to be empty (nil), got '%s'", defValue)
}
// Verify the flag type
if thinkFlag.Value.Type() != "bool" {
t.Errorf("Expected think flag type to be 'bool', got '%s'", thinkFlag.Value.Type())
}
// Test setting the flag to true
err := cmd.Flags().Set("think", "true")
if err != nil {
t.Errorf("Failed to set think flag to true: %v", err)
}
// Verify the value was set
if thinkFlag.Value.String() != "true" {
t.Errorf("Expected think flag value to be 'true', got '%s'", thinkFlag.Value.String())
}
}
func TestConfigureCmdGPUMemoryUtilizationFlag(t *testing.T) {
// Create the configure command
cmd := newConfigureCmd()
// Verify the --gpu-memory-utilization flag exists
gpuMemFlag := cmd.Flags().Lookup("gpu-memory-utilization")
if gpuMemFlag == nil {
t.Fatal("--gpu-memory-utilization flag not found")
return // unreachable but satisfies staticcheck SA5011
}
// Get values to avoid potential nil dereference flagged by linter
defValue := gpuMemFlag.DefValue
// Verify the default value is empty (nil pointer)
if defValue != "" {
t.Errorf("Expected default gpu-memory-utilization value to be '' (nil), got '%s'", defValue)
}
// Verify the flag type
if gpuMemFlag.Value.Type() != "float64" {
t.Errorf("Expected gpu-memory-utilization flag type to be 'float64', got '%s'", gpuMemFlag.Value.Type())
}
// Test setting the flag value
err := cmd.Flags().Set("gpu-memory-utilization", "0.7")
if err != nil {
t.Errorf("Failed to set gpu-memory-utilization flag: %v", err)
}
// Verify the value was set
gpuMemValue := gpuMemFlag.Value.String()
if gpuMemValue != "0.7" {
t.Errorf("Expected gpu-memory-utilization flag value to be '0.7', got '%s'", gpuMemValue)
}
}
func TestGPUMemoryUtilizationBehavior(t *testing.T) {
// Helper to create float64 pointer
float64Ptr := func(f float64) *float64 { return &f }
tests := []struct {
name string
gpuMemValue *float64
expectError bool
expectGPUMemSet bool
expectedGPUMemUtil float64
}{
{
name: "default - not set (nil)",
gpuMemValue: nil,
expectError: false,
expectGPUMemSet: false,
},
{
name: "valid value 0.5",
gpuMemValue: float64Ptr(0.5),
expectError: false,
expectGPUMemSet: true,
expectedGPUMemUtil: 0.5,
},
{
name: "edge case 0.0",
gpuMemValue: float64Ptr(0.0),
expectError: false,
expectGPUMemSet: true,
expectedGPUMemUtil: 0.0,
},
{
name: "edge case 1.0",
gpuMemValue: float64Ptr(1.0),
expectError: false,
expectGPUMemSet: true,
expectedGPUMemUtil: 1.0,
},
{
name: "invalid - negative value",
gpuMemValue: float64Ptr(-0.1),
expectError: true,
},
{
name: "invalid - value > 1.0",
gpuMemValue: float64Ptr(1.5),
expectError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
flags := ConfigureFlags{
GPUMemoryUtilization: tt.gpuMemValue,
}
req, err := flags.BuildConfigureRequest("test-model")
if tt.expectError {
if err == nil {
t.Fatal("Expected error but got none")
}
return
}
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if tt.expectGPUMemSet {
// GPU memory utilization should be set
if req.VLLM == nil || req.VLLM.GPUMemoryUtilization == nil {
t.Fatal("Expected GPU memory utilization to be set")
}
if *req.VLLM.GPUMemoryUtilization != tt.expectedGPUMemUtil {
t.Errorf("Expected GPU memory utilization to be %f, got %f", tt.expectedGPUMemUtil, *req.VLLM.GPUMemoryUtilization)
}
} else {
// GPU memory utilization should NOT be set
if req.VLLM != nil && req.VLLM.GPUMemoryUtilization != nil {
t.Errorf("Expected GPU memory utilization to be nil when not set, got %f", *req.VLLM.GPUMemoryUtilization)
}
}
})
}
}
func TestThinkFlagBehavior(t *testing.T) {
// Helper to create bool pointer
boolPtr := func(b bool) *bool { return &b }
tests := []struct {
name string
thinkValue *bool
expectBudget bool
expectedBudget int32
}{
{
name: "default - not set (nil)",
thinkValue: nil,
expectBudget: false,
},
{
name: "explicitly set to true (--think)",
thinkValue: boolPtr(true),
expectBudget: true,
expectedBudget: -1,
},
{
name: "explicitly set to false (--think=false)",
thinkValue: boolPtr(false),
expectBudget: true,
expectedBudget: 0,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
flags := ConfigureFlags{
Think: tt.thinkValue,
}
req, err := flags.BuildConfigureRequest("test-model")
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if tt.expectBudget {
// Reasoning budget should be set
if req.LlamaCpp == nil || req.LlamaCpp.ReasoningBudget == nil {
t.Fatal("Expected reasoning budget to be set")
}
if *req.LlamaCpp.ReasoningBudget != tt.expectedBudget {
t.Errorf("Expected reasoning budget to be %d, got %d", tt.expectedBudget, *req.LlamaCpp.ReasoningBudget)
}
} else {
// Reasoning budget should NOT be set
if req.LlamaCpp != nil && req.LlamaCpp.ReasoningBudget != nil {
t.Errorf("Expected reasoning budget to be nil when not set, got %d", *req.LlamaCpp.ReasoningBudget)
}
}
})
}
}
func TestConfigureCmdKeepAliveFlag(t *testing.T) {
// Create the configure command
cmd := newConfigureCmd()
// Verify the --keep-alive flag exists
keepAliveFlag := cmd.Flags().Lookup("keep-alive")
if keepAliveFlag == nil {
t.Fatal("--keep-alive flag not found")
return
}
// Verify the default value is empty
if keepAliveFlag.DefValue != "" {
t.Errorf("Expected default keep-alive value to be empty, got '%s'", keepAliveFlag.DefValue)
}
// Verify the flag type
if keepAliveFlag.Value.Type() != "string" {
t.Errorf("Expected keep-alive flag type to be 'string', got '%s'", keepAliveFlag.Value.Type())
}
// Test setting the flag value
if err := cmd.Flags().Set("keep-alive", "10m"); err != nil {
t.Errorf("Failed to set keep-alive flag: %v", err)
}
// Verify the value was set
if keepAliveFlag.Value.String() != "10m" {
t.Errorf("Expected keep-alive flag value to be '10m', got '%s'", keepAliveFlag.Value.String())
}
}
func TestKeepAliveFlagBehavior(t *testing.T) {
tests := []struct {
name string
keepAlive string
expectSet bool
expectError bool
expectedValue inference.KeepAlive
}{
{
name: "default - not set",
keepAlive: "",
expectSet: false,
},
{
name: "5 minutes",
keepAlive: "5m",
expectSet: true,
expectedValue: inference.KeepAliveDefault,
},
{
name: "unload immediately",
keepAlive: "0",
expectSet: true,
expectedValue: inference.KeepAliveImmediate,
},
{
name: "never unload",
keepAlive: "-1",
expectSet: true,
expectedValue: inference.KeepAliveForever,
},
{
name: "negative duration means forever",
keepAlive: "-1m",
expectSet: true,
expectedValue: inference.KeepAliveForever,
},
{
name: "invalid value",
keepAlive: "abc",
expectError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
flags := ConfigureFlags{
KeepAlive: tt.keepAlive,
}
req, err := flags.BuildConfigureRequest("test-model")
if tt.expectError {
if err == nil {
t.Fatal("Expected error but got none")
}
return
}
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if tt.expectSet {
if req.KeepAlive == nil {
t.Fatal("Expected KeepAlive to be set")
}
if *req.KeepAlive != tt.expectedValue {
t.Errorf("Expected KeepAlive to be %v, got %v", tt.expectedValue, *req.KeepAlive)
}
} else {
if req.KeepAlive != nil {
t.Errorf("Expected KeepAlive to be nil, got %v", *req.KeepAlive)
}
}
})
}
}
func TestRuntimeFlagsValidation(t *testing.T) {
tests := []struct {
name string
runtimeFlags []string
expectError bool
errorContains string
}{
{
name: "valid runtime flags without paths",
runtimeFlags: []string{"--verbose", "--threads", "4"},
expectError: false,
},
{
name: "empty runtime flags",
runtimeFlags: []string{},
expectError: false,
},
{
name: "reject absolute path in value",
runtimeFlags: []string{"--log-file", "/var/log/model.log"},
expectError: true,
errorContains: "paths are not allowed",
},
{
name: "reject absolute path in flag=value format",
runtimeFlags: []string{"--output-file=/tmp/output.txt"},
expectError: true,
errorContains: "paths are not allowed",
},
{
name: "reject relative path",
runtimeFlags: []string{"--config", "../config.yaml"},
expectError: true,
errorContains: "paths are not allowed",
},
{
name: "reject URL",
runtimeFlags: []string{"--endpoint", "http://example.com/api"},
expectError: true,
errorContains: "paths are not allowed",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
flags := ConfigureFlags{}
req, err := flags.BuildConfigureRequest("test-model")
if err != nil {
t.Fatalf("BuildConfigureRequest failed: %v", err)
}
// Set runtime flags after building request
req.RuntimeFlags = tt.runtimeFlags
// Note: The actual validation happens in scheduler.ConfigureRunner,
// but we're testing that the BuildConfigureRequest correctly
// preserves the RuntimeFlags for validation downstream.
// For a true integration test, we would need to mock the scheduler.
if tt.expectError {
// In this unit test context, we verify the flags are preserved
// The actual validation will happen in the scheduler
if len(req.RuntimeFlags) == 0 && len(tt.runtimeFlags) > 0 {
t.Error("RuntimeFlags should be preserved in the request")
}
} else {
if !equalStringSlices(req.RuntimeFlags, tt.runtimeFlags) {
t.Errorf("Expected RuntimeFlags %v, got %v", tt.runtimeFlags, req.RuntimeFlags)
}
}
})
}
}
// equalStringSlices checks if two string slices are equal
func equalStringSlices(a, b []string) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}