|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/VirtualMetric/PipeBench/internal/config" |
| 7 | +) |
| 8 | + |
| 9 | +// TestApplyCasePin verifies that a case-level subject pin is applied with the |
| 10 | +// correct precedence: |
| 11 | +// |
| 12 | +// CLI --image/--version > case subject_image/subject_version > registry default |
| 13 | +func TestApplyCasePin(t *testing.T) { |
| 14 | + t.Run("pin overrides registry default", func(t *testing.T) { |
| 15 | + base := config.Subject{Image: "vmetric/director", Version: "2.0.3"} |
| 16 | + tc := &config.TestCase{ |
| 17 | + SubjectImage: "vmetric/director-enterprise", |
| 18 | + SubjectVersion: "latest", |
| 19 | + } |
| 20 | + got := applyCasePin(base, tc) |
| 21 | + if got.Image != "vmetric/director-enterprise" { |
| 22 | + t.Errorf("image: got %q, want %q", got.Image, "vmetric/director-enterprise") |
| 23 | + } |
| 24 | + if got.Version != "latest" { |
| 25 | + t.Errorf("version: got %q, want %q", got.Version, "latest") |
| 26 | + } |
| 27 | + }) |
| 28 | + |
| 29 | + t.Run("empty pin leaves subject unchanged", func(t *testing.T) { |
| 30 | + base := config.Subject{Image: "vmetric/director", Version: "2.0.3"} |
| 31 | + tc := &config.TestCase{} |
| 32 | + got := applyCasePin(base, tc) |
| 33 | + if got.Image != "vmetric/director" { |
| 34 | + t.Errorf("image: got %q, want %q", got.Image, "vmetric/director") |
| 35 | + } |
| 36 | + if got.Version != "2.0.3" { |
| 37 | + t.Errorf("version: got %q, want %q", got.Version, "2.0.3") |
| 38 | + } |
| 39 | + }) |
| 40 | + |
| 41 | + t.Run("CLI WithImage/WithVersion overrides pin (precedence)", func(t *testing.T) { |
| 42 | + // Simulate: registry default → applyCasePin (enterprise pin) → |
| 43 | + // Subject.WithImage/WithVersion (CLI flags, applied by applySubjectOverrides). |
| 44 | + base := config.Subject{Image: "vmetric/director", Version: "2.0.3"} |
| 45 | + tc := &config.TestCase{ |
| 46 | + SubjectImage: "vmetric/director-enterprise", |
| 47 | + SubjectVersion: "latest", |
| 48 | + } |
| 49 | + pinned := applyCasePin(base, tc) |
| 50 | + |
| 51 | + // CLI --image=vmetric/director overrides the enterprise pin. |
| 52 | + final := pinned.WithImage("vmetric/director").WithVersion("3.0.0") |
| 53 | + if final.Image != "vmetric/director" { |
| 54 | + t.Errorf("image: got %q, want %q (CLI must win)", final.Image, "vmetric/director") |
| 55 | + } |
| 56 | + if final.Version != "3.0.0" { |
| 57 | + t.Errorf("version: got %q, want %q (CLI must win)", final.Version, "3.0.0") |
| 58 | + } |
| 59 | + // Pin must not mutate the original. |
| 60 | + if pinned.Image != "vmetric/director-enterprise" { |
| 61 | + t.Errorf("pin must not mutate: got %q after WithImage call", pinned.Image) |
| 62 | + } |
| 63 | + }) |
| 64 | +} |
0 commit comments