github.com/lymingtonprecision/terraform@v0.9.9-0.20170613092852-62acef9611a9/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  	"time"
     9  
    10  	"github.com/hashicorp/terraform/config"
    11  	"github.com/hashicorp/terraform/helper/schema"
    12  	"github.com/hashicorp/terraform/terraform"
    13  )
    14  
    15  func TestResourceProvisioner_impl(t *testing.T) {
    16  	var _ terraform.ResourceProvisioner = Provisioner()
    17  }
    18  
    19  func TestProvisioner(t *testing.T) {
    20  	if err := Provisioner().(*schema.Provisioner).InternalValidate(); err != nil {
    21  		t.Fatalf("err: %s", err)
    22  	}
    23  }
    24  
    25  func TestResourceProvider_Apply(t *testing.T) {
    26  	defer os.Remove("test_out")
    27  	c := testConfig(t, map[string]interface{}{
    28  		"command": "echo foo > test_out",
    29  	})
    30  
    31  	output := new(terraform.MockUIOutput)
    32  	p := Provisioner()
    33  
    34  	if err := p.Apply(output, nil, c); err != nil {
    35  		t.Fatalf("err: %v", err)
    36  	}
    37  
    38  	// Check the file
    39  	raw, err := ioutil.ReadFile("test_out")
    40  	if err != nil {
    41  		t.Fatalf("err: %v", err)
    42  	}
    43  
    44  	actual := strings.TrimSpace(string(raw))
    45  	expected := "foo"
    46  	if actual != expected {
    47  		t.Fatalf("bad: %#v", actual)
    48  	}
    49  }
    50  
    51  func TestResourceProvider_stop(t *testing.T) {
    52  	c := testConfig(t, map[string]interface{}{
    53  		// bash/zsh/ksh will exec a single command in the same process. This
    54  		// makes certain there's a subprocess in the shell.
    55  		"command": "sleep 30; sleep 30",
    56  	})
    57  
    58  	output := new(terraform.MockUIOutput)
    59  	p := Provisioner()
    60  
    61  	var err error
    62  	doneCh := make(chan struct{})
    63  	go func() {
    64  		defer close(doneCh)
    65  		err = p.Apply(output, nil, c)
    66  	}()
    67  
    68  	select {
    69  	case <-doneCh:
    70  		t.Fatal("should not finish quickly")
    71  	case <-time.After(50 * time.Millisecond):
    72  	}
    73  
    74  	// Stop it
    75  	p.Stop()
    76  
    77  	select {
    78  	case <-doneCh:
    79  	case <-time.After(2 * time.Second):
    80  		t.Fatal("should finish")
    81  	}
    82  }
    83  
    84  func TestResourceProvider_Validate_good(t *testing.T) {
    85  	c := testConfig(t, map[string]interface{}{
    86  		"command": "echo foo",
    87  	})
    88  
    89  	warn, errs := Provisioner().Validate(c)
    90  	if len(warn) > 0 {
    91  		t.Fatalf("Warnings: %v", warn)
    92  	}
    93  	if len(errs) > 0 {
    94  		t.Fatalf("Errors: %v", errs)
    95  	}
    96  }
    97  
    98  func TestResourceProvider_Validate_missing(t *testing.T) {
    99  	c := testConfig(t, map[string]interface{}{})
   100  
   101  	warn, errs := Provisioner().Validate(c)
   102  	if len(warn) > 0 {
   103  		t.Fatalf("Warnings: %v", warn)
   104  	}
   105  	if len(errs) == 0 {
   106  		t.Fatalf("Should have errors")
   107  	}
   108  }
   109  
   110  func testConfig(t *testing.T, c map[string]interface{}) *terraform.ResourceConfig {
   111  	r, err := config.NewRawConfig(c)
   112  	if err != nil {
   113  		t.Fatalf("bad: %s", err)
   114  	}
   115  
   116  	return terraform.NewResourceConfig(r)
   117  }