github.com/chalford/terraform@v0.3.7-0.20150113080010-a78c69a8c81f/builtin/provisioners/remote-exec/resource_provisioner_test.go (about) 1 package remoteexec 2 3 import ( 4 "bytes" 5 "io" 6 "testing" 7 8 "github.com/hashicorp/terraform/config" 9 "github.com/hashicorp/terraform/terraform" 10 ) 11 12 func TestResourceProvisioner_impl(t *testing.T) { 13 var _ terraform.ResourceProvisioner = new(ResourceProvisioner) 14 } 15 16 func TestResourceProvider_Validate_good(t *testing.T) { 17 c := testConfig(t, map[string]interface{}{ 18 "inline": "echo foo", 19 }) 20 p := new(ResourceProvisioner) 21 warn, errs := p.Validate(c) 22 if len(warn) > 0 { 23 t.Fatalf("Warnings: %v", warn) 24 } 25 if len(errs) > 0 { 26 t.Fatalf("Errors: %v", errs) 27 } 28 } 29 30 func TestResourceProvider_Validate_bad(t *testing.T) { 31 c := testConfig(t, map[string]interface{}{ 32 "invalid": "nope", 33 }) 34 p := new(ResourceProvisioner) 35 warn, errs := p.Validate(c) 36 if len(warn) > 0 { 37 t.Fatalf("Warnings: %v", warn) 38 } 39 if len(errs) == 0 { 40 t.Fatalf("Should have errors") 41 } 42 } 43 44 func TestResourceProvider_generateScript(t *testing.T) { 45 p := new(ResourceProvisioner) 46 conf := testConfig(t, map[string]interface{}{ 47 "inline": []interface{}{ 48 "cd /tmp", 49 "wget http://foobar", 50 "exit 0", 51 }, 52 }) 53 out, err := p.generateScript(conf) 54 if err != nil { 55 t.Fatalf("err: %v", err) 56 } 57 58 if out != expectedScriptOut { 59 t.Fatalf("bad: %v", out) 60 } 61 } 62 63 var expectedScriptOut = `#!/bin/sh 64 cd /tmp 65 wget http://foobar 66 exit 0 67 ` 68 69 func TestResourceProvider_CollectScripts_inline(t *testing.T) { 70 p := new(ResourceProvisioner) 71 conf := testConfig(t, map[string]interface{}{ 72 "inline": []interface{}{ 73 "cd /tmp", 74 "wget http://foobar", 75 "exit 0", 76 }, 77 }) 78 79 scripts, err := p.collectScripts(conf) 80 if err != nil { 81 t.Fatalf("err: %v", err) 82 } 83 84 if len(scripts) != 1 { 85 t.Fatalf("bad: %v", scripts) 86 } 87 88 var out bytes.Buffer 89 _, err = io.Copy(&out, scripts[0]) 90 if err != nil { 91 t.Fatalf("err: %v", err) 92 } 93 94 if string(out.Bytes()) != expectedScriptOut { 95 t.Fatalf("bad: %v", out.Bytes()) 96 } 97 } 98 99 func TestResourceProvider_CollectScripts_script(t *testing.T) { 100 p := new(ResourceProvisioner) 101 conf := testConfig(t, map[string]interface{}{ 102 "script": "test-fixtures/script1.sh", 103 }) 104 105 scripts, err := p.collectScripts(conf) 106 if err != nil { 107 t.Fatalf("err: %v", err) 108 } 109 110 if len(scripts) != 1 { 111 t.Fatalf("bad: %v", scripts) 112 } 113 114 var out bytes.Buffer 115 _, err = io.Copy(&out, scripts[0]) 116 if err != nil { 117 t.Fatalf("err: %v", err) 118 } 119 120 if string(out.Bytes()) != expectedScriptOut { 121 t.Fatalf("bad: %v", out.Bytes()) 122 } 123 } 124 125 func TestResourceProvider_CollectScripts_scripts(t *testing.T) { 126 p := new(ResourceProvisioner) 127 conf := testConfig(t, map[string]interface{}{ 128 "scripts": []interface{}{ 129 "test-fixtures/script1.sh", 130 "test-fixtures/script1.sh", 131 "test-fixtures/script1.sh", 132 }, 133 }) 134 135 scripts, err := p.collectScripts(conf) 136 if err != nil { 137 t.Fatalf("err: %v", err) 138 } 139 140 if len(scripts) != 3 { 141 t.Fatalf("bad: %v", scripts) 142 } 143 144 for idx := range scripts { 145 var out bytes.Buffer 146 _, err = io.Copy(&out, scripts[idx]) 147 if err != nil { 148 t.Fatalf("err: %v", err) 149 } 150 151 if string(out.Bytes()) != expectedScriptOut { 152 t.Fatalf("bad: %v", out.Bytes()) 153 } 154 } 155 } 156 157 func testConfig( 158 t *testing.T, 159 c map[string]interface{}) *terraform.ResourceConfig { 160 r, err := config.NewRawConfig(c) 161 if err != nil { 162 t.Fatalf("bad: %s", err) 163 } 164 165 return terraform.NewResourceConfig(r) 166 }