1/*
2Copyright 2014 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package auth_test
18
19import (
20	"io/ioutil"
21	"os"
22	"reflect"
23	"testing"
24
25	clientauth "k8s.io/client-go/tools/auth"
26)
27
28func 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}
70