|
1 | 1 | package commands |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "encoding/json" |
4 | 5 | "testing" |
5 | 6 |
|
| 7 | + "github.com/deislabs/duffle/pkg/bundle" |
| 8 | + "github.com/docker/app/internal" |
6 | 9 | "github.com/docker/cli/cli/command" |
| 10 | + "github.com/docker/cli/cli/config/configfile" |
| 11 | + "github.com/docker/cli/cli/config/types" |
7 | 12 | cliflags "github.com/docker/cli/cli/flags" |
8 | 13 | "gotest.tools/assert" |
9 | 14 | ) |
@@ -122,3 +127,108 @@ func TestSocketPath(t *testing.T) { |
122 | 127 | }) |
123 | 128 | } |
124 | 129 | } |
| 130 | + |
| 131 | +type registryConfigMock struct { |
| 132 | + command.Cli |
| 133 | + configFile *configfile.ConfigFile |
| 134 | +} |
| 135 | + |
| 136 | +func (r *registryConfigMock) ConfigFile() *configfile.ConfigFile { |
| 137 | + return r.configFile |
| 138 | +} |
| 139 | + |
| 140 | +func TestShareRegistryCreds(t *testing.T) { |
| 141 | + cases := []struct { |
| 142 | + name string |
| 143 | + shareCreds bool |
| 144 | + stored map[string]types.AuthConfig |
| 145 | + expected map[string]types.AuthConfig |
| 146 | + images map[string]bundle.Image |
| 147 | + }{ |
| 148 | + { |
| 149 | + name: "no-share", |
| 150 | + shareCreds: false, |
| 151 | + stored: map[string]types.AuthConfig{ |
| 152 | + "my-registry.com": { |
| 153 | + Username: "test", |
| 154 | + Password: "test", |
| 155 | + }, |
| 156 | + }, |
| 157 | + expected: map[string]types.AuthConfig{}, |
| 158 | + images: map[string]bundle.Image{ |
| 159 | + "component1": { |
| 160 | + BaseImage: bundle.BaseImage{ |
| 161 | + Image: "my-registry.com/ns/repo:tag", |
| 162 | + }, |
| 163 | + }, |
| 164 | + }, |
| 165 | + }, |
| 166 | + { |
| 167 | + name: "share", |
| 168 | + shareCreds: true, |
| 169 | + stored: map[string]types.AuthConfig{ |
| 170 | + "my-registry.com": { |
| 171 | + Username: "test", |
| 172 | + Password: "test", |
| 173 | + }, |
| 174 | + "my-registry2.com": { |
| 175 | + Username: "test", |
| 176 | + Password: "test", |
| 177 | + }, |
| 178 | + }, |
| 179 | + expected: map[string]types.AuthConfig{ |
| 180 | + "my-registry.com": { |
| 181 | + Username: "test", |
| 182 | + Password: "test", |
| 183 | + }}, |
| 184 | + images: map[string]bundle.Image{ |
| 185 | + "component1": { |
| 186 | + BaseImage: bundle.BaseImage{ |
| 187 | + Image: "my-registry.com/ns/repo:tag", |
| 188 | + }, |
| 189 | + }, |
| 190 | + }, |
| 191 | + }, |
| 192 | + { |
| 193 | + name: "share-missing", |
| 194 | + shareCreds: true, |
| 195 | + stored: map[string]types.AuthConfig{ |
| 196 | + "my-registry2.com": { |
| 197 | + Username: "test", |
| 198 | + Password: "test", |
| 199 | + }, |
| 200 | + }, |
| 201 | + expected: map[string]types.AuthConfig{ |
| 202 | + "my-registry.com": {}}, |
| 203 | + images: map[string]bundle.Image{ |
| 204 | + "component1": { |
| 205 | + BaseImage: bundle.BaseImage{ |
| 206 | + Image: "my-registry.com/ns/repo:tag", |
| 207 | + }, |
| 208 | + }, |
| 209 | + }, |
| 210 | + }, |
| 211 | + } |
| 212 | + |
| 213 | + for _, c := range cases { |
| 214 | + t.Run(c.name, func(t *testing.T) { |
| 215 | + params := map[string]interface{}{ |
| 216 | + internal.ParameterShareRegistryCredsName: c.shareCreds, |
| 217 | + } |
| 218 | + creds, err := prepareCredentialSet( |
| 219 | + &bundle.Bundle{ |
| 220 | + Credentials: map[string]bundle.Location{internal.CredentialRegistryName: {}}, |
| 221 | + Images: c.images, |
| 222 | + }, |
| 223 | + addNamedCredentialSets(nil), |
| 224 | + addDockerCredentials("", nil), |
| 225 | + addRegistryCredentials(params, ®istryConfigMock{configFile: &configfile.ConfigFile{ |
| 226 | + AuthConfigs: c.stored, |
| 227 | + }})) |
| 228 | + assert.NilError(t, err) |
| 229 | + var result map[string]types.AuthConfig |
| 230 | + assert.NilError(t, json.Unmarshal([]byte(creds[internal.CredentialRegistryName]), &result)) |
| 231 | + assert.DeepEqual(t, c.expected, result) |
| 232 | + }) |
| 233 | + } |
| 234 | +} |
0 commit comments