refactor: show errors if set password and key at the same time (#72)

This commit is contained in:
Bo-Yi Wu 2017-04-23 11:49:40 +08:00 committed by GitHub
parent 4d8adbffca
commit b63f275e9e
2 changed files with 23 additions and 2 deletions

View File

@ -17,6 +17,7 @@ const (
missingHostOrUser = "Error: missing server host or user" missingHostOrUser = "Error: missing server host or user"
missingPasswordOrKey = "Error: can't connect without a private SSH key or password" missingPasswordOrKey = "Error: can't connect without a private SSH key or password"
commandTimeOut = "Error: command timeout" commandTimeOut = "Error: command timeout"
setPasswordandKey = "can't set password and key at the same time"
) )
type ( type (
@ -46,14 +47,18 @@ func (p Plugin) log(host string, message ...interface{}) {
// Exec executes the plugin. // Exec executes the plugin.
func (p Plugin) Exec() error { func (p Plugin) Exec() error {
if len(p.Config.Host) == 0 && p.Config.UserName == "" { if len(p.Config.Host) == 0 && len(p.Config.UserName) == 0 {
return fmt.Errorf(missingHostOrUser) return fmt.Errorf(missingHostOrUser)
} }
if p.Config.Key == "" && p.Config.Password == "" && p.Config.KeyPath == "" { if len(p.Config.Key) == 0 && len(p.Config.Password) == 0 && len(p.Config.KeyPath) == 0 {
return fmt.Errorf(missingPasswordOrKey) return fmt.Errorf(missingPasswordOrKey)
} }
if len(p.Config.Key) != 0 && len(p.Config.Password) != 0 {
return fmt.Errorf(setPasswordandKey)
}
wg.Add(len(p.Config.Host)) wg.Add(len(p.Config.Host))
errChannel := make(chan error, 1) errChannel := make(chan error, 1)
finished := make(chan bool, 1) finished := make(chan bool, 1)

View File

@ -30,6 +30,22 @@ func TestMissingKeyOrPassword(t *testing.T) {
assert.Equal(t, missingPasswordOrKey, err.Error()) assert.Equal(t, missingPasswordOrKey, err.Error())
} }
func TestSetPasswordAndKey(t *testing.T) {
plugin := Plugin{
Config{
Host: []string{"localhost"},
UserName: "ubuntu",
Password: "1234",
Key: "1234",
},
}
err := plugin.Exec()
assert.NotNil(t, err)
assert.Equal(t, setPasswordandKey, err.Error())
}
func TestIncorrectPassword(t *testing.T) { func TestIncorrectPassword(t *testing.T) {
plugin := Plugin{ plugin := Plugin{
Config: Config{ Config: Config{