|
1 | 1 | package commands |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "strings" |
4 | 5 | "testing" |
5 | 6 |
|
6 | 7 | "github.com/docker/model-runner/pkg/inference/backends/llamacpp" |
@@ -154,3 +155,141 @@ func TestInstallRunnerValidArgsFunction(t *testing.T) { |
154 | 155 | t.Error("Expected ValidArgsFunction to be set") |
155 | 156 | } |
156 | 157 | } |
| 158 | + |
| 159 | +func TestExistingRunnerOptionsHintNoExplicitOptions(t *testing.T) { |
| 160 | + cmd := newInstallRunner() |
| 161 | + |
| 162 | + // Default install-runner options should not print a reinstall hint. |
| 163 | + got := existingRunnerOptionsHint(cmd, runnerOptions{ |
| 164 | + backend: "", |
| 165 | + gpuMode: "auto", |
| 166 | + }) |
| 167 | + |
| 168 | + if got != "" { |
| 169 | + t.Fatalf("expected no hint when backend/gpu flags are not explicitly changed, got %q", got) |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +func TestExistingRunnerOptionsHintWithBackendOnly(t *testing.T) { |
| 174 | + cmd := newInstallRunner() |
| 175 | + |
| 176 | + if err := cmd.Flags().Set("backend", vllm.Name); err != nil { |
| 177 | + t.Fatal(err) |
| 178 | + } |
| 179 | + |
| 180 | + // A backend-only request should preserve only the explicit backend flag. |
| 181 | + got := existingRunnerOptionsHint(cmd, runnerOptions{ |
| 182 | + backend: vllm.Name, |
| 183 | + gpuMode: "auto", |
| 184 | + }) |
| 185 | + |
| 186 | + if !strings.Contains(got, `docker model reinstall-runner --backend "vllm"`) { |
| 187 | + t.Fatalf("expected backend-only reinstall hint, got %q", got) |
| 188 | + } |
| 189 | + if strings.Contains(got, "--gpu") { |
| 190 | + t.Fatalf("did not expect gpu flag in backend-only hint, got %q", got) |
| 191 | + } |
| 192 | +} |
| 193 | + |
| 194 | +func TestExistingRunnerOptionsHintWithCUDA(t *testing.T) { |
| 195 | + cmd := newInstallRunner() |
| 196 | + |
| 197 | + if err := cmd.Flags().Set("gpu", "cuda"); err != nil { |
| 198 | + t.Fatal(err) |
| 199 | + } |
| 200 | + |
| 201 | + // This fakes a user explicitly requesting CUDA without requiring local GPU hardware. |
| 202 | + got := existingRunnerOptionsHint(cmd, runnerOptions{ |
| 203 | + gpuMode: "cuda", |
| 204 | + }) |
| 205 | + |
| 206 | + if !strings.Contains(got, `docker model reinstall-runner --gpu "cuda"`) { |
| 207 | + t.Fatalf("expected cuda reinstall hint, got %q", got) |
| 208 | + } |
| 209 | + if strings.Contains(got, "--backend") { |
| 210 | + t.Fatalf("did not expect backend flag in cuda-only hint, got %q", got) |
| 211 | + } |
| 212 | +} |
| 213 | + |
| 214 | +func TestExistingRunnerOptionsHintWithBackendAndCUDA(t *testing.T) { |
| 215 | + cmd := newInstallRunner() |
| 216 | + |
| 217 | + if err := cmd.Flags().Set("backend", vllm.Name); err != nil { |
| 218 | + t.Fatal(err) |
| 219 | + } |
| 220 | + if err := cmd.Flags().Set("gpu", "cuda"); err != nil { |
| 221 | + t.Fatal(err) |
| 222 | + } |
| 223 | + |
| 224 | + // This covers the WSL2/vLLM issue path: the existing runner needs reinstall-runner. |
| 225 | + got := existingRunnerOptionsHint(cmd, runnerOptions{ |
| 226 | + backend: vllm.Name, |
| 227 | + gpuMode: "cuda", |
| 228 | + }) |
| 229 | + |
| 230 | + expectedFragments := []string{ |
| 231 | + "The requested runner options were not applied", |
| 232 | + `docker model reinstall-runner --backend "vllm" --gpu "cuda"`, |
| 233 | + } |
| 234 | + |
| 235 | + for _, fragment := range expectedFragments { |
| 236 | + if !strings.Contains(got, fragment) { |
| 237 | + t.Fatalf("expected hint to contain %q, got %q", fragment, got) |
| 238 | + } |
| 239 | + } |
| 240 | +} |
| 241 | + |
| 242 | +func TestExistingRunnerOptionsHintWithNoGPU(t *testing.T) { |
| 243 | + cmd := newInstallRunner() |
| 244 | + |
| 245 | + if err := cmd.Flags().Set("gpu", "none"); err != nil { |
| 246 | + t.Fatal(err) |
| 247 | + } |
| 248 | + |
| 249 | + // An explicit CPU/no-GPU request should be preserved in the reinstall command. |
| 250 | + got := existingRunnerOptionsHint(cmd, runnerOptions{ |
| 251 | + gpuMode: "none", |
| 252 | + }) |
| 253 | + |
| 254 | + if !strings.Contains(got, `docker model reinstall-runner --gpu "none"`) { |
| 255 | + t.Fatalf("expected no-gpu reinstall hint, got %q", got) |
| 256 | + } |
| 257 | + if strings.Contains(got, "--backend") { |
| 258 | + t.Fatalf("did not expect backend flag in no-gpu hint, got %q", got) |
| 259 | + } |
| 260 | +} |
| 261 | + |
| 262 | +func TestExistingRunnerOptionsHintQuotesFlagValues(t *testing.T) { |
| 263 | + cmd := newInstallRunner() |
| 264 | + |
| 265 | + if err := cmd.Flags().Set("gpu", "cuda; echo bad"); err != nil { |
| 266 | + t.Fatal(err) |
| 267 | + } |
| 268 | + |
| 269 | + // Suggested command values should be quoted before being shown to the user. |
| 270 | + got := existingRunnerOptionsHint(cmd, runnerOptions{ |
| 271 | + gpuMode: "cuda; echo bad", |
| 272 | + }) |
| 273 | + |
| 274 | + if !strings.Contains(got, `docker model reinstall-runner --gpu "cuda; echo bad"`) { |
| 275 | + t.Fatalf("expected quoted gpu reinstall hint, got %q", got) |
| 276 | + } |
| 277 | + if strings.Contains(got, "--gpu cuda;") { |
| 278 | + t.Fatalf("expected gpu value to be quoted in reinstall hint, got %q", got) |
| 279 | + } |
| 280 | +} |
| 281 | + |
| 282 | +func TestCommandFlagChangedDefensiveCases(t *testing.T) { |
| 283 | + cmd := newInstallRunner() |
| 284 | + |
| 285 | + // Missing commands and flags should be treated as unchanged. |
| 286 | + if commandFlagChanged(nil, "gpu") { |
| 287 | + t.Fatal("expected nil command to report unchanged flag") |
| 288 | + } |
| 289 | + if commandFlagChanged(cmd, "missing") { |
| 290 | + t.Fatal("expected missing flag to report unchanged") |
| 291 | + } |
| 292 | + if commandFlagChanged(cmd, "gpu") { |
| 293 | + t.Fatal("expected default gpu flag to report unchanged") |
| 294 | + } |
| 295 | +} |
0 commit comments