github.com/ns1/terraform@v0.7.10-0.20161109153551-8949419bef40/terraform/context_validate_test.go (about)

     1  package terraform
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  )
     8  
     9  func TestContext2Validate_badCount(t *testing.T) {
    10  	p := testProvider("aws")
    11  	m := testModule(t, "validate-bad-count")
    12  	c := testContext2(t, &ContextOpts{
    13  		Module: m,
    14  		Providers: map[string]ResourceProviderFactory{
    15  			"aws": testProviderFuncFixed(p),
    16  		},
    17  	})
    18  
    19  	w, e := c.Validate()
    20  	if len(w) > 0 {
    21  		t.Fatalf("bad: %#v", w)
    22  	}
    23  	if len(e) == 0 {
    24  		t.Fatalf("bad: %#v", e)
    25  	}
    26  }
    27  
    28  func TestContext2Validate_badVar(t *testing.T) {
    29  	p := testProvider("aws")
    30  	m := testModule(t, "validate-bad-var")
    31  	c := testContext2(t, &ContextOpts{
    32  		Module: m,
    33  		Providers: map[string]ResourceProviderFactory{
    34  			"aws": testProviderFuncFixed(p),
    35  		},
    36  	})
    37  
    38  	w, e := c.Validate()
    39  	if len(w) > 0 {
    40  		t.Fatalf("bad: %#v", w)
    41  	}
    42  	if len(e) == 0 {
    43  		t.Fatalf("bad: %#v", e)
    44  	}
    45  }
    46  
    47  func TestContext2Validate_varNoDefaultExplicitType(t *testing.T) {
    48  	m := testModule(t, "validate-var-no-default-explicit-type")
    49  	c := testContext2(t, &ContextOpts{
    50  		Module: m,
    51  	})
    52  
    53  	w, e := c.Validate()
    54  	if len(w) > 0 {
    55  		t.Fatalf("bad: %#v", w)
    56  	}
    57  	if len(e) == 0 {
    58  		t.Fatalf("bad: %#v", e)
    59  	}
    60  }
    61  
    62  func TestContext2Validate_computedVar(t *testing.T) {
    63  	p := testProvider("aws")
    64  	m := testModule(t, "validate-computed-var")
    65  	c := testContext2(t, &ContextOpts{
    66  		Module: m,
    67  		Providers: map[string]ResourceProviderFactory{
    68  			"aws":  testProviderFuncFixed(p),
    69  			"test": testProviderFuncFixed(testProvider("test")),
    70  		},
    71  	})
    72  
    73  	p.ValidateFn = func(c *ResourceConfig) ([]string, []error) {
    74  		if !c.IsComputed("value") {
    75  			return nil, []error{fmt.Errorf("value isn't computed")}
    76  		}
    77  
    78  		return nil, c.CheckSet([]string{"value"})
    79  	}
    80  
    81  	p.ConfigureFn = func(c *ResourceConfig) error {
    82  		return fmt.Errorf("Configure should not be called for provider")
    83  	}
    84  
    85  	w, e := c.Validate()
    86  	if len(w) > 0 {
    87  		t.Fatalf("bad: %#v", w)
    88  	}
    89  	if len(e) > 0 {
    90  		t.Fatalf("bad: %#v", e)
    91  	}
    92  }
    93  
    94  func TestContext2Validate_countNegative(t *testing.T) {
    95  	p := testProvider("aws")
    96  	m := testModule(t, "validate-count-negative")
    97  	c := testContext2(t, &ContextOpts{
    98  		Module: m,
    99  		Providers: map[string]ResourceProviderFactory{
   100  			"aws": testProviderFuncFixed(p),
   101  		},
   102  	})
   103  
   104  	w, e := c.Validate()
   105  	if len(w) > 0 {
   106  		t.Fatalf("bad: %#v", w)
   107  	}
   108  	if len(e) == 0 {
   109  		t.Fatalf("bad: %#v", e)
   110  	}
   111  }
   112  
   113  func TestContext2Validate_countVariable(t *testing.T) {
   114  	p := testProvider("aws")
   115  	m := testModule(t, "apply-count-variable")
   116  	c := testContext2(t, &ContextOpts{
   117  		Module: m,
   118  		Providers: map[string]ResourceProviderFactory{
   119  			"aws": testProviderFuncFixed(p),
   120  		},
   121  	})
   122  
   123  	w, e := c.Validate()
   124  	if len(w) > 0 {
   125  		t.Fatalf("bad: %#v", w)
   126  	}
   127  	if len(e) > 0 {
   128  		t.Fatalf("bad: %s", e)
   129  	}
   130  }
   131  
   132  func TestContext2Validate_countVariableNoDefault(t *testing.T) {
   133  	p := testProvider("aws")
   134  	m := testModule(t, "validate-count-variable")
   135  	c := testContext2(t, &ContextOpts{
   136  		Module: m,
   137  		Providers: map[string]ResourceProviderFactory{
   138  			"aws": testProviderFuncFixed(p),
   139  		},
   140  	})
   141  
   142  	w, e := c.Validate()
   143  	if len(w) > 0 {
   144  		t.Fatalf("bad: %#v", w)
   145  	}
   146  	if len(e) != 1 {
   147  		t.Fatalf("bad: %s", e)
   148  	}
   149  }
   150  
   151  /*
   152  TODO: What should we do here?
   153  func TestContext2Validate_cycle(t *testing.T) {
   154  	p := testProvider("aws")
   155  	m := testModule(t, "validate-cycle")
   156  	c := testContext2(t, &ContextOpts{
   157  		Module: m,
   158  		Providers: map[string]ResourceProviderFactory{
   159  			"aws": testProviderFuncFixed(p),
   160  		},
   161  	})
   162  
   163  	w, e := c.Validate()
   164  	if len(w) > 0 {
   165  		t.Fatalf("expected no warns, got: %#v", w)
   166  	}
   167  	if len(e) != 1 {
   168  		t.Fatalf("expected 1 err, got: %s", e)
   169  	}
   170  }
   171  */
   172  
   173  func TestContext2Validate_moduleBadOutput(t *testing.T) {
   174  	p := testProvider("aws")
   175  	m := testModule(t, "validate-bad-module-output")
   176  	c := testContext2(t, &ContextOpts{
   177  		Module: m,
   178  		Providers: map[string]ResourceProviderFactory{
   179  			"aws": testProviderFuncFixed(p),
   180  		},
   181  	})
   182  
   183  	w, e := c.Validate()
   184  	if len(w) > 0 {
   185  		t.Fatalf("bad: %#v", w)
   186  	}
   187  	if len(e) == 0 {
   188  		t.Fatalf("bad: %s", e)
   189  	}
   190  }
   191  
   192  func TestContext2Validate_moduleGood(t *testing.T) {
   193  	p := testProvider("aws")
   194  	m := testModule(t, "validate-good-module")
   195  	c := testContext2(t, &ContextOpts{
   196  		Module: m,
   197  		Providers: map[string]ResourceProviderFactory{
   198  			"aws": testProviderFuncFixed(p),
   199  		},
   200  	})
   201  
   202  	w, e := c.Validate()
   203  	if len(w) > 0 {
   204  		t.Fatalf("bad: %#v", w)
   205  	}
   206  	if len(e) > 0 {
   207  		t.Fatalf("bad: %#v", e)
   208  	}
   209  }
   210  
   211  func TestContext2Validate_moduleBadResource(t *testing.T) {
   212  	m := testModule(t, "validate-module-bad-rc")
   213  	p := testProvider("aws")
   214  	c := testContext2(t, &ContextOpts{
   215  		Module: m,
   216  		Providers: map[string]ResourceProviderFactory{
   217  			"aws": testProviderFuncFixed(p),
   218  		},
   219  	})
   220  
   221  	p.ValidateResourceReturnErrors = []error{fmt.Errorf("bad")}
   222  
   223  	w, e := c.Validate()
   224  	if len(w) > 0 {
   225  		t.Fatalf("bad: %#v", w)
   226  	}
   227  	if len(e) == 0 {
   228  		t.Fatalf("bad: %#v", e)
   229  	}
   230  }
   231  
   232  func TestContext2Validate_moduleDepsShouldNotCycle(t *testing.T) {
   233  	m := testModule(t, "validate-module-deps-cycle")
   234  	p := testProvider("aws")
   235  	ctx := testContext2(t, &ContextOpts{
   236  		Module: m,
   237  		Providers: map[string]ResourceProviderFactory{
   238  			"aws": testProviderFuncFixed(p),
   239  		},
   240  	})
   241  
   242  	w, e := ctx.Validate()
   243  
   244  	if len(w) > 0 {
   245  		t.Fatalf("expected no warnings, got: %s", w)
   246  	}
   247  	if len(e) > 0 {
   248  		t.Fatalf("expected no errors, got: %s", e)
   249  	}
   250  }
   251  
   252  func TestContext2Validate_moduleProviderInherit(t *testing.T) {
   253  	m := testModule(t, "validate-module-pc-inherit")
   254  	p := testProvider("aws")
   255  	c := testContext2(t, &ContextOpts{
   256  		Module: m,
   257  		Providers: map[string]ResourceProviderFactory{
   258  			"aws": testProviderFuncFixed(p),
   259  		},
   260  	})
   261  
   262  	p.ValidateFn = func(c *ResourceConfig) ([]string, []error) {
   263  		return nil, c.CheckSet([]string{"set"})
   264  	}
   265  
   266  	w, e := c.Validate()
   267  	if len(w) > 0 {
   268  		t.Fatalf("bad: %#v", w)
   269  	}
   270  	if len(e) > 0 {
   271  		t.Fatalf("bad: %s", e)
   272  	}
   273  }
   274  
   275  func TestContext2Validate_moduleProviderInheritOrphan(t *testing.T) {
   276  	m := testModule(t, "validate-module-pc-inherit-orphan")
   277  	p := testProvider("aws")
   278  	c := testContext2(t, &ContextOpts{
   279  		Module: m,
   280  		Providers: map[string]ResourceProviderFactory{
   281  			"aws": testProviderFuncFixed(p),
   282  		},
   283  		State: &State{
   284  			Modules: []*ModuleState{
   285  				&ModuleState{
   286  					Path: []string{"root", "child"},
   287  					Resources: map[string]*ResourceState{
   288  						"aws_instance.bar": &ResourceState{
   289  							Type: "aws_instance",
   290  							Primary: &InstanceState{
   291  								ID: "bar",
   292  							},
   293  						},
   294  					},
   295  				},
   296  			},
   297  		},
   298  	})
   299  
   300  	p.ValidateFn = func(c *ResourceConfig) ([]string, []error) {
   301  		v, ok := c.Get("set")
   302  		if !ok {
   303  			return nil, []error{fmt.Errorf("not set")}
   304  		}
   305  		if v != "bar" {
   306  			return nil, []error{fmt.Errorf("bad: %#v", v)}
   307  		}
   308  
   309  		return nil, nil
   310  	}
   311  
   312  	w, e := c.Validate()
   313  	if len(w) > 0 {
   314  		t.Fatalf("bad: %#v", w)
   315  	}
   316  	if len(e) > 0 {
   317  		t.Fatalf("bad: %s", e)
   318  	}
   319  }
   320  
   321  func TestContext2Validate_moduleProviderVar(t *testing.T) {
   322  	m := testModule(t, "validate-module-pc-vars")
   323  	p := testProvider("aws")
   324  	c := testContext2(t, &ContextOpts{
   325  		Module: m,
   326  		Providers: map[string]ResourceProviderFactory{
   327  			"aws": testProviderFuncFixed(p),
   328  		},
   329  		Variables: map[string]interface{}{
   330  			"provider_var": "bar",
   331  		},
   332  	})
   333  
   334  	p.ValidateFn = func(c *ResourceConfig) ([]string, []error) {
   335  		return nil, c.CheckSet([]string{"foo"})
   336  	}
   337  
   338  	w, e := c.Validate()
   339  	if len(w) > 0 {
   340  		t.Fatalf("bad: %#v", w)
   341  	}
   342  	if len(e) > 0 {
   343  		t.Fatalf("bad: %s", e)
   344  	}
   345  }
   346  
   347  func TestContext2Validate_moduleProviderInheritUnused(t *testing.T) {
   348  	m := testModule(t, "validate-module-pc-inherit-unused")
   349  	p := testProvider("aws")
   350  	c := testContext2(t, &ContextOpts{
   351  		Module: m,
   352  		Providers: map[string]ResourceProviderFactory{
   353  			"aws": testProviderFuncFixed(p),
   354  		},
   355  	})
   356  
   357  	p.ValidateFn = func(c *ResourceConfig) ([]string, []error) {
   358  		return nil, c.CheckSet([]string{"foo"})
   359  	}
   360  
   361  	w, e := c.Validate()
   362  	if len(w) > 0 {
   363  		t.Fatalf("bad: %#v", w)
   364  	}
   365  	if len(e) > 0 {
   366  		t.Fatalf("bad: %s", e)
   367  	}
   368  }
   369  
   370  func TestContext2Validate_orphans(t *testing.T) {
   371  	p := testProvider("aws")
   372  	m := testModule(t, "validate-good")
   373  	state := &State{
   374  		Modules: []*ModuleState{
   375  			&ModuleState{
   376  				Path: rootModulePath,
   377  				Resources: map[string]*ResourceState{
   378  					"aws_instance.web": &ResourceState{
   379  						Type: "aws_instance",
   380  						Primary: &InstanceState{
   381  							ID: "bar",
   382  						},
   383  					},
   384  				},
   385  			},
   386  		},
   387  	}
   388  	c := testContext2(t, &ContextOpts{
   389  		Module: m,
   390  		Providers: map[string]ResourceProviderFactory{
   391  			"aws": testProviderFuncFixed(p),
   392  		},
   393  		State: state,
   394  	})
   395  
   396  	p.ValidateResourceFn = func(
   397  		t string, c *ResourceConfig) ([]string, []error) {
   398  		return nil, c.CheckSet([]string{"foo"})
   399  	}
   400  
   401  	w, e := c.Validate()
   402  	if len(w) > 0 {
   403  		t.Fatalf("bad: %#v", w)
   404  	}
   405  	if len(e) > 0 {
   406  		t.Fatalf("bad: %s", e)
   407  	}
   408  }
   409  
   410  func TestContext2Validate_providerConfig_bad(t *testing.T) {
   411  	m := testModule(t, "validate-bad-pc")
   412  	p := testProvider("aws")
   413  	c := testContext2(t, &ContextOpts{
   414  		Module: m,
   415  		Providers: map[string]ResourceProviderFactory{
   416  			"aws": testProviderFuncFixed(p),
   417  		},
   418  	})
   419  
   420  	p.ValidateReturnErrors = []error{fmt.Errorf("bad")}
   421  
   422  	w, e := c.Validate()
   423  	if len(w) > 0 {
   424  		t.Fatalf("bad: %#v", w)
   425  	}
   426  	if len(e) == 0 {
   427  		t.Fatalf("bad: %s", e)
   428  	}
   429  	if !strings.Contains(fmt.Sprintf("%s", e), "bad") {
   430  		t.Fatalf("bad: %s", e)
   431  	}
   432  }
   433  
   434  func TestContext2Validate_providerConfig_badEmpty(t *testing.T) {
   435  	m := testModule(t, "validate-bad-pc-empty")
   436  	p := testProvider("aws")
   437  	c := testContext2(t, &ContextOpts{
   438  		Module: m,
   439  		Providers: map[string]ResourceProviderFactory{
   440  			"aws": testProviderFuncFixed(p),
   441  		},
   442  	})
   443  
   444  	p.ValidateReturnErrors = []error{fmt.Errorf("bad")}
   445  
   446  	w, e := c.Validate()
   447  	if len(w) > 0 {
   448  		t.Fatalf("bad: %#v", w)
   449  	}
   450  	if len(e) == 0 {
   451  		t.Fatalf("bad: %#v", e)
   452  	}
   453  }
   454  
   455  func TestContext2Validate_providerConfig_good(t *testing.T) {
   456  	m := testModule(t, "validate-bad-pc")
   457  	p := testProvider("aws")
   458  	c := testContext2(t, &ContextOpts{
   459  		Module: m,
   460  		Providers: map[string]ResourceProviderFactory{
   461  			"aws": testProviderFuncFixed(p),
   462  		},
   463  	})
   464  
   465  	w, e := c.Validate()
   466  	if len(w) > 0 {
   467  		t.Fatalf("bad: %#v", w)
   468  	}
   469  	if len(e) > 0 {
   470  		t.Fatalf("bad: %#v", e)
   471  	}
   472  }
   473  
   474  func TestContext2Validate_provisionerConfig_bad(t *testing.T) {
   475  	m := testModule(t, "validate-bad-prov-conf")
   476  	p := testProvider("aws")
   477  	pr := testProvisioner()
   478  	c := testContext2(t, &ContextOpts{
   479  		Module: m,
   480  		Providers: map[string]ResourceProviderFactory{
   481  			"aws": testProviderFuncFixed(p),
   482  		},
   483  		Provisioners: map[string]ResourceProvisionerFactory{
   484  			"shell": testProvisionerFuncFixed(pr),
   485  		},
   486  	})
   487  
   488  	pr.ValidateReturnErrors = []error{fmt.Errorf("bad")}
   489  
   490  	w, e := c.Validate()
   491  	if len(w) > 0 {
   492  		t.Fatalf("bad: %#v", w)
   493  	}
   494  	if len(e) == 0 {
   495  		t.Fatalf("bad: %#v", e)
   496  	}
   497  }
   498  
   499  func TestContext2Validate_provisionerConfig_good(t *testing.T) {
   500  	m := testModule(t, "validate-bad-prov-conf")
   501  	p := testProvider("aws")
   502  	pr := testProvisioner()
   503  	pr.ValidateFn = func(c *ResourceConfig) ([]string, []error) {
   504  		if c == nil {
   505  			t.Fatalf("missing resource config for provisioner")
   506  		}
   507  		return nil, c.CheckSet([]string{"command"})
   508  	}
   509  	c := testContext2(t, &ContextOpts{
   510  		Module: m,
   511  		Providers: map[string]ResourceProviderFactory{
   512  			"aws": testProviderFuncFixed(p),
   513  		},
   514  		Provisioners: map[string]ResourceProvisionerFactory{
   515  			"shell": testProvisionerFuncFixed(pr),
   516  		},
   517  	})
   518  
   519  	w, e := c.Validate()
   520  	if len(w) > 0 {
   521  		t.Fatalf("bad: %#v", w)
   522  	}
   523  	if len(e) > 0 {
   524  		t.Fatalf("bad: %#v", e)
   525  	}
   526  }
   527  
   528  func TestContext2Validate_requiredVar(t *testing.T) {
   529  	m := testModule(t, "validate-required-var")
   530  	p := testProvider("aws")
   531  	c := testContext2(t, &ContextOpts{
   532  		Module: m,
   533  		Providers: map[string]ResourceProviderFactory{
   534  			"aws": testProviderFuncFixed(p),
   535  		},
   536  	})
   537  
   538  	w, e := c.Validate()
   539  	if len(w) > 0 {
   540  		t.Fatalf("bad: %#v", w)
   541  	}
   542  	if len(e) == 0 {
   543  		t.Fatalf("bad: %s", e)
   544  	}
   545  }
   546  
   547  func TestContext2Validate_resourceConfig_bad(t *testing.T) {
   548  	m := testModule(t, "validate-bad-rc")
   549  	p := testProvider("aws")
   550  	c := testContext2(t, &ContextOpts{
   551  		Module: m,
   552  		Providers: map[string]ResourceProviderFactory{
   553  			"aws": testProviderFuncFixed(p),
   554  		},
   555  	})
   556  
   557  	p.ValidateResourceReturnErrors = []error{fmt.Errorf("bad")}
   558  
   559  	w, e := c.Validate()
   560  	if len(w) > 0 {
   561  		t.Fatalf("bad: %#v", w)
   562  	}
   563  	if len(e) == 0 {
   564  		t.Fatalf("bad: %s", e)
   565  	}
   566  }
   567  
   568  func TestContext2Validate_resourceConfig_good(t *testing.T) {
   569  	m := testModule(t, "validate-bad-rc")
   570  	p := testProvider("aws")
   571  	c := testContext2(t, &ContextOpts{
   572  		Module: m,
   573  		Providers: map[string]ResourceProviderFactory{
   574  			"aws": testProviderFuncFixed(p),
   575  		},
   576  	})
   577  
   578  	w, e := c.Validate()
   579  	if len(w) > 0 {
   580  		t.Fatalf("bad: %#v", w)
   581  	}
   582  	if len(e) > 0 {
   583  		t.Fatalf("bad: %#v", e)
   584  	}
   585  }
   586  
   587  func TestContext2Validate_resourceNameSymbol(t *testing.T) {
   588  	p := testProvider("aws")
   589  	m := testModule(t, "validate-resource-name-symbol")
   590  	c := testContext2(t, &ContextOpts{
   591  		Module: m,
   592  		Providers: map[string]ResourceProviderFactory{
   593  			"aws": testProviderFuncFixed(p),
   594  		},
   595  	})
   596  
   597  	w, e := c.Validate()
   598  	if len(w) > 0 {
   599  		t.Fatalf("bad: %#v", w)
   600  	}
   601  	if len(e) == 0 {
   602  		t.Fatalf("bad: %s", e)
   603  	}
   604  }
   605  
   606  func TestContext2Validate_selfRef(t *testing.T) {
   607  	p := testProvider("aws")
   608  	m := testModule(t, "validate-self-ref")
   609  	c := testContext2(t, &ContextOpts{
   610  		Module: m,
   611  		Providers: map[string]ResourceProviderFactory{
   612  			"aws": testProviderFuncFixed(p),
   613  		},
   614  	})
   615  
   616  	w, e := c.Validate()
   617  	if len(w) > 0 {
   618  		t.Fatalf("bad: %#v", w)
   619  	}
   620  	if len(e) == 0 {
   621  		t.Fatalf("bad: %s", e)
   622  	}
   623  }
   624  
   625  func TestContext2Validate_selfRefMulti(t *testing.T) {
   626  	p := testProvider("aws")
   627  	m := testModule(t, "validate-self-ref-multi")
   628  	c := testContext2(t, &ContextOpts{
   629  		Module: m,
   630  		Providers: map[string]ResourceProviderFactory{
   631  			"aws": testProviderFuncFixed(p),
   632  		},
   633  	})
   634  
   635  	w, e := c.Validate()
   636  	if len(w) > 0 {
   637  		t.Fatalf("bad: %#v", w)
   638  	}
   639  	if len(e) == 0 {
   640  		t.Fatalf("bad: %#v", e)
   641  	}
   642  }
   643  
   644  func TestContext2Validate_selfRefMultiAll(t *testing.T) {
   645  	p := testProvider("aws")
   646  	m := testModule(t, "validate-self-ref-multi-all")
   647  	c := testContext2(t, &ContextOpts{
   648  		Module: m,
   649  		Providers: map[string]ResourceProviderFactory{
   650  			"aws": testProviderFuncFixed(p),
   651  		},
   652  	})
   653  
   654  	w, e := c.Validate()
   655  	if len(w) > 0 {
   656  		t.Fatalf("bad: %#v", w)
   657  	}
   658  	if len(e) == 0 {
   659  		t.Fatalf("bad: %#v", e)
   660  	}
   661  }
   662  
   663  func TestContext2Validate_tainted(t *testing.T) {
   664  	p := testProvider("aws")
   665  	m := testModule(t, "validate-good")
   666  	state := &State{
   667  		Modules: []*ModuleState{
   668  			&ModuleState{
   669  				Path: rootModulePath,
   670  				Resources: map[string]*ResourceState{
   671  					"aws_instance.foo": &ResourceState{
   672  						Type: "aws_instance",
   673  						Primary: &InstanceState{
   674  							ID:      "bar",
   675  							Tainted: true,
   676  						},
   677  					},
   678  				},
   679  			},
   680  		},
   681  	}
   682  	c := testContext2(t, &ContextOpts{
   683  		Module: m,
   684  		Providers: map[string]ResourceProviderFactory{
   685  			"aws": testProviderFuncFixed(p),
   686  		},
   687  		State: state,
   688  	})
   689  
   690  	p.ValidateResourceFn = func(
   691  		t string, c *ResourceConfig) ([]string, []error) {
   692  		return nil, c.CheckSet([]string{"foo"})
   693  	}
   694  
   695  	w, e := c.Validate()
   696  	if len(w) > 0 {
   697  		t.Fatalf("bad: %#v", w)
   698  	}
   699  	if len(e) > 0 {
   700  		t.Fatalf("bad: %#v", e)
   701  	}
   702  }
   703  
   704  func TestContext2Validate_targetedDestroy(t *testing.T) {
   705  	m := testModule(t, "validate-targeted")
   706  	p := testProvider("aws")
   707  	pr := testProvisioner()
   708  	p.ApplyFn = testApplyFn
   709  	p.DiffFn = testDiffFn
   710  	ctx := testContext2(t, &ContextOpts{
   711  		Module: m,
   712  		Providers: map[string]ResourceProviderFactory{
   713  			"aws": testProviderFuncFixed(p),
   714  		},
   715  		Provisioners: map[string]ResourceProvisionerFactory{
   716  			"shell": testProvisionerFuncFixed(pr),
   717  		},
   718  		State: &State{
   719  			Modules: []*ModuleState{
   720  				&ModuleState{
   721  					Path: rootModulePath,
   722  					Resources: map[string]*ResourceState{
   723  						"aws_instance.foo": resourceState("aws_instance", "i-bcd345"),
   724  						"aws_instance.bar": resourceState("aws_instance", "i-abc123"),
   725  					},
   726  				},
   727  			},
   728  		},
   729  		Targets: []string{"aws_instance.foo"},
   730  		Destroy: true,
   731  	})
   732  
   733  	w, e := ctx.Validate()
   734  	if len(w) > 0 {
   735  		warnStr := ""
   736  		for _, v := range w {
   737  			warnStr = warnStr + " " + v
   738  		}
   739  		t.Fatalf("bad: %s", warnStr)
   740  	}
   741  	if len(e) > 0 {
   742  		t.Fatalf("bad: %s", e)
   743  	}
   744  }
   745  
   746  func TestContext2Validate_varRefFilled(t *testing.T) {
   747  	m := testModule(t, "validate-variable-ref")
   748  	p := testProvider("aws")
   749  	c := testContext2(t, &ContextOpts{
   750  		Module: m,
   751  		Providers: map[string]ResourceProviderFactory{
   752  			"aws": testProviderFuncFixed(p),
   753  		},
   754  		Variables: map[string]interface{}{
   755  			"foo": "bar",
   756  		},
   757  	})
   758  
   759  	var value interface{}
   760  	p.ValidateResourceFn = func(t string, c *ResourceConfig) ([]string, []error) {
   761  		value, _ = c.Get("foo")
   762  		return nil, nil
   763  	}
   764  
   765  	c.Validate()
   766  	if value != "bar" {
   767  		t.Fatalf("bad: %#v", value)
   768  	}
   769  }
   770  
   771  // Module variables weren't being interpolated during Validate phase.
   772  // related to https://github.com/hashicorp/terraform/issues/5322
   773  func TestContext2Validate_interpolateVar(t *testing.T) {
   774  	input := new(MockUIInput)
   775  
   776  	m := testModule(t, "input-interpolate-var")
   777  	p := testProvider("null")
   778  	p.ApplyFn = testApplyFn
   779  	p.DiffFn = testDiffFn
   780  
   781  	ctx := testContext2(t, &ContextOpts{
   782  		Module: m,
   783  		Providers: map[string]ResourceProviderFactory{
   784  			"template": testProviderFuncFixed(p),
   785  		},
   786  		UIInput: input,
   787  	})
   788  
   789  	w, e := ctx.Validate()
   790  	if w != nil {
   791  		t.Log("warnings:", w)
   792  	}
   793  	if e != nil {
   794  		t.Fatal("err:", e)
   795  	}
   796  }
   797  
   798  // When module vars reference something that is actually computed, this
   799  // shouldn't cause validation to fail.
   800  func TestContext2Validate_interpolateComputedModuleVarDef(t *testing.T) {
   801  	input := new(MockUIInput)
   802  
   803  	m := testModule(t, "validate-computed-module-var-ref")
   804  	p := testProvider("aws")
   805  	p.ApplyFn = testApplyFn
   806  	p.DiffFn = testDiffFn
   807  
   808  	ctx := testContext2(t, &ContextOpts{
   809  		Module: m,
   810  		Providers: map[string]ResourceProviderFactory{
   811  			"aws": testProviderFuncFixed(p),
   812  		},
   813  		UIInput: input,
   814  	})
   815  
   816  	w, e := ctx.Validate()
   817  	if w != nil {
   818  		t.Log("warnings:", w)
   819  	}
   820  	if e != nil {
   821  		t.Fatal("err:", e)
   822  	}
   823  }