From 0e3a7e6d0d97b69ed8b3bcf59de71c511b6baa54 Mon Sep 17 00:00:00 2001 From: James Nugent Date: Tue, 28 Mar 2017 15:38:42 -0400 Subject: [PATCH] helper/resource: Allow unknown pending states (#13099) Sometimes when waiting on a target state, the set of valid states through which a value will transition is unknown. This commit adds support for an empty Pending slice and will treat any states that are not the target as valid provided the timeouts are not exceeded. --- helper/resource/state.go | 2 +- helper/resource/state_test.go | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/helper/resource/state.go b/helper/resource/state.go index aafa7f3bc..7473a105e 100644 --- a/helper/resource/state.go +++ b/helper/resource/state.go @@ -141,7 +141,7 @@ func (conf *StateChangeConf) WaitForState() (interface{}, error) { } } - if !found { + if !found && len(conf.Pending) > 0 { result.Error = &UnexpectedStateError{ LastError: err, State: result.State, diff --git a/helper/resource/state_test.go b/helper/resource/state_test.go index dcbb3ce67..4b4731351 100644 --- a/helper/resource/state_test.go +++ b/helper/resource/state_test.go @@ -70,6 +70,23 @@ func InconsistentStateRefreshFunc() StateRefreshFunc { } } +func UnknownPendingStateRefreshFunc() StateRefreshFunc { + sequence := []string{ + "unknown1", "unknown2", "done", + } + + r := NewStateGenerator(sequence) + + return func() (interface{}, string, error) { + idx, s, err := r.NextState() + if err != nil { + return nil, "", err + } + + return idx, s, nil + } +} + func TestWaitForState_inconsistent_positive(t *testing.T) { conf := &StateChangeConf{ Pending: []string{"replicating"}, @@ -154,6 +171,22 @@ func TestWaitForState_success(t *testing.T) { } } +func TestWaitForState_successUnknownPending(t *testing.T) { + conf := &StateChangeConf{ + Target: []string{"done"}, + Refresh: UnknownPendingStateRefreshFunc(), + Timeout: 200 * time.Second, + } + + obj, err := conf.WaitForState() + if err != nil { + t.Fatalf("err: %s", err) + } + if obj == nil { + t.Fatalf("should return obj") + } +} + func TestWaitForState_successEmpty(t *testing.T) { conf := &StateChangeConf{ Pending: []string{"pending", "incomplete"},