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