github.com/adrian-bl/terraform@v0.7.0-rc2.0.20160705220747-de0a34fc3517/terraform/context_plan_test.go (about)

     1  package terraform
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"os"
     7  	"reflect"
     8  	"sort"
     9  	"strings"
    10  	"sync"
    11  	"testing"
    12  )
    13  
    14  func TestContext2Plan(t *testing.T) {
    15  	m := testModule(t, "plan-good")
    16  	p := testProvider("aws")
    17  	p.DiffFn = testDiffFn
    18  	ctx := testContext2(t, &ContextOpts{
    19  		Module: m,
    20  		Providers: map[string]ResourceProviderFactory{
    21  			"aws": testProviderFuncFixed(p),
    22  		},
    23  	})
    24  
    25  	plan, err := ctx.Plan()
    26  	if err != nil {
    27  		t.Fatalf("err: %s", err)
    28  	}
    29  
    30  	if len(plan.Diff.RootModule().Resources) < 2 {
    31  		t.Fatalf("bad: %#v", plan.Diff.RootModule().Resources)
    32  	}
    33  
    34  	actual := strings.TrimSpace(plan.String())
    35  	expected := strings.TrimSpace(testTerraformPlanStr)
    36  	if actual != expected {
    37  		t.Fatalf("bad:\n%s", actual)
    38  	}
    39  }
    40  
    41  func TestContext2Plan_createBefore_maintainRoot(t *testing.T) {
    42  	m := testModule(t, "plan-cbd-maintain-root")
    43  	p := testProvider("aws")
    44  	p.DiffFn = testDiffFn
    45  	ctx := testContext2(t, &ContextOpts{
    46  		Module: m,
    47  		Providers: map[string]ResourceProviderFactory{
    48  			"aws": testProviderFuncFixed(p),
    49  		},
    50  		Variables: map[string]string{
    51  			"in": "a,b,c",
    52  		},
    53  	})
    54  
    55  	plan, err := ctx.Plan()
    56  	if err != nil {
    57  		t.Fatalf("err: %s", err)
    58  	}
    59  
    60  	actual := strings.TrimSpace(plan.String())
    61  	expected := strings.TrimSpace(`
    62  DIFF:
    63  
    64  CREATE: aws_instance.bar.0
    65  CREATE: aws_instance.bar.1
    66  CREATE: aws_instance.foo.0
    67  CREATE: aws_instance.foo.1
    68  
    69  STATE:
    70  
    71  <no state>
    72  		`)
    73  	if actual != expected {
    74  		t.Fatalf("expected:\n%s, got:\n%s", expected, actual)
    75  	}
    76  }
    77  
    78  func TestContext2Plan_emptyDiff(t *testing.T) {
    79  	m := testModule(t, "plan-empty")
    80  	p := testProvider("aws")
    81  	p.DiffFn = func(
    82  		info *InstanceInfo,
    83  		s *InstanceState,
    84  		c *ResourceConfig) (*InstanceDiff, error) {
    85  		return nil, nil
    86  	}
    87  
    88  	ctx := testContext2(t, &ContextOpts{
    89  		Module: m,
    90  		Providers: map[string]ResourceProviderFactory{
    91  			"aws": testProviderFuncFixed(p),
    92  		},
    93  	})
    94  
    95  	plan, err := ctx.Plan()
    96  	if err != nil {
    97  		t.Fatalf("err: %s", err)
    98  	}
    99  
   100  	actual := strings.TrimSpace(plan.String())
   101  	expected := strings.TrimSpace(testTerraformPlanEmptyStr)
   102  	if actual != expected {
   103  		t.Fatalf("bad:\n%s", actual)
   104  	}
   105  }
   106  
   107  func TestContext2Plan_escapedVar(t *testing.T) {
   108  	m := testModule(t, "plan-escaped-var")
   109  	p := testProvider("aws")
   110  	p.DiffFn = testDiffFn
   111  	ctx := testContext2(t, &ContextOpts{
   112  		Module: m,
   113  		Providers: map[string]ResourceProviderFactory{
   114  			"aws": testProviderFuncFixed(p),
   115  		},
   116  	})
   117  
   118  	plan, err := ctx.Plan()
   119  	if err != nil {
   120  		t.Fatalf("err: %s", err)
   121  	}
   122  
   123  	actual := strings.TrimSpace(plan.String())
   124  	expected := strings.TrimSpace(testTerraformPlanEscapedVarStr)
   125  	if actual != expected {
   126  		t.Fatalf("bad:\n%s", actual)
   127  	}
   128  }
   129  
   130  func TestContext2Plan_minimal(t *testing.T) {
   131  	m := testModule(t, "plan-empty")
   132  	p := testProvider("aws")
   133  	p.DiffFn = testDiffFn
   134  	ctx := testContext2(t, &ContextOpts{
   135  		Module: m,
   136  		Providers: map[string]ResourceProviderFactory{
   137  			"aws": testProviderFuncFixed(p),
   138  		},
   139  	})
   140  
   141  	plan, err := ctx.Plan()
   142  	if err != nil {
   143  		t.Fatalf("err: %s", err)
   144  	}
   145  
   146  	actual := strings.TrimSpace(plan.String())
   147  	expected := strings.TrimSpace(testTerraformPlanEmptyStr)
   148  	if actual != expected {
   149  		t.Fatalf("bad:\n%s", actual)
   150  	}
   151  }
   152  
   153  func TestContext2Plan_modules(t *testing.T) {
   154  	m := testModule(t, "plan-modules")
   155  	p := testProvider("aws")
   156  	p.DiffFn = testDiffFn
   157  	ctx := testContext2(t, &ContextOpts{
   158  		Module: m,
   159  		Providers: map[string]ResourceProviderFactory{
   160  			"aws": testProviderFuncFixed(p),
   161  		},
   162  	})
   163  
   164  	plan, err := ctx.Plan()
   165  	if err != nil {
   166  		t.Fatalf("err: %s", err)
   167  	}
   168  
   169  	actual := strings.TrimSpace(plan.String())
   170  	expected := strings.TrimSpace(testTerraformPlanModulesStr)
   171  	if actual != expected {
   172  		t.Fatalf("bad:\n%s", actual)
   173  	}
   174  }
   175  
   176  // GH-1475
   177  func TestContext2Plan_moduleCycle(t *testing.T) {
   178  	m := testModule(t, "plan-module-cycle")
   179  	p := testProvider("aws")
   180  	p.DiffFn = testDiffFn
   181  	ctx := testContext2(t, &ContextOpts{
   182  		Module: m,
   183  		Providers: map[string]ResourceProviderFactory{
   184  			"aws": testProviderFuncFixed(p),
   185  		},
   186  	})
   187  
   188  	plan, err := ctx.Plan()
   189  	if err != nil {
   190  		t.Fatalf("err: %s", err)
   191  	}
   192  
   193  	actual := strings.TrimSpace(plan.String())
   194  	expected := strings.TrimSpace(testTerraformPlanModuleCycleStr)
   195  	if actual != expected {
   196  		t.Fatalf("bad:\n%s", actual)
   197  	}
   198  }
   199  
   200  func TestContext2Plan_moduleDeadlock(t *testing.T) {
   201  	testCheckDeadlock(t, func() {
   202  		m := testModule(t, "plan-module-deadlock")
   203  		p := testProvider("aws")
   204  		p.DiffFn = testDiffFn
   205  
   206  		ctx := testContext2(t, &ContextOpts{
   207  			Module: m,
   208  			Providers: map[string]ResourceProviderFactory{
   209  				"aws": testProviderFuncFixed(p),
   210  			},
   211  		})
   212  
   213  		plan, err := ctx.Plan()
   214  		if err != nil {
   215  			t.Fatalf("err: %s", err)
   216  		}
   217  
   218  		actual := strings.TrimSpace(plan.String())
   219  		expected := strings.TrimSpace(`
   220  DIFF:
   221  
   222  module.child:
   223    CREATE: aws_instance.foo.0
   224    CREATE: aws_instance.foo.1
   225    CREATE: aws_instance.foo.2
   226  
   227  STATE:
   228  
   229  <no state>
   230  		`)
   231  		if actual != expected {
   232  			t.Fatalf("expected:\n%sgot:\n%s", expected, actual)
   233  		}
   234  	})
   235  }
   236  
   237  func TestContext2Plan_moduleInput(t *testing.T) {
   238  	m := testModule(t, "plan-module-input")
   239  	p := testProvider("aws")
   240  	p.DiffFn = testDiffFn
   241  	ctx := testContext2(t, &ContextOpts{
   242  		Module: m,
   243  		Providers: map[string]ResourceProviderFactory{
   244  			"aws": testProviderFuncFixed(p),
   245  		},
   246  	})
   247  
   248  	plan, err := ctx.Plan()
   249  	if err != nil {
   250  		t.Fatalf("err: %s", err)
   251  	}
   252  
   253  	actual := strings.TrimSpace(plan.String())
   254  	expected := strings.TrimSpace(testTerraformPlanModuleInputStr)
   255  	if actual != expected {
   256  		t.Fatalf("bad:\n%s", actual)
   257  	}
   258  }
   259  
   260  func TestContext2Plan_moduleInputComputed(t *testing.T) {
   261  	m := testModule(t, "plan-module-input-computed")
   262  	p := testProvider("aws")
   263  	p.DiffFn = testDiffFn
   264  	ctx := testContext2(t, &ContextOpts{
   265  		Module: m,
   266  		Providers: map[string]ResourceProviderFactory{
   267  			"aws": testProviderFuncFixed(p),
   268  		},
   269  	})
   270  
   271  	plan, err := ctx.Plan()
   272  	if err != nil {
   273  		t.Fatalf("err: %s", err)
   274  	}
   275  
   276  	actual := strings.TrimSpace(plan.String())
   277  	expected := strings.TrimSpace(testTerraformPlanModuleInputComputedStr)
   278  	if actual != expected {
   279  		t.Fatalf("bad:\n%s", actual)
   280  	}
   281  }
   282  
   283  func TestContext2Plan_moduleInputFromVar(t *testing.T) {
   284  	m := testModule(t, "plan-module-input-var")
   285  	p := testProvider("aws")
   286  	p.DiffFn = testDiffFn
   287  	ctx := testContext2(t, &ContextOpts{
   288  		Module: m,
   289  		Providers: map[string]ResourceProviderFactory{
   290  			"aws": testProviderFuncFixed(p),
   291  		},
   292  		Variables: map[string]string{
   293  			"foo": "52",
   294  		},
   295  	})
   296  
   297  	plan, err := ctx.Plan()
   298  	if err != nil {
   299  		t.Fatalf("err: %s", err)
   300  	}
   301  
   302  	actual := strings.TrimSpace(plan.String())
   303  	expected := strings.TrimSpace(testTerraformPlanModuleInputVarStr)
   304  	if actual != expected {
   305  		t.Fatalf("bad:\n%s", actual)
   306  	}
   307  }
   308  
   309  func TestContext2Plan_moduleMultiVar(t *testing.T) {
   310  	m := testModule(t, "plan-module-multi-var")
   311  	p := testProvider("aws")
   312  	p.DiffFn = testDiffFn
   313  	ctx := testContext2(t, &ContextOpts{
   314  		Module: m,
   315  		Providers: map[string]ResourceProviderFactory{
   316  			"aws": testProviderFuncFixed(p),
   317  		},
   318  	})
   319  
   320  	plan, err := ctx.Plan()
   321  	if err != nil {
   322  		t.Fatalf("err: %s", err)
   323  	}
   324  
   325  	actual := strings.TrimSpace(plan.String())
   326  	expected := strings.TrimSpace(testTerraformPlanModuleMultiVarStr)
   327  	if actual != expected {
   328  		t.Fatalf("bad:\n%s", actual)
   329  	}
   330  }
   331  
   332  func TestContext2Plan_moduleOrphans(t *testing.T) {
   333  	m := testModule(t, "plan-modules-remove")
   334  	p := testProvider("aws")
   335  	p.DiffFn = testDiffFn
   336  	s := &State{
   337  		Modules: []*ModuleState{
   338  			&ModuleState{
   339  				Path: []string{"root", "child"},
   340  				Resources: map[string]*ResourceState{
   341  					"aws_instance.foo": &ResourceState{
   342  						Type: "aws_instance",
   343  						Primary: &InstanceState{
   344  							ID: "baz",
   345  						},
   346  					},
   347  				},
   348  			},
   349  		},
   350  	}
   351  	ctx := testContext2(t, &ContextOpts{
   352  		Module: m,
   353  		Providers: map[string]ResourceProviderFactory{
   354  			"aws": testProviderFuncFixed(p),
   355  		},
   356  		State: s,
   357  	})
   358  
   359  	plan, err := ctx.Plan()
   360  	if err != nil {
   361  		t.Fatalf("err: %s", err)
   362  	}
   363  
   364  	actual := strings.TrimSpace(plan.String())
   365  	expected := strings.TrimSpace(testTerraformPlanModuleOrphansStr)
   366  	if actual != expected {
   367  		t.Fatalf("bad:\n%s", actual)
   368  	}
   369  }
   370  
   371  // https://github.com/hashicorp/terraform/issues/3114
   372  func TestContext2Plan_moduleOrphansWithProvisioner(t *testing.T) {
   373  	m := testModule(t, "plan-modules-remove-provisioners")
   374  	p := testProvider("aws")
   375  	pr := testProvisioner()
   376  	p.DiffFn = testDiffFn
   377  	s := &State{
   378  		Modules: []*ModuleState{
   379  			&ModuleState{
   380  				Path: []string{"root"},
   381  				Resources: map[string]*ResourceState{
   382  					"aws_instance.top": &ResourceState{
   383  						Type: "aws_instance",
   384  						Primary: &InstanceState{
   385  							ID: "top",
   386  						},
   387  					},
   388  				},
   389  			},
   390  			&ModuleState{
   391  				Path: []string{"root", "parent", "childone"},
   392  				Resources: map[string]*ResourceState{
   393  					"aws_instance.foo": &ResourceState{
   394  						Type: "aws_instance",
   395  						Primary: &InstanceState{
   396  							ID: "baz",
   397  						},
   398  					},
   399  				},
   400  			},
   401  			&ModuleState{
   402  				Path: []string{"root", "parent", "childtwo"},
   403  				Resources: map[string]*ResourceState{
   404  					"aws_instance.foo": &ResourceState{
   405  						Type: "aws_instance",
   406  						Primary: &InstanceState{
   407  							ID: "baz",
   408  						},
   409  					},
   410  				},
   411  			},
   412  		},
   413  	}
   414  	ctx := testContext2(t, &ContextOpts{
   415  		Module: m,
   416  		Providers: map[string]ResourceProviderFactory{
   417  			"aws": testProviderFuncFixed(p),
   418  		},
   419  		Provisioners: map[string]ResourceProvisionerFactory{
   420  			"shell": testProvisionerFuncFixed(pr),
   421  		},
   422  		State: s,
   423  	})
   424  
   425  	plan, err := ctx.Plan()
   426  	if err != nil {
   427  		t.Fatalf("err: %s", err)
   428  	}
   429  
   430  	actual := strings.TrimSpace(plan.String())
   431  	expected := strings.TrimSpace(`
   432  DIFF:
   433  
   434  module.parent.childone:
   435    DESTROY: aws_instance.foo
   436  module.parent.childtwo:
   437    DESTROY: aws_instance.foo
   438  
   439  STATE:
   440  
   441  aws_instance.top:
   442    ID = top
   443  
   444  module.parent.childone:
   445    aws_instance.foo:
   446      ID = baz
   447  module.parent.childtwo:
   448    aws_instance.foo:
   449      ID = baz
   450  	`)
   451  	if actual != expected {
   452  		t.Fatalf("bad:\n%s", actual)
   453  	}
   454  }
   455  
   456  func TestContext2Plan_moduleProviderInherit(t *testing.T) {
   457  	var l sync.Mutex
   458  	var calls []string
   459  
   460  	m := testModule(t, "plan-module-provider-inherit")
   461  	ctx := testContext2(t, &ContextOpts{
   462  		Module: m,
   463  		Providers: map[string]ResourceProviderFactory{
   464  			"aws": func() (ResourceProvider, error) {
   465  				l.Lock()
   466  				defer l.Unlock()
   467  
   468  				p := testProvider("aws")
   469  				p.ConfigureFn = func(c *ResourceConfig) error {
   470  					if v, ok := c.Get("from"); !ok || v.(string) != "root" {
   471  						return fmt.Errorf("bad")
   472  					}
   473  
   474  					return nil
   475  				}
   476  				p.DiffFn = func(
   477  					info *InstanceInfo,
   478  					state *InstanceState,
   479  					c *ResourceConfig) (*InstanceDiff, error) {
   480  					v, _ := c.Get("from")
   481  					calls = append(calls, v.(string))
   482  					return testDiffFn(info, state, c)
   483  				}
   484  				return p, nil
   485  			},
   486  		},
   487  	})
   488  
   489  	_, err := ctx.Plan()
   490  	if err != nil {
   491  		t.Fatalf("err: %s", err)
   492  	}
   493  
   494  	actual := calls
   495  	sort.Strings(actual)
   496  	expected := []string{"child", "root"}
   497  	if !reflect.DeepEqual(actual, expected) {
   498  		t.Fatalf("bad: %#v", actual)
   499  	}
   500  }
   501  
   502  func TestContext2Plan_moduleProviderDefaults(t *testing.T) {
   503  	var l sync.Mutex
   504  	var calls []string
   505  	toCount := 0
   506  
   507  	m := testModule(t, "plan-module-provider-defaults")
   508  	ctx := testContext2(t, &ContextOpts{
   509  		Module: m,
   510  		Providers: map[string]ResourceProviderFactory{
   511  			"aws": func() (ResourceProvider, error) {
   512  				l.Lock()
   513  				defer l.Unlock()
   514  
   515  				p := testProvider("aws")
   516  				p.ConfigureFn = func(c *ResourceConfig) error {
   517  					if v, ok := c.Get("from"); !ok || v.(string) != "root" {
   518  						return fmt.Errorf("bad")
   519  					}
   520  					if v, ok := c.Get("to"); ok && v.(string) == "child" {
   521  						toCount++
   522  					}
   523  
   524  					return nil
   525  				}
   526  				p.DiffFn = func(
   527  					info *InstanceInfo,
   528  					state *InstanceState,
   529  					c *ResourceConfig) (*InstanceDiff, error) {
   530  					v, _ := c.Get("from")
   531  					calls = append(calls, v.(string))
   532  					return testDiffFn(info, state, c)
   533  				}
   534  				return p, nil
   535  			},
   536  		},
   537  	})
   538  
   539  	_, err := ctx.Plan()
   540  	if err != nil {
   541  		t.Fatalf("err: %s", err)
   542  	}
   543  
   544  	if toCount != 1 {
   545  		t.Fatalf(
   546  			"provider in child didn't set proper config\n\n"+
   547  				"toCount: %d", toCount)
   548  	}
   549  
   550  	actual := calls
   551  	sort.Strings(actual)
   552  	expected := []string{"child", "root"}
   553  	if !reflect.DeepEqual(actual, expected) {
   554  		t.Fatalf("bad: %#v", actual)
   555  	}
   556  }
   557  
   558  func TestContext2Plan_moduleProviderDefaultsVar(t *testing.T) {
   559  	var l sync.Mutex
   560  	var calls []string
   561  
   562  	m := testModule(t, "plan-module-provider-defaults-var")
   563  	ctx := testContext2(t, &ContextOpts{
   564  		Module: m,
   565  		Providers: map[string]ResourceProviderFactory{
   566  			"aws": func() (ResourceProvider, error) {
   567  				l.Lock()
   568  				defer l.Unlock()
   569  
   570  				p := testProvider("aws")
   571  				p.ConfigureFn = func(c *ResourceConfig) error {
   572  					var buf bytes.Buffer
   573  					if v, ok := c.Get("from"); ok {
   574  						buf.WriteString(v.(string) + "\n")
   575  					}
   576  					if v, ok := c.Get("to"); ok {
   577  						buf.WriteString(v.(string) + "\n")
   578  					}
   579  
   580  					calls = append(calls, buf.String())
   581  					return nil
   582  				}
   583  				p.DiffFn = testDiffFn
   584  				return p, nil
   585  			},
   586  		},
   587  		Variables: map[string]string{
   588  			"foo": "root",
   589  		},
   590  	})
   591  
   592  	_, err := ctx.Plan()
   593  	if err != nil {
   594  		t.Fatalf("err: %s", err)
   595  	}
   596  
   597  	expected := []string{
   598  		"root\n",
   599  		"root\nchild\n",
   600  	}
   601  	if !reflect.DeepEqual(calls, expected) {
   602  		t.Fatalf("BAD: %#v", calls)
   603  	}
   604  }
   605  
   606  func TestContext2Plan_moduleVar(t *testing.T) {
   607  	m := testModule(t, "plan-module-var")
   608  	p := testProvider("aws")
   609  	p.DiffFn = testDiffFn
   610  	ctx := testContext2(t, &ContextOpts{
   611  		Module: m,
   612  		Providers: map[string]ResourceProviderFactory{
   613  			"aws": testProviderFuncFixed(p),
   614  		},
   615  	})
   616  
   617  	plan, err := ctx.Plan()
   618  	if err != nil {
   619  		t.Fatalf("err: %s", err)
   620  	}
   621  
   622  	actual := strings.TrimSpace(plan.String())
   623  	expected := strings.TrimSpace(testTerraformPlanModuleVarStr)
   624  	if actual != expected {
   625  		t.Fatalf("bad:\n%s", actual)
   626  	}
   627  }
   628  
   629  func TestContext2Plan_moduleVarWrongType(t *testing.T) {
   630  	m := testModule(t, "plan-module-wrong-var-type")
   631  	p := testProvider("aws")
   632  	p.DiffFn = testDiffFn
   633  	ctx := testContext2(t, &ContextOpts{
   634  		Module: m,
   635  		Providers: map[string]ResourceProviderFactory{
   636  			"aws": testProviderFuncFixed(p),
   637  		},
   638  	})
   639  
   640  	_, err := ctx.Plan()
   641  	if err == nil {
   642  		t.Fatalf("should error")
   643  	}
   644  }
   645  
   646  func TestContext2Plan_moduleVarWrongTypeNested(t *testing.T) {
   647  	m := testModule(t, "plan-module-wrong-var-type-nested")
   648  	p := testProvider("aws")
   649  	p.DiffFn = testDiffFn
   650  	ctx := testContext2(t, &ContextOpts{
   651  		Module: m,
   652  		Providers: map[string]ResourceProviderFactory{
   653  			"aws": testProviderFuncFixed(p),
   654  		},
   655  	})
   656  
   657  	_, err := ctx.Plan()
   658  	if err == nil {
   659  		t.Fatalf("should error")
   660  	}
   661  }
   662  
   663  func TestContext2Plan_moduleVarWithDefaultValue(t *testing.T) {
   664  	m := testModule(t, "plan-module-var-with-default-value")
   665  	p := testProvider("null")
   666  	p.DiffFn = testDiffFn
   667  	ctx := testContext2(t, &ContextOpts{
   668  		Module: m,
   669  		Providers: map[string]ResourceProviderFactory{
   670  			"null": testProviderFuncFixed(p),
   671  		},
   672  	})
   673  
   674  	_, err := ctx.Plan()
   675  	if err != nil {
   676  		t.Fatalf("bad: %s", err)
   677  	}
   678  }
   679  
   680  func TestContext2Plan_moduleVarComputed(t *testing.T) {
   681  	m := testModule(t, "plan-module-var-computed")
   682  	p := testProvider("aws")
   683  	p.DiffFn = testDiffFn
   684  	ctx := testContext2(t, &ContextOpts{
   685  		Module: m,
   686  		Providers: map[string]ResourceProviderFactory{
   687  			"aws": testProviderFuncFixed(p),
   688  		},
   689  	})
   690  
   691  	plan, err := ctx.Plan()
   692  	if err != nil {
   693  		t.Fatalf("err: %s", err)
   694  	}
   695  
   696  	actual := strings.TrimSpace(plan.String())
   697  	expected := strings.TrimSpace(testTerraformPlanModuleVarComputedStr)
   698  	if actual != expected {
   699  		t.Fatalf("bad:\n%s", actual)
   700  	}
   701  }
   702  
   703  func TestContext2Plan_nil(t *testing.T) {
   704  	m := testModule(t, "plan-nil")
   705  	p := testProvider("aws")
   706  	p.DiffFn = testDiffFn
   707  	ctx := testContext2(t, &ContextOpts{
   708  		Module: m,
   709  		Providers: map[string]ResourceProviderFactory{
   710  			"aws": testProviderFuncFixed(p),
   711  		},
   712  		State: &State{
   713  			Modules: []*ModuleState{
   714  				&ModuleState{
   715  					Path: rootModulePath,
   716  					Resources: map[string]*ResourceState{
   717  						"aws_instance.foo": &ResourceState{
   718  							Type: "aws_instance",
   719  							Primary: &InstanceState{
   720  								ID: "bar",
   721  							},
   722  						},
   723  					},
   724  				},
   725  			},
   726  		},
   727  	})
   728  
   729  	plan, err := ctx.Plan()
   730  	if err != nil {
   731  		t.Fatalf("err: %s", err)
   732  	}
   733  	if len(plan.Diff.RootModule().Resources) != 0 {
   734  		t.Fatalf("bad: %#v", plan.Diff.RootModule().Resources)
   735  	}
   736  }
   737  
   738  func TestContext2Plan_preventDestroy_bad(t *testing.T) {
   739  	m := testModule(t, "plan-prevent-destroy-bad")
   740  	p := testProvider("aws")
   741  	p.DiffFn = testDiffFn
   742  	ctx := testContext2(t, &ContextOpts{
   743  		Module: m,
   744  		Providers: map[string]ResourceProviderFactory{
   745  			"aws": testProviderFuncFixed(p),
   746  		},
   747  		State: &State{
   748  			Modules: []*ModuleState{
   749  				&ModuleState{
   750  					Path: rootModulePath,
   751  					Resources: map[string]*ResourceState{
   752  						"aws_instance.foo": &ResourceState{
   753  							Type: "aws_instance",
   754  							Primary: &InstanceState{
   755  								ID: "i-abc123",
   756  							},
   757  						},
   758  					},
   759  				},
   760  			},
   761  		},
   762  	})
   763  
   764  	plan, err := ctx.Plan()
   765  
   766  	expectedErr := "aws_instance.foo: the plan would destroy"
   767  	if !strings.Contains(fmt.Sprintf("%s", err), expectedErr) {
   768  		t.Fatalf("expected err would contain %q\nerr: %s\nplan: %s",
   769  			expectedErr, err, plan)
   770  	}
   771  }
   772  
   773  func TestContext2Plan_preventDestroy_good(t *testing.T) {
   774  	m := testModule(t, "plan-prevent-destroy-good")
   775  	p := testProvider("aws")
   776  	p.DiffFn = testDiffFn
   777  	ctx := testContext2(t, &ContextOpts{
   778  		Module: m,
   779  		Providers: map[string]ResourceProviderFactory{
   780  			"aws": testProviderFuncFixed(p),
   781  		},
   782  		State: &State{
   783  			Modules: []*ModuleState{
   784  				&ModuleState{
   785  					Path: rootModulePath,
   786  					Resources: map[string]*ResourceState{
   787  						"aws_instance.foo": &ResourceState{
   788  							Type: "aws_instance",
   789  							Primary: &InstanceState{
   790  								ID: "i-abc123",
   791  							},
   792  						},
   793  					},
   794  				},
   795  			},
   796  		},
   797  	})
   798  
   799  	plan, err := ctx.Plan()
   800  	if err != nil {
   801  		t.Fatalf("err: %s", err)
   802  	}
   803  	if !plan.Diff.Empty() {
   804  		t.Fatalf("Expected empty plan, got %s", plan.String())
   805  	}
   806  }
   807  
   808  func TestContext2Plan_preventDestroy_destroyPlan(t *testing.T) {
   809  	m := testModule(t, "plan-prevent-destroy-good")
   810  	p := testProvider("aws")
   811  	p.DiffFn = testDiffFn
   812  	ctx := testContext2(t, &ContextOpts{
   813  		Module: m,
   814  		Providers: map[string]ResourceProviderFactory{
   815  			"aws": testProviderFuncFixed(p),
   816  		},
   817  		State: &State{
   818  			Modules: []*ModuleState{
   819  				&ModuleState{
   820  					Path: rootModulePath,
   821  					Resources: map[string]*ResourceState{
   822  						"aws_instance.foo": &ResourceState{
   823  							Type: "aws_instance",
   824  							Primary: &InstanceState{
   825  								ID: "i-abc123",
   826  							},
   827  						},
   828  					},
   829  				},
   830  			},
   831  		},
   832  		Destroy: true,
   833  	})
   834  
   835  	plan, err := ctx.Plan()
   836  
   837  	expectedErr := "aws_instance.foo: the plan would destroy"
   838  	if !strings.Contains(fmt.Sprintf("%s", err), expectedErr) {
   839  		t.Fatalf("expected err would contain %q\nerr: %s\nplan: %s",
   840  			expectedErr, err, plan)
   841  	}
   842  }
   843  
   844  func TestContext2Plan_computed(t *testing.T) {
   845  	m := testModule(t, "plan-computed")
   846  	p := testProvider("aws")
   847  	p.DiffFn = testDiffFn
   848  	ctx := testContext2(t, &ContextOpts{
   849  		Module: m,
   850  		Providers: map[string]ResourceProviderFactory{
   851  			"aws": testProviderFuncFixed(p),
   852  		},
   853  	})
   854  
   855  	plan, err := ctx.Plan()
   856  	if err != nil {
   857  		t.Fatalf("err: %s", err)
   858  	}
   859  
   860  	actual := strings.TrimSpace(plan.String())
   861  	expected := strings.TrimSpace(testTerraformPlanComputedStr)
   862  	if actual != expected {
   863  		t.Fatalf("bad:\n%s", actual)
   864  	}
   865  }
   866  
   867  func TestContext2Plan_computedDataResource(t *testing.T) {
   868  	m := testModule(t, "plan-computed-data-resource")
   869  	p := testProvider("aws")
   870  	p.DiffFn = testDiffFn
   871  	ctx := testContext2(t, &ContextOpts{
   872  		Module: m,
   873  		Providers: map[string]ResourceProviderFactory{
   874  			"aws": testProviderFuncFixed(p),
   875  		},
   876  	})
   877  
   878  	plan, err := ctx.Plan()
   879  	if err != nil {
   880  		t.Fatalf("err: %s", err)
   881  	}
   882  
   883  	if got := len(plan.Diff.Modules); got != 1 {
   884  		t.Fatalf("got %d modules; want 1", got)
   885  	}
   886  
   887  	moduleDiff := plan.Diff.Modules[0]
   888  
   889  	if _, ok := moduleDiff.Resources["aws_instance.foo"]; !ok {
   890  		t.Fatalf("missing diff for aws_instance.foo")
   891  	}
   892  	iDiff, ok := moduleDiff.Resources["data.aws_vpc.bar"]
   893  	if !ok {
   894  		t.Fatalf("missing diff for data.aws_vpc.bar")
   895  	}
   896  
   897  	expectedDiff := &InstanceDiff{
   898  		Attributes: map[string]*ResourceAttrDiff{
   899  			"id": {
   900  				NewComputed: true,
   901  				RequiresNew: true,
   902  				Type:        DiffAttrOutput,
   903  			},
   904  		},
   905  	}
   906  	if same, _ := expectedDiff.Same(iDiff); !same {
   907  		t.Fatalf(
   908  			"incorrect diff for data.aws_vpc.bar\ngot:  %#v\nwant: %#v",
   909  			iDiff, expectedDiff,
   910  		)
   911  	}
   912  }
   913  
   914  // Higher level test at TestResource_dataSourceListPlanPanic
   915  func TestContext2Plan_dataSourceTypeMismatch(t *testing.T) {
   916  	m := testModule(t, "plan-data-source-type-mismatch")
   917  	p := testProvider("aws")
   918  	p.ValidateResourceFn = func(t string, c *ResourceConfig) (ws []string, es []error) {
   919  		// Emulate the type checking behavior of helper/schema based validation
   920  		if t == "aws_instance" {
   921  			ami, _ := c.Get("ami")
   922  			switch a := ami.(type) {
   923  			case string:
   924  				// ok
   925  			default:
   926  				es = append(es, fmt.Errorf("Expected ami to be string, got %T", a))
   927  			}
   928  		}
   929  		return
   930  	}
   931  	p.DiffFn = func(
   932  		info *InstanceInfo,
   933  		state *InstanceState,
   934  		c *ResourceConfig) (*InstanceDiff, error) {
   935  		if info.Type == "aws_instance" {
   936  			// If we get to the diff, we should be able to assume types
   937  			ami, _ := c.Get("ami")
   938  			_ = ami.(string)
   939  		}
   940  		return nil, nil
   941  	}
   942  	ctx := testContext2(t, &ContextOpts{
   943  		Module: m,
   944  		// Pretend like we ran a Refresh and the AZs data source was populated.
   945  		State: &State{
   946  			Modules: []*ModuleState{
   947  				&ModuleState{
   948  					Path: rootModulePath,
   949  					Resources: map[string]*ResourceState{
   950  						"data.aws_availability_zones.azs": &ResourceState{
   951  							Type: "aws_availability_zones",
   952  							Primary: &InstanceState{
   953  								ID: "i-abc123",
   954  								Attributes: map[string]string{
   955  									"names.#": "2",
   956  									"names.0": "us-east-1a",
   957  									"names.1": "us-east-1b",
   958  								},
   959  							},
   960  						},
   961  					},
   962  				},
   963  			},
   964  		},
   965  		Providers: map[string]ResourceProviderFactory{
   966  			"aws": testProviderFuncFixed(p),
   967  		},
   968  	})
   969  
   970  	_, err := ctx.Plan()
   971  
   972  	if err == nil {
   973  		t.Fatalf("Expected err, got none!")
   974  	}
   975  	expected := "Expected ami to be string"
   976  	if !strings.Contains(err.Error(), expected) {
   977  		t.Fatalf("expected:\n\n%s\n\nto contain:\n\n%s", err, expected)
   978  	}
   979  }
   980  
   981  func TestContext2Plan_dataResourceBecomesComputed(t *testing.T) {
   982  	m := testModule(t, "plan-data-resource-becomes-computed")
   983  	p := testProvider("aws")
   984  
   985  	p.DiffFn = func(info *InstanceInfo, state *InstanceState, config *ResourceConfig) (*InstanceDiff, error) {
   986  		if info.Type != "aws_instance" {
   987  			t.Fatalf("don't know how to diff %s", info.Id)
   988  			return nil, nil
   989  		}
   990  
   991  		return &InstanceDiff{
   992  			Attributes: map[string]*ResourceAttrDiff{
   993  				"computed": &ResourceAttrDiff{
   994  					Old:         "",
   995  					New:         "",
   996  					NewComputed: true,
   997  				},
   998  			},
   999  		}, nil
  1000  	}
  1001  	p.ReadDataDiffReturn = &InstanceDiff{
  1002  		Attributes: map[string]*ResourceAttrDiff{
  1003  			"foo": &ResourceAttrDiff{
  1004  				Old:         "",
  1005  				New:         "",
  1006  				NewComputed: true,
  1007  			},
  1008  		},
  1009  	}
  1010  
  1011  	ctx := testContext2(t, &ContextOpts{
  1012  		Module: m,
  1013  		Providers: map[string]ResourceProviderFactory{
  1014  			"aws": testProviderFuncFixed(p),
  1015  		},
  1016  		State: &State{
  1017  			Modules: []*ModuleState{
  1018  				&ModuleState{
  1019  					Path: rootModulePath,
  1020  					Resources: map[string]*ResourceState{
  1021  						"data.aws_data_resource.foo": &ResourceState{
  1022  							Type: "aws_data_resource",
  1023  							Primary: &InstanceState{
  1024  								ID: "i-abc123",
  1025  								Attributes: map[string]string{
  1026  									"id":    "i-abc123",
  1027  									"value": "baz",
  1028  								},
  1029  							},
  1030  						},
  1031  					},
  1032  				},
  1033  			},
  1034  		},
  1035  	})
  1036  
  1037  	plan, err := ctx.Plan()
  1038  	if err != nil {
  1039  		t.Fatalf("err: %s", err)
  1040  	}
  1041  
  1042  	if got := len(plan.Diff.Modules); got != 1 {
  1043  		t.Fatalf("got %d modules; want 1", got)
  1044  	}
  1045  
  1046  	if !p.ReadDataDiffCalled {
  1047  		t.Fatal("ReadDataDiff wasn't called, but should've been")
  1048  	}
  1049  	if got, want := p.ReadDataDiffInfo.Id, "data.aws_data_resource.foo"; got != want {
  1050  		t.Fatalf("ReadDataDiff info id is %s; want %s", got, want)
  1051  	}
  1052  
  1053  	moduleDiff := plan.Diff.Modules[0]
  1054  
  1055  	iDiff, ok := moduleDiff.Resources["data.aws_data_resource.foo"]
  1056  	if !ok {
  1057  		t.Fatalf("missing diff for data.aws_data_resource.foo")
  1058  	}
  1059  
  1060  	if same, _ := p.ReadDataDiffReturn.Same(iDiff); !same {
  1061  		t.Fatalf(
  1062  			"incorrect diff for data.data_resource.foo\ngot:  %#v\nwant: %#v",
  1063  			iDiff, p.ReadDataDiffReturn,
  1064  		)
  1065  	}
  1066  }
  1067  
  1068  func TestContext2Plan_computedList(t *testing.T) {
  1069  	m := testModule(t, "plan-computed-list")
  1070  	p := testProvider("aws")
  1071  	p.DiffFn = testDiffFn
  1072  	ctx := testContext2(t, &ContextOpts{
  1073  		Module: m,
  1074  		Providers: map[string]ResourceProviderFactory{
  1075  			"aws": testProviderFuncFixed(p),
  1076  		},
  1077  	})
  1078  
  1079  	plan, err := ctx.Plan()
  1080  	if err != nil {
  1081  		t.Fatalf("err: %s", err)
  1082  	}
  1083  
  1084  	actual := strings.TrimSpace(plan.String())
  1085  	expected := strings.TrimSpace(testTerraformPlanComputedListStr)
  1086  	if actual != expected {
  1087  		t.Fatalf("bad:\n%s", actual)
  1088  	}
  1089  }
  1090  
  1091  func TestContext2Plan_count(t *testing.T) {
  1092  	m := testModule(t, "plan-count")
  1093  	p := testProvider("aws")
  1094  	p.DiffFn = testDiffFn
  1095  	ctx := testContext2(t, &ContextOpts{
  1096  		Module: m,
  1097  		Providers: map[string]ResourceProviderFactory{
  1098  			"aws": testProviderFuncFixed(p),
  1099  		},
  1100  	})
  1101  
  1102  	plan, err := ctx.Plan()
  1103  	if err != nil {
  1104  		t.Fatalf("err: %s", err)
  1105  	}
  1106  
  1107  	if len(plan.Diff.RootModule().Resources) < 6 {
  1108  		t.Fatalf("bad: %#v", plan.Diff.RootModule().Resources)
  1109  	}
  1110  
  1111  	actual := strings.TrimSpace(plan.String())
  1112  	expected := strings.TrimSpace(testTerraformPlanCountStr)
  1113  	if actual != expected {
  1114  		t.Fatalf("bad:\n%s", actual)
  1115  	}
  1116  }
  1117  
  1118  func TestContext2Plan_countComputed(t *testing.T) {
  1119  	m := testModule(t, "plan-count-computed")
  1120  	p := testProvider("aws")
  1121  	p.DiffFn = testDiffFn
  1122  	ctx := testContext2(t, &ContextOpts{
  1123  		Module: m,
  1124  		Providers: map[string]ResourceProviderFactory{
  1125  			"aws": testProviderFuncFixed(p),
  1126  		},
  1127  	})
  1128  
  1129  	_, err := ctx.Plan()
  1130  	if err == nil {
  1131  		t.Fatal("should error")
  1132  	}
  1133  }
  1134  
  1135  func TestContext2Plan_countIndex(t *testing.T) {
  1136  	m := testModule(t, "plan-count-index")
  1137  	p := testProvider("aws")
  1138  	p.DiffFn = testDiffFn
  1139  	ctx := testContext2(t, &ContextOpts{
  1140  		Module: m,
  1141  		Providers: map[string]ResourceProviderFactory{
  1142  			"aws": testProviderFuncFixed(p),
  1143  		},
  1144  	})
  1145  
  1146  	plan, err := ctx.Plan()
  1147  	if err != nil {
  1148  		t.Fatalf("err: %s", err)
  1149  	}
  1150  
  1151  	actual := strings.TrimSpace(plan.String())
  1152  	expected := strings.TrimSpace(testTerraformPlanCountIndexStr)
  1153  	if actual != expected {
  1154  		t.Fatalf("bad:\n%s", actual)
  1155  	}
  1156  }
  1157  
  1158  func TestContext2Plan_countIndexZero(t *testing.T) {
  1159  	m := testModule(t, "plan-count-index-zero")
  1160  	p := testProvider("aws")
  1161  	p.DiffFn = testDiffFn
  1162  	ctx := testContext2(t, &ContextOpts{
  1163  		Module: m,
  1164  		Providers: map[string]ResourceProviderFactory{
  1165  			"aws": testProviderFuncFixed(p),
  1166  		},
  1167  	})
  1168  
  1169  	plan, err := ctx.Plan()
  1170  	if err != nil {
  1171  		t.Fatalf("err: %s", err)
  1172  	}
  1173  
  1174  	actual := strings.TrimSpace(plan.String())
  1175  	expected := strings.TrimSpace(testTerraformPlanCountIndexZeroStr)
  1176  	if actual != expected {
  1177  		t.Fatalf("bad:\n%s", actual)
  1178  	}
  1179  }
  1180  
  1181  func TestContext2Plan_countVar(t *testing.T) {
  1182  	m := testModule(t, "plan-count-var")
  1183  	p := testProvider("aws")
  1184  	p.DiffFn = testDiffFn
  1185  	ctx := testContext2(t, &ContextOpts{
  1186  		Module: m,
  1187  		Providers: map[string]ResourceProviderFactory{
  1188  			"aws": testProviderFuncFixed(p),
  1189  		},
  1190  		Variables: map[string]string{
  1191  			"count": "3",
  1192  		},
  1193  	})
  1194  
  1195  	plan, err := ctx.Plan()
  1196  	if err != nil {
  1197  		t.Fatalf("err: %s", err)
  1198  	}
  1199  
  1200  	actual := strings.TrimSpace(plan.String())
  1201  	expected := strings.TrimSpace(testTerraformPlanCountVarStr)
  1202  	if actual != expected {
  1203  		t.Fatalf("bad:\n%s", actual)
  1204  	}
  1205  }
  1206  
  1207  func TestContext2Plan_countZero(t *testing.T) {
  1208  	m := testModule(t, "plan-count-zero")
  1209  	p := testProvider("aws")
  1210  	p.DiffFn = testDiffFn
  1211  	ctx := testContext2(t, &ContextOpts{
  1212  		Module: m,
  1213  		Providers: map[string]ResourceProviderFactory{
  1214  			"aws": testProviderFuncFixed(p),
  1215  		},
  1216  	})
  1217  
  1218  	plan, err := ctx.Plan()
  1219  	if err != nil {
  1220  		t.Fatalf("err: %s", err)
  1221  	}
  1222  
  1223  	actual := strings.TrimSpace(plan.String())
  1224  	expected := strings.TrimSpace(testTerraformPlanCountZeroStr)
  1225  	if actual != expected {
  1226  		t.Fatalf("bad:\n%s", actual)
  1227  	}
  1228  }
  1229  
  1230  func TestContext2Plan_countOneIndex(t *testing.T) {
  1231  	m := testModule(t, "plan-count-one-index")
  1232  	p := testProvider("aws")
  1233  	p.DiffFn = testDiffFn
  1234  	ctx := testContext2(t, &ContextOpts{
  1235  		Module: m,
  1236  		Providers: map[string]ResourceProviderFactory{
  1237  			"aws": testProviderFuncFixed(p),
  1238  		},
  1239  	})
  1240  
  1241  	plan, err := ctx.Plan()
  1242  	if err != nil {
  1243  		t.Fatalf("err: %s", err)
  1244  	}
  1245  
  1246  	actual := strings.TrimSpace(plan.String())
  1247  	expected := strings.TrimSpace(testTerraformPlanCountOneIndexStr)
  1248  	if actual != expected {
  1249  		t.Fatalf("bad:\n%s", actual)
  1250  	}
  1251  }
  1252  
  1253  func TestContext2Plan_countDecreaseToOne(t *testing.T) {
  1254  	m := testModule(t, "plan-count-dec")
  1255  	p := testProvider("aws")
  1256  	p.DiffFn = testDiffFn
  1257  	s := &State{
  1258  		Modules: []*ModuleState{
  1259  			&ModuleState{
  1260  				Path: rootModulePath,
  1261  				Resources: map[string]*ResourceState{
  1262  					"aws_instance.foo.0": &ResourceState{
  1263  						Type: "aws_instance",
  1264  						Primary: &InstanceState{
  1265  							ID: "bar",
  1266  							Attributes: map[string]string{
  1267  								"foo":  "foo",
  1268  								"type": "aws_instance",
  1269  							},
  1270  						},
  1271  					},
  1272  					"aws_instance.foo.1": &ResourceState{
  1273  						Type: "aws_instance",
  1274  						Primary: &InstanceState{
  1275  							ID: "bar",
  1276  						},
  1277  					},
  1278  					"aws_instance.foo.2": &ResourceState{
  1279  						Type: "aws_instance",
  1280  						Primary: &InstanceState{
  1281  							ID: "bar",
  1282  						},
  1283  					},
  1284  				},
  1285  			},
  1286  		},
  1287  	}
  1288  	ctx := testContext2(t, &ContextOpts{
  1289  		Module: m,
  1290  		Providers: map[string]ResourceProviderFactory{
  1291  			"aws": testProviderFuncFixed(p),
  1292  		},
  1293  		State: s,
  1294  	})
  1295  
  1296  	plan, err := ctx.Plan()
  1297  	if err != nil {
  1298  		t.Fatalf("err: %s", err)
  1299  	}
  1300  
  1301  	actual := strings.TrimSpace(plan.String())
  1302  	expected := strings.TrimSpace(testTerraformPlanCountDecreaseStr)
  1303  	if actual != expected {
  1304  		t.Fatalf("bad:\n%s", actual)
  1305  	}
  1306  }
  1307  
  1308  func TestContext2Plan_countIncreaseFromNotSet(t *testing.T) {
  1309  	m := testModule(t, "plan-count-inc")
  1310  	p := testProvider("aws")
  1311  	p.DiffFn = testDiffFn
  1312  	s := &State{
  1313  		Modules: []*ModuleState{
  1314  			&ModuleState{
  1315  				Path: rootModulePath,
  1316  				Resources: map[string]*ResourceState{
  1317  					"aws_instance.foo": &ResourceState{
  1318  						Type: "aws_instance",
  1319  						Primary: &InstanceState{
  1320  							ID: "bar",
  1321  							Attributes: map[string]string{
  1322  								"foo":  "foo",
  1323  								"type": "aws_instance",
  1324  							},
  1325  						},
  1326  					},
  1327  				},
  1328  			},
  1329  		},
  1330  	}
  1331  	ctx := testContext2(t, &ContextOpts{
  1332  		Module: m,
  1333  		Providers: map[string]ResourceProviderFactory{
  1334  			"aws": testProviderFuncFixed(p),
  1335  		},
  1336  		State: s,
  1337  	})
  1338  
  1339  	plan, err := ctx.Plan()
  1340  	if err != nil {
  1341  		t.Fatalf("err: %s", err)
  1342  	}
  1343  
  1344  	actual := strings.TrimSpace(plan.String())
  1345  	expected := strings.TrimSpace(testTerraformPlanCountIncreaseStr)
  1346  	if actual != expected {
  1347  		t.Fatalf("bad:\n%s", actual)
  1348  	}
  1349  }
  1350  
  1351  func TestContext2Plan_countIncreaseFromOne(t *testing.T) {
  1352  	m := testModule(t, "plan-count-inc")
  1353  	p := testProvider("aws")
  1354  	p.DiffFn = testDiffFn
  1355  	s := &State{
  1356  		Modules: []*ModuleState{
  1357  			&ModuleState{
  1358  				Path: rootModulePath,
  1359  				Resources: map[string]*ResourceState{
  1360  					"aws_instance.foo.0": &ResourceState{
  1361  						Type: "aws_instance",
  1362  						Primary: &InstanceState{
  1363  							ID: "bar",
  1364  							Attributes: map[string]string{
  1365  								"foo":  "foo",
  1366  								"type": "aws_instance",
  1367  							},
  1368  						},
  1369  					},
  1370  				},
  1371  			},
  1372  		},
  1373  	}
  1374  	ctx := testContext2(t, &ContextOpts{
  1375  		Module: m,
  1376  		Providers: map[string]ResourceProviderFactory{
  1377  			"aws": testProviderFuncFixed(p),
  1378  		},
  1379  		State: s,
  1380  	})
  1381  
  1382  	plan, err := ctx.Plan()
  1383  	if err != nil {
  1384  		t.Fatalf("err: %s", err)
  1385  	}
  1386  
  1387  	actual := strings.TrimSpace(plan.String())
  1388  	expected := strings.TrimSpace(testTerraformPlanCountIncreaseFromOneStr)
  1389  	if actual != expected {
  1390  		t.Fatalf("bad:\n%s", actual)
  1391  	}
  1392  }
  1393  
  1394  // https://github.com/PeoplePerHour/terraform/pull/11
  1395  //
  1396  // This tests a case where both a "resource" and "resource.0" are in
  1397  // the state file, which apparently is a reasonable backwards compatibility
  1398  // concern found in the above 3rd party repo.
  1399  func TestContext2Plan_countIncreaseFromOneCorrupted(t *testing.T) {
  1400  	m := testModule(t, "plan-count-inc")
  1401  	p := testProvider("aws")
  1402  	p.DiffFn = testDiffFn
  1403  	s := &State{
  1404  		Modules: []*ModuleState{
  1405  			&ModuleState{
  1406  				Path: rootModulePath,
  1407  				Resources: map[string]*ResourceState{
  1408  					"aws_instance.foo": &ResourceState{
  1409  						Type: "aws_instance",
  1410  						Primary: &InstanceState{
  1411  							ID: "bar",
  1412  							Attributes: map[string]string{
  1413  								"foo":  "foo",
  1414  								"type": "aws_instance",
  1415  							},
  1416  						},
  1417  					},
  1418  					"aws_instance.foo.0": &ResourceState{
  1419  						Type: "aws_instance",
  1420  						Primary: &InstanceState{
  1421  							ID: "bar",
  1422  							Attributes: map[string]string{
  1423  								"foo":  "foo",
  1424  								"type": "aws_instance",
  1425  							},
  1426  						},
  1427  					},
  1428  				},
  1429  			},
  1430  		},
  1431  	}
  1432  	ctx := testContext2(t, &ContextOpts{
  1433  		Module: m,
  1434  		Providers: map[string]ResourceProviderFactory{
  1435  			"aws": testProviderFuncFixed(p),
  1436  		},
  1437  		State: s,
  1438  	})
  1439  
  1440  	plan, err := ctx.Plan()
  1441  	if err != nil {
  1442  		t.Fatalf("err: %s", err)
  1443  	}
  1444  
  1445  	actual := strings.TrimSpace(plan.String())
  1446  	expected := strings.TrimSpace(testTerraformPlanCountIncreaseFromOneCorruptedStr)
  1447  	if actual != expected {
  1448  		t.Fatalf("bad:\n%s", actual)
  1449  	}
  1450  }
  1451  
  1452  func TestContext2Plan_destroy(t *testing.T) {
  1453  	m := testModule(t, "plan-destroy")
  1454  	p := testProvider("aws")
  1455  	p.DiffFn = testDiffFn
  1456  	s := &State{
  1457  		Modules: []*ModuleState{
  1458  			&ModuleState{
  1459  				Path: rootModulePath,
  1460  				Resources: map[string]*ResourceState{
  1461  					"aws_instance.one": &ResourceState{
  1462  						Type: "aws_instance",
  1463  						Primary: &InstanceState{
  1464  							ID: "bar",
  1465  						},
  1466  					},
  1467  					"aws_instance.two": &ResourceState{
  1468  						Type: "aws_instance",
  1469  						Primary: &InstanceState{
  1470  							ID: "baz",
  1471  						},
  1472  					},
  1473  				},
  1474  			},
  1475  		},
  1476  	}
  1477  	ctx := testContext2(t, &ContextOpts{
  1478  		Module: m,
  1479  		Providers: map[string]ResourceProviderFactory{
  1480  			"aws": testProviderFuncFixed(p),
  1481  		},
  1482  		State:   s,
  1483  		Destroy: true,
  1484  	})
  1485  
  1486  	plan, err := ctx.Plan()
  1487  	if err != nil {
  1488  		t.Fatalf("err: %s", err)
  1489  	}
  1490  
  1491  	if len(plan.Diff.RootModule().Resources) != 2 {
  1492  		t.Fatalf("bad: %#v", plan.Diff.RootModule().Resources)
  1493  	}
  1494  
  1495  	actual := strings.TrimSpace(plan.String())
  1496  	expected := strings.TrimSpace(testTerraformPlanDestroyStr)
  1497  	if actual != expected {
  1498  		t.Fatalf("bad:\n%s", actual)
  1499  	}
  1500  }
  1501  
  1502  func TestContext2Plan_moduleDestroy(t *testing.T) {
  1503  	m := testModule(t, "plan-module-destroy")
  1504  	p := testProvider("aws")
  1505  	p.DiffFn = testDiffFn
  1506  	s := &State{
  1507  		Modules: []*ModuleState{
  1508  			&ModuleState{
  1509  				Path: rootModulePath,
  1510  				Resources: map[string]*ResourceState{
  1511  					"aws_instance.foo": &ResourceState{
  1512  						Type: "aws_instance",
  1513  						Primary: &InstanceState{
  1514  							ID: "bar",
  1515  						},
  1516  					},
  1517  				},
  1518  			},
  1519  			&ModuleState{
  1520  				Path: []string{"root", "child"},
  1521  				Resources: map[string]*ResourceState{
  1522  					"aws_instance.foo": &ResourceState{
  1523  						Type: "aws_instance",
  1524  						Primary: &InstanceState{
  1525  							ID: "bar",
  1526  						},
  1527  					},
  1528  				},
  1529  			},
  1530  		},
  1531  	}
  1532  	ctx := testContext2(t, &ContextOpts{
  1533  		Module: m,
  1534  		Providers: map[string]ResourceProviderFactory{
  1535  			"aws": testProviderFuncFixed(p),
  1536  		},
  1537  		State:   s,
  1538  		Destroy: true,
  1539  	})
  1540  
  1541  	plan, err := ctx.Plan()
  1542  	if err != nil {
  1543  		t.Fatalf("err: %s", err)
  1544  	}
  1545  
  1546  	actual := strings.TrimSpace(plan.String())
  1547  	expected := strings.TrimSpace(testTerraformPlanModuleDestroyStr)
  1548  	if actual != expected {
  1549  		t.Fatalf("bad:\n%s", actual)
  1550  	}
  1551  }
  1552  
  1553  // GH-1835
  1554  func TestContext2Plan_moduleDestroyCycle(t *testing.T) {
  1555  	m := testModule(t, "plan-module-destroy-gh-1835")
  1556  	p := testProvider("aws")
  1557  	p.DiffFn = testDiffFn
  1558  	s := &State{
  1559  		Modules: []*ModuleState{
  1560  			&ModuleState{
  1561  				Path: []string{"root", "a_module"},
  1562  				Resources: map[string]*ResourceState{
  1563  					"aws_instance.a": &ResourceState{
  1564  						Type: "aws_instance",
  1565  						Primary: &InstanceState{
  1566  							ID: "a",
  1567  						},
  1568  					},
  1569  				},
  1570  			},
  1571  			&ModuleState{
  1572  				Path: []string{"root", "b_module"},
  1573  				Resources: map[string]*ResourceState{
  1574  					"aws_instance.b": &ResourceState{
  1575  						Type: "aws_instance",
  1576  						Primary: &InstanceState{
  1577  							ID: "b",
  1578  						},
  1579  					},
  1580  				},
  1581  			},
  1582  		},
  1583  	}
  1584  	ctx := testContext2(t, &ContextOpts{
  1585  		Module: m,
  1586  		Providers: map[string]ResourceProviderFactory{
  1587  			"aws": testProviderFuncFixed(p),
  1588  		},
  1589  		State:   s,
  1590  		Destroy: true,
  1591  	})
  1592  
  1593  	plan, err := ctx.Plan()
  1594  	if err != nil {
  1595  		t.Fatalf("err: %s", err)
  1596  	}
  1597  
  1598  	actual := strings.TrimSpace(plan.String())
  1599  	expected := strings.TrimSpace(testTerraformPlanModuleDestroyCycleStr)
  1600  	if actual != expected {
  1601  		t.Fatalf("bad:\n%s", actual)
  1602  	}
  1603  }
  1604  
  1605  func TestContext2Plan_moduleDestroyMultivar(t *testing.T) {
  1606  	m := testModule(t, "plan-module-destroy-multivar")
  1607  	p := testProvider("aws")
  1608  	p.DiffFn = testDiffFn
  1609  	s := &State{
  1610  		Modules: []*ModuleState{
  1611  			&ModuleState{
  1612  				Path:      rootModulePath,
  1613  				Resources: map[string]*ResourceState{},
  1614  			},
  1615  			&ModuleState{
  1616  				Path: []string{"root", "child"},
  1617  				Resources: map[string]*ResourceState{
  1618  					"aws_instance.foo.0": &ResourceState{
  1619  						Type: "aws_instance",
  1620  						Primary: &InstanceState{
  1621  							ID: "bar0",
  1622  						},
  1623  					},
  1624  					"aws_instance.foo.1": &ResourceState{
  1625  						Type: "aws_instance",
  1626  						Primary: &InstanceState{
  1627  							ID: "bar1",
  1628  						},
  1629  					},
  1630  				},
  1631  			},
  1632  		},
  1633  	}
  1634  	ctx := testContext2(t, &ContextOpts{
  1635  		Module: m,
  1636  		Providers: map[string]ResourceProviderFactory{
  1637  			"aws": testProviderFuncFixed(p),
  1638  		},
  1639  		State:   s,
  1640  		Destroy: true,
  1641  	})
  1642  
  1643  	plan, err := ctx.Plan()
  1644  	if err != nil {
  1645  		t.Fatalf("err: %s", err)
  1646  	}
  1647  
  1648  	actual := strings.TrimSpace(plan.String())
  1649  	expected := strings.TrimSpace(testTerraformPlanModuleDestroyMultivarStr)
  1650  	if actual != expected {
  1651  		t.Fatalf("bad:\n%s", actual)
  1652  	}
  1653  }
  1654  
  1655  func TestContext2Plan_pathVar(t *testing.T) {
  1656  	cwd, err := os.Getwd()
  1657  	if err != nil {
  1658  		t.Fatalf("err: %s", err)
  1659  	}
  1660  
  1661  	m := testModule(t, "plan-path-var")
  1662  	p := testProvider("aws")
  1663  	p.DiffFn = testDiffFn
  1664  	ctx := testContext2(t, &ContextOpts{
  1665  		Module: m,
  1666  		Providers: map[string]ResourceProviderFactory{
  1667  			"aws": testProviderFuncFixed(p),
  1668  		},
  1669  	})
  1670  
  1671  	plan, err := ctx.Plan()
  1672  	if err != nil {
  1673  		t.Fatalf("err: %s", err)
  1674  	}
  1675  
  1676  	actual := strings.TrimSpace(plan.String())
  1677  	expected := strings.TrimSpace(testTerraformPlanPathVarStr)
  1678  
  1679  	// Warning: this ordering REALLY matters for this test. The
  1680  	// order is: cwd, module, root.
  1681  	expected = fmt.Sprintf(
  1682  		expected,
  1683  		cwd,
  1684  		m.Config().Dir,
  1685  		m.Config().Dir)
  1686  
  1687  	if actual != expected {
  1688  		t.Fatalf("bad:\n%s\n\nexpected:\n\n%s", actual, expected)
  1689  	}
  1690  }
  1691  
  1692  func TestContext2Plan_diffVar(t *testing.T) {
  1693  	m := testModule(t, "plan-diffvar")
  1694  	p := testProvider("aws")
  1695  	s := &State{
  1696  		Modules: []*ModuleState{
  1697  			&ModuleState{
  1698  				Path: rootModulePath,
  1699  				Resources: map[string]*ResourceState{
  1700  					"aws_instance.foo": &ResourceState{
  1701  						Primary: &InstanceState{
  1702  							ID: "bar",
  1703  							Attributes: map[string]string{
  1704  								"num": "2",
  1705  							},
  1706  						},
  1707  					},
  1708  				},
  1709  			},
  1710  		},
  1711  	}
  1712  	ctx := testContext2(t, &ContextOpts{
  1713  		Module: m,
  1714  		Providers: map[string]ResourceProviderFactory{
  1715  			"aws": testProviderFuncFixed(p),
  1716  		},
  1717  		State: s,
  1718  	})
  1719  
  1720  	p.DiffFn = func(
  1721  		info *InstanceInfo,
  1722  		s *InstanceState,
  1723  		c *ResourceConfig) (*InstanceDiff, error) {
  1724  		if s.ID != "bar" {
  1725  			return testDiffFn(info, s, c)
  1726  		}
  1727  
  1728  		return &InstanceDiff{
  1729  			Attributes: map[string]*ResourceAttrDiff{
  1730  				"num": &ResourceAttrDiff{
  1731  					Old: "2",
  1732  					New: "3",
  1733  				},
  1734  			},
  1735  		}, nil
  1736  	}
  1737  
  1738  	plan, err := ctx.Plan()
  1739  	if err != nil {
  1740  		t.Fatalf("err: %s", err)
  1741  	}
  1742  
  1743  	actual := strings.TrimSpace(plan.String())
  1744  	expected := strings.TrimSpace(testTerraformPlanDiffVarStr)
  1745  	if actual != expected {
  1746  		t.Fatalf("actual:\n%s\n\nexpected:\n%s", actual, expected)
  1747  	}
  1748  }
  1749  
  1750  func TestContext2Plan_hook(t *testing.T) {
  1751  	m := testModule(t, "plan-good")
  1752  	h := new(MockHook)
  1753  	p := testProvider("aws")
  1754  	p.DiffFn = testDiffFn
  1755  	ctx := testContext2(t, &ContextOpts{
  1756  		Module: m,
  1757  		Hooks:  []Hook{h},
  1758  		Providers: map[string]ResourceProviderFactory{
  1759  			"aws": testProviderFuncFixed(p),
  1760  		},
  1761  	})
  1762  
  1763  	_, err := ctx.Plan()
  1764  	if err != nil {
  1765  		t.Fatalf("err: %s", err)
  1766  	}
  1767  
  1768  	if !h.PreDiffCalled {
  1769  		t.Fatal("should be called")
  1770  	}
  1771  	if !h.PostDiffCalled {
  1772  		t.Fatal("should be called")
  1773  	}
  1774  }
  1775  
  1776  func TestContext2Plan_orphan(t *testing.T) {
  1777  	m := testModule(t, "plan-orphan")
  1778  	p := testProvider("aws")
  1779  	p.DiffFn = testDiffFn
  1780  	s := &State{
  1781  		Modules: []*ModuleState{
  1782  			&ModuleState{
  1783  				Path: rootModulePath,
  1784  				Resources: map[string]*ResourceState{
  1785  					"aws_instance.baz": &ResourceState{
  1786  						Type: "aws_instance",
  1787  						Primary: &InstanceState{
  1788  							ID: "bar",
  1789  						},
  1790  					},
  1791  				},
  1792  			},
  1793  		},
  1794  	}
  1795  	ctx := testContext2(t, &ContextOpts{
  1796  		Module: m,
  1797  		Providers: map[string]ResourceProviderFactory{
  1798  			"aws": testProviderFuncFixed(p),
  1799  		},
  1800  		State: s,
  1801  	})
  1802  
  1803  	plan, err := ctx.Plan()
  1804  	if err != nil {
  1805  		t.Fatalf("err: %s", err)
  1806  	}
  1807  
  1808  	actual := strings.TrimSpace(plan.String())
  1809  	expected := strings.TrimSpace(testTerraformPlanOrphanStr)
  1810  	if actual != expected {
  1811  		t.Fatalf("bad:\n%s", actual)
  1812  	}
  1813  }
  1814  
  1815  func TestContext2Plan_state(t *testing.T) {
  1816  	m := testModule(t, "plan-good")
  1817  	p := testProvider("aws")
  1818  	p.DiffFn = testDiffFn
  1819  	s := &State{
  1820  		Modules: []*ModuleState{
  1821  			&ModuleState{
  1822  				Path: rootModulePath,
  1823  				Resources: map[string]*ResourceState{
  1824  					"aws_instance.foo": &ResourceState{
  1825  						Primary: &InstanceState{
  1826  							ID: "bar",
  1827  						},
  1828  					},
  1829  				},
  1830  			},
  1831  		},
  1832  	}
  1833  	ctx := testContext2(t, &ContextOpts{
  1834  		Module: m,
  1835  		Providers: map[string]ResourceProviderFactory{
  1836  			"aws": testProviderFuncFixed(p),
  1837  		},
  1838  		State: s,
  1839  	})
  1840  
  1841  	plan, err := ctx.Plan()
  1842  	if err != nil {
  1843  		t.Fatalf("err: %s", err)
  1844  	}
  1845  
  1846  	if len(plan.Diff.RootModule().Resources) < 2 {
  1847  		t.Fatalf("bad: %#v", plan.Diff.RootModule().Resources)
  1848  	}
  1849  
  1850  	actual := strings.TrimSpace(plan.String())
  1851  	expected := strings.TrimSpace(testTerraformPlanStateStr)
  1852  	if actual != expected {
  1853  		t.Fatalf("bad:\n%s\n\nexpected:\n\n%s", actual, expected)
  1854  	}
  1855  }
  1856  
  1857  func TestContext2Plan_taint(t *testing.T) {
  1858  	m := testModule(t, "plan-taint")
  1859  	p := testProvider("aws")
  1860  	p.DiffFn = testDiffFn
  1861  	s := &State{
  1862  		Modules: []*ModuleState{
  1863  			&ModuleState{
  1864  				Path: rootModulePath,
  1865  				Resources: map[string]*ResourceState{
  1866  					"aws_instance.foo": &ResourceState{
  1867  						Type: "aws_instance",
  1868  						Primary: &InstanceState{
  1869  							ID:         "bar",
  1870  							Attributes: map[string]string{"num": "2"},
  1871  						},
  1872  					},
  1873  					"aws_instance.bar": &ResourceState{
  1874  						Type: "aws_instance",
  1875  						Primary: &InstanceState{
  1876  							ID:      "baz",
  1877  							Tainted: true,
  1878  						},
  1879  					},
  1880  				},
  1881  			},
  1882  		},
  1883  	}
  1884  	ctx := testContext2(t, &ContextOpts{
  1885  		Module: m,
  1886  		Providers: map[string]ResourceProviderFactory{
  1887  			"aws": testProviderFuncFixed(p),
  1888  		},
  1889  		State: s,
  1890  	})
  1891  
  1892  	plan, err := ctx.Plan()
  1893  	if err != nil {
  1894  		t.Fatalf("err: %s", err)
  1895  	}
  1896  
  1897  	actual := strings.TrimSpace(plan.String())
  1898  	expected := strings.TrimSpace(testTerraformPlanTaintStr)
  1899  	if actual != expected {
  1900  		t.Fatalf("bad:\n%s", actual)
  1901  	}
  1902  }
  1903  
  1904  // Fails about 50% of the time before the fix for GH-4982, covers the fix.
  1905  func TestContext2Plan_taintDestroyInterpolatedCountRace(t *testing.T) {
  1906  	m := testModule(t, "plan-taint-interpolated-count")
  1907  	p := testProvider("aws")
  1908  	p.DiffFn = testDiffFn
  1909  	s := &State{
  1910  		Modules: []*ModuleState{
  1911  			&ModuleState{
  1912  				Path: rootModulePath,
  1913  				Resources: map[string]*ResourceState{
  1914  					"aws_instance.foo.0": &ResourceState{
  1915  						Type: "aws_instance",
  1916  						Primary: &InstanceState{
  1917  							ID:      "bar",
  1918  							Tainted: true,
  1919  						},
  1920  					},
  1921  					"aws_instance.foo.1": &ResourceState{
  1922  						Type:    "aws_instance",
  1923  						Primary: &InstanceState{ID: "bar"},
  1924  					},
  1925  					"aws_instance.foo.2": &ResourceState{
  1926  						Type:    "aws_instance",
  1927  						Primary: &InstanceState{ID: "bar"},
  1928  					},
  1929  				},
  1930  			},
  1931  		},
  1932  	}
  1933  	ctx := testContext2(t, &ContextOpts{
  1934  		Module: m,
  1935  		Providers: map[string]ResourceProviderFactory{
  1936  			"aws": testProviderFuncFixed(p),
  1937  		},
  1938  		State: s,
  1939  	})
  1940  
  1941  	for i := 0; i < 100; i++ {
  1942  		plan, err := ctx.Plan()
  1943  		if err != nil {
  1944  			t.Fatalf("err: %s", err)
  1945  		}
  1946  
  1947  		actual := strings.TrimSpace(plan.String())
  1948  		expected := strings.TrimSpace(`
  1949  DIFF:
  1950  
  1951  DESTROY/CREATE: aws_instance.foo.0
  1952  
  1953  STATE:
  1954  
  1955  aws_instance.foo.0: (tainted)
  1956    ID = bar
  1957  aws_instance.foo.1:
  1958    ID = bar
  1959  aws_instance.foo.2:
  1960    ID = bar
  1961  		`)
  1962  		if actual != expected {
  1963  			t.Fatalf("bad:\n%s", actual)
  1964  		}
  1965  	}
  1966  }
  1967  
  1968  func TestContext2Plan_targeted(t *testing.T) {
  1969  	m := testModule(t, "plan-targeted")
  1970  	p := testProvider("aws")
  1971  	p.DiffFn = testDiffFn
  1972  	ctx := testContext2(t, &ContextOpts{
  1973  		Module: m,
  1974  		Providers: map[string]ResourceProviderFactory{
  1975  			"aws": testProviderFuncFixed(p),
  1976  		},
  1977  		Targets: []string{"aws_instance.foo"},
  1978  	})
  1979  
  1980  	plan, err := ctx.Plan()
  1981  	if err != nil {
  1982  		t.Fatalf("err: %s", err)
  1983  	}
  1984  
  1985  	actual := strings.TrimSpace(plan.String())
  1986  	expected := strings.TrimSpace(`
  1987  DIFF:
  1988  
  1989  CREATE: aws_instance.foo
  1990    num:  "" => "2"
  1991    type: "" => "aws_instance"
  1992  
  1993  STATE:
  1994  
  1995  <no state>
  1996  	`)
  1997  	if actual != expected {
  1998  		t.Fatalf("expected:\n%s\n\ngot:\n%s", expected, actual)
  1999  	}
  2000  }
  2001  
  2002  func TestContext2Plan_targetedOrphan(t *testing.T) {
  2003  	m := testModule(t, "plan-targeted-orphan")
  2004  	p := testProvider("aws")
  2005  	p.DiffFn = testDiffFn
  2006  	ctx := testContext2(t, &ContextOpts{
  2007  		Module: m,
  2008  		Providers: map[string]ResourceProviderFactory{
  2009  			"aws": testProviderFuncFixed(p),
  2010  		},
  2011  		State: &State{
  2012  			Modules: []*ModuleState{
  2013  				&ModuleState{
  2014  					Path: rootModulePath,
  2015  					Resources: map[string]*ResourceState{
  2016  						"aws_instance.orphan": &ResourceState{
  2017  							Type: "aws_instance",
  2018  							Primary: &InstanceState{
  2019  								ID: "i-789xyz",
  2020  							},
  2021  						},
  2022  						"aws_instance.nottargeted": &ResourceState{
  2023  							Type: "aws_instance",
  2024  							Primary: &InstanceState{
  2025  								ID: "i-abc123",
  2026  							},
  2027  						},
  2028  					},
  2029  				},
  2030  			},
  2031  		},
  2032  		Destroy: true,
  2033  		Targets: []string{"aws_instance.orphan"},
  2034  	})
  2035  
  2036  	plan, err := ctx.Plan()
  2037  	if err != nil {
  2038  		t.Fatalf("err: %s", err)
  2039  	}
  2040  
  2041  	actual := strings.TrimSpace(plan.String())
  2042  	expected := strings.TrimSpace(`DIFF:
  2043  
  2044  DESTROY: aws_instance.orphan
  2045  
  2046  STATE:
  2047  
  2048  aws_instance.nottargeted:
  2049    ID = i-abc123
  2050  aws_instance.orphan:
  2051    ID = i-789xyz
  2052  `)
  2053  	if actual != expected {
  2054  		t.Fatalf("expected:\n%s\n\ngot:\n%s", expected, actual)
  2055  	}
  2056  }
  2057  
  2058  // https://github.com/hashicorp/terraform/issues/2538
  2059  func TestContext2Plan_targetedModuleOrphan(t *testing.T) {
  2060  	m := testModule(t, "plan-targeted-module-orphan")
  2061  	p := testProvider("aws")
  2062  	p.DiffFn = testDiffFn
  2063  	ctx := testContext2(t, &ContextOpts{
  2064  		Module: m,
  2065  		Providers: map[string]ResourceProviderFactory{
  2066  			"aws": testProviderFuncFixed(p),
  2067  		},
  2068  		State: &State{
  2069  			Modules: []*ModuleState{
  2070  				&ModuleState{
  2071  					Path: []string{"root", "child"},
  2072  					Resources: map[string]*ResourceState{
  2073  						"aws_instance.orphan": &ResourceState{
  2074  							Type: "aws_instance",
  2075  							Primary: &InstanceState{
  2076  								ID: "i-789xyz",
  2077  							},
  2078  						},
  2079  						"aws_instance.nottargeted": &ResourceState{
  2080  							Type: "aws_instance",
  2081  							Primary: &InstanceState{
  2082  								ID: "i-abc123",
  2083  							},
  2084  						},
  2085  					},
  2086  				},
  2087  			},
  2088  		},
  2089  		Destroy: true,
  2090  		Targets: []string{"module.child.aws_instance.orphan"},
  2091  	})
  2092  
  2093  	plan, err := ctx.Plan()
  2094  	if err != nil {
  2095  		t.Fatalf("err: %s", err)
  2096  	}
  2097  
  2098  	actual := strings.TrimSpace(plan.String())
  2099  	expected := strings.TrimSpace(`DIFF:
  2100  
  2101  module.child:
  2102    DESTROY: aws_instance.orphan
  2103  
  2104  STATE:
  2105  
  2106  module.child:
  2107    aws_instance.nottargeted:
  2108      ID = i-abc123
  2109    aws_instance.orphan:
  2110      ID = i-789xyz
  2111  `)
  2112  	if actual != expected {
  2113  		t.Fatalf("expected:\n%s\n\ngot:\n%s", expected, actual)
  2114  	}
  2115  }
  2116  
  2117  // https://github.com/hashicorp/terraform/issues/4515
  2118  func TestContext2Plan_targetedOverTen(t *testing.T) {
  2119  	m := testModule(t, "plan-targeted-over-ten")
  2120  	p := testProvider("aws")
  2121  	p.DiffFn = testDiffFn
  2122  
  2123  	resources := make(map[string]*ResourceState)
  2124  	var expectedState []string
  2125  	for i := 0; i < 13; i++ {
  2126  		key := fmt.Sprintf("aws_instance.foo.%d", i)
  2127  		id := fmt.Sprintf("i-abc%d", i)
  2128  		resources[key] = &ResourceState{
  2129  			Type:    "aws_instance",
  2130  			Primary: &InstanceState{ID: id},
  2131  		}
  2132  		expectedState = append(expectedState,
  2133  			fmt.Sprintf("%s:\n  ID = %s\n", key, id))
  2134  	}
  2135  	ctx := testContext2(t, &ContextOpts{
  2136  		Module: m,
  2137  		Providers: map[string]ResourceProviderFactory{
  2138  			"aws": testProviderFuncFixed(p),
  2139  		},
  2140  		State: &State{
  2141  			Modules: []*ModuleState{
  2142  				&ModuleState{
  2143  					Path:      rootModulePath,
  2144  					Resources: resources,
  2145  				},
  2146  			},
  2147  		},
  2148  		Targets: []string{"aws_instance.foo[1]"},
  2149  	})
  2150  
  2151  	plan, err := ctx.Plan()
  2152  	if err != nil {
  2153  		t.Fatalf("err: %s", err)
  2154  	}
  2155  
  2156  	actual := strings.TrimSpace(plan.String())
  2157  	sort.Strings(expectedState)
  2158  	expected := strings.TrimSpace(`
  2159  DIFF:
  2160  
  2161  
  2162  
  2163  STATE:
  2164  
  2165  aws_instance.foo.0:
  2166    ID = i-abc0
  2167  aws_instance.foo.1:
  2168    ID = i-abc1
  2169  aws_instance.foo.10:
  2170    ID = i-abc10
  2171  aws_instance.foo.11:
  2172    ID = i-abc11
  2173  aws_instance.foo.12:
  2174    ID = i-abc12
  2175  aws_instance.foo.2:
  2176    ID = i-abc2
  2177  aws_instance.foo.3:
  2178    ID = i-abc3
  2179  aws_instance.foo.4:
  2180    ID = i-abc4
  2181  aws_instance.foo.5:
  2182    ID = i-abc5
  2183  aws_instance.foo.6:
  2184    ID = i-abc6
  2185  aws_instance.foo.7:
  2186    ID = i-abc7
  2187  aws_instance.foo.8:
  2188    ID = i-abc8
  2189  aws_instance.foo.9:
  2190    ID = i-abc9
  2191  	`)
  2192  	if actual != expected {
  2193  		t.Fatalf("expected:\n%s\n\ngot:\n%s", expected, actual)
  2194  	}
  2195  }
  2196  
  2197  func TestContext2Plan_provider(t *testing.T) {
  2198  	m := testModule(t, "plan-provider")
  2199  	p := testProvider("aws")
  2200  	p.DiffFn = testDiffFn
  2201  
  2202  	var value interface{}
  2203  	p.ConfigureFn = func(c *ResourceConfig) error {
  2204  		value, _ = c.Get("foo")
  2205  		return nil
  2206  	}
  2207  
  2208  	ctx := testContext2(t, &ContextOpts{
  2209  		Module: m,
  2210  		Providers: map[string]ResourceProviderFactory{
  2211  			"aws": testProviderFuncFixed(p),
  2212  		},
  2213  		Variables: map[string]string{
  2214  			"foo": "bar",
  2215  		},
  2216  	})
  2217  
  2218  	if _, err := ctx.Plan(); err != nil {
  2219  		t.Fatalf("err: %s", err)
  2220  	}
  2221  
  2222  	if value != "bar" {
  2223  		t.Fatalf("bad: %#v", value)
  2224  	}
  2225  }
  2226  
  2227  func TestContext2Plan_varListErr(t *testing.T) {
  2228  	m := testModule(t, "plan-var-list-err")
  2229  	p := testProvider("aws")
  2230  	ctx := testContext2(t, &ContextOpts{
  2231  		Module: m,
  2232  		Providers: map[string]ResourceProviderFactory{
  2233  			"aws": testProviderFuncFixed(p),
  2234  		},
  2235  	})
  2236  
  2237  	_, err := ctx.Plan()
  2238  
  2239  	if err == nil {
  2240  		t.Fatal("should error")
  2241  	}
  2242  }
  2243  
  2244  func TestContext2Plan_ignoreChanges(t *testing.T) {
  2245  	m := testModule(t, "plan-ignore-changes")
  2246  	p := testProvider("aws")
  2247  	p.DiffFn = testDiffFn
  2248  	s := &State{
  2249  		Modules: []*ModuleState{
  2250  			&ModuleState{
  2251  				Path: rootModulePath,
  2252  				Resources: map[string]*ResourceState{
  2253  					"aws_instance.foo": &ResourceState{
  2254  						Primary: &InstanceState{
  2255  							ID:         "bar",
  2256  							Attributes: map[string]string{"ami": "ami-abcd1234"},
  2257  						},
  2258  					},
  2259  				},
  2260  			},
  2261  		},
  2262  	}
  2263  	ctx := testContext2(t, &ContextOpts{
  2264  		Module: m,
  2265  		Providers: map[string]ResourceProviderFactory{
  2266  			"aws": testProviderFuncFixed(p),
  2267  		},
  2268  		Variables: map[string]string{
  2269  			"foo": "ami-1234abcd",
  2270  		},
  2271  		State: s,
  2272  	})
  2273  
  2274  	plan, err := ctx.Plan()
  2275  	if err != nil {
  2276  		t.Fatalf("err: %s", err)
  2277  	}
  2278  
  2279  	if len(plan.Diff.RootModule().Resources) < 1 {
  2280  		t.Fatalf("bad: %#v", plan.Diff.RootModule().Resources)
  2281  	}
  2282  
  2283  	actual := strings.TrimSpace(plan.String())
  2284  	expected := strings.TrimSpace(testTerraformPlanIgnoreChangesStr)
  2285  	if actual != expected {
  2286  		t.Fatalf("bad:\n%s\n\nexpected\n\n%s", actual, expected)
  2287  	}
  2288  }