-
Notifications
You must be signed in to change notification settings - Fork 141
Expand file tree
/
Copy pathinstall-runner_test.go
More file actions
295 lines (242 loc) · 8.03 KB
/
Copy pathinstall-runner_test.go
File metadata and controls
295 lines (242 loc) · 8.03 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
package commands
import (
"strings"
"testing"
"github.com/docker/model-runner/pkg/inference/backends/llamacpp"
"github.com/docker/model-runner/pkg/inference/backends/vllm"
)
func TestInstallRunnerHostFlag(t *testing.T) {
// Create the install-runner command
cmd := newInstallRunner()
// Verify the --host flag exists
hostFlag := cmd.Flags().Lookup("host")
if hostFlag == nil {
t.Fatal("--host flag not found")
return // unreachable but satisfies staticcheck SA5011
}
// Get values to avoid potential nil dereference flagged by linter
defValue := hostFlag.DefValue
// Verify the default value
if defValue != "127.0.0.1" {
t.Errorf("Expected default host value to be '127.0.0.1', got '%s'", defValue)
}
// Verify the flag type
if hostFlag.Value.Type() != "string" {
t.Errorf("Expected host flag type to be 'string', got '%s'", hostFlag.Value.Type())
}
// Test setting the flag value
testCases := []struct {
name string
value string
}{
{"localhost", "127.0.0.1"},
{"all interfaces", "0.0.0.0"},
{"specific IP", "192.168.1.100"},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
// Reset the command for each test
cmd := newInstallRunner()
err := cmd.Flags().Set("host", tc.value)
if err != nil {
t.Errorf("Failed to set host flag to '%s': %v", tc.value, err)
}
// Verify the value was set
hostValue, err := cmd.Flags().GetString("host")
if err != nil {
t.Errorf("Failed to get host flag value: %v", err)
}
if hostValue != tc.value {
t.Errorf("Expected host value to be '%s', got '%s'", tc.value, hostValue)
}
})
}
}
func TestInstallRunnerCommandFlags(t *testing.T) {
cmd := newInstallRunner()
// Verify all expected flags exist
expectedFlags := []string{"port", "host", "gpu", "backend", "do-not-track"}
for _, flagName := range expectedFlags {
if cmd.Flags().Lookup(flagName) == nil {
t.Errorf("Expected flag '--%s' not found", flagName)
}
}
}
func TestInstallRunnerBackendFlag(t *testing.T) {
cmd := newInstallRunner()
// Verify the --backend flag exists
backendFlag := cmd.Flags().Lookup("backend")
if backendFlag == nil {
t.Fatal("--backend flag not found")
return // unreachable but satisfies staticcheck SA5011
}
// Get values to avoid potential nil dereference flagged by linter
defValue := backendFlag.DefValue
// Verify the default value
if defValue != "" {
t.Errorf("Expected default backend value to be empty, got '%s'", defValue)
}
// Verify the flag type
if backendFlag.Value.Type() != "string" {
t.Errorf("Expected backend flag type to be 'string', got '%s'", backendFlag.Value.Type())
}
// Test setting the flag to vllm
err := cmd.Flags().Set("backend", vllm.Name)
if err != nil {
t.Errorf("Failed to set backend flag: %v", err)
}
// Verify the value was set
backendValue, err := cmd.Flags().GetString("backend")
if err != nil {
t.Errorf("Failed to get backend flag value: %v", err)
}
if backendValue != vllm.Name {
t.Errorf("Expected backend value to be 'vllm', got '%s'", backendValue)
}
// Test setting the flag to llama.cpp
err = cmd.Flags().Set("backend", llamacpp.Name)
if err != nil {
t.Errorf("Failed to set backend flag to llama.cpp: %v", err)
}
backendValue, err = cmd.Flags().GetString("backend")
if err != nil {
t.Errorf("Failed to get backend flag value: %v", err)
}
if backendValue != llamacpp.Name {
t.Errorf("Expected backend value to be 'llama.cpp', got '%s'", backendValue)
}
}
func TestInstallRunnerCommandType(t *testing.T) {
cmd := newInstallRunner()
// Verify command properties
if cmd.Use != "install-runner" {
t.Errorf("Expected command Use to be 'install-runner', got '%s'", cmd.Use)
}
if cmd.Short != "Install Docker Model Runner (Docker Engine only)" {
t.Errorf("Unexpected command Short description: %s", cmd.Short)
}
// Verify RunE is set
if cmd.RunE == nil {
t.Error("Expected RunE to be set")
}
}
func TestInstallRunnerValidArgsFunction(t *testing.T) {
cmd := newInstallRunner()
// The install-runner command should not accept any arguments
// So ValidArgsFunction should be set to handle no arguments
if cmd.ValidArgsFunction == nil {
t.Error("Expected ValidArgsFunction to be set")
}
}
func TestExistingRunnerOptionsHintNoExplicitOptions(t *testing.T) {
cmd := newInstallRunner()
// Default install-runner options should not print a reinstall hint.
got := existingRunnerOptionsHint(cmd, runnerOptions{
backend: "",
gpuMode: "auto",
})
if got != "" {
t.Fatalf("expected no hint when backend/gpu flags are not explicitly changed, got %q", got)
}
}
func TestExistingRunnerOptionsHintWithBackendOnly(t *testing.T) {
cmd := newInstallRunner()
if err := cmd.Flags().Set("backend", vllm.Name); err != nil {
t.Fatal(err)
}
// A backend-only request should preserve only the explicit backend flag.
got := existingRunnerOptionsHint(cmd, runnerOptions{
backend: vllm.Name,
gpuMode: "auto",
})
if !strings.Contains(got, `docker model reinstall-runner --backend "vllm"`) {
t.Fatalf("expected backend-only reinstall hint, got %q", got)
}
if strings.Contains(got, "--gpu") {
t.Fatalf("did not expect gpu flag in backend-only hint, got %q", got)
}
}
func TestExistingRunnerOptionsHintWithCUDA(t *testing.T) {
cmd := newInstallRunner()
if err := cmd.Flags().Set("gpu", "cuda"); err != nil {
t.Fatal(err)
}
// This fakes a user explicitly requesting CUDA without requiring local GPU hardware.
got := existingRunnerOptionsHint(cmd, runnerOptions{
gpuMode: "cuda",
})
if !strings.Contains(got, `docker model reinstall-runner --gpu "cuda"`) {
t.Fatalf("expected cuda reinstall hint, got %q", got)
}
if strings.Contains(got, "--backend") {
t.Fatalf("did not expect backend flag in cuda-only hint, got %q", got)
}
}
func TestExistingRunnerOptionsHintWithBackendAndCUDA(t *testing.T) {
cmd := newInstallRunner()
if err := cmd.Flags().Set("backend", vllm.Name); err != nil {
t.Fatal(err)
}
if err := cmd.Flags().Set("gpu", "cuda"); err != nil {
t.Fatal(err)
}
// This covers the WSL2/vLLM issue path: the existing runner needs reinstall-runner.
got := existingRunnerOptionsHint(cmd, runnerOptions{
backend: vllm.Name,
gpuMode: "cuda",
})
expectedFragments := []string{
"The requested runner options were not applied",
`docker model reinstall-runner --backend "vllm" --gpu "cuda"`,
}
for _, fragment := range expectedFragments {
if !strings.Contains(got, fragment) {
t.Fatalf("expected hint to contain %q, got %q", fragment, got)
}
}
}
func TestExistingRunnerOptionsHintWithNoGPU(t *testing.T) {
cmd := newInstallRunner()
if err := cmd.Flags().Set("gpu", "none"); err != nil {
t.Fatal(err)
}
// An explicit CPU/no-GPU request should be preserved in the reinstall command.
got := existingRunnerOptionsHint(cmd, runnerOptions{
gpuMode: "none",
})
if !strings.Contains(got, `docker model reinstall-runner --gpu "none"`) {
t.Fatalf("expected no-gpu reinstall hint, got %q", got)
}
if strings.Contains(got, "--backend") {
t.Fatalf("did not expect backend flag in no-gpu hint, got %q", got)
}
}
func TestExistingRunnerOptionsHintQuotesFlagValues(t *testing.T) {
cmd := newInstallRunner()
if err := cmd.Flags().Set("gpu", "cuda; echo bad"); err != nil {
t.Fatal(err)
}
// Suggested command values should be quoted before being shown to the user.
got := existingRunnerOptionsHint(cmd, runnerOptions{
gpuMode: "cuda; echo bad",
})
if !strings.Contains(got, `docker model reinstall-runner --gpu "cuda; echo bad"`) {
t.Fatalf("expected quoted gpu reinstall hint, got %q", got)
}
if strings.Contains(got, "--gpu cuda;") {
t.Fatalf("expected gpu value to be quoted in reinstall hint, got %q", got)
}
}
func TestCommandFlagChangedDefensiveCases(t *testing.T) {
cmd := newInstallRunner()
// Missing commands and flags should be treated as unchanged.
if commandFlagChanged(nil, "gpu") {
t.Fatal("expected nil command to report unchanged flag")
}
if commandFlagChanged(cmd, "missing") {
t.Fatal("expected missing flag to report unchanged")
}
if commandFlagChanged(cmd, "gpu") {
t.Fatal("expected default gpu flag to report unchanged")
}
}