github.com/ndarilek/terraform@v0.3.8-0.20150320140257-d3135c1b2bac/helper/resource/state_test.go (about) 1 package resource 2 3 import ( 4 "errors" 5 "testing" 6 "time" 7 ) 8 9 func FailedStateRefreshFunc() StateRefreshFunc { 10 return func() (interface{}, string, error) { 11 return nil, "", errors.New("failed") 12 } 13 } 14 15 func TimeoutStateRefreshFunc() StateRefreshFunc { 16 return func() (interface{}, string, error) { 17 time.Sleep(100 * time.Second) 18 return nil, "", errors.New("failed") 19 } 20 } 21 22 func SuccessfulStateRefreshFunc() StateRefreshFunc { 23 return func() (interface{}, string, error) { 24 return struct{}{}, "running", nil 25 } 26 } 27 28 func TestWaitForState_timeout(t *testing.T) { 29 conf := &StateChangeConf{ 30 Pending: []string{"pending", "incomplete"}, 31 Target: "running", 32 Refresh: TimeoutStateRefreshFunc(), 33 Timeout: 1 * time.Millisecond, 34 } 35 36 obj, err := conf.WaitForState() 37 38 if err == nil && err.Error() != "timeout while waiting for state to become 'running'" { 39 t.Fatalf("err: %s", err) 40 } 41 42 if obj != nil { 43 t.Fatalf("should not return obj") 44 } 45 46 } 47 48 func TestWaitForState_success(t *testing.T) { 49 conf := &StateChangeConf{ 50 Pending: []string{"pending", "incomplete"}, 51 Target: "running", 52 Refresh: SuccessfulStateRefreshFunc(), 53 Timeout: 200 * time.Second, 54 } 55 56 obj, err := conf.WaitForState() 57 if err != nil { 58 t.Fatalf("err: %s", err) 59 } 60 if obj == nil { 61 t.Fatalf("should return obj") 62 } 63 } 64 65 func TestWaitForState_successEmpty(t *testing.T) { 66 conf := &StateChangeConf{ 67 Pending: []string{"pending", "incomplete"}, 68 Target: "", 69 Refresh: func() (interface{}, string, error) { 70 return nil, "", nil 71 }, 72 Timeout: 200 * time.Second, 73 } 74 75 obj, err := conf.WaitForState() 76 if err != nil { 77 t.Fatalf("err: %s", err) 78 } 79 if obj != nil { 80 t.Fatalf("obj should be nil") 81 } 82 } 83 84 func TestWaitForState_failure(t *testing.T) { 85 conf := &StateChangeConf{ 86 Pending: []string{"pending", "incomplete"}, 87 Target: "running", 88 Refresh: FailedStateRefreshFunc(), 89 Timeout: 200 * time.Second, 90 } 91 92 obj, err := conf.WaitForState() 93 if err == nil && err.Error() != "failed" { 94 t.Fatalf("err: %s", err) 95 } 96 if obj != nil { 97 t.Fatalf("should not return obj") 98 } 99 }