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