github.com/richardbowden/terraform@v0.6.12-0.20160901200758-30ea22c25211/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 var expectedScriptOut = `cd /tmp 45 wget http://foobar 46 exit 0 47 ` 48 49 func TestResourceProvider_generateScript(t *testing.T) { 50 p := new(ResourceProvisioner) 51 conf := testConfig(t, map[string]interface{}{ 52 "inline": []interface{}{ 53 "cd /tmp", 54 "wget http://foobar", 55 "exit 0", 56 }, 57 }) 58 out, err := p.generateScript(conf) 59 if err != nil { 60 t.Fatalf("err: %v", err) 61 } 62 63 if out != expectedScriptOut { 64 t.Fatalf("bad: %v", out) 65 } 66 } 67 68 func TestResourceProvider_CollectScripts_inline(t *testing.T) { 69 p := new(ResourceProvisioner) 70 conf := testConfig(t, map[string]interface{}{ 71 "inline": []interface{}{ 72 "cd /tmp", 73 "wget http://foobar", 74 "exit 0", 75 }, 76 }) 77 78 scripts, err := p.collectScripts(conf) 79 if err != nil { 80 t.Fatalf("err: %v", err) 81 } 82 83 if len(scripts) != 1 { 84 t.Fatalf("bad: %v", scripts) 85 } 86 87 var out bytes.Buffer 88 _, err = io.Copy(&out, scripts[0]) 89 if err != nil { 90 t.Fatalf("err: %v", err) 91 } 92 93 if out.String() != expectedScriptOut { 94 t.Fatalf("bad: %v", out.String()) 95 } 96 } 97 98 func TestResourceProvider_CollectScripts_script(t *testing.T) { 99 p := new(ResourceProvisioner) 100 conf := testConfig(t, map[string]interface{}{ 101 "script": "test-fixtures/script1.sh", 102 }) 103 104 scripts, err := p.collectScripts(conf) 105 if err != nil { 106 t.Fatalf("err: %v", err) 107 } 108 109 if len(scripts) != 1 { 110 t.Fatalf("bad: %v", scripts) 111 } 112 113 var out bytes.Buffer 114 _, err = io.Copy(&out, scripts[0]) 115 if err != nil { 116 t.Fatalf("err: %v", err) 117 } 118 119 if out.String() != expectedScriptOut { 120 t.Fatalf("bad: %v", out.String()) 121 } 122 } 123 124 func TestResourceProvider_CollectScripts_scripts(t *testing.T) { 125 p := new(ResourceProvisioner) 126 conf := testConfig(t, map[string]interface{}{ 127 "scripts": []interface{}{ 128 "test-fixtures/script1.sh", 129 "test-fixtures/script1.sh", 130 "test-fixtures/script1.sh", 131 }, 132 }) 133 134 scripts, err := p.collectScripts(conf) 135 if err != nil { 136 t.Fatalf("err: %v", err) 137 } 138 139 if len(scripts) != 3 { 140 t.Fatalf("bad: %v", scripts) 141 } 142 143 for idx := range scripts { 144 var out bytes.Buffer 145 _, err = io.Copy(&out, scripts[idx]) 146 if err != nil { 147 t.Fatalf("err: %v", err) 148 } 149 150 if out.String() != expectedScriptOut { 151 t.Fatalf("bad: %v", out.String()) 152 } 153 } 154 } 155 156 func testConfig( 157 t *testing.T, 158 c map[string]interface{}) *terraform.ResourceConfig { 159 r, err := config.NewRawConfig(c) 160 if err != nil { 161 t.Fatalf("bad: %s", err) 162 } 163 164 return terraform.NewResourceConfig(r) 165 }