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.
pull/6/head
Kelsey Hightower 2014-07-26 15:00:37 -07:00
parent 6b5690a259
commit 1ca199379f
3 changed files with 57 additions and 35 deletions

View File

@ -130,7 +130,7 @@ func main() {
var auth *kube_client.AuthInfo var auth *kube_client.AuthInfo
if secure { if secure {
auth, err = kubecfg.LoadAuthInfo(*authConfig) auth, err = kubecfg.LoadAuthInfo(*authConfig, os.Stdin)
if err != nil { if err != nil {
glog.Fatalf("Error loading auth: %v", err) glog.Fatalf("Error loading auth: %v", err)
} }

View File

@ -19,6 +19,7 @@ package kubecfg
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io"
"io/ioutil" "io/ioutil"
"os" "os"
"strconv" "strconv"
@ -32,19 +33,19 @@ import (
"gopkg.in/v1/yaml" "gopkg.in/v1/yaml"
) )
func promptForString(field string) string { func promptForString(field string, r io.Reader) string {
fmt.Printf("Please enter %s: ", field) fmt.Printf("Please enter %s: ", field)
var result string var result string
fmt.Scan(&result) fmt.Fscan(r, &result)
return result return result
} }
// LoadAuthInfo parses an AuthInfo object from a file path. It prompts user and creates file if it doesn't exist. // 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 var auth client.AuthInfo
if _, err := os.Stat(path); os.IsNotExist(err) { if _, err := os.Stat(path); os.IsNotExist(err) {
auth.User = promptForString("Username") auth.User = promptForString("Username", r)
auth.Password = promptForString("Password") auth.Password = promptForString("Password", r)
data, err := json.Marshal(auth) data, err := json.Marshal(auth)
if err != nil { if err != nil {
return &auth, err return &auth, err

View File

@ -17,7 +17,8 @@ limitations under the License.
package kubecfg package kubecfg
import ( import (
"encoding/json" "bytes"
"io"
"io/ioutil" "io/ioutil"
"os" "os"
"reflect" "reflect"
@ -256,35 +257,55 @@ func TestCloudCfgDeleteControllerWithReplicas(t *testing.T) {
} }
func TestLoadAuthInfo(t *testing.T) { func TestLoadAuthInfo(t *testing.T) {
testAuthInfo := &client.AuthInfo{ loadAuthInfoTests := []struct {
User: "TestUser", authData string
Password: "TestPassword", 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") for _, loadAuthInfoTest := range loadAuthInfoTests {
if err != nil { tt := loadAuthInfoTest
t.Error("Could not open temp file") aifile, err := ioutil.TempFile("", "testAuthInfo")
} if err != nil {
defer os.Remove(aifile.Name()) t.Errorf("Unexpected error: %v", err)
defer aifile.Close() }
if tt.authData != "missing" {
ai, err := LoadAuthInfo(aifile.Name()) defer os.Remove(aifile.Name())
if err == nil { defer aifile.Close()
t.Error("LoadAuthInfo didn't fail on empty file") _, err = aifile.WriteString(tt.authData)
} if err != nil {
data, err := json.Marshal(testAuthInfo) t.Errorf("Unexpected error: %v", err)
if err != nil { }
t.Fatal("Unexpected JSON marshal error") } else {
} aifile.Close()
_, err = aifile.Write(data) os.Remove(aifile.Name())
if err != nil { }
t.Fatal("Unexpected error in writing test file") authInfo, err := LoadAuthInfo(aifile.Name(), tt.r)
} if len(tt.authData) == 0 && tt.authData != "missing" {
ai, err = LoadAuthInfo(aifile.Name()) if err == nil {
if err != nil { t.Error("LoadAuthInfo didn't fail on empty file")
t.Fatal(err) }
} continue
if *testAuthInfo != *ai { }
t.Error("Test data and loaded data are not equal") if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if !reflect.DeepEqual(authInfo, tt.authInfo) {
t.Errorf("Expected %v, got %v", tt.authInfo, authInfo)
}
} }
} }