provisioner/local-exec: Adding tests for Apply and Validate

This commit is contained in:
Armon Dadgar 2014-07-09 16:36:46 -07:00
parent 6ace8e12e5
commit 7721caf867
1 changed files with 63 additions and 0 deletions

View File

@ -1,11 +1,74 @@
package localexec package localexec
import ( import (
"io/ioutil"
"os"
"testing" "testing"
"github.com/hashicorp/terraform/config"
"github.com/hashicorp/terraform/terraform" "github.com/hashicorp/terraform/terraform"
) )
func TestResourceProvisioner_impl(t *testing.T) { func TestResourceProvisioner_impl(t *testing.T) {
var _ terraform.ResourceProvisioner = new(ResourceProvisioner) var _ terraform.ResourceProvisioner = new(ResourceProvisioner)
} }
func TestResourceProvider_Apply(t *testing.T) {
defer os.Remove("test_out")
c := testConfig(t, map[string]interface{}{
"command": "echo foo > test_out",
})
p := new(ResourceProvisioner)
_, err := p.Apply(nil, c)
if err != nil {
t.Fatalf("err: %v", err)
}
// Check the file
raw, err := ioutil.ReadFile("test_out")
if err != nil {
t.Fatalf("err: %v", err)
}
if string(raw) != "foo\n" {
t.Fatalf("bad: %s", raw)
}
}
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)
}