github.com/dougneal/terraform@v0.6.15-0.20170330092735-b6a3840768a4/terraform/eval_validate_test.go (about) 1 package terraform 2 3 import ( 4 "errors" 5 "strings" 6 "testing" 7 8 "github.com/hashicorp/terraform/config" 9 ) 10 11 func TestEvalValidateResource_managedResource(t *testing.T) { 12 mp := testProvider("aws") 13 mp.ValidateResourceFn = func(rt string, c *ResourceConfig) (ws []string, es []error) { 14 expected := "aws_instance" 15 if rt != expected { 16 t.Fatalf("expected: %s, got: %s", expected, rt) 17 } 18 expected = "bar" 19 val, _ := c.Get("foo") 20 if val != expected { 21 t.Fatalf("expected: %s, got: %s", expected, val) 22 } 23 return 24 } 25 26 p := ResourceProvider(mp) 27 rc := testResourceConfig(t, map[string]interface{}{"foo": "bar"}) 28 node := &EvalValidateResource{ 29 Provider: &p, 30 Config: &rc, 31 ResourceName: "foo", 32 ResourceType: "aws_instance", 33 ResourceMode: config.ManagedResourceMode, 34 } 35 36 _, err := node.Eval(&MockEvalContext{}) 37 if err != nil { 38 t.Fatalf("err: %s", err) 39 } 40 41 if !mp.ValidateResourceCalled { 42 t.Fatal("Expected ValidateResource to be called, but it was not!") 43 } 44 } 45 46 func TestEvalValidateResource_dataSource(t *testing.T) { 47 mp := testProvider("aws") 48 mp.ValidateDataSourceFn = func(rt string, c *ResourceConfig) (ws []string, es []error) { 49 expected := "aws_ami" 50 if rt != expected { 51 t.Fatalf("expected: %s, got: %s", expected, rt) 52 } 53 expected = "bar" 54 val, _ := c.Get("foo") 55 if val != expected { 56 t.Fatalf("expected: %s, got: %s", expected, val) 57 } 58 return 59 } 60 61 p := ResourceProvider(mp) 62 rc := testResourceConfig(t, map[string]interface{}{"foo": "bar"}) 63 node := &EvalValidateResource{ 64 Provider: &p, 65 Config: &rc, 66 ResourceName: "foo", 67 ResourceType: "aws_ami", 68 ResourceMode: config.DataResourceMode, 69 } 70 71 _, err := node.Eval(&MockEvalContext{}) 72 if err != nil { 73 t.Fatalf("err: %s", err) 74 } 75 76 if !mp.ValidateDataSourceCalled { 77 t.Fatal("Expected ValidateDataSource to be called, but it was not!") 78 } 79 } 80 81 func TestEvalValidateResource_validReturnsNilError(t *testing.T) { 82 mp := testProvider("aws") 83 mp.ValidateResourceFn = func(rt string, c *ResourceConfig) (ws []string, es []error) { 84 return 85 } 86 87 p := ResourceProvider(mp) 88 rc := &ResourceConfig{} 89 node := &EvalValidateResource{ 90 Provider: &p, 91 Config: &rc, 92 ResourceName: "foo", 93 ResourceType: "aws_instance", 94 ResourceMode: config.ManagedResourceMode, 95 } 96 97 _, err := node.Eval(&MockEvalContext{}) 98 if err != nil { 99 t.Fatalf("Expected nil error, got: %s", err) 100 } 101 } 102 103 func TestEvalValidateResource_warningsAndErrorsPassedThrough(t *testing.T) { 104 mp := testProvider("aws") 105 mp.ValidateResourceFn = func(rt string, c *ResourceConfig) (ws []string, es []error) { 106 ws = append(ws, "warn") 107 es = append(es, errors.New("err")) 108 return 109 } 110 111 p := ResourceProvider(mp) 112 rc := &ResourceConfig{} 113 node := &EvalValidateResource{ 114 Provider: &p, 115 Config: &rc, 116 ResourceName: "foo", 117 ResourceType: "aws_instance", 118 ResourceMode: config.ManagedResourceMode, 119 } 120 121 _, err := node.Eval(&MockEvalContext{}) 122 if err == nil { 123 t.Fatal("Expected an error, got none!") 124 } 125 126 verr := err.(*EvalValidateError) 127 if len(verr.Warnings) != 1 || verr.Warnings[0] != "warn" { 128 t.Fatalf("Expected 1 warning 'warn', got: %#v", verr.Warnings) 129 } 130 if len(verr.Errors) != 1 || verr.Errors[0].Error() != "err" { 131 t.Fatalf("Expected 1 error 'err', got: %#v", verr.Errors) 132 } 133 } 134 135 func TestEvalValidateResource_checksResourceName(t *testing.T) { 136 mp := testProvider("aws") 137 p := ResourceProvider(mp) 138 rc := &ResourceConfig{} 139 node := &EvalValidateResource{ 140 Provider: &p, 141 Config: &rc, 142 ResourceName: "bad*name", 143 ResourceType: "aws_instance", 144 ResourceMode: config.ManagedResourceMode, 145 } 146 147 _, err := node.Eval(&MockEvalContext{}) 148 if err == nil { 149 t.Fatal("Expected an error, got none!") 150 } 151 expectErr := "resource name can only contain" 152 if !strings.Contains(err.Error(), expectErr) { 153 t.Fatalf("Expected err: %s to contain %s", err, expectErr) 154 } 155 } 156 157 func TestEvalValidateResource_ignoreWarnings(t *testing.T) { 158 mp := testProvider("aws") 159 mp.ValidateResourceFn = func(rt string, c *ResourceConfig) (ws []string, es []error) { 160 ws = append(ws, "warn") 161 return 162 } 163 164 p := ResourceProvider(mp) 165 rc := &ResourceConfig{} 166 node := &EvalValidateResource{ 167 Provider: &p, 168 Config: &rc, 169 ResourceName: "foo", 170 ResourceType: "aws_instance", 171 ResourceMode: config.ManagedResourceMode, 172 173 IgnoreWarnings: true, 174 } 175 176 _, err := node.Eval(&MockEvalContext{}) 177 if err != nil { 178 t.Fatalf("Expected no error, got: %s", err) 179 } 180 }