|
| 1 | +package image |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | +) |
| 6 | + |
| 7 | +func TestExtractDigest(t *testing.T) { |
| 8 | + tests := []struct { |
| 9 | + name string |
| 10 | + imageID string |
| 11 | + expected string |
| 12 | + }{ |
| 13 | + { |
| 14 | + name: "empty string", |
| 15 | + imageID: "", |
| 16 | + expected: "", |
| 17 | + }, |
| 18 | + { |
| 19 | + name: "docker-pullable format", |
| 20 | + imageID: "docker-pullable://nginx@sha256:abc123def456", |
| 21 | + expected: "sha256:abc123def456", |
| 22 | + }, |
| 23 | + { |
| 24 | + name: "docker format", |
| 25 | + imageID: "docker://sha256:abc123def456789", |
| 26 | + expected: "sha256:abc123def456789", |
| 27 | + }, |
| 28 | + { |
| 29 | + name: "just sha256 digest", |
| 30 | + imageID: "sha256:0123456789abcdef", |
| 31 | + expected: "sha256:0123456789abcdef", |
| 32 | + }, |
| 33 | + { |
| 34 | + name: "full gcr image with digest", |
| 35 | + imageID: "docker-pullable://gcr.io/my-project/my-image@sha256:fedcba9876543210", |
| 36 | + expected: "sha256:fedcba9876543210", |
| 37 | + }, |
| 38 | + { |
| 39 | + name: "registry with port and digest", |
| 40 | + imageID: "docker-pullable://localhost:5000/myapp@sha256:1234567890abcdef", |
| 41 | + expected: "sha256:1234567890abcdef", |
| 42 | + }, |
| 43 | + { |
| 44 | + name: "no sha256 prefix returns original", |
| 45 | + imageID: "some-random-id-without-sha", |
| 46 | + expected: "some-random-id-without-sha", |
| 47 | + }, |
| 48 | + { |
| 49 | + name: "digest with trailing space", |
| 50 | + imageID: "docker://sha256:abc123 extra", |
| 51 | + expected: "sha256:abc123", |
| 52 | + }, |
| 53 | + { |
| 54 | + name: "digest with trailing @", |
| 55 | + imageID: "sha256:abc123@extra", |
| 56 | + expected: "sha256:abc123", |
| 57 | + }, |
| 58 | + { |
| 59 | + name: "real world kubernetes imageID", |
| 60 | + imageID: "docker-pullable://ghcr.io/github/deployment-tracker@sha256:a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2", |
| 61 | + expected: "sha256:a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2", |
| 62 | + }, |
| 63 | + { |
| 64 | + name: "containerd format", |
| 65 | + imageID: "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", |
| 66 | + expected: "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", |
| 67 | + }, |
| 68 | + } |
| 69 | + |
| 70 | + for _, tt := range tests { |
| 71 | + t.Run(tt.name, func(t *testing.T) { |
| 72 | + result := ExtractDigest(tt.imageID) |
| 73 | + if result != tt.expected { |
| 74 | + t.Errorf("ExtractDigest(%q) = %q, want %q", tt.imageID, result, tt.expected) |
| 75 | + } |
| 76 | + }) |
| 77 | + } |
| 78 | +} |
0 commit comments