github.com/khulnasoft/cli@v0.0.0-20240402070845-01bcad7beefa/cli/command/registry_test.go (about) 1 package command_test 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/docker/docker/api/types/registry" 8 "github.com/khulnasoft/cli/cli/command" 9 "github.com/khulnasoft/cli/cli/config/configfile" 10 configtypes "github.com/khulnasoft/cli/cli/config/types" 11 "gotest.tools/v3/assert" 12 is "gotest.tools/v3/assert/cmp" 13 ) 14 15 var testAuthConfigs = []registry.AuthConfig{ 16 { 17 ServerAddress: "https://index.docker.io/v1/", 18 Username: "u0", 19 Password: "p0", 20 }, 21 { 22 ServerAddress: "server1.io", 23 Username: "u1", 24 Password: "p1", 25 }, 26 } 27 28 func TestGetDefaultAuthConfig(t *testing.T) { 29 testCases := []struct { 30 checkCredStore bool 31 inputServerAddress string 32 expectedErr string 33 expectedAuthConfig registry.AuthConfig 34 }{ 35 { 36 checkCredStore: false, 37 inputServerAddress: "", 38 expectedErr: "", 39 expectedAuthConfig: registry.AuthConfig{ 40 ServerAddress: "", 41 Username: "", 42 Password: "", 43 }, 44 }, 45 { 46 checkCredStore: true, 47 inputServerAddress: testAuthConfigs[0].ServerAddress, 48 expectedErr: "", 49 expectedAuthConfig: testAuthConfigs[0], 50 }, 51 { 52 checkCredStore: true, 53 inputServerAddress: testAuthConfigs[1].ServerAddress, 54 expectedErr: "", 55 expectedAuthConfig: testAuthConfigs[1], 56 }, 57 { 58 checkCredStore: true, 59 inputServerAddress: fmt.Sprintf("https://%s", testAuthConfigs[1].ServerAddress), 60 expectedErr: "", 61 expectedAuthConfig: testAuthConfigs[1], 62 }, 63 } 64 cfg := configfile.New("filename") 65 for _, authconfig := range testAuthConfigs { 66 assert.Check(t, cfg.GetCredentialsStore(authconfig.ServerAddress).Store(configtypes.AuthConfig(authconfig))) 67 } 68 for _, tc := range testCases { 69 serverAddress := tc.inputServerAddress 70 authconfig, err := command.GetDefaultAuthConfig(cfg, tc.checkCredStore, serverAddress, serverAddress == "https://index.docker.io/v1/") 71 if tc.expectedErr != "" { 72 assert.Check(t, err != nil) 73 assert.Check(t, is.Equal(tc.expectedErr, err.Error())) 74 } else { 75 assert.NilError(t, err) 76 assert.Check(t, is.DeepEqual(tc.expectedAuthConfig, authconfig)) 77 } 78 } 79 } 80 81 func TestGetDefaultAuthConfig_HelperError(t *testing.T) { 82 cfg := configfile.New("filename") 83 cfg.CredentialsStore = "fake-does-not-exist" 84 85 const serverAddress = "test-server-address" 86 expectedAuthConfig := registry.AuthConfig{ 87 ServerAddress: serverAddress, 88 } 89 const isDefaultRegistry = false // registry is not "https://index.docker.io/v1/" 90 authconfig, err := command.GetDefaultAuthConfig(cfg, true, serverAddress, isDefaultRegistry) 91 assert.Check(t, is.DeepEqual(expectedAuthConfig, authconfig)) 92 assert.Check(t, is.ErrorContains(err, "docker-credential-fake-does-not-exist")) 93 }