helper/resource: Show last state in timeout err message

This commit is contained in:
Radek Simko 2016-08-27 21:25:14 +01:00
parent aef2513b44
commit 506c118383
No known key found for this signature in database
GPG Key ID: 6823F3DCCE01BB19
3 changed files with 41 additions and 7 deletions

View File

@ -40,15 +40,26 @@ func (e *UnexpectedStateError) Error() string {
// TimeoutError is returned when WaitForState times out // TimeoutError is returned when WaitForState times out
type TimeoutError struct { type TimeoutError struct {
LastError error LastError error
LastState string
ExpectedState []string ExpectedState []string
} }
func (e *TimeoutError) Error() string { func (e *TimeoutError) Error() string {
if e.LastError != nil { expectedState := "resource to be gone"
return fmt.Sprintf("timeout while waiting for state to become '%s': %s", if len(e.ExpectedState) > 0 {
strings.Join(e.ExpectedState, ", "), e.LastError) expectedState = fmt.Sprintf("state to become '%s'", strings.Join(e.ExpectedState, ", "))
} }
return fmt.Sprintf("timeout while waiting for state to become '%s'", lastState := ""
strings.Join(e.ExpectedState, ", ")) if e.LastState != "" {
lastState = fmt.Sprintf(" (last state: '%s')", e.LastState)
}
if e.LastError != nil {
return fmt.Sprintf("timeout while waiting for %s%s: %s",
expectedState, lastState, e.LastError)
}
return fmt.Sprintf("timeout while waiting for %s%s",
expectedState, lastState)
} }

View File

@ -63,6 +63,7 @@ func (conf *StateChangeConf) WaitForState() (interface{}, error) {
var result interface{} var result interface{}
var resulterr error var resulterr error
var currentState string
doneCh := make(chan struct{}) doneCh := make(chan struct{})
go func() { go func() {
@ -98,7 +99,6 @@ func (conf *StateChangeConf) WaitForState() (interface{}, error) {
} }
} }
var currentState string
result, currentState, err = conf.Refresh() result, currentState, err = conf.Refresh()
if err != nil { if err != nil {
resulterr = err resulterr = err
@ -168,6 +168,7 @@ func (conf *StateChangeConf) WaitForState() (interface{}, error) {
case <-time.After(conf.Timeout): case <-time.After(conf.Timeout):
return nil, &TimeoutError{ return nil, &TimeoutError{
LastError: resulterr, LastError: resulterr,
LastState: currentState,
ExpectedState: conf.Target, ExpectedState: conf.Target,
} }
} }

View File

@ -106,7 +106,7 @@ func TestWaitForState_inconsistent_negative(t *testing.T) {
if err == nil { if err == nil {
t.Fatal("Expected timeout error. No error returned.") t.Fatal("Expected timeout error. No error returned.")
} }
expectedErr := "timeout while waiting for state to become 'done'" expectedErr := "timeout while waiting for state to become 'done' (last state: 'done')"
if err.Error() != expectedErr { if err.Error() != expectedErr {
t.Fatalf("Errors don't match.\nExpected: %q\nGiven: %q\n", expectedErr, err.Error()) t.Fatalf("Errors don't match.\nExpected: %q\nGiven: %q\n", expectedErr, err.Error())
} }
@ -173,6 +173,28 @@ func TestWaitForState_successEmpty(t *testing.T) {
} }
} }
func TestWaitForState_failureEmpty(t *testing.T) {
conf := &StateChangeConf{
Pending: []string{"pending", "incomplete"},
Target: []string{},
NotFoundChecks: 1,
Refresh: func() (interface{}, string, error) {
return 42, "pending", nil
},
PollInterval: 10 * time.Millisecond,
Timeout: 100 * time.Millisecond,
}
_, err := conf.WaitForState()
if err == nil {
t.Fatal("Expected timeout error. Got none.")
}
expectedErr := "timeout while waiting for resource to be gone (last state: 'pending')"
if err.Error() != expectedErr {
t.Fatalf("Errors don't match.\nExpected: %q\nGiven: %q\n", expectedErr, err.Error())
}
}
func TestWaitForState_failure(t *testing.T) { func TestWaitForState_failure(t *testing.T) {
conf := &StateChangeConf{ conf := &StateChangeConf{
Pending: []string{"pending", "incomplete"}, Pending: []string{"pending", "incomplete"},