diff --git a/helper/resource/error.go b/helper/resource/error.go index 07fcce58e..8d5927454 100644 --- a/helper/resource/error.go +++ b/helper/resource/error.go @@ -3,6 +3,7 @@ package resource import ( "fmt" "strings" + "time" ) type NotFoundError struct { @@ -41,6 +42,7 @@ func (e *UnexpectedStateError) Error() string { type TimeoutError struct { LastError error LastState string + Timeout time.Duration ExpectedState []string } @@ -50,16 +52,24 @@ func (e *TimeoutError) Error() string { expectedState = fmt.Sprintf("state to become '%s'", strings.Join(e.ExpectedState, ", ")) } - lastState := "" + extraInfo := make([]string, 0) if e.LastState != "" { - lastState = fmt.Sprintf(" (last state: '%s')", e.LastState) + extraInfo = append(extraInfo, fmt.Sprintf("last state: '%s'", e.LastState)) + } + if e.Timeout > 0 { + extraInfo = append(extraInfo, fmt.Sprintf("timeout: %s", e.Timeout.String())) + } + + suffix := "" + if len(extraInfo) > 0 { + suffix = fmt.Sprintf(" (%s)", strings.Join(extraInfo, ", ")) } if e.LastError != nil { return fmt.Sprintf("timeout while waiting for %s%s: %s", - expectedState, lastState, e.LastError) + expectedState, suffix, e.LastError) } return fmt.Sprintf("timeout while waiting for %s%s", - expectedState, lastState) + expectedState, suffix) } diff --git a/helper/resource/state.go b/helper/resource/state.go index 300d50b78..b762d64fa 100644 --- a/helper/resource/state.go +++ b/helper/resource/state.go @@ -183,6 +183,7 @@ func (conf *StateChangeConf) WaitForState() (interface{}, error) { return nil, &TimeoutError{ LastError: r.Error, LastState: r.State, + Timeout: conf.Timeout, ExpectedState: conf.Target, } } diff --git a/helper/resource/state_test.go b/helper/resource/state_test.go index 9da049e9f..dcbb3ce67 100644 --- a/helper/resource/state_test.go +++ b/helper/resource/state_test.go @@ -106,7 +106,7 @@ func TestWaitForState_inconsistent_negative(t *testing.T) { if err == nil { t.Fatal("Expected timeout error. No error returned.") } - expectedErr := "timeout while waiting for state to become 'done' (last state: 'done')" + expectedErr := "timeout while waiting for state to become 'done' (last state: 'done', timeout: 90ms)" if err.Error() != expectedErr { t.Fatalf("Errors don't match.\nExpected: %q\nGiven: %q\n", expectedErr, err.Error()) } @@ -126,7 +126,7 @@ func TestWaitForState_timeout(t *testing.T) { t.Fatal("Expected timeout error. No error returned.") } - expectedErr := "timeout while waiting for state to become 'running'" + expectedErr := "timeout while waiting for state to become 'running' (timeout: 1ms)" if err.Error() != expectedErr { t.Fatalf("Errors don't match.\nExpected: %q\nGiven: %q\n", expectedErr, err.Error()) } @@ -189,7 +189,7 @@ func TestWaitForState_failureEmpty(t *testing.T) { if err == nil { t.Fatal("Expected timeout error. Got none.") } - expectedErr := "timeout while waiting for resource to be gone (last state: 'pending')" + expectedErr := "timeout while waiting for resource to be gone (last state: 'pending', timeout: 100ms)" if err.Error() != expectedErr { t.Fatalf("Errors don't match.\nExpected: %q\nGiven: %q\n", expectedErr, err.Error()) }