helper/resource: Add timeout to TimeoutError msg (#8773)

This commit is contained in:
Radek Simko 2016-09-15 10:53:25 +01:00 committed by GitHub
parent f9db94621b
commit 86acdccaf2
3 changed files with 18 additions and 7 deletions

View File

@ -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)
}

View File

@ -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,
}
}

View File

@ -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())
}