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