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