Skip to content

Commit

Permalink
kubecfg: improve tests around authentication
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kelseyhightower committed Jul 27, 2014
1 parent 6b5690a commit 1ca1993
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 35 deletions.
2 changes: 1 addition & 1 deletion cmd/kubecfg/kubecfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
11 changes: 6 additions & 5 deletions pkg/kubecfg/kubecfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package kubecfg
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"strconv"
Expand All @@ -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
Expand Down
79 changes: 50 additions & 29 deletions pkg/kubecfg/kubecfg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ limitations under the License.
package kubecfg

import (
"encoding/json"
"bytes"
"io"
"io/ioutil"
"os"
"reflect"
Expand Down Expand Up @@ -256,35 +257,55 @@ func TestCloudCfgDeleteControllerWithReplicas(t *testing.T) {
}

func TestLoadAuthInfo(t *testing.T) {
testAuthInfo := &client.AuthInfo{
User: "TestUser",
Password: "TestPassword",
}
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)
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"),
},
}
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)
}
}
}

Expand Down

0 comments on commit 1ca1993

Please sign in to comment.