github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/provisioners/remote-exec/resource_provisioner_test.go (about) 1 package remoteexec 2 3 import ( 4 "bytes" 5 "context" 6 "errors" 7 "io" 8 "net" 9 "testing" 10 "time" 11 12 "github.com/hashicorp/terraform/config" 13 "github.com/hashicorp/terraform/helper/schema" 14 "github.com/hashicorp/terraform/terraform" 15 ) 16 17 func TestResourceProvider_Validate_good(t *testing.T) { 18 c := testConfig(t, map[string]interface{}{ 19 "inline": "echo foo", 20 }) 21 p := Provisioner() 22 warn, errs := p.Validate(c) 23 if len(warn) > 0 { 24 t.Fatalf("Warnings: %v", warn) 25 } 26 if len(errs) > 0 { 27 t.Fatalf("Errors: %v", errs) 28 } 29 } 30 31 func TestResourceProvider_Validate_bad(t *testing.T) { 32 c := testConfig(t, map[string]interface{}{ 33 "invalid": "nope", 34 }) 35 p := Provisioner() 36 warn, errs := p.Validate(c) 37 if len(warn) > 0 { 38 t.Fatalf("Warnings: %v", warn) 39 } 40 if len(errs) == 0 { 41 t.Fatalf("Should have errors") 42 } 43 } 44 45 var expectedScriptOut = `cd /tmp 46 wget http://foobar 47 exit 0 48 ` 49 50 func TestResourceProvider_generateScript(t *testing.T) { 51 p := Provisioner().(*schema.Provisioner) 52 conf := map[string]interface{}{ 53 "inline": []interface{}{ 54 "cd /tmp", 55 "wget http://foobar", 56 "exit 0", 57 }, 58 } 59 out, err := generateScripts(schema.TestResourceDataRaw( 60 t, p.Schema, conf)) 61 if err != nil { 62 t.Fatalf("err: %v", err) 63 } 64 65 if len(out) != 1 { 66 t.Fatal("expected 1 out") 67 } 68 69 if out[0] != expectedScriptOut { 70 t.Fatalf("bad: %v", out) 71 } 72 } 73 74 func TestResourceProvider_CollectScripts_inline(t *testing.T) { 75 p := Provisioner().(*schema.Provisioner) 76 conf := map[string]interface{}{ 77 "inline": []interface{}{ 78 "cd /tmp", 79 "wget http://foobar", 80 "exit 0", 81 }, 82 } 83 84 scripts, err := collectScripts(schema.TestResourceDataRaw( 85 t, p.Schema, conf)) 86 if err != nil { 87 t.Fatalf("err: %v", err) 88 } 89 90 if len(scripts) != 1 { 91 t.Fatalf("bad: %v", scripts) 92 } 93 94 var out bytes.Buffer 95 _, err = io.Copy(&out, scripts[0]) 96 if err != nil { 97 t.Fatalf("err: %v", err) 98 } 99 100 if out.String() != expectedScriptOut { 101 t.Fatalf("bad: %v", out.String()) 102 } 103 } 104 105 func TestResourceProvider_CollectScripts_script(t *testing.T) { 106 p := Provisioner().(*schema.Provisioner) 107 conf := map[string]interface{}{ 108 "script": "test-fixtures/script1.sh", 109 } 110 111 scripts, err := collectScripts(schema.TestResourceDataRaw( 112 t, p.Schema, conf)) 113 if err != nil { 114 t.Fatalf("err: %v", err) 115 } 116 117 if len(scripts) != 1 { 118 t.Fatalf("bad: %v", scripts) 119 } 120 121 var out bytes.Buffer 122 _, err = io.Copy(&out, scripts[0]) 123 if err != nil { 124 t.Fatalf("err: %v", err) 125 } 126 127 if out.String() != expectedScriptOut { 128 t.Fatalf("bad: %v", out.String()) 129 } 130 } 131 132 func TestResourceProvider_CollectScripts_scripts(t *testing.T) { 133 p := Provisioner().(*schema.Provisioner) 134 conf := map[string]interface{}{ 135 "scripts": []interface{}{ 136 "test-fixtures/script1.sh", 137 "test-fixtures/script1.sh", 138 "test-fixtures/script1.sh", 139 }, 140 } 141 142 scripts, err := collectScripts(schema.TestResourceDataRaw( 143 t, p.Schema, conf)) 144 if err != nil { 145 t.Fatalf("err: %v", err) 146 } 147 148 if len(scripts) != 3 { 149 t.Fatalf("bad: %v", scripts) 150 } 151 152 for idx := range scripts { 153 var out bytes.Buffer 154 _, err = io.Copy(&out, scripts[idx]) 155 if err != nil { 156 t.Fatalf("err: %v", err) 157 } 158 159 if out.String() != expectedScriptOut { 160 t.Fatalf("bad: %v", out.String()) 161 } 162 } 163 } 164 165 func TestRetryFunc(t *testing.T) { 166 // succeed on the third try 167 errs := []error{io.EOF, &net.OpError{Err: errors.New("ERROR")}, nil} 168 count := 0 169 170 err := retryFunc(context.Background(), time.Second, func() error { 171 if count >= len(errs) { 172 return errors.New("failed to stop after nil error") 173 } 174 175 err := errs[count] 176 count++ 177 178 return err 179 }) 180 181 if count != 3 { 182 t.Fatal("retry func should have been called 3 times") 183 } 184 185 if err != nil { 186 t.Fatal(err) 187 } 188 } 189 190 func testConfig( 191 t *testing.T, 192 c map[string]interface{}) *terraform.ResourceConfig { 193 r, err := config.NewRawConfig(c) 194 if err != nil { 195 t.Fatalf("bad: %s", err) 196 } 197 198 return terraform.NewResourceConfig(r) 199 }