From 1c725896ca55faf56a934370584443dd4c876f12 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 7 Jul 2014 15:21:54 -0700 Subject: [PATCH] helper/resource: support waiting on absense of thing --- helper/resource/wait.go | 6 ++++++ helper/resource/wait_test.go | 26 +++++++++++++++++++------- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/helper/resource/wait.go b/helper/resource/wait.go index 407ce5085..8518323ca 100644 --- a/helper/resource/wait.go +++ b/helper/resource/wait.go @@ -51,6 +51,12 @@ func (conf *StateChangeConf) WaitForState() (i interface{}, err error) { return } + // If we're waiting for the absense of a thing, then return + if i == nil && conf.Target == "" { + result <- waitResult{nil, nil} + return + } + if i == nil { // If we didn't find the resource, check if we have been // not finding it for awhile, and if so, report an error. diff --git a/helper/resource/wait_test.go b/helper/resource/wait_test.go index f12c037a5..5a611e50f 100644 --- a/helper/resource/wait_test.go +++ b/helper/resource/wait_test.go @@ -6,8 +6,6 @@ import ( "time" ) -type nullObject struct{} - func FailedStateRefreshFunc() StateRefreshFunc { return func() (interface{}, string, error) { return nil, "", errors.New("failed") @@ -23,7 +21,7 @@ func TimeoutStateRefreshFunc() StateRefreshFunc { func SuccessfulStateRefreshFunc() StateRefreshFunc { return func() (interface{}, string, error) { - return &nullObject{}, "running", nil + return struct{}{}, "running", nil } } @@ -56,15 +54,31 @@ func TestWaitForState_success(t *testing.T) { } 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"}, + Target: "", + Refresh: func() (interface{}, string, error) { + return nil, "", nil + }, + Timeout: 200 * time.Second, + } + + obj, err := conf.WaitForState() + if err != nil { + t.Fatalf("err: %s", err) + } + if obj != nil { + t.Fatalf("obj should be nil") + } } func TestWaitForState_failure(t *testing.T) { @@ -76,11 +90,9 @@ func TestWaitForState_failure(t *testing.T) { } obj, err := conf.WaitForState() - if err == nil && err.Error() != "failed" { t.Fatalf("err: %s", err) } - if obj != nil { t.Fatalf("should not return obj") }