github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/provisioners/local-exec/resource_provisioner_test.go (about)

     1  package localexec
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/hashicorp/terraform/config"
    10  	"github.com/hashicorp/terraform/terraform"
    11  )
    12  
    13  func TestResourceProvisioner_impl(t *testing.T) {
    14  	var _ terraform.ResourceProvisioner = new(ResourceProvisioner)
    15  }
    16  
    17  func TestResourceProvider_Apply(t *testing.T) {
    18  	defer os.Remove("test_out")
    19  	c := testConfig(t, map[string]interface{}{
    20  		"command": "echo foo > test_out",
    21  	})
    22  
    23  	output := new(terraform.MockUIOutput)
    24  	p := new(ResourceProvisioner)
    25  	if err := p.Apply(output, nil, c); err != nil {
    26  		t.Fatalf("err: %v", err)
    27  	}
    28  
    29  	// Check the file
    30  	raw, err := ioutil.ReadFile("test_out")
    31  	if err != nil {
    32  		t.Fatalf("err: %v", err)
    33  	}
    34  
    35  	actual := strings.TrimSpace(string(raw))
    36  	expected := "foo"
    37  	if actual != expected {
    38  		t.Fatalf("bad: %#v", actual)
    39  	}
    40  }
    41  
    42  func TestResourceProvider_Validate_good(t *testing.T) {
    43  	c := testConfig(t, map[string]interface{}{
    44  		"command": "echo foo",
    45  	})
    46  	p := new(ResourceProvisioner)
    47  	warn, errs := p.Validate(c)
    48  	if len(warn) > 0 {
    49  		t.Fatalf("Warnings: %v", warn)
    50  	}
    51  	if len(errs) > 0 {
    52  		t.Fatalf("Errors: %v", errs)
    53  	}
    54  }
    55  
    56  func TestResourceProvider_Validate_missing(t *testing.T) {
    57  	c := testConfig(t, map[string]interface{}{})
    58  	p := new(ResourceProvisioner)
    59  	warn, errs := p.Validate(c)
    60  	if len(warn) > 0 {
    61  		t.Fatalf("Warnings: %v", warn)
    62  	}
    63  	if len(errs) == 0 {
    64  		t.Fatalf("Should have errors")
    65  	}
    66  }
    67  
    68  func testConfig(
    69  	t *testing.T,
    70  	c map[string]interface{}) *terraform.ResourceConfig {
    71  	r, err := config.NewRawConfig(c)
    72  	if err != nil {
    73  		t.Fatalf("bad: %s", err)
    74  	}
    75  
    76  	return terraform.NewResourceConfig(r)
    77  }