github.com/hooklift/terraform@v0.11.0-beta1.0.20171117000744-6786c1361ffe/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_good_unknown_variable_value(t *testing.T) {
    52  	c := testConfig(t, map[string]interface{}{
    53  		"content":     config.UnknownVariableValue,
    54  		"destination": "/tmp/bar",
    55  	})
    56  
    57  	warn, errs := Provisioner().Validate(c)
    58  	if len(warn) > 0 {
    59  		t.Fatalf("Warnings: %v", warn)
    60  	}
    61  	if len(errs) > 0 {
    62  		t.Fatalf("Errors: %v", errs)
    63  	}
    64  }
    65  
    66  func TestResourceProvider_Validate_bad_not_destination(t *testing.T) {
    67  	c := testConfig(t, map[string]interface{}{
    68  		"source": "nope",
    69  	})
    70  
    71  	warn, errs := Provisioner().Validate(c)
    72  	if len(warn) > 0 {
    73  		t.Fatalf("Warnings: %v", warn)
    74  	}
    75  	if len(errs) == 0 {
    76  		t.Fatalf("Should have errors")
    77  	}
    78  }
    79  
    80  func TestResourceProvider_Validate_bad_no_source(t *testing.T) {
    81  	c := testConfig(t, map[string]interface{}{
    82  		"destination": "/tmp/bar",
    83  	})
    84  
    85  	warn, errs := Provisioner().Validate(c)
    86  	if len(warn) > 0 {
    87  		t.Fatalf("Warnings: %v", warn)
    88  	}
    89  	if len(errs) == 0 {
    90  		t.Fatalf("Should have errors")
    91  	}
    92  }
    93  
    94  func TestResourceProvider_Validate_bad_to_many_src(t *testing.T) {
    95  	c := testConfig(t, map[string]interface{}{
    96  		"source":      "nope",
    97  		"content":     "value to copy",
    98  		"destination": "/tmp/bar",
    99  	})
   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  }