github.com/docker/app@v0.9.1-beta3.0.20210611140623-a48f773ab002/internal/commands/credentials_test.go (about) 1 package commands 2 3 import ( 4 "encoding/json" 5 "testing" 6 7 "github.com/deislabs/cnab-go/bundle" 8 "github.com/docker/app/internal" 9 "github.com/docker/cli/cli/command" 10 "github.com/docker/cli/cli/config/configfile" 11 "github.com/docker/cli/cli/config/types" 12 "gotest.tools/assert" 13 ) 14 15 type registryConfigMock struct { 16 command.Cli 17 configFile *configfile.ConfigFile 18 } 19 20 func (r *registryConfigMock) ConfigFile() *configfile.ConfigFile { 21 return r.configFile 22 } 23 24 func TestShareRegistryCreds(t *testing.T) { 25 cases := []struct { 26 name string 27 shareCreds bool 28 stored map[string]types.AuthConfig 29 expected map[string]types.AuthConfig 30 images map[string]bundle.Image 31 }{ 32 { 33 name: "no-share", 34 shareCreds: false, 35 stored: map[string]types.AuthConfig{ 36 "my-registry.com": { 37 Username: "test", 38 Password: "test", 39 }, 40 }, 41 expected: map[string]types.AuthConfig{}, 42 images: map[string]bundle.Image{ 43 "component1": { 44 BaseImage: bundle.BaseImage{ 45 Image: "my-registry.com/ns/repo:tag", 46 }, 47 }, 48 }, 49 }, 50 { 51 name: "share", 52 shareCreds: true, 53 stored: map[string]types.AuthConfig{ 54 "my-registry.com": { 55 Username: "test", 56 Password: "test", 57 }, 58 "my-registry2.com": { 59 Username: "test", 60 Password: "test", 61 }, 62 }, 63 expected: map[string]types.AuthConfig{ 64 "my-registry.com": { 65 Username: "test", 66 Password: "test", 67 }}, 68 images: map[string]bundle.Image{ 69 "component1": { 70 BaseImage: bundle.BaseImage{ 71 Image: "my-registry.com/ns/repo:tag", 72 }, 73 }, 74 }, 75 }, 76 { 77 name: "share-missing", 78 shareCreds: true, 79 stored: map[string]types.AuthConfig{ 80 "my-registry2.com": { 81 Username: "test", 82 Password: "test", 83 }, 84 }, 85 expected: map[string]types.AuthConfig{ 86 "my-registry.com": {}}, 87 images: map[string]bundle.Image{ 88 "component1": { 89 BaseImage: bundle.BaseImage{ 90 Image: "my-registry.com/ns/repo:tag", 91 }, 92 }, 93 }, 94 }, 95 } 96 97 for _, c := range cases { 98 t.Run(c.name, func(t *testing.T) { 99 creds, err := prepareCredentialSet( 100 &bundle.Bundle{ 101 Credentials: map[string]bundle.Credential{internal.CredentialRegistryName: {}}, 102 Images: c.images, 103 }, 104 addNamedCredentialSets(nil, nil), 105 addDockerCredentials("", nil), 106 addRegistryCredentials(c.shareCreds, ®istryConfigMock{configFile: &configfile.ConfigFile{ 107 AuthConfigs: c.stored, 108 }})) 109 assert.NilError(t, err) 110 var result map[string]types.AuthConfig 111 assert.NilError(t, json.Unmarshal([]byte(creds[internal.CredentialRegistryName]), &result)) 112 assert.DeepEqual(t, c.expected, result) 113 }) 114 } 115 } 116 117 func TestParseCommandlineCredential(t *testing.T) { 118 for _, tc := range []struct { 119 in string 120 n, v string 121 err string // either err or n+v are non-"" 122 }{ 123 {in: "", err: `failed to parse "" as a credential name=value`}, 124 {in: "A", err: `failed to parse "A" as a credential name=value`}, 125 {in: "=B", err: `failed to parse "=B" as a credential name=value`}, 126 {in: "A=", n: "A", v: ""}, 127 {in: "A=B", n: "A", v: "B"}, 128 {in: "A==", n: "A", v: "="}, 129 {in: "A=B=C", n: "A", v: "B=C"}, 130 } { 131 n := tc.in 132 if n == "" { 133 n = "«empty»" 134 } 135 t.Run(n, func(t *testing.T) { 136 n, v, err := parseCommandlineCredential(tc.in) 137 if tc.err != "" { 138 assert.Error(t, err, tc.err) 139 } else { 140 assert.NilError(t, err) 141 assert.Equal(t, tc.n, n) 142 assert.Equal(t, tc.v, v) 143 } 144 }) 145 } 146 }