vitess.io/vitess@v0.16.2/go/vt/vtadmin/credentials/credentials_test.go (about) 1 /* 2 Copyright 2021 The Vitess Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package credentials 18 19 import ( 20 "os" 21 "testing" 22 23 "github.com/stretchr/testify/assert" 24 "github.com/stretchr/testify/require" 25 26 "vitess.io/vitess/go/vt/grpcclient" 27 ) 28 29 func Test_loadCredentials(t *testing.T) { 30 t.Parallel() 31 32 tests := []struct { 33 name string 34 contents []byte 35 expected grpcclient.StaticAuthClientCreds 36 shouldErr bool 37 }{ 38 { 39 name: "success", 40 contents: []byte(`{ 41 "Username": "vtadmin", 42 "Password": "hunter2" 43 }`), 44 expected: grpcclient.StaticAuthClientCreds{ 45 Username: "vtadmin", 46 Password: "hunter2", 47 }, 48 shouldErr: false, 49 }, 50 { 51 name: "not found", 52 contents: nil, 53 expected: grpcclient.StaticAuthClientCreds{}, 54 shouldErr: true, 55 }, 56 } 57 58 for _, tt := range tests { 59 tt := tt 60 61 t.Run(tt.name, func(t *testing.T) { 62 t.Parallel() 63 64 path := "" 65 66 if len(tt.contents) > 0 { 67 f, err := os.CreateTemp("", "vtsql-credentials-test-*") 68 require.NoError(t, err) 69 _, err = f.Write(tt.contents) 70 require.NoError(t, err) 71 72 path = f.Name() 73 f.Close() 74 defer os.Remove(path) 75 } 76 77 creds, err := loadCredentials(path) 78 if tt.shouldErr { 79 assert.Error(t, err) 80 return 81 } 82 83 assert.NoError(t, err) 84 assert.Equal(t, tt.expected, *creds) 85 }) 86 } 87 }