|
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,73 @@ 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 | + pull, resolve, push := docker.HostCapabilityPull, docker.HostCapabilityResolve, docker.HostCapabilityPush |
| 84 | + tests := map[string][]struct { |
| 85 | + host string |
| 86 | + scheme string |
| 87 | + path string |
| 88 | + capabilities docker.HostCapabilities |
| 89 | + }{ |
| 90 | + "docker.io": { |
| 91 | + { |
| 92 | + host: "yourmirror.local", |
| 93 | + scheme: "http", |
| 94 | + path: defaultPath, |
| 95 | + capabilities: pull | resolve, |
| 96 | + }, |
| 97 | + { |
| 98 | + host: "proxy.local:5000", |
| 99 | + scheme: "https", |
| 100 | + path: path.Join(defaultPath, "proxy.docker.io"), |
| 101 | + capabilities: pull | resolve | push, |
| 102 | + }, |
| 103 | + { |
| 104 | + host: "registry-1.docker.io", |
| 105 | + scheme: "https", |
| 106 | + path: defaultPath, |
| 107 | + capabilities: pull | resolve | push, |
| 108 | + }, |
| 109 | + }, |
| 110 | + "yourmirror.local": { |
| 111 | + { |
| 112 | + host: "yourmirror.local", |
| 113 | + scheme: "http", |
| 114 | + path: defaultPath, |
| 115 | + capabilities: pull | resolve | push, |
| 116 | + }, |
| 117 | + }, |
| 118 | + } |
| 119 | + |
| 120 | + cfg, err := config.Load(bytes.NewBuffer([]byte(testConfig))) |
| 121 | + require.NoError(t, err) |
| 122 | + |
| 123 | + require.NotEqual(t, 0, len(cfg.Registries)) |
| 124 | + registryHosts := NewRegistryConfig(cfg.Registries) |
| 125 | + require.NotNil(t, registryHosts) |
| 126 | + |
| 127 | + for hostname, testHost := range tests { |
| 128 | + hosts, err := registryHosts(hostname) |
| 129 | + require.NoError(t, err) |
| 130 | + require.Equal(t, len(testHost), len(hosts)) |
| 131 | + for i, host := range hosts { |
| 132 | + test := testHost[i] |
| 133 | + require.Equal(t, test.host, host.Host) |
| 134 | + require.Equal(t, test.capabilities, host.Capabilities) |
| 135 | + require.Equal(t, test.scheme, host.Scheme) |
| 136 | + require.Equal(t, test.path, host.Path) |
| 137 | + } |
| 138 | + } |
| 139 | +} |
0 commit comments