Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

helper/resource: Show last state in timeout err message #8510

Merged
merged 2 commits into from Aug 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 15 additions & 4 deletions helper/resource/error.go
Expand Up @@ -40,15 +40,26 @@ func (e *UnexpectedStateError) Error() string {
// TimeoutError is returned when WaitForState times out
type TimeoutError struct {
LastError error
LastState string
ExpectedState []string
}

func (e *TimeoutError) Error() string {
expectedState := "resource to be gone"
if len(e.ExpectedState) > 0 {
expectedState = fmt.Sprintf("state to become '%s'", strings.Join(e.ExpectedState, ", "))
}

lastState := ""
if e.LastState != "" {
lastState = fmt.Sprintf(" (last state: '%s')", e.LastState)
}

if e.LastError != nil {
return fmt.Sprintf("timeout while waiting for state to become '%s': %s",
strings.Join(e.ExpectedState, ", "), e.LastError)
return fmt.Sprintf("timeout while waiting for %s%s: %s",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit pick - space required between %s values

expectedState, lastState, e.LastError)
}

return fmt.Sprintf("timeout while waiting for state to become '%s'",
strings.Join(e.ExpectedState, ", "))
return fmt.Sprintf("timeout while waiting for %s%s",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit pick - should be a space between the %s values

expectedState, lastState)
}
3 changes: 2 additions & 1 deletion helper/resource/state.go
Expand Up @@ -63,6 +63,7 @@ func (conf *StateChangeConf) WaitForState() (interface{}, error) {

var result interface{}
var resulterr error
var currentState string

doneCh := make(chan struct{})
go func() {
Expand Down Expand Up @@ -98,7 +99,6 @@ func (conf *StateChangeConf) WaitForState() (interface{}, error) {
}
}

var currentState string
result, currentState, err = conf.Refresh()
if err != nil {
resulterr = err
Expand Down Expand Up @@ -168,6 +168,7 @@ func (conf *StateChangeConf) WaitForState() (interface{}, error) {
case <-time.After(conf.Timeout):
return nil, &TimeoutError{
LastError: resulterr,
LastState: currentState,
ExpectedState: conf.Target,
}
}
Expand Down
53 changes: 45 additions & 8 deletions helper/resource/state_test.go
Expand Up @@ -75,7 +75,8 @@ func TestWaitForState_inconsistent_positive(t *testing.T) {
Pending: []string{"replicating"},
Target: []string{"done"},
Refresh: InconsistentStateRefreshFunc(),
Timeout: 10 * time.Second,
Timeout: 90 * time.Millisecond,
PollInterval: 10 * time.Millisecond,
ContinuousTargetOccurence: 3,
}

Expand All @@ -95,14 +96,19 @@ func TestWaitForState_inconsistent_negative(t *testing.T) {
Pending: []string{"replicating"},
Target: []string{"done"},
Refresh: InconsistentStateRefreshFunc(),
Timeout: 10 * time.Second,
Timeout: 90 * time.Millisecond,
PollInterval: 10 * time.Millisecond,
ContinuousTargetOccurence: 4,
}

_, err := conf.WaitForState()

if err == nil && err.Error() != "timeout while waiting for state to become 'done'" {
t.Fatalf("err: %s", err)
if err == nil {
t.Fatal("Expected timeout error. No error returned.")
}
expectedErr := "timeout while waiting for state to become 'done' (last state: 'done')"
if err.Error() != expectedErr {
t.Fatalf("Errors don't match.\nExpected: %q\nGiven: %q\n", expectedErr, err.Error())
}
}

Expand All @@ -116,8 +122,13 @@ func TestWaitForState_timeout(t *testing.T) {

obj, err := conf.WaitForState()

if err == nil && err.Error() != "timeout while waiting for state to become 'running'" {
t.Fatalf("err: %s", err)
if err == nil {
t.Fatal("Expected timeout error. No error returned.")
}

expectedErr := "timeout while waiting for state to become 'running'"
if err.Error() != expectedErr {
t.Fatalf("Errors don't match.\nExpected: %q\nGiven: %q\n", expectedErr, err.Error())
}

if obj != nil {
Expand Down Expand Up @@ -162,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) {
conf := &StateChangeConf{
Pending: []string{"pending", "incomplete"},
Expand All @@ -171,8 +204,12 @@ func TestWaitForState_failure(t *testing.T) {
}

obj, err := conf.WaitForState()
if err == nil && err.Error() != "failed" {
t.Fatalf("err: %s", err)
if err == nil {
t.Fatal("Expected error. No error returned.")
}
expectedErr := "failed"
if err.Error() != expectedErr {
t.Fatalf("Errors don't match.\nExpected: %q\nGiven: %q\n", expectedErr, err.Error())
}
if obj != nil {
t.Fatalf("should not return obj")
Expand Down