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