1package domain_test
2
3import (
4 "testing"
5
6 "github.com/stretchr/testify/assert"
7
8 "github.com/bnema/gordon/internal/domain"
9)
10
11func TestRegistryImageKnownGordonRegistryDomains(t *testing.T) {
12 got := domain.KnownGordonRegistryDomains(" new-registry.example.com/ ", []string{
13 "",
14 " old-registry.example.com/ ",
15 "old-registry.example.com:5000///",
16 "new-registry.example.com",
17 "old-registry.example.com",
18 })
19
20 want := []string{
21 "new-registry.example.com",
22 "old-registry.example.com",
23 "old-registry.example.com:5000",
24 }
25
26 assert.Equal(t, want, got)
27}
28
29func TestRegistryImageStripKnownGordonRegistry(t *testing.T) {
30 current := "new-registry.example.com/"
31 legacy := []string{" old-registry.example.com/ ", "old-registry.example.com:5000/"}
32
33 tests := []struct {
34 name string
35 imageRef string
36 want string
37 }{
38 {name: "current host strip", imageRef: "new-registry.example.com/app:latest", want: "app:latest"},
39 {name: "legacy host strip", imageRef: "old-registry.example.com/app:latest", want: "app:latest"},
40 {name: "explicit port strip", imageRef: "old-registry.example.com:5000/app:latest", want: "app:latest"},
41 {name: "digest ref strip", imageRef: "old-registry.example.com/app@sha256:deadbeef", want: "app@sha256:deadbeef"},
42 {name: "bare image pass through", imageRef: "app:latest", want: "app:latest"},
43 {name: "external image preserved", imageRef: "docker.io/library/nginx:latest", want: "docker.io/library/nginx:latest"},
44 {name: "hostile lookalike preserved", imageRef: "old-registry.example.com.evil/app:latest", want: "old-registry.example.com.evil/app:latest"},
45 }
46
47 for _, tt := range tests {
48 t.Run(tt.name, func(t *testing.T) {
49 got := domain.StripKnownGordonRegistry(tt.imageRef, current, legacy)
50 assert.Equal(t, tt.want, got)
51 })
52 }
53}
54
55func TestRegistryImageExtractGordonRepoName(t *testing.T) {
56 current := "new-registry.example.com/"
57 legacy := []string{"old-registry.example.com/"}
58
59 tests := []struct {
60 name string
61 imageRef string
62 want string
63 }{
64 {name: "current host with tag", imageRef: "new-registry.example.com/app:latest", want: "app"},
65 {name: "legacy host with namespace", imageRef: "old-registry.example.com/org/app:v1", want: "org/app"},
66 {name: "digest ref", imageRef: "old-registry.example.com/app@sha256:deadbeef", want: "app"},
67 {name: "bare image", imageRef: "app:latest", want: "app"},
68 {name: "external registry retained in repo name", imageRef: "docker.io/library/nginx:latest", want: "docker.io/library/nginx"},
69 {name: "nested external path under gordon registry", imageRef: "old-registry.example.com/docker.io/library/nginx:latest", want: "docker.io/library/nginx"},
70 }
71
72 for _, tt := range tests {
73 t.Run(tt.name, func(t *testing.T) {
74 got := domain.ExtractGordonRepoName(tt.imageRef, current, legacy)
75 assert.Equal(t, tt.want, got)
76 })
77 }
78}
79
80func TestRegistryImageCanonicalizeGordonImageRef(t *testing.T) {
81 legacy := []string{"old-registry.example.com/", "old-registry.example.com:5000/"}
82
83 tests := []struct {
84 name string
85 imageRef string
86 current string
87 want string
88 }{
89 {name: "canonicalization", imageRef: "old-registry.example.com/app:latest", current: "new-registry.example.com/", want: "new-registry.example.com/app:latest"},
90 {name: "explicit port canonicalization", imageRef: "old-registry.example.com:5000/app:latest", current: "new-registry.example.com/", want: "new-registry.example.com/app:latest"},
91 {name: "external images are not canonicalized", imageRef: "docker.io/library/nginx:latest", current: "new-registry.example.com/", want: "docker.io/library/nginx:latest"},
92 {name: "bare refs are not canonicalized", imageRef: "app:latest", current: "new-registry.example.com/", want: "app:latest"},
93 {name: "empty current domain leaves ref unchanged", imageRef: "old-registry.example.com/app:latest", current: "", want: "old-registry.example.com/app:latest"},
94 }
95
96 for _, tt := range tests {
97 t.Run(tt.name, func(t *testing.T) {
98 got := domain.CanonicalizeGordonImageRef(tt.imageRef, tt.current, legacy)
99 assert.Equal(t, tt.want, got)
100 })
101 }
102}
103
104func TestRegistryImageIsGordonRegistryImageRef(t *testing.T) {
105 current := "new-registry.example.com/"
106 legacy := []string{"old-registry.example.com/", "old-registry.example.com:5000/"}
107
108 tests := []struct {
109 name string
110 imageRef string
111 want bool
112 }{
113 {name: "current host", imageRef: "new-registry.example.com/app:latest", want: true},
114 {name: "legacy host", imageRef: "old-registry.example.com/app:latest", want: true},
115 {name: "legacy host with explicit port", imageRef: "old-registry.example.com:5000/app:latest", want: true},
116 {name: "bare ref", imageRef: "app:latest", want: false},
117 {name: "external ref", imageRef: "docker.io/library/nginx:latest", want: false},
118 {name: "hostile lookalike", imageRef: "old-registry.example.com.evil/app:latest", want: false},
119 }
120
121 for _, tt := range tests {
122 t.Run(tt.name, func(t *testing.T) {
123 got := domain.IsGordonRegistryImageRef(tt.imageRef, current, legacy)
124 assert.Equal(t, tt.want, got)
125 })
126 }
127}