From 1ca199379f9dce72fa38c01bdd962199bc86f595 Mon Sep 17 00:00:00 2001 From: Kelsey Hightower Date: Sat, 26 Jul 2014 15:00:37 -0700 Subject: [PATCH] kubecfg: improve tests around authentication This change adds additional test coverage for the kubecfg command. There is now a test for the case when the auth info file does not exist. LoadAuthInfo tests have been refactored to use table testing. --- cmd/kubecfg/kubecfg.go | 2 +- pkg/kubecfg/kubecfg.go | 11 +++--- pkg/kubecfg/kubecfg_test.go | 79 +++++++++++++++++++++++-------------- 3 files changed, 57 insertions(+), 35 deletions(-) diff --git a/cmd/kubecfg/kubecfg.go b/cmd/kubecfg/kubecfg.go index 57e431b41f..bc20466beb 100644 --- a/cmd/kubecfg/kubecfg.go +++ b/cmd/kubecfg/kubecfg.go @@ -130,7 +130,7 @@ func main() { var auth *kube_client.AuthInfo if secure { - auth, err = kubecfg.LoadAuthInfo(*authConfig) + auth, err = kubecfg.LoadAuthInfo(*authConfig, os.Stdin) if err != nil { glog.Fatalf("Error loading auth: %v", err) } diff --git a/pkg/kubecfg/kubecfg.go b/pkg/kubecfg/kubecfg.go index f35aa4ee46..2b340cb1c5 100644 --- a/pkg/kubecfg/kubecfg.go +++ b/pkg/kubecfg/kubecfg.go @@ -19,6 +19,7 @@ package kubecfg import ( "encoding/json" "fmt" + "io" "io/ioutil" "os" "strconv" @@ -32,19 +33,19 @@ import ( "gopkg.in/v1/yaml" ) -func promptForString(field string) string { +func promptForString(field string, r io.Reader) string { fmt.Printf("Please enter %s: ", field) var result string - fmt.Scan(&result) + fmt.Fscan(r, &result) return result } // LoadAuthInfo parses an AuthInfo object from a file path. It prompts user and creates file if it doesn't exist. -func LoadAuthInfo(path string) (*client.AuthInfo, error) { +func LoadAuthInfo(path string, r io.Reader) (*client.AuthInfo, error) { var auth client.AuthInfo if _, err := os.Stat(path); os.IsNotExist(err) { - auth.User = promptForString("Username") - auth.Password = promptForString("Password") + auth.User = promptForString("Username", r) + auth.Password = promptForString("Password", r) data, err := json.Marshal(auth) if err != nil { return &auth, err diff --git a/pkg/kubecfg/kubecfg_test.go b/pkg/kubecfg/kubecfg_test.go index f9ab4b27b0..029d9d23d0 100644 --- a/pkg/kubecfg/kubecfg_test.go +++ b/pkg/kubecfg/kubecfg_test.go @@ -17,7 +17,8 @@ limitations under the License. package kubecfg import ( - "encoding/json" + "bytes" + "io" "io/ioutil" "os" "reflect" @@ -256,35 +257,55 @@ func TestCloudCfgDeleteControllerWithReplicas(t *testing.T) { } func TestLoadAuthInfo(t *testing.T) { - testAuthInfo := &client.AuthInfo{ - User: "TestUser", - Password: "TestPassword", + loadAuthInfoTests := []struct { + authData string + authInfo *client.AuthInfo + r io.Reader + }{ + { + `{"user": "user", "password": "pass"}`, + &client.AuthInfo{User: "user", Password: "pass"}, + nil, + }, + { + "", nil, nil, + }, + { + "missing", + &client.AuthInfo{User: "user", Password: "pass"}, + bytes.NewBufferString("user\npass"), + }, } - aifile, err := ioutil.TempFile("", "testAuthInfo") - if err != nil { - t.Error("Could not open temp file") - } - defer os.Remove(aifile.Name()) - defer aifile.Close() - - ai, err := LoadAuthInfo(aifile.Name()) - if err == nil { - t.Error("LoadAuthInfo didn't fail on empty file") - } - data, err := json.Marshal(testAuthInfo) - if err != nil { - t.Fatal("Unexpected JSON marshal error") - } - _, err = aifile.Write(data) - if err != nil { - t.Fatal("Unexpected error in writing test file") - } - ai, err = LoadAuthInfo(aifile.Name()) - if err != nil { - t.Fatal(err) - } - if *testAuthInfo != *ai { - t.Error("Test data and loaded data are not equal") + for _, loadAuthInfoTest := range loadAuthInfoTests { + tt := loadAuthInfoTest + aifile, err := ioutil.TempFile("", "testAuthInfo") + if err != nil { + t.Errorf("Unexpected error: %v", err) + } + if tt.authData != "missing" { + defer os.Remove(aifile.Name()) + defer aifile.Close() + _, err = aifile.WriteString(tt.authData) + if err != nil { + t.Errorf("Unexpected error: %v", err) + } + } else { + aifile.Close() + os.Remove(aifile.Name()) + } + authInfo, err := LoadAuthInfo(aifile.Name(), tt.r) + if len(tt.authData) == 0 && tt.authData != "missing" { + if err == nil { + t.Error("LoadAuthInfo didn't fail on empty file") + } + continue + } + if err != nil { + t.Errorf("Unexpected error: %v", err) + } + if !reflect.DeepEqual(authInfo, tt.authInfo) { + t.Errorf("Expected %v, got %v", tt.authInfo, authInfo) + } } }