@@ -7,8 +7,11 @@ import (
7
7
"testing"
8
8
"time"
9
9
10
+ "github.com/argoproj-labs/argocd-image-updater/registry-scanner/pkg/image"
11
+
10
12
"github.com/stretchr/testify/assert"
11
13
"github.com/stretchr/testify/require"
14
+
12
15
)
13
16
14
17
func TestInferRegistryEndpointFromPrefix (t * testing.T ) {
@@ -116,21 +119,21 @@ func Test_GetEndpoints(t *testing.T) {
116
119
RestoreDefaultRegistryConfiguration ()
117
120
118
121
t .Run ("Get default endpoint" , func (t * testing.T ) {
119
- ep , err := GetRegistryEndpoint ("" )
122
+ ep , err := GetRegistryEndpoint (& image. ContainerImage { RegistryURL : "" } )
120
123
require .NoError (t , err )
121
124
require .NotNil (t , ep )
122
125
assert .Equal (t , "docker.io" , ep .RegistryPrefix )
123
126
})
124
127
125
128
t .Run ("Get GCR endpoint" , func (t * testing.T ) {
126
- ep , err := GetRegistryEndpoint ("gcr.io" )
129
+ ep , err := GetRegistryEndpoint (& image. ContainerImage { RegistryURL : "gcr.io" } )
127
130
require .NoError (t , err )
128
131
require .NotNil (t , ep )
129
132
assert .Equal (t , ep .RegistryPrefix , "gcr.io" )
130
133
})
131
134
132
135
t .Run ("Infer endpoint" , func (t * testing.T ) {
133
- ep , err := GetRegistryEndpoint ("foobar.com" )
136
+ ep , err := GetRegistryEndpoint (& image. ContainerImage { RegistryURL : "foobar.com" } )
134
137
require .NoError (t , err )
135
138
require .NotNil (t , ep )
136
139
assert .Equal (t , "foobar.com" , ep .RegistryPrefix )
@@ -146,7 +149,7 @@ func Test_AddEndpoint(t *testing.T) {
146
149
require .NoError (t , err )
147
150
})
148
151
t .Run ("Get example.com endpoint" , func (t * testing.T ) {
149
- ep , err := GetRegistryEndpoint ("example.com" )
152
+ ep , err := GetRegistryEndpoint (& image. ContainerImage { RegistryURL : "example.com" } )
150
153
require .NoError (t , err )
151
154
require .NotNil (t , ep )
152
155
assert .Equal (t , ep .RegistryPrefix , "example.com" )
@@ -159,7 +162,7 @@ func Test_AddEndpoint(t *testing.T) {
159
162
t .Run ("Change existing endpoint" , func (t * testing.T ) {
160
163
err := AddRegistryEndpoint (NewRegistryEndpoint ("example.com" , "Example" , "https://example.com" , "" , "library" , true , TagListSortLatestFirst , 5 , 0 ))
161
164
require .NoError (t , err )
162
- ep , err := GetRegistryEndpoint ("example.com" )
165
+ ep , err := GetRegistryEndpoint (& image. ContainerImage { RegistryURL : "example.com" } )
163
166
require .NoError (t , err )
164
167
require .NotNil (t , ep )
165
168
assert .Equal (t , ep .Insecure , true )
@@ -174,7 +177,7 @@ func Test_SetEndpointCredentials(t *testing.T) {
174
177
t .Run ("Set credentials on default registry" , func (t * testing.T ) {
175
178
err := SetRegistryEndpointCredentials ("" , "env:FOOBAR" )
176
179
require .NoError (t , err )
177
- ep , err := GetRegistryEndpoint ("" )
180
+ ep , err := GetRegistryEndpoint (& image. ContainerImage { RegistryURL : "" } )
178
181
require .NoError (t , err )
179
182
require .NotNil (t , ep )
180
183
assert .Equal (t , ep .Credentials , "env:FOOBAR" )
@@ -183,13 +186,31 @@ func Test_SetEndpointCredentials(t *testing.T) {
183
186
t .Run ("Unset credentials on default registry" , func (t * testing.T ) {
184
187
err := SetRegistryEndpointCredentials ("" , "" )
185
188
require .NoError (t , err )
186
- ep , err := GetRegistryEndpoint ("" )
189
+ ep , err := GetRegistryEndpoint (& image. ContainerImage { RegistryURL : "" } )
187
190
require .NoError (t , err )
188
191
require .NotNil (t , ep )
189
192
assert .Equal (t , ep .Credentials , "" )
190
193
})
191
194
}
192
195
196
+ func Test_SelectRegistryBasedOnMaxPrefixContains (t * testing.T ) {
197
+ RestoreDefaultRegistryConfiguration ()
198
+
199
+ t .Run ("Set credentials on default registry" , func (t * testing.T ) {
200
+ err := SetRegistryEndpointCredentials ("foo.bar/prefix1" , "env:FOOBAR_1" )
201
+ require .NoError (t , err )
202
+ err = SetRegistryEndpointCredentials ("foo.bar/prefix2" , "env:FOOBAR_2" )
203
+ require .NoError (t , err )
204
+ err = SetRegistryEndpointCredentials ("foo.bar/prefix1/sub-prefix" , "env:FOOBAR_SUB_1" )
205
+ require .NoError (t , err )
206
+
207
+ ep , err := GetRegistryEndpoint (& image.ContainerImage {RegistryURL : "foo.bar" , ImageName : "prefix1/sub-prefix/image" })
208
+ require .NoError (t , err )
209
+ require .NotNil (t , ep )
210
+ assert .Equal (t , ep .Credentials , "env:FOOBAR_SUB_1" )
211
+ })
212
+ }
213
+
193
214
func Test_EndpointConcurrentAccess (t * testing.T ) {
194
215
RestoreDefaultRegistryConfiguration ()
195
216
const numRuns = 50
@@ -199,7 +220,7 @@ func Test_EndpointConcurrentAccess(t *testing.T) {
199
220
wg .Add (numRuns )
200
221
for i := 0 ; i < numRuns ; i ++ {
201
222
go func () {
202
- ep , err := GetRegistryEndpoint ("gcr.io" )
223
+ ep , err := GetRegistryEndpoint (& image. ContainerImage { RegistryURL : "gcr.io" } )
203
224
require .NoError (t , err )
204
225
require .NotNil (t , ep )
205
226
wg .Done ()
@@ -217,7 +238,7 @@ func Test_EndpointConcurrentAccess(t *testing.T) {
217
238
creds := fmt .Sprintf ("secret:foo/secret-%d" , i )
218
239
err := SetRegistryEndpointCredentials ("" , creds )
219
240
require .NoError (t , err )
220
- ep , err := GetRegistryEndpoint ("" )
241
+ ep , err := GetRegistryEndpoint (& image. ContainerImage { RegistryURL : "" } )
221
242
require .NoError (t , err )
222
243
require .NotNil (t , ep )
223
244
wg .Done ()
@@ -235,7 +256,7 @@ func Test_SetDefault(t *testing.T) {
235
256
assert .Equal (t , "docker.io" , dep .RegistryPrefix )
236
257
assert .True (t , dep .IsDefault )
237
258
238
- ep , err := GetRegistryEndpoint ("ghcr.io" )
259
+ ep , err := GetRegistryEndpoint (& image. ContainerImage { RegistryURL : "ghcr.io" } )
239
260
require .NoError (t , err )
240
261
require .NotNil (t , ep )
241
262
require .False (t , ep .IsDefault )
@@ -249,7 +270,7 @@ func Test_SetDefault(t *testing.T) {
249
270
250
271
func Test_DeepCopy (t * testing.T ) {
251
272
t .Run ("DeepCopy endpoint object" , func (t * testing.T ) {
252
- ep , err := GetRegistryEndpoint ("docker.pkg.github.com" )
273
+ ep , err := GetRegistryEndpoint (& image. ContainerImage { RegistryURL : "docker.pkg.github.com" } )
253
274
require .NoError (t , err )
254
275
require .NotNil (t , ep )
255
276
newEp := ep .DeepCopy ()
0 commit comments