k8s.io/client-go@v0.22.2/tools/auth/clientauth_test.go (about) 1 /* 2 Copyright 2014 The Kubernetes 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 auth_test 18 19 import ( 20 "io/ioutil" 21 "os" 22 "reflect" 23 "testing" 24 25 clientauth "k8s.io/client-go/tools/auth" 26 ) 27 28 func TestLoadFromFile(t *testing.T) { 29 loadAuthInfoTests := []struct { 30 authData string 31 authInfo *clientauth.Info 32 expectErr bool 33 }{ 34 { 35 `{"user": "user", "password": "pass"}`, 36 &clientauth.Info{User: "user", Password: "pass"}, 37 false, 38 }, 39 { 40 "", nil, true, 41 }, 42 } 43 for _, loadAuthInfoTest := range loadAuthInfoTests { 44 tt := loadAuthInfoTest 45 aifile, err := ioutil.TempFile("", "testAuthInfo") 46 if err != nil { 47 t.Errorf("Unexpected error: %v", err) 48 } 49 if tt.authData != "missing" { 50 defer os.Remove(aifile.Name()) 51 defer aifile.Close() 52 _, err = aifile.WriteString(tt.authData) 53 if err != nil { 54 t.Errorf("Unexpected error: %v", err) 55 } 56 } else { 57 aifile.Close() 58 os.Remove(aifile.Name()) 59 } 60 authInfo, err := clientauth.LoadFromFile(aifile.Name()) 61 gotErr := err != nil 62 if gotErr != tt.expectErr { 63 t.Errorf("expected errorness: %v, actual errorness: %v", tt.expectErr, gotErr) 64 } 65 if !reflect.DeepEqual(authInfo, tt.authInfo) { 66 t.Errorf("Expected %v, got %v", tt.authInfo, authInfo) 67 } 68 } 69 }