github.com/ezbercih/terraform@v0.1.1-0.20140729011846-3c33865e0839/builtin/provisioners/local-exec/resource_provisioner_test.go (about) 1 package localexec 2 3 import ( 4 "io/ioutil" 5 "os" 6 "strings" 7 "testing" 8 9 "github.com/hashicorp/terraform/config" 10 "github.com/hashicorp/terraform/terraform" 11 ) 12 13 func TestResourceProvisioner_impl(t *testing.T) { 14 var _ terraform.ResourceProvisioner = new(ResourceProvisioner) 15 } 16 17 func TestResourceProvider_Apply(t *testing.T) { 18 defer os.Remove("test_out") 19 c := testConfig(t, map[string]interface{}{ 20 "command": "echo foo > test_out", 21 }) 22 23 p := new(ResourceProvisioner) 24 if err := p.Apply(nil, c); err != nil { 25 t.Fatalf("err: %v", err) 26 } 27 28 // Check the file 29 raw, err := ioutil.ReadFile("test_out") 30 if err != nil { 31 t.Fatalf("err: %v", err) 32 } 33 34 actual := strings.TrimSpace(string(raw)) 35 expected := "foo" 36 if actual != expected { 37 t.Fatalf("bad: %#v", actual) 38 } 39 } 40 41 func TestResourceProvider_Validate_good(t *testing.T) { 42 c := testConfig(t, map[string]interface{}{ 43 "command": "echo foo", 44 }) 45 p := new(ResourceProvisioner) 46 warn, errs := p.Validate(c) 47 if len(warn) > 0 { 48 t.Fatalf("Warnings: %v", warn) 49 } 50 if len(errs) > 0 { 51 t.Fatalf("Errors: %v", errs) 52 } 53 } 54 55 func TestResourceProvider_Validate_missing(t *testing.T) { 56 c := testConfig(t, map[string]interface{}{}) 57 p := new(ResourceProvisioner) 58 warn, errs := p.Validate(c) 59 if len(warn) > 0 { 60 t.Fatalf("Warnings: %v", warn) 61 } 62 if len(errs) == 0 { 63 t.Fatalf("Should have errors") 64 } 65 } 66 67 func testConfig( 68 t *testing.T, 69 c map[string]interface{}) *terraform.ResourceConfig { 70 r, err := config.NewRawConfig(c) 71 if err != nil { 72 t.Fatalf("bad: %s", err) 73 } 74 75 return terraform.NewResourceConfig(r) 76 }