Skip to content

Commit bc60b07

Browse files
committed
Registries proxy config: add tests
Signed-off-by: cyqsimon <28627918+cyqsimon@users.noreply.github.com>
1 parent 4278ca8 commit bc60b07

3 files changed

Lines changed: 109 additions & 0 deletions

File tree

image/docker/docker_client_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,3 +445,49 @@ func TestIsManifestUnknownError(t *testing.T) {
445445
assert.True(t, res, "%s: %#v", c.name, err)
446446
}
447447
}
448+
449+
// Helper function to test that the selected proxy for a registry matches expected.
450+
func testProxyForRegistry(t *testing.T, ctx context.Context, sys *types.SystemContext, registry string, expectedProxy string) {
451+
t.Run(fmt.Sprintf(`Expecting proxy "%s" for registry "%s"`, expectedProxy, registry), func(t *testing.T) {
452+
req, err := http.NewRequest("GET", fmt.Sprintf("http://%s/v2/", registry), nil)
453+
require.NoError(t, err)
454+
455+
// Proxy configured using environment variables have priority, so we skip if it's set.
456+
envProxy, _ := http.ProxyFromEnvironment(req)
457+
if envProxy != nil {
458+
t.Skip("Skipping registry proxy test: proxy configured using environment variables")
459+
}
460+
461+
client, err := newDockerClient(sys, registry, registry)
462+
require.NoError(t, err)
463+
464+
// Ping will fail, but we only care about the side effect of setting the proxy.
465+
_ = client.detectProperties(ctx)
466+
467+
transport, ok := client.client.Transport.(*http.Transport)
468+
require.True(t, ok)
469+
require.NotNil(t, transport.Proxy)
470+
471+
proxyURL, err := transport.Proxy(req)
472+
require.NoError(t, err)
473+
474+
if expectedProxy == "" {
475+
require.Nil(t, proxyURL)
476+
} else {
477+
require.NotNil(t, proxyURL)
478+
assert.Equal(t, expectedProxy, proxyURL.String())
479+
}
480+
})
481+
}
482+
483+
func TestRegistrySpecificProxy(t *testing.T) {
484+
ctx := context.Background()
485+
sys := &types.SystemContext{
486+
SystemRegistriesConfPath: "../pkg/sysregistriesv2/testdata/proxy.conf",
487+
SystemRegistriesConfDirPath: "../pkg/sysregistriesv2/testdata/this-does-not-exist",
488+
DockerInsecureSkipTLSVerify: types.OptionalBoolTrue,
489+
}
490+
491+
testProxyForRegistry(t, ctx, sys, "registry-1.com", "")
492+
testProxyForRegistry(t, ctx, sys, "registry-2.com", "https://proxy-2.example.com")
493+
}

image/pkg/sysregistriesv2/system_registries_v2_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,28 @@ func TestParseLocation(t *testing.T) {
107107
assert.Equal(t, "example.com:5000/with/path", location)
108108
}
109109

110+
func TestParseProxy(t *testing.T) {
111+
for _, valid := range []string{
112+
"",
113+
"http://proxy.example.com",
114+
"https://proxy.example.com",
115+
"socks5://proxy.example.com",
116+
"socks5h://proxy.example.com:1080",
117+
} {
118+
_, err := ParseProxy(valid)
119+
assert.Nil(t, err, valid)
120+
}
121+
122+
for _, invalid := range []string{
123+
"no-scheme.example.com",
124+
"ftp://bad-scheme.example.com",
125+
"ssh://bad-scheme.example.com:2222",
126+
} {
127+
_, err := ParseProxy(invalid)
128+
assert.NotNil(t, err)
129+
}
130+
}
131+
110132
func TestEmptyConfig(t *testing.T) {
111133
registries, err := GetRegistries(&types.SystemContext{
112134
SystemRegistriesConfPath: "testdata/empty.conf",
@@ -983,3 +1005,31 @@ func TestCredentialHelpers(t *testing.T) {
9831005
require.Equal(t, test.helpers, helpers, "%v", test)
9841006
}
9851007
}
1008+
1009+
func TestProxyConfiguration(t *testing.T) {
1010+
sys := &types.SystemContext{
1011+
SystemRegistriesConfPath: "testdata/proxy.conf",
1012+
SystemRegistriesConfDirPath: "testdata/this-does-not-exist",
1013+
}
1014+
1015+
registries, err := GetRegistries(sys)
1016+
require.NoError(t, err)
1017+
require.Equal(t, 2, len(registries))
1018+
1019+
reg1 := registries[0]
1020+
assert.Equal(t, "registry-1.com", reg1.Location)
1021+
assert.Equal(t, "", reg1.Proxy)
1022+
require.Equal(t, 2, len(reg1.Mirrors))
1023+
1024+
mirror1 := reg1.Mirrors[0]
1025+
assert.Equal(t, "mirror-1.registry-1.com", mirror1.Location)
1026+
assert.Equal(t, "", mirror1.Proxy)
1027+
1028+
mirror2 := reg1.Mirrors[1]
1029+
assert.Equal(t, "mirror-2.registry-1.com", mirror2.Location)
1030+
assert.Equal(t, "http://proxy-1.example.com", mirror2.Proxy)
1031+
1032+
reg2 := registries[1]
1033+
assert.Equal(t, "registry-2.com", reg2.Location)
1034+
assert.Equal(t, "https://proxy-2.example.com", reg2.Proxy)
1035+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[[registry]]
2+
location = "registry-1.com"
3+
4+
[[registry.mirror]]
5+
location = "mirror-1.registry-1.com"
6+
7+
[[registry.mirror]]
8+
location = "mirror-2.registry-1.com"
9+
proxy = "http://proxy-1.example.com"
10+
11+
[[registry]]
12+
location = "registry-2.com"
13+
proxy = "https://proxy-2.example.com"

0 commit comments

Comments
 (0)