|
5 | 5 | "path"
|
6 | 6 | "testing"
|
7 | 7 |
|
| 8 | + "github.com/containerd/containerd/v2/core/remotes/docker" |
8 | 9 | "github.com/moby/buildkit/cmd/buildkitd/config"
|
9 | 10 | "github.com/stretchr/testify/require"
|
10 | 11 | )
|
@@ -66,3 +67,67 @@ mirrors = ["https://url/", "https://url/path/"]
|
66 | 67 | }
|
67 | 68 | }
|
68 | 69 | }
|
| 70 | + |
| 71 | +func TestNewRegistryConfig(t *testing.T) { |
| 72 | + const testConfig = ` |
| 73 | +[registry."docker.io"] |
| 74 | +mirrors = ["yourmirror.local", "proxy.local:5000/proxy.docker.io"] |
| 75 | +
|
| 76 | +[registry."yourmirror.local"] |
| 77 | +http = true |
| 78 | +
|
| 79 | +[registry."proxy.local:5000"] |
| 80 | +capabilities = ["pull", "resolve", "push"] |
| 81 | +` |
| 82 | + |
| 83 | + tests := map[string]struct { |
| 84 | + description string |
| 85 | + http bool |
| 86 | + capabilities docker.HostCapabilities |
| 87 | + mirrors []string |
| 88 | + scheme string |
| 89 | + path string |
| 90 | + }{ |
| 91 | + "registry-1.docker.io": { |
| 92 | + description: "valid_docker_io_with_mirrors", |
| 93 | + mirrors: []string{"yourmirror.local", "proxy.local:5000", "registry-1.docker.io"}, |
| 94 | + scheme: "https", |
| 95 | + path: defaultPath, |
| 96 | + capabilities: docker.HostCapabilityPull | docker.HostCapabilityResolve | docker.HostCapabilityPush, |
| 97 | + }, |
| 98 | + "yourmirror.local": { |
| 99 | + description: "http_mirror", |
| 100 | + scheme: "http", |
| 101 | + path: defaultPath, |
| 102 | + capabilities: docker.HostCapabilityPull | docker.HostCapabilityResolve, |
| 103 | + }, |
| 104 | + "proxy.local:5000": { |
| 105 | + description: "proxy_push_mirror", |
| 106 | + scheme: "https", |
| 107 | + path: path.Join(defaultPath, "proxy.docker.io"), |
| 108 | + capabilities: docker.HostCapabilityPull | docker.HostCapabilityResolve | docker.HostCapabilityPush, |
| 109 | + }, |
| 110 | + } |
| 111 | + |
| 112 | + cfg, err := config.Load(bytes.NewBuffer([]byte(testConfig))) |
| 113 | + require.NoError(t, err) |
| 114 | + |
| 115 | + require.NotEqual(t, 0, len(cfg.Registries)) |
| 116 | + registryHosts := NewRegistryConfig(cfg.Registries) |
| 117 | + require.NotNil(t, registryHosts) |
| 118 | + hosts, err := registryHosts("docker.io") |
| 119 | + require.NoError(t, err) |
| 120 | + hostnames := make([]string, 0, len(hosts)) |
| 121 | + for _, host := range hosts { |
| 122 | + test := tests[host.Host] |
| 123 | + desc := test.description |
| 124 | + t.Run(desc, func(t *testing.T) { |
| 125 | + hostnames = append(hostnames, host.Host) |
| 126 | + require.NotNil(t, test, "unexpected registry host: %s", host.Host) |
| 127 | + require.Equal(t, test.capabilities, host.Capabilities) |
| 128 | + require.Equal(t, test.scheme, host.Scheme) |
| 129 | + require.Equal(t, test.path, host.Path) |
| 130 | + }) |
| 131 | + } |
| 132 | + require.Equal(t, tests["registry-1.docker.io"].mirrors, hostnames) |
| 133 | +} |
0 commit comments