helper/resource: support waiting on absense of thing

This commit is contained in:
Mitchell Hashimoto 2014-07-07 15:21:54 -07:00
parent b80e8364d0
commit 1c725896ca
2 changed files with 25 additions and 7 deletions

View File

@ -51,6 +51,12 @@ func (conf *StateChangeConf) WaitForState() (i interface{}, err error) {
return 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 i == nil {
// If we didn't find the resource, check if we have been // If we didn't find the resource, check if we have been
// not finding it for awhile, and if so, report an error. // not finding it for awhile, and if so, report an error.

View File

@ -6,8 +6,6 @@ import (
"time" "time"
) )
type nullObject struct{}
func FailedStateRefreshFunc() StateRefreshFunc { func FailedStateRefreshFunc() StateRefreshFunc {
return func() (interface{}, string, error) { return func() (interface{}, string, error) {
return nil, "", errors.New("failed") return nil, "", errors.New("failed")
@ -23,7 +21,7 @@ func TimeoutStateRefreshFunc() StateRefreshFunc {
func SuccessfulStateRefreshFunc() StateRefreshFunc { func SuccessfulStateRefreshFunc() StateRefreshFunc {
return func() (interface{}, string, error) { 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() obj, err := conf.WaitForState()
if err != nil { if err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
if obj == nil { if obj == nil {
t.Fatalf("should return obj") 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) { func TestWaitForState_failure(t *testing.T) {
@ -76,11 +90,9 @@ func TestWaitForState_failure(t *testing.T) {
} }
obj, err := conf.WaitForState() obj, err := conf.WaitForState()
if err == nil && err.Error() != "failed" { if err == nil && err.Error() != "failed" {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
if obj != nil { if obj != nil {
t.Fatalf("should not return obj") t.Fatalf("should not return obj")
} }