|
| 1 | +package image |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | +) |
| 6 | + |
| 7 | +func TestParseImage(t *testing.T) { |
| 8 | + tests := []struct { |
| 9 | + name string |
| 10 | + image string |
| 11 | + expectedImageName string |
| 12 | + expectedTag string |
| 13 | + expectedDigest string |
| 14 | + expectError bool |
| 15 | + }{ |
| 16 | + { |
| 17 | + name: "image with tag only", |
| 18 | + image: "nginx:1.21.0", |
| 19 | + expectedImageName: "nginx", |
| 20 | + expectedTag: "1.21.0", |
| 21 | + expectedDigest: "", |
| 22 | + expectError: false, |
| 23 | + }, |
| 24 | + { |
| 25 | + name: "image with tag and digest", |
| 26 | + image: "nginx:1.21.0@sha256:abc123def456", |
| 27 | + expectedImageName: "nginx", |
| 28 | + expectedTag: "1.21.0", |
| 29 | + expectedDigest: "sha256:abc123def456", |
| 30 | + expectError: false, |
| 31 | + }, |
| 32 | + { |
| 33 | + name: "image with latest tag", |
| 34 | + image: "ubuntu:latest", |
| 35 | + expectedImageName: "ubuntu", |
| 36 | + expectedTag: "latest", |
| 37 | + expectedDigest: "", |
| 38 | + expectError: false, |
| 39 | + }, |
| 40 | + { |
| 41 | + name: "image with version tag and digest", |
| 42 | + image: "registry.io/myapp:v2.1.3@sha256:fedcba987654", |
| 43 | + expectedImageName: "registry.io/myapp", |
| 44 | + expectedTag: "v2.1.3", |
| 45 | + expectedDigest: "sha256:fedcba987654", |
| 46 | + expectError: false, |
| 47 | + }, |
| 48 | + { |
| 49 | + name: "image without explicit tag (defaults to latest)", |
| 50 | + image: "nginx", |
| 51 | + expectedImageName: "nginx", |
| 52 | + expectedTag: "latest", |
| 53 | + expectedDigest: "", |
| 54 | + expectError: false, |
| 55 | + }, |
| 56 | + { |
| 57 | + name: "empty image string", |
| 58 | + image: "", |
| 59 | + expectedImageName: "", |
| 60 | + expectedTag: "", |
| 61 | + expectedDigest: "", |
| 62 | + expectError: true, |
| 63 | + }, |
| 64 | + { |
| 65 | + name: "image with multiple colons in name", |
| 66 | + image: "registry.io:5000/namespace/image:v1.0.0", |
| 67 | + expectedImageName: "registry.io:5000/namespace/image", |
| 68 | + expectedTag: "v1.0.0", |
| 69 | + expectedDigest: "", |
| 70 | + expectError: false, |
| 71 | + }, |
| 72 | + { |
| 73 | + name: "image with multiple colons and digest", |
| 74 | + image: "registry.io:5000/namespace/image:v1.0.0@sha256:123456789abc", |
| 75 | + expectedImageName: "registry.io:5000/namespace/image", |
| 76 | + expectedTag: "v1.0.0", |
| 77 | + expectedDigest: "sha256:123456789abc", |
| 78 | + expectError: false, |
| 79 | + }, |
| 80 | + { |
| 81 | + name: "image with digest only (no tag)", |
| 82 | + image: "nginx@sha256:abc123def456", |
| 83 | + expectedImageName: "nginx", |
| 84 | + expectedTag: "", |
| 85 | + expectedDigest: "sha256:abc123def456", |
| 86 | + expectError: false, |
| 87 | + }, |
| 88 | + { |
| 89 | + name: "complex registry with namespace and tag", |
| 90 | + image: "ghcr.io/openmcp-project/components/github.com/openmcp-project/openmcp:v0.0.11", |
| 91 | + expectedImageName: "ghcr.io/openmcp-project/components/github.com/openmcp-project/openmcp", |
| 92 | + expectedTag: "v0.0.11", |
| 93 | + expectedDigest: "", |
| 94 | + expectError: false, |
| 95 | + }, |
| 96 | + { |
| 97 | + name: "image with port and path", |
| 98 | + image: "localhost:5000/my-namespace/my-image:1.2.3", |
| 99 | + expectedImageName: "localhost:5000/my-namespace/my-image", |
| 100 | + expectedTag: "1.2.3", |
| 101 | + expectedDigest: "", |
| 102 | + expectError: false, |
| 103 | + }, |
| 104 | + { |
| 105 | + name: "image with port, path and digest", |
| 106 | + image: "localhost:5000/my-namespace/my-image:1.2.3@sha256:abcdef123456", |
| 107 | + expectedImageName: "localhost:5000/my-namespace/my-image", |
| 108 | + expectedTag: "1.2.3", |
| 109 | + expectedDigest: "sha256:abcdef123456", |
| 110 | + expectError: false, |
| 111 | + }, |
| 112 | + } |
| 113 | + |
| 114 | + for _, tt := range tests { |
| 115 | + t.Run(tt.name, func(t *testing.T) { |
| 116 | + imageName, tag, digest, err := ParseImage(tt.image) |
| 117 | + |
| 118 | + if tt.expectError { |
| 119 | + if err == nil { |
| 120 | + t.Errorf("expected error but got none") |
| 121 | + } |
| 122 | + return |
| 123 | + } |
| 124 | + |
| 125 | + if err != nil { |
| 126 | + t.Errorf("unexpected error: %v", err) |
| 127 | + return |
| 128 | + } |
| 129 | + |
| 130 | + if imageName != tt.expectedImageName { |
| 131 | + t.Errorf("expected image name %q, got %q", tt.expectedImageName, imageName) |
| 132 | + } |
| 133 | + |
| 134 | + if tag != tt.expectedTag { |
| 135 | + t.Errorf("expected tag %q, got %q", tt.expectedTag, tag) |
| 136 | + } |
| 137 | + |
| 138 | + if digest != tt.expectedDigest { |
| 139 | + t.Errorf("expected digest %q, got %q", tt.expectedDigest, digest) |
| 140 | + } |
| 141 | + }) |
| 142 | + } |
| 143 | +} |
0 commit comments