github.com/lymingtonprecision/terraform@v0.9.9-0.20170613092852-62acef9611a9/builtin/provisioners/file/resource_provisioner_test.go (about)

     1  package file
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/hashicorp/terraform/config"
     7  	"github.com/hashicorp/terraform/helper/schema"
     8  	"github.com/hashicorp/terraform/terraform"
     9  )
    10  
    11  func TestResourceProvisioner_impl(t *testing.T) {
    12  	var _ terraform.ResourceProvisioner = Provisioner()
    13  }
    14  
    15  func TestProvisioner(t *testing.T) {
    16  	if err := Provisioner().(*schema.Provisioner).InternalValidate(); err != nil {
    17  		t.Fatalf("err: %s", err)
    18  	}
    19  }
    20  
    21  func TestResourceProvider_Validate_good_source(t *testing.T) {
    22  	c := testConfig(t, map[string]interface{}{
    23  		"source":      "/tmp/foo",
    24  		"destination": "/tmp/bar",
    25  	})
    26  
    27  	warn, errs := Provisioner().Validate(c)
    28  	if len(warn) > 0 {
    29  		t.Fatalf("Warnings: %v", warn)
    30  	}
    31  	if len(errs) > 0 {
    32  		t.Fatalf("Errors: %v", errs)
    33  	}
    34  }
    35  
    36  func TestResourceProvider_Validate_good_content(t *testing.T) {
    37  	c := testConfig(t, map[string]interface{}{
    38  		"content":     "value to copy",
    39  		"destination": "/tmp/bar",
    40  	})
    41  
    42  	warn, errs := Provisioner().Validate(c)
    43  	if len(warn) > 0 {
    44  		t.Fatalf("Warnings: %v", warn)
    45  	}
    46  	if len(errs) > 0 {
    47  		t.Fatalf("Errors: %v", errs)
    48  	}
    49  }
    50  
    51  func TestResourceProvider_Validate_bad_not_destination(t *testing.T) {
    52  	c := testConfig(t, map[string]interface{}{
    53  		"source": "nope",
    54  	})
    55  
    56  	warn, errs := Provisioner().Validate(c)
    57  	if len(warn) > 0 {
    58  		t.Fatalf("Warnings: %v", warn)
    59  	}
    60  	if len(errs) == 0 {
    61  		t.Fatalf("Should have errors")
    62  	}
    63  }
    64  
    65  func TestResourceProvider_Validate_bad_no_source(t *testing.T) {
    66  	c := testConfig(t, map[string]interface{}{
    67  		"destination": "/tmp/bar",
    68  	})
    69  
    70  	warn, errs := Provisioner().Validate(c)
    71  	if len(warn) > 0 {
    72  		t.Fatalf("Warnings: %v", warn)
    73  	}
    74  	if len(errs) == 0 {
    75  		t.Fatalf("Should have errors")
    76  	}
    77  }
    78  
    79  func TestResourceProvider_Validate_bad_to_many_src(t *testing.T) {
    80  	c := testConfig(t, map[string]interface{}{
    81  		"source":      "nope",
    82  		"content":     "value to copy",
    83  		"destination": "/tmp/bar",
    84  	})
    85  
    86  	warn, errs := Provisioner().Validate(c)
    87  	if len(warn) > 0 {
    88  		t.Fatalf("Warnings: %v", warn)
    89  	}
    90  	if len(errs) == 0 {
    91  		t.Fatalf("Should have errors")
    92  	}
    93  }
    94  
    95  func testConfig(t *testing.T, c map[string]interface{}) *terraform.ResourceConfig {
    96  	r, err := config.NewRawConfig(c)
    97  	if err != nil {
    98  		t.Fatalf("bad: %s", err)
    99  	}
   100  
   101  	return terraform.NewResourceConfig(r)
   102  }