github.com/paybyphone/terraform@v0.9.5-0.20170613192930-9706042ddd51/terraform/eval_validate_test.go (about)

     1  package terraform
     2  
     3  import (
     4  	"errors"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/terraform/config"
     9  )
    10  
    11  func TestEvalValidateResource_managedResource(t *testing.T) {
    12  	mp := testProvider("aws")
    13  	mp.ValidateResourceFn = func(rt string, c *ResourceConfig) (ws []string, es []error) {
    14  		expected := "aws_instance"
    15  		if rt != expected {
    16  			t.Fatalf("expected: %s, got: %s", expected, rt)
    17  		}
    18  		expected = "bar"
    19  		val, _ := c.Get("foo")
    20  		if val != expected {
    21  			t.Fatalf("expected: %s, got: %s", expected, val)
    22  		}
    23  		return
    24  	}
    25  
    26  	p := ResourceProvider(mp)
    27  	rc := testResourceConfig(t, map[string]interface{}{"foo": "bar"})
    28  	node := &EvalValidateResource{
    29  		Provider:     &p,
    30  		Config:       &rc,
    31  		ResourceName: "foo",
    32  		ResourceType: "aws_instance",
    33  		ResourceMode: config.ManagedResourceMode,
    34  	}
    35  
    36  	_, err := node.Eval(&MockEvalContext{})
    37  	if err != nil {
    38  		t.Fatalf("err: %s", err)
    39  	}
    40  
    41  	if !mp.ValidateResourceCalled {
    42  		t.Fatal("Expected ValidateResource to be called, but it was not!")
    43  	}
    44  }
    45  
    46  func TestEvalValidateResource_dataSource(t *testing.T) {
    47  	mp := testProvider("aws")
    48  	mp.ValidateDataSourceFn = func(rt string, c *ResourceConfig) (ws []string, es []error) {
    49  		expected := "aws_ami"
    50  		if rt != expected {
    51  			t.Fatalf("expected: %s, got: %s", expected, rt)
    52  		}
    53  		expected = "bar"
    54  		val, _ := c.Get("foo")
    55  		if val != expected {
    56  			t.Fatalf("expected: %s, got: %s", expected, val)
    57  		}
    58  		return
    59  	}
    60  
    61  	p := ResourceProvider(mp)
    62  	rc := testResourceConfig(t, map[string]interface{}{"foo": "bar"})
    63  	node := &EvalValidateResource{
    64  		Provider:     &p,
    65  		Config:       &rc,
    66  		ResourceName: "foo",
    67  		ResourceType: "aws_ami",
    68  		ResourceMode: config.DataResourceMode,
    69  	}
    70  
    71  	_, err := node.Eval(&MockEvalContext{})
    72  	if err != nil {
    73  		t.Fatalf("err: %s", err)
    74  	}
    75  
    76  	if !mp.ValidateDataSourceCalled {
    77  		t.Fatal("Expected ValidateDataSource to be called, but it was not!")
    78  	}
    79  }
    80  
    81  func TestEvalValidateResource_validReturnsNilError(t *testing.T) {
    82  	mp := testProvider("aws")
    83  	mp.ValidateResourceFn = func(rt string, c *ResourceConfig) (ws []string, es []error) {
    84  		return
    85  	}
    86  
    87  	p := ResourceProvider(mp)
    88  	rc := &ResourceConfig{}
    89  	node := &EvalValidateResource{
    90  		Provider:     &p,
    91  		Config:       &rc,
    92  		ResourceName: "foo",
    93  		ResourceType: "aws_instance",
    94  		ResourceMode: config.ManagedResourceMode,
    95  	}
    96  
    97  	_, err := node.Eval(&MockEvalContext{})
    98  	if err != nil {
    99  		t.Fatalf("Expected nil error, got: %s", err)
   100  	}
   101  }
   102  
   103  func TestEvalValidateResource_warningsAndErrorsPassedThrough(t *testing.T) {
   104  	mp := testProvider("aws")
   105  	mp.ValidateResourceFn = func(rt string, c *ResourceConfig) (ws []string, es []error) {
   106  		ws = append(ws, "warn")
   107  		es = append(es, errors.New("err"))
   108  		return
   109  	}
   110  
   111  	p := ResourceProvider(mp)
   112  	rc := &ResourceConfig{}
   113  	node := &EvalValidateResource{
   114  		Provider:     &p,
   115  		Config:       &rc,
   116  		ResourceName: "foo",
   117  		ResourceType: "aws_instance",
   118  		ResourceMode: config.ManagedResourceMode,
   119  	}
   120  
   121  	_, err := node.Eval(&MockEvalContext{})
   122  	if err == nil {
   123  		t.Fatal("Expected an error, got none!")
   124  	}
   125  
   126  	verr := err.(*EvalValidateError)
   127  	if len(verr.Warnings) != 1 || verr.Warnings[0] != "warn" {
   128  		t.Fatalf("Expected 1 warning 'warn', got: %#v", verr.Warnings)
   129  	}
   130  	if len(verr.Errors) != 1 || verr.Errors[0].Error() != "err" {
   131  		t.Fatalf("Expected 1 error 'err', got: %#v", verr.Errors)
   132  	}
   133  }
   134  
   135  func TestEvalValidateResource_checksResourceName(t *testing.T) {
   136  	mp := testProvider("aws")
   137  	p := ResourceProvider(mp)
   138  	rc := &ResourceConfig{}
   139  	node := &EvalValidateResource{
   140  		Provider:     &p,
   141  		Config:       &rc,
   142  		ResourceName: "bad*name",
   143  		ResourceType: "aws_instance",
   144  		ResourceMode: config.ManagedResourceMode,
   145  	}
   146  
   147  	_, err := node.Eval(&MockEvalContext{})
   148  	if err == nil {
   149  		t.Fatal("Expected an error, got none!")
   150  	}
   151  	expectErr := "resource name can only contain"
   152  	if !strings.Contains(err.Error(), expectErr) {
   153  		t.Fatalf("Expected err: %s to contain %s", err, expectErr)
   154  	}
   155  }
   156  
   157  func TestEvalValidateResource_ignoreWarnings(t *testing.T) {
   158  	mp := testProvider("aws")
   159  	mp.ValidateResourceFn = func(rt string, c *ResourceConfig) (ws []string, es []error) {
   160  		ws = append(ws, "warn")
   161  		return
   162  	}
   163  
   164  	p := ResourceProvider(mp)
   165  	rc := &ResourceConfig{}
   166  	node := &EvalValidateResource{
   167  		Provider:     &p,
   168  		Config:       &rc,
   169  		ResourceName: "foo",
   170  		ResourceType: "aws_instance",
   171  		ResourceMode: config.ManagedResourceMode,
   172  
   173  		IgnoreWarnings: true,
   174  	}
   175  
   176  	_, err := node.Eval(&MockEvalContext{})
   177  	if err != nil {
   178  		t.Fatalf("Expected no error, got: %s", err)
   179  	}
   180  }
   181  
   182  func TestEvalValidateProvisioner_valid(t *testing.T) {
   183  	mp := &MockResourceProvisioner{}
   184  	var p ResourceProvisioner = mp
   185  	ctx := &MockEvalContext{}
   186  
   187  	cfg := &ResourceConfig{}
   188  	connInfo, err := config.NewRawConfig(map[string]interface{}{})
   189  	if err != nil {
   190  		t.Fatalf("failed to make connInfo: %s", err)
   191  	}
   192  	connConfig := NewResourceConfig(connInfo)
   193  
   194  	node := &EvalValidateProvisioner{
   195  		Provisioner: &p,
   196  		Config:      &cfg,
   197  		ConnConfig:  &connConfig,
   198  	}
   199  
   200  	result, err := node.Eval(ctx)
   201  	if err != nil {
   202  		t.Fatalf("node.Eval failed: %s", err)
   203  	}
   204  	if result != nil {
   205  		t.Errorf("node.Eval returned non-nil result")
   206  	}
   207  
   208  	if !mp.ValidateCalled {
   209  		t.Fatalf("p.Config not called")
   210  	}
   211  	if mp.ValidateConfig != cfg {
   212  		t.Errorf("p.Config called with wrong config")
   213  	}
   214  }
   215  
   216  func TestEvalValidateProvisioner_warning(t *testing.T) {
   217  	mp := &MockResourceProvisioner{}
   218  	var p ResourceProvisioner = mp
   219  	ctx := &MockEvalContext{}
   220  
   221  	cfg := &ResourceConfig{}
   222  	connInfo, err := config.NewRawConfig(map[string]interface{}{})
   223  	if err != nil {
   224  		t.Fatalf("failed to make connInfo: %s", err)
   225  	}
   226  	connConfig := NewResourceConfig(connInfo)
   227  
   228  	node := &EvalValidateProvisioner{
   229  		Provisioner: &p,
   230  		Config:      &cfg,
   231  		ConnConfig:  &connConfig,
   232  	}
   233  
   234  	mp.ValidateReturnWarns = []string{"foo is deprecated"}
   235  
   236  	_, err = node.Eval(ctx)
   237  	if err == nil {
   238  		t.Fatalf("node.Eval succeeded; want error")
   239  	}
   240  
   241  	valErr, ok := err.(*EvalValidateError)
   242  	if !ok {
   243  		t.Fatalf("node.Eval error is %#v; want *EvalValidateError", valErr)
   244  	}
   245  
   246  	warns := valErr.Warnings
   247  	if warns == nil || len(warns) != 1 {
   248  		t.Fatalf("wrong number of warnings in %#v; want one warning", warns)
   249  	}
   250  	if warns[0] != mp.ValidateReturnWarns[0] {
   251  		t.Fatalf("wrong warning %q; want %q", warns[0], mp.ValidateReturnWarns[0])
   252  	}
   253  }
   254  
   255  func TestEvalValidateProvisioner_connectionInvalid(t *testing.T) {
   256  	var p ResourceProvisioner = &MockResourceProvisioner{}
   257  	ctx := &MockEvalContext{}
   258  
   259  	cfg := &ResourceConfig{}
   260  	connInfo, err := config.NewRawConfig(map[string]interface{}{
   261  		"bananananananana": "foo",
   262  		"bazaz":            "bar",
   263  	})
   264  	if err != nil {
   265  		t.Fatalf("failed to make connInfo: %s", err)
   266  	}
   267  	connConfig := NewResourceConfig(connInfo)
   268  
   269  	node := &EvalValidateProvisioner{
   270  		Provisioner: &p,
   271  		Config:      &cfg,
   272  		ConnConfig:  &connConfig,
   273  	}
   274  
   275  	_, err = node.Eval(ctx)
   276  	if err == nil {
   277  		t.Fatalf("node.Eval succeeded; want error")
   278  	}
   279  
   280  	valErr, ok := err.(*EvalValidateError)
   281  	if !ok {
   282  		t.Fatalf("node.Eval error is %#v; want *EvalValidateError", valErr)
   283  	}
   284  
   285  	errs := valErr.Errors
   286  	if errs == nil || len(errs) != 2 {
   287  		t.Fatalf("wrong number of errors in %#v; want two errors", errs)
   288  	}
   289  
   290  	errStr := errs[0].Error()
   291  	if !(strings.Contains(errStr, "bananananananana") || strings.Contains(errStr, "bazaz")) {
   292  		t.Fatalf("wrong first error %q; want something about our invalid connInfo keys", errStr)
   293  	}
   294  }