|
1 | 1 | package platform_test |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "fmt" |
4 | 5 | "path/filepath" |
5 | 6 | "testing" |
6 | 7 |
|
@@ -277,4 +278,119 @@ func testRunImage(t *testing.T, when spec.G, it spec.S) { |
277 | 278 | }) |
278 | 279 | }) |
279 | 280 | }) |
| 281 | + |
| 282 | + when(".ResolveRunImageFromAnalyzed", func() { |
| 283 | + when("Platform API 0.14+", func() { |
| 284 | + it("should use already-selected mirror from analyzed.toml", func() { |
| 285 | + // Reproduces issue #1590 |
| 286 | + // The restorer should validate the already-selected mirror from analyzed.toml |
| 287 | + // without retrying the primary image first |
| 288 | + // |
| 289 | + // The scenario: |
| 290 | + // 1. Analyzer already selected "localhost:5000/pack-test/run" (accessible mirror) |
| 291 | + // 2. Wrote it to analyzed.toml as run-image.image |
| 292 | + // 3. Restorer reads this value: runImageName = "localhost:5000/pack-test/run" |
| 293 | + // 4. Restorer has run.toml with primary + mirrors |
| 294 | + // 5. Restorer should validate the already-selected mirror |
| 295 | + |
| 296 | + // STEP 1: The run image name from analyzed.toml (already selected by analyzer) |
| 297 | + runImageNameFromAnalyzed := "localhost:5000/pack-test/run" |
| 298 | + |
| 299 | + // STEP 2: The run.toml with full entry (primary + mirrors) |
| 300 | + runToml := files.Run{ |
| 301 | + Images: []files.RunImageForExport{ |
| 302 | + { |
| 303 | + Image: "pack-test/run", // Primary - INACCESSIBLE |
| 304 | + Mirrors: []string{"localhost:5000/pack-test/run"}, // Mirror - accessible |
| 305 | + }, |
| 306 | + }, |
| 307 | + } |
| 308 | + |
| 309 | + // STEP 3: Access checker - simulates the real scenario |
| 310 | + callCount := make(map[string]int) |
| 311 | + checkAccess := func(image string, _ authn.Keychain) (bool, error) { |
| 312 | + callCount[image]++ |
| 313 | + if image == "pack-test/run" { |
| 314 | + // Primary is NOT accessible (401 error) |
| 315 | + return false, fmt.Errorf("failed to get remote image: unauthorized") |
| 316 | + } |
| 317 | + if image == "localhost:5000/pack-test/run" { |
| 318 | + // Mirror IS accessible |
| 319 | + return true, nil |
| 320 | + } |
| 321 | + return false, fmt.Errorf("unknown image: %s", image) |
| 322 | + } |
| 323 | + |
| 324 | + // STEP 4: Call the function (simulates what restorer does) |
| 325 | + result, err := platform.ResolveRunImageFromAnalyzed(runImageNameFromAnalyzed, runToml, checkAccess) |
| 326 | + |
| 327 | + // EXPECTATIONS: |
| 328 | + // 1. Should succeed |
| 329 | + h.AssertNil(t, err) |
| 330 | + h.AssertEq(t, result, "localhost:5000/pack-test/run") |
| 331 | + |
| 332 | + // 2. CRITICAL: Should NOT have checked the primary image! |
| 333 | + // Since we already know the mirror was selected, we shouldn't retry primary |
| 334 | + if callCount["pack-test/run"] > 0 { |
| 335 | + t.Fatalf("FAIL: Should not check primary 'pack-test/run' when mirror was already selected. Call count: %v", callCount) |
| 336 | + } |
| 337 | + |
| 338 | + // 3. Should have checked the selected mirror |
| 339 | + h.AssertEq(t, callCount["localhost:5000/pack-test/run"], 1) |
| 340 | + }) |
| 341 | + |
| 342 | + it("returns the run image as-is when not in run.toml", func() { |
| 343 | + // If the run image from analyzed.toml is not found in run.toml |
| 344 | + // (e.g., extensions switched the run image), just return it |
| 345 | + runImageNameFromAnalyzed := "some-extension-switched-image:latest" |
| 346 | + |
| 347 | + runToml := files.Run{ |
| 348 | + Images: []files.RunImageForExport{ |
| 349 | + { |
| 350 | + Image: "pack-test/run", |
| 351 | + Mirrors: []string{"localhost:5000/pack-test/run"}, |
| 352 | + }, |
| 353 | + }, |
| 354 | + } |
| 355 | + |
| 356 | + checkAccess := func(_ string, _ authn.Keychain) (bool, error) { |
| 357 | + return true, nil |
| 358 | + } |
| 359 | + |
| 360 | + result, err := platform.ResolveRunImageFromAnalyzed(runImageNameFromAnalyzed, runToml, checkAccess) |
| 361 | + |
| 362 | + h.AssertNil(t, err) |
| 363 | + h.AssertEq(t, result, "some-extension-switched-image:latest") |
| 364 | + }) |
| 365 | + |
| 366 | + it("fails with helpful message when selected mirror is not accessible", func() { |
| 367 | + // If the environment changed between analyzer and restorer phases, |
| 368 | + // and the previously-selected mirror is no longer accessible, |
| 369 | + // we should fail with a clear error message that includes the image name |
| 370 | + runImageNameFromAnalyzed := "localhost:5000/pack-test/run" |
| 371 | + |
| 372 | + runToml := files.Run{ |
| 373 | + Images: []files.RunImageForExport{ |
| 374 | + { |
| 375 | + Image: "pack-test/run", |
| 376 | + Mirrors: []string{"localhost:5000/pack-test/run"}, |
| 377 | + }, |
| 378 | + }, |
| 379 | + } |
| 380 | + |
| 381 | + checkAccess := func(_ string, _ authn.Keychain) (bool, error) { |
| 382 | + // Mirror is no longer accessible |
| 383 | + return false, fmt.Errorf("failed to get remote image: connection refused") |
| 384 | + } |
| 385 | + |
| 386 | + _, err := platform.ResolveRunImageFromAnalyzed(runImageNameFromAnalyzed, runToml, checkAccess) |
| 387 | + |
| 388 | + // Should fail with clear message including the image name |
| 389 | + h.AssertNotNil(t, err) |
| 390 | + h.AssertStringContains(t, err.Error(), "selected run image") |
| 391 | + h.AssertStringContains(t, err.Error(), "localhost:5000/pack-test/run") |
| 392 | + h.AssertStringContains(t, err.Error(), "not accessible") |
| 393 | + }) |
| 394 | + }) |
| 395 | + }) |
280 | 396 | } |
0 commit comments