|
| 1 | +package registry |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/sha256" |
| 5 | + "fmt" |
| 6 | + "testing" |
| 7 | + |
| 8 | + . "github.com/onsi/gomega" |
| 9 | +) |
| 10 | + |
| 11 | +func sha224(str string) string { |
| 12 | + return fmt.Sprintf("%x", sha256.Sum224([]byte(str))) |
| 13 | +} |
| 14 | + |
| 15 | +func TestSanitizeName(t *testing.T) { |
| 16 | + tests := []struct { |
| 17 | + name string |
| 18 | + image string |
| 19 | + expectedSanitizedImage string |
| 20 | + }{ |
| 21 | + { |
| 22 | + name: "Basic", |
| 23 | + image: "docker.io/library/alpine", |
| 24 | + expectedSanitizedImage: "docker.io-library-alpine", |
| 25 | + }, |
| 26 | + { |
| 27 | + name: "Advanced", |
| 28 | + image: "some-gitlab-registry.com:5000/group/another-group/project/backend:v1.0.0", |
| 29 | + expectedSanitizedImage: "some-gitlab-registry.com-5000-group-another-group-project-backend-v1.0.0", |
| 30 | + }, |
| 31 | + } |
| 32 | + |
| 33 | + g := NewWithT(t) |
| 34 | + for _, tt := range tests { |
| 35 | + t.Run(tt.name, func(t *testing.T) { |
| 36 | + label := SanitizeName(tt.image) |
| 37 | + g.Expect(label).To(Equal(tt.expectedSanitizedImage)) |
| 38 | + }) |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +func TestRepositoryLabel(t *testing.T) { |
| 43 | + tests := []struct { |
| 44 | + name string |
| 45 | + repositoryName string |
| 46 | + expectedLabel string |
| 47 | + }{ |
| 48 | + { |
| 49 | + name: "Basic", |
| 50 | + repositoryName: "docker.io/library/alpine", |
| 51 | + expectedLabel: "docker.io-library-alpine", |
| 52 | + }, |
| 53 | + { |
| 54 | + name: "Long name", |
| 55 | + repositoryName: "docker.io/rancher/mirrored-prometheus-operator-prometheus-operator", |
| 56 | + expectedLabel: sha224("docker.io-rancher-mirrored-prometheus-operator-prometheus-operator"), |
| 57 | + }, |
| 58 | + { |
| 59 | + name: "63 chars", |
| 60 | + repositoryName: "docker.io/rancher/mirrored-prometheus-operator-prometheus-opera", |
| 61 | + expectedLabel: "docker.io-rancher-mirrored-prometheus-operator-prometheus-opera", |
| 62 | + }, |
| 63 | + { |
| 64 | + name: "64 chars", |
| 65 | + repositoryName: "docker.io/rancher/mirrored-prometheus-operator-prometheus-operat", |
| 66 | + expectedLabel: sha224("docker.io-rancher-mirrored-prometheus-operator-prometheus-operat"), |
| 67 | + }, |
| 68 | + } |
| 69 | + |
| 70 | + g := NewWithT(t) |
| 71 | + for _, tt := range tests { |
| 72 | + t.Run(tt.name, func(t *testing.T) { |
| 73 | + label := RepositoryLabel(tt.repositoryName) |
| 74 | + g.Expect(label).To(Equal(tt.expectedLabel)) |
| 75 | + }) |
| 76 | + } |
| 77 | +} |
0 commit comments