2014-07-09 22:34:08 +02:00
|
|
|
package localexec
|
|
|
|
|
|
|
|
import (
|
2014-07-10 01:36:46 +02:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2014-07-10 22:09:09 +02:00
|
|
|
"strings"
|
2014-07-09 22:34:08 +02:00
|
|
|
"testing"
|
|
|
|
|
2014-07-10 01:36:46 +02:00
|
|
|
"github.com/hashicorp/terraform/config"
|
2014-07-09 22:34:08 +02:00
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestResourceProvisioner_impl(t *testing.T) {
|
|
|
|
var _ terraform.ResourceProvisioner = new(ResourceProvisioner)
|
|
|
|
}
|
2014-07-10 01:36:46 +02:00
|
|
|
|
|
|
|
func TestResourceProvider_Apply(t *testing.T) {
|
|
|
|
defer os.Remove("test_out")
|
|
|
|
c := testConfig(t, map[string]interface{}{
|
|
|
|
"command": "echo foo > test_out",
|
|
|
|
})
|
|
|
|
|
2014-10-05 01:29:33 +02:00
|
|
|
output := new(terraform.MockUIOutput)
|
2014-07-10 01:36:46 +02:00
|
|
|
p := new(ResourceProvisioner)
|
2014-10-05 01:29:33 +02:00
|
|
|
if err := p.Apply(output, nil, c); err != nil {
|
2014-07-10 01:36:46 +02:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the file
|
|
|
|
raw, err := ioutil.ReadFile("test_out")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-07-10 22:09:09 +02:00
|
|
|
actual := strings.TrimSpace(string(raw))
|
|
|
|
expected := "foo"
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad: %#v", actual)
|
2014-07-10 01:36:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestResourceProvider_Validate_good(t *testing.T) {
|
|
|
|
c := testConfig(t, map[string]interface{}{
|
|
|
|
"command": "echo foo",
|
|
|
|
})
|
|
|
|
p := new(ResourceProvisioner)
|
|
|
|
warn, errs := p.Validate(c)
|
|
|
|
if len(warn) > 0 {
|
|
|
|
t.Fatalf("Warnings: %v", warn)
|
|
|
|
}
|
|
|
|
if len(errs) > 0 {
|
|
|
|
t.Fatalf("Errors: %v", errs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestResourceProvider_Validate_missing(t *testing.T) {
|
|
|
|
c := testConfig(t, map[string]interface{}{})
|
|
|
|
p := new(ResourceProvisioner)
|
|
|
|
warn, errs := p.Validate(c)
|
|
|
|
if len(warn) > 0 {
|
|
|
|
t.Fatalf("Warnings: %v", warn)
|
|
|
|
}
|
|
|
|
if len(errs) == 0 {
|
|
|
|
t.Fatalf("Should have errors")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testConfig(
|
|
|
|
t *testing.T,
|
|
|
|
c map[string]interface{}) *terraform.ResourceConfig {
|
|
|
|
r, err := config.NewRawConfig(c)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bad: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return terraform.NewResourceConfig(r)
|
|
|
|
}
|