github.com/bengesoff/terraform@v0.3.1-0.20141018223233-b25a53629922/helper/schema/resource_test.go (about) 1 package schema 2 3 import ( 4 "fmt" 5 "reflect" 6 "testing" 7 8 "github.com/hashicorp/terraform/terraform" 9 ) 10 11 func TestResourceApply_create(t *testing.T) { 12 r := &Resource{ 13 Schema: map[string]*Schema{ 14 "foo": &Schema{ 15 Type: TypeInt, 16 Optional: true, 17 }, 18 }, 19 } 20 21 called := false 22 r.Create = func(d *ResourceData, m interface{}) error { 23 called = true 24 d.SetId("foo") 25 return nil 26 } 27 28 var s *terraform.InstanceState = nil 29 30 d := &terraform.InstanceDiff{ 31 Attributes: map[string]*terraform.ResourceAttrDiff{ 32 "foo": &terraform.ResourceAttrDiff{ 33 New: "42", 34 }, 35 }, 36 } 37 38 actual, err := r.Apply(s, d, nil) 39 if err != nil { 40 t.Fatalf("err: %s", err) 41 } 42 43 if !called { 44 t.Fatal("not called") 45 } 46 47 expected := &terraform.InstanceState{ 48 ID: "foo", 49 Attributes: map[string]string{ 50 "id": "foo", 51 "foo": "42", 52 }, 53 } 54 55 if !reflect.DeepEqual(actual, expected) { 56 t.Fatalf("bad: %#v", actual) 57 } 58 } 59 60 func TestResourceApply_destroy(t *testing.T) { 61 r := &Resource{ 62 Schema: map[string]*Schema{ 63 "foo": &Schema{ 64 Type: TypeInt, 65 Optional: true, 66 }, 67 }, 68 } 69 70 called := false 71 r.Delete = func(d *ResourceData, m interface{}) error { 72 called = true 73 return nil 74 } 75 76 s := &terraform.InstanceState{ 77 ID: "bar", 78 } 79 80 d := &terraform.InstanceDiff{ 81 Destroy: true, 82 } 83 84 actual, err := r.Apply(s, d, nil) 85 if err != nil { 86 t.Fatalf("err: %s", err) 87 } 88 89 if !called { 90 t.Fatal("delete not called") 91 } 92 93 if actual != nil { 94 t.Fatalf("bad: %#v", actual) 95 } 96 } 97 98 func TestResourceApply_destroyPartial(t *testing.T) { 99 r := &Resource{ 100 Schema: map[string]*Schema{ 101 "foo": &Schema{ 102 Type: TypeInt, 103 Optional: true, 104 }, 105 }, 106 } 107 108 r.Delete = func(d *ResourceData, m interface{}) error { 109 d.Set("foo", 42) 110 return fmt.Errorf("some error") 111 } 112 113 s := &terraform.InstanceState{ 114 ID: "bar", 115 Attributes: map[string]string{ 116 "foo": "12", 117 }, 118 } 119 120 d := &terraform.InstanceDiff{ 121 Destroy: true, 122 } 123 124 actual, err := r.Apply(s, d, nil) 125 if err == nil { 126 t.Fatal("should error") 127 } 128 129 expected := &terraform.InstanceState{ 130 ID: "bar", 131 Attributes: map[string]string{ 132 "id": "bar", 133 "foo": "42", 134 }, 135 } 136 137 if !reflect.DeepEqual(actual, expected) { 138 t.Fatalf("bad: %#v", actual) 139 } 140 } 141 142 func TestResourceApply_update(t *testing.T) { 143 r := &Resource{ 144 Schema: map[string]*Schema{ 145 "foo": &Schema{ 146 Type: TypeInt, 147 Optional: true, 148 }, 149 }, 150 } 151 152 r.Update = func(d *ResourceData, m interface{}) error { 153 d.Set("foo", 42) 154 return nil 155 } 156 157 s := &terraform.InstanceState{ 158 ID: "foo", 159 Attributes: map[string]string{ 160 "foo": "12", 161 }, 162 } 163 164 d := &terraform.InstanceDiff{ 165 Attributes: map[string]*terraform.ResourceAttrDiff{ 166 "foo": &terraform.ResourceAttrDiff{ 167 New: "13", 168 }, 169 }, 170 } 171 172 actual, err := r.Apply(s, d, nil) 173 if err != nil { 174 t.Fatalf("err: %s", err) 175 } 176 177 expected := &terraform.InstanceState{ 178 ID: "foo", 179 Attributes: map[string]string{ 180 "id": "foo", 181 "foo": "42", 182 }, 183 } 184 185 if !reflect.DeepEqual(actual, expected) { 186 t.Fatalf("bad: %#v", actual) 187 } 188 } 189 190 func TestResourceApply_updateNoCallback(t *testing.T) { 191 r := &Resource{ 192 Schema: map[string]*Schema{ 193 "foo": &Schema{ 194 Type: TypeInt, 195 Optional: true, 196 }, 197 }, 198 } 199 200 r.Update = nil 201 202 s := &terraform.InstanceState{ 203 ID: "foo", 204 Attributes: map[string]string{ 205 "foo": "12", 206 }, 207 } 208 209 d := &terraform.InstanceDiff{ 210 Attributes: map[string]*terraform.ResourceAttrDiff{ 211 "foo": &terraform.ResourceAttrDiff{ 212 New: "13", 213 }, 214 }, 215 } 216 217 actual, err := r.Apply(s, d, nil) 218 if err == nil { 219 t.Fatal("should error") 220 } 221 222 expected := &terraform.InstanceState{ 223 ID: "foo", 224 Attributes: map[string]string{ 225 "foo": "12", 226 }, 227 } 228 229 if !reflect.DeepEqual(actual, expected) { 230 t.Fatalf("bad: %#v", actual) 231 } 232 } 233 234 func TestResourceInternalValidate(t *testing.T) { 235 cases := []struct { 236 In *Resource 237 Err bool 238 }{ 239 { 240 nil, 241 true, 242 }, 243 244 // No optional and no required 245 { 246 &Resource{ 247 Schema: map[string]*Schema{ 248 "foo": &Schema{ 249 Type: TypeInt, 250 Optional: true, 251 Required: true, 252 }, 253 }, 254 }, 255 true, 256 }, 257 } 258 259 for i, tc := range cases { 260 err := tc.In.InternalValidate() 261 if (err != nil) != tc.Err { 262 t.Fatalf("%d: bad: %s", i, err) 263 } 264 } 265 } 266 267 func TestResourceRefresh(t *testing.T) { 268 r := &Resource{ 269 Schema: map[string]*Schema{ 270 "foo": &Schema{ 271 Type: TypeInt, 272 Optional: true, 273 }, 274 }, 275 } 276 277 r.Read = func(d *ResourceData, m interface{}) error { 278 if m != 42 { 279 return fmt.Errorf("meta not passed") 280 } 281 282 return d.Set("foo", d.Get("foo").(int)+1) 283 } 284 285 s := &terraform.InstanceState{ 286 ID: "bar", 287 Attributes: map[string]string{ 288 "foo": "12", 289 }, 290 } 291 292 expected := &terraform.InstanceState{ 293 ID: "bar", 294 Attributes: map[string]string{ 295 "id": "bar", 296 "foo": "13", 297 }, 298 } 299 300 actual, err := r.Refresh(s, 42) 301 if err != nil { 302 t.Fatalf("err: %s", err) 303 } 304 305 if !reflect.DeepEqual(actual, expected) { 306 t.Fatalf("bad: %#v", actual) 307 } 308 } 309 310 func TestResourceRefresh_delete(t *testing.T) { 311 r := &Resource{ 312 Schema: map[string]*Schema{ 313 "foo": &Schema{ 314 Type: TypeInt, 315 Optional: true, 316 }, 317 }, 318 } 319 320 r.Read = func(d *ResourceData, m interface{}) error { 321 d.SetId("") 322 return nil 323 } 324 325 s := &terraform.InstanceState{ 326 ID: "bar", 327 Attributes: map[string]string{ 328 "foo": "12", 329 }, 330 } 331 332 actual, err := r.Refresh(s, 42) 333 if err != nil { 334 t.Fatalf("err: %s", err) 335 } 336 337 if actual != nil { 338 t.Fatalf("bad: %#v", actual) 339 } 340 }