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

     1  package terraform
     2  
     3  import (
     4  	"reflect"
     5  	"sort"
     6  	"strings"
     7  	"sync"
     8  	"testing"
     9  )
    10  
    11  func TestContext2Refresh(t *testing.T) {
    12  	p := testProvider("aws")
    13  	m := testModule(t, "refresh-basic")
    14  	ctx := testContext2(t, &ContextOpts{
    15  		Module: m,
    16  		Providers: map[string]ResourceProviderFactory{
    17  			"aws": testProviderFuncFixed(p),
    18  		},
    19  		State: &State{
    20  			Modules: []*ModuleState{
    21  				&ModuleState{
    22  					Path: rootModulePath,
    23  					Resources: map[string]*ResourceState{
    24  						"aws_instance.web": &ResourceState{
    25  							Type: "aws_instance",
    26  							Primary: &InstanceState{
    27  								ID: "foo",
    28  							},
    29  						},
    30  					},
    31  				},
    32  			},
    33  		},
    34  	})
    35  
    36  	p.RefreshFn = nil
    37  	p.RefreshReturn = &InstanceState{
    38  		ID: "foo",
    39  	}
    40  
    41  	s, err := ctx.Refresh()
    42  	mod := s.RootModule()
    43  	if err != nil {
    44  		t.Fatalf("err: %s", err)
    45  	}
    46  	if !p.RefreshCalled {
    47  		t.Fatal("refresh should be called")
    48  	}
    49  	if p.RefreshState.ID != "foo" {
    50  		t.Fatalf("bad: %#v", p.RefreshState)
    51  	}
    52  	if !reflect.DeepEqual(mod.Resources["aws_instance.web"].Primary, p.RefreshReturn) {
    53  		t.Fatalf("bad: %#v %#v", mod.Resources["aws_instance.web"], p.RefreshReturn)
    54  	}
    55  
    56  	for _, r := range mod.Resources {
    57  		if r.Type == "" {
    58  			t.Fatalf("no type: %#v", r)
    59  		}
    60  	}
    61  }
    62  
    63  func TestContext2Refresh_targeted(t *testing.T) {
    64  	p := testProvider("aws")
    65  	m := testModule(t, "refresh-targeted")
    66  	ctx := testContext2(t, &ContextOpts{
    67  		Module: m,
    68  		Providers: map[string]ResourceProviderFactory{
    69  			"aws": testProviderFuncFixed(p),
    70  		},
    71  		State: &State{
    72  			Modules: []*ModuleState{
    73  				&ModuleState{
    74  					Path: rootModulePath,
    75  					Resources: map[string]*ResourceState{
    76  						"aws_vpc.metoo":      resourceState("aws_vpc", "vpc-abc123"),
    77  						"aws_instance.notme": resourceState("aws_instance", "i-bcd345"),
    78  						"aws_instance.me":    resourceState("aws_instance", "i-abc123"),
    79  						"aws_elb.meneither":  resourceState("aws_elb", "lb-abc123"),
    80  					},
    81  				},
    82  			},
    83  		},
    84  		Targets: []string{"aws_instance.me"},
    85  	})
    86  
    87  	refreshedResources := make([]string, 0, 2)
    88  	p.RefreshFn = func(i *InstanceInfo, is *InstanceState) (*InstanceState, error) {
    89  		refreshedResources = append(refreshedResources, i.Id)
    90  		return is, nil
    91  	}
    92  
    93  	_, err := ctx.Refresh()
    94  	if err != nil {
    95  		t.Fatalf("err: %s", err)
    96  	}
    97  
    98  	expected := []string{"aws_vpc.metoo", "aws_instance.me"}
    99  	if !reflect.DeepEqual(refreshedResources, expected) {
   100  		t.Fatalf("expected: %#v, got: %#v", expected, refreshedResources)
   101  	}
   102  }
   103  
   104  func TestContext2Refresh_targetedCount(t *testing.T) {
   105  	p := testProvider("aws")
   106  	m := testModule(t, "refresh-targeted-count")
   107  	ctx := testContext2(t, &ContextOpts{
   108  		Module: m,
   109  		Providers: map[string]ResourceProviderFactory{
   110  			"aws": testProviderFuncFixed(p),
   111  		},
   112  		State: &State{
   113  			Modules: []*ModuleState{
   114  				&ModuleState{
   115  					Path: rootModulePath,
   116  					Resources: map[string]*ResourceState{
   117  						"aws_vpc.metoo":      resourceState("aws_vpc", "vpc-abc123"),
   118  						"aws_instance.notme": resourceState("aws_instance", "i-bcd345"),
   119  						"aws_instance.me.0":  resourceState("aws_instance", "i-abc123"),
   120  						"aws_instance.me.1":  resourceState("aws_instance", "i-cde567"),
   121  						"aws_instance.me.2":  resourceState("aws_instance", "i-cde789"),
   122  						"aws_elb.meneither":  resourceState("aws_elb", "lb-abc123"),
   123  					},
   124  				},
   125  			},
   126  		},
   127  		Targets: []string{"aws_instance.me"},
   128  	})
   129  
   130  	refreshedResources := make([]string, 0, 2)
   131  	p.RefreshFn = func(i *InstanceInfo, is *InstanceState) (*InstanceState, error) {
   132  		refreshedResources = append(refreshedResources, i.Id)
   133  		return is, nil
   134  	}
   135  
   136  	_, err := ctx.Refresh()
   137  	if err != nil {
   138  		t.Fatalf("err: %s", err)
   139  	}
   140  
   141  	// Target didn't specify index, so we should get all our instances
   142  	expected := []string{
   143  		"aws_vpc.metoo",
   144  		"aws_instance.me.0",
   145  		"aws_instance.me.1",
   146  		"aws_instance.me.2",
   147  	}
   148  	sort.Strings(expected)
   149  	sort.Strings(refreshedResources)
   150  	if !reflect.DeepEqual(refreshedResources, expected) {
   151  		t.Fatalf("expected: %#v, got: %#v", expected, refreshedResources)
   152  	}
   153  }
   154  
   155  func TestContext2Refresh_targetedCountIndex(t *testing.T) {
   156  	p := testProvider("aws")
   157  	m := testModule(t, "refresh-targeted-count")
   158  	ctx := testContext2(t, &ContextOpts{
   159  		Module: m,
   160  		Providers: map[string]ResourceProviderFactory{
   161  			"aws": testProviderFuncFixed(p),
   162  		},
   163  		State: &State{
   164  			Modules: []*ModuleState{
   165  				&ModuleState{
   166  					Path: rootModulePath,
   167  					Resources: map[string]*ResourceState{
   168  						"aws_vpc.metoo":      resourceState("aws_vpc", "vpc-abc123"),
   169  						"aws_instance.notme": resourceState("aws_instance", "i-bcd345"),
   170  						"aws_instance.me.0":  resourceState("aws_instance", "i-abc123"),
   171  						"aws_instance.me.1":  resourceState("aws_instance", "i-cde567"),
   172  						"aws_instance.me.2":  resourceState("aws_instance", "i-cde789"),
   173  						"aws_elb.meneither":  resourceState("aws_elb", "lb-abc123"),
   174  					},
   175  				},
   176  			},
   177  		},
   178  		Targets: []string{"aws_instance.me[0]"},
   179  	})
   180  
   181  	refreshedResources := make([]string, 0, 2)
   182  	p.RefreshFn = func(i *InstanceInfo, is *InstanceState) (*InstanceState, error) {
   183  		refreshedResources = append(refreshedResources, i.Id)
   184  		return is, nil
   185  	}
   186  
   187  	_, err := ctx.Refresh()
   188  	if err != nil {
   189  		t.Fatalf("err: %s", err)
   190  	}
   191  
   192  	expected := []string{"aws_vpc.metoo", "aws_instance.me.0"}
   193  	if !reflect.DeepEqual(refreshedResources, expected) {
   194  		t.Fatalf("expected: %#v, got: %#v", expected, refreshedResources)
   195  	}
   196  }
   197  
   198  func TestContext2Refresh_moduleComputedVar(t *testing.T) {
   199  	p := testProvider("aws")
   200  	m := testModule(t, "refresh-module-computed-var")
   201  	ctx := testContext2(t, &ContextOpts{
   202  		Module: m,
   203  		Providers: map[string]ResourceProviderFactory{
   204  			"aws": testProviderFuncFixed(p),
   205  		},
   206  	})
   207  
   208  	// This was failing (see GH-2188) at some point, so this test just
   209  	// verifies that the failure goes away.
   210  	if _, err := ctx.Refresh(); err != nil {
   211  		t.Fatalf("err: %s", err)
   212  	}
   213  }
   214  
   215  func TestContext2Refresh_delete(t *testing.T) {
   216  	p := testProvider("aws")
   217  	m := testModule(t, "refresh-basic")
   218  	ctx := testContext2(t, &ContextOpts{
   219  		Module: m,
   220  		Providers: map[string]ResourceProviderFactory{
   221  			"aws": testProviderFuncFixed(p),
   222  		},
   223  		State: &State{
   224  			Modules: []*ModuleState{
   225  				&ModuleState{
   226  					Path: rootModulePath,
   227  					Resources: map[string]*ResourceState{
   228  						"aws_instance.web": &ResourceState{
   229  							Type: "aws_instance",
   230  							Primary: &InstanceState{
   231  								ID: "foo",
   232  							},
   233  						},
   234  					},
   235  				},
   236  			},
   237  		},
   238  	})
   239  
   240  	p.RefreshFn = nil
   241  	p.RefreshReturn = nil
   242  
   243  	s, err := ctx.Refresh()
   244  	if err != nil {
   245  		t.Fatalf("err: %s", err)
   246  	}
   247  
   248  	mod := s.RootModule()
   249  	if len(mod.Resources) > 0 {
   250  		t.Fatal("resources should be empty")
   251  	}
   252  }
   253  
   254  func TestContext2Refresh_ignoreUncreated(t *testing.T) {
   255  	p := testProvider("aws")
   256  	m := testModule(t, "refresh-basic")
   257  	ctx := testContext2(t, &ContextOpts{
   258  		Module: m,
   259  		Providers: map[string]ResourceProviderFactory{
   260  			"aws": testProviderFuncFixed(p),
   261  		},
   262  		State: nil,
   263  	})
   264  
   265  	p.RefreshFn = nil
   266  	p.RefreshReturn = &InstanceState{
   267  		ID: "foo",
   268  	}
   269  
   270  	_, err := ctx.Refresh()
   271  	if err != nil {
   272  		t.Fatalf("err: %s", err)
   273  	}
   274  	if p.RefreshCalled {
   275  		t.Fatal("refresh should not be called")
   276  	}
   277  }
   278  
   279  func TestContext2Refresh_hook(t *testing.T) {
   280  	h := new(MockHook)
   281  	p := testProvider("aws")
   282  	m := testModule(t, "refresh-basic")
   283  	ctx := testContext2(t, &ContextOpts{
   284  		Module: m,
   285  		Hooks:  []Hook{h},
   286  		Providers: map[string]ResourceProviderFactory{
   287  			"aws": testProviderFuncFixed(p),
   288  		},
   289  		State: &State{
   290  			Modules: []*ModuleState{
   291  				&ModuleState{
   292  					Path: rootModulePath,
   293  					Resources: map[string]*ResourceState{
   294  						"aws_instance.web": &ResourceState{
   295  							Type: "aws_instance",
   296  							Primary: &InstanceState{
   297  								ID: "foo",
   298  							},
   299  						},
   300  					},
   301  				},
   302  			},
   303  		},
   304  	})
   305  
   306  	if _, err := ctx.Refresh(); err != nil {
   307  		t.Fatalf("err: %s", err)
   308  	}
   309  	if !h.PreRefreshCalled {
   310  		t.Fatal("should be called")
   311  	}
   312  	if !h.PostRefreshCalled {
   313  		t.Fatal("should be called")
   314  	}
   315  }
   316  
   317  func TestContext2Refresh_modules(t *testing.T) {
   318  	p := testProvider("aws")
   319  	m := testModule(t, "refresh-modules")
   320  	state := &State{
   321  		Modules: []*ModuleState{
   322  			&ModuleState{
   323  				Path: rootModulePath,
   324  				Resources: map[string]*ResourceState{
   325  					"aws_instance.web": &ResourceState{
   326  						Type: "aws_instance",
   327  						Primary: &InstanceState{
   328  							ID:      "bar",
   329  							Tainted: true,
   330  						},
   331  					},
   332  				},
   333  			},
   334  
   335  			&ModuleState{
   336  				Path: []string{"root", "child"},
   337  				Resources: map[string]*ResourceState{
   338  					"aws_instance.web": &ResourceState{
   339  						Type: "aws_instance",
   340  						Primary: &InstanceState{
   341  							ID: "baz",
   342  						},
   343  					},
   344  				},
   345  			},
   346  		},
   347  	}
   348  	ctx := testContext2(t, &ContextOpts{
   349  		Module: m,
   350  		Providers: map[string]ResourceProviderFactory{
   351  			"aws": testProviderFuncFixed(p),
   352  		},
   353  		State: state,
   354  	})
   355  
   356  	p.RefreshFn = func(info *InstanceInfo, s *InstanceState) (*InstanceState, error) {
   357  		if s.ID != "baz" {
   358  			return s, nil
   359  		}
   360  
   361  		s.ID = "new"
   362  		return s, nil
   363  	}
   364  
   365  	s, err := ctx.Refresh()
   366  	if err != nil {
   367  		t.Fatalf("err: %s", err)
   368  	}
   369  
   370  	actual := strings.TrimSpace(s.String())
   371  	expected := strings.TrimSpace(testContextRefreshModuleStr)
   372  	if actual != expected {
   373  		t.Fatalf("bad:\n\n%s\n\n%s", actual, expected)
   374  	}
   375  }
   376  
   377  func TestContext2Refresh_moduleInputComputedOutput(t *testing.T) {
   378  	m := testModule(t, "refresh-module-input-computed-output")
   379  	p := testProvider("aws")
   380  	p.DiffFn = testDiffFn
   381  	ctx := testContext2(t, &ContextOpts{
   382  		Module: m,
   383  		Providers: map[string]ResourceProviderFactory{
   384  			"aws": testProviderFuncFixed(p),
   385  		},
   386  	})
   387  
   388  	if _, err := ctx.Refresh(); err != nil {
   389  		t.Fatalf("err: %s", err)
   390  	}
   391  }
   392  
   393  func TestContext2Refresh_moduleVarModule(t *testing.T) {
   394  	m := testModule(t, "refresh-module-var-module")
   395  	p := testProvider("aws")
   396  	p.DiffFn = testDiffFn
   397  	ctx := testContext2(t, &ContextOpts{
   398  		Module: m,
   399  		Providers: map[string]ResourceProviderFactory{
   400  			"aws": testProviderFuncFixed(p),
   401  		},
   402  	})
   403  
   404  	if _, err := ctx.Refresh(); err != nil {
   405  		t.Fatalf("err: %s", err)
   406  	}
   407  }
   408  
   409  // GH-70
   410  func TestContext2Refresh_noState(t *testing.T) {
   411  	p := testProvider("aws")
   412  	m := testModule(t, "refresh-no-state")
   413  	ctx := testContext2(t, &ContextOpts{
   414  		Module: m,
   415  		Providers: map[string]ResourceProviderFactory{
   416  			"aws": testProviderFuncFixed(p),
   417  		},
   418  	})
   419  
   420  	p.RefreshFn = nil
   421  	p.RefreshReturn = &InstanceState{
   422  		ID: "foo",
   423  	}
   424  
   425  	if _, err := ctx.Refresh(); err != nil {
   426  		t.Fatalf("err: %s", err)
   427  	}
   428  }
   429  
   430  func TestContext2Refresh_output(t *testing.T) {
   431  	p := testProvider("aws")
   432  	m := testModule(t, "refresh-output")
   433  	ctx := testContext2(t, &ContextOpts{
   434  		Module: m,
   435  		Providers: map[string]ResourceProviderFactory{
   436  			"aws": testProviderFuncFixed(p),
   437  		},
   438  		State: &State{
   439  			Modules: []*ModuleState{
   440  				&ModuleState{
   441  					Path: rootModulePath,
   442  					Resources: map[string]*ResourceState{
   443  						"aws_instance.web": &ResourceState{
   444  							Type: "aws_instance",
   445  							Primary: &InstanceState{
   446  								ID: "foo",
   447  								Attributes: map[string]string{
   448  									"foo": "bar",
   449  								},
   450  							},
   451  						},
   452  					},
   453  
   454  					Outputs: map[string]*OutputState{
   455  						"foo": &OutputState{
   456  							Type:      "string",
   457  							Sensitive: false,
   458  							Value:     "foo",
   459  						},
   460  					},
   461  				},
   462  			},
   463  		},
   464  	})
   465  
   466  	p.RefreshFn = func(info *InstanceInfo, s *InstanceState) (*InstanceState, error) {
   467  		return s, nil
   468  	}
   469  
   470  	s, err := ctx.Refresh()
   471  	if err != nil {
   472  		t.Fatalf("err: %s", err)
   473  	}
   474  
   475  	actual := strings.TrimSpace(s.String())
   476  	expected := strings.TrimSpace(testContextRefreshOutputStr)
   477  	if actual != expected {
   478  		t.Fatalf("bad:\n\n%s\n\n%s", actual, expected)
   479  	}
   480  }
   481  
   482  func TestContext2Refresh_outputPartial(t *testing.T) {
   483  	p := testProvider("aws")
   484  	m := testModule(t, "refresh-output-partial")
   485  	ctx := testContext2(t, &ContextOpts{
   486  		Module: m,
   487  		Providers: map[string]ResourceProviderFactory{
   488  			"aws": testProviderFuncFixed(p),
   489  		},
   490  		State: &State{
   491  			Modules: []*ModuleState{
   492  				&ModuleState{
   493  					Path: rootModulePath,
   494  					Resources: map[string]*ResourceState{
   495  						"aws_instance.foo": &ResourceState{
   496  							Type: "aws_instance",
   497  							Primary: &InstanceState{
   498  								ID: "foo",
   499  							},
   500  						},
   501  					},
   502  					Outputs: map[string]*OutputState{},
   503  				},
   504  			},
   505  		},
   506  	})
   507  
   508  	p.RefreshFn = nil
   509  	p.RefreshReturn = nil
   510  
   511  	s, err := ctx.Refresh()
   512  	if err != nil {
   513  		t.Fatalf("err: %s", err)
   514  	}
   515  
   516  	actual := strings.TrimSpace(s.String())
   517  	expected := strings.TrimSpace(testContextRefreshOutputPartialStr)
   518  	if actual != expected {
   519  		t.Fatalf("bad:\n\n%s\n\n%s", actual, expected)
   520  	}
   521  }
   522  
   523  func TestContext2Refresh_state(t *testing.T) {
   524  	p := testProvider("aws")
   525  	m := testModule(t, "refresh-basic")
   526  	state := &State{
   527  		Modules: []*ModuleState{
   528  			&ModuleState{
   529  				Path: rootModulePath,
   530  				Resources: map[string]*ResourceState{
   531  					"aws_instance.web": &ResourceState{
   532  						Primary: &InstanceState{
   533  							ID: "bar",
   534  						},
   535  					},
   536  				},
   537  			},
   538  		},
   539  	}
   540  	ctx := testContext2(t, &ContextOpts{
   541  		Module: m,
   542  		Providers: map[string]ResourceProviderFactory{
   543  			"aws": testProviderFuncFixed(p),
   544  		},
   545  		State: state,
   546  	})
   547  
   548  	p.RefreshFn = nil
   549  	p.RefreshReturn = &InstanceState{
   550  		ID: "foo",
   551  	}
   552  
   553  	s, err := ctx.Refresh()
   554  	if err != nil {
   555  		t.Fatalf("err: %s", err)
   556  	}
   557  	originalMod := state.RootModule()
   558  	mod := s.RootModule()
   559  	if !p.RefreshCalled {
   560  		t.Fatal("refresh should be called")
   561  	}
   562  	if !reflect.DeepEqual(p.RefreshState, originalMod.Resources["aws_instance.web"].Primary) {
   563  		t.Fatalf(
   564  			"bad:\n\n%#v\n\n%#v",
   565  			p.RefreshState,
   566  			originalMod.Resources["aws_instance.web"].Primary)
   567  	}
   568  	if !reflect.DeepEqual(mod.Resources["aws_instance.web"].Primary, p.RefreshReturn) {
   569  		t.Fatalf("bad: %#v", mod.Resources)
   570  	}
   571  }
   572  
   573  func TestContext2Refresh_dataState(t *testing.T) {
   574  	p := testProvider("null")
   575  	m := testModule(t, "refresh-data-resource-basic")
   576  	state := &State{
   577  		Modules: []*ModuleState{
   578  			&ModuleState{
   579  				Path: rootModulePath,
   580  				// Intentionally no resources since data resources are
   581  				// supposed to refresh themselves even if they didn't
   582  				// already exist.
   583  				Resources: map[string]*ResourceState{},
   584  			},
   585  		},
   586  	}
   587  	ctx := testContext2(t, &ContextOpts{
   588  		Module: m,
   589  		Providers: map[string]ResourceProviderFactory{
   590  			"null": testProviderFuncFixed(p),
   591  		},
   592  		State: state,
   593  	})
   594  
   595  	p.ReadDataDiffFn = nil
   596  	p.ReadDataDiffReturn = &InstanceDiff{
   597  		Attributes: map[string]*ResourceAttrDiff{
   598  			"inputs.#": {
   599  				Old:  "0",
   600  				New:  "1",
   601  				Type: DiffAttrInput,
   602  			},
   603  			"inputs.test": {
   604  				Old:  "",
   605  				New:  "yes",
   606  				Type: DiffAttrInput,
   607  			},
   608  			"outputs.#": {
   609  				Old:         "",
   610  				New:         "",
   611  				NewComputed: true,
   612  				Type:        DiffAttrOutput,
   613  			},
   614  		},
   615  	}
   616  
   617  	p.ReadDataApplyFn = nil
   618  	p.ReadDataApplyReturn = &InstanceState{
   619  		ID: "-",
   620  	}
   621  
   622  	s, err := ctx.Refresh()
   623  	if err != nil {
   624  		t.Fatalf("err: %s", err)
   625  	}
   626  
   627  	if !p.ReadDataDiffCalled {
   628  		t.Fatal("ReadDataDiff should have been called")
   629  	}
   630  	if !p.ReadDataApplyCalled {
   631  		t.Fatal("ReadDataApply should have been called")
   632  	}
   633  
   634  	mod := s.RootModule()
   635  	if got := mod.Resources["data.null_data_source.testing"].Primary.ID; got != "-" {
   636  		t.Fatalf("resource id is %q; want %s", got, "-")
   637  	}
   638  	if !reflect.DeepEqual(mod.Resources["data.null_data_source.testing"].Primary, p.ReadDataApplyReturn) {
   639  		t.Fatalf("bad: %#v", mod.Resources)
   640  	}
   641  }
   642  
   643  func TestContext2Refresh_tainted(t *testing.T) {
   644  	p := testProvider("aws")
   645  	m := testModule(t, "refresh-basic")
   646  	state := &State{
   647  		Modules: []*ModuleState{
   648  			&ModuleState{
   649  				Path: rootModulePath,
   650  				Resources: map[string]*ResourceState{
   651  					"aws_instance.web": &ResourceState{
   652  						Type: "aws_instance",
   653  						Primary: &InstanceState{
   654  							ID:      "bar",
   655  							Tainted: true,
   656  						},
   657  					},
   658  				},
   659  			},
   660  		},
   661  	}
   662  	ctx := testContext2(t, &ContextOpts{
   663  		Module: m,
   664  		Providers: map[string]ResourceProviderFactory{
   665  			"aws": testProviderFuncFixed(p),
   666  		},
   667  		State: state,
   668  	})
   669  
   670  	p.RefreshFn = nil
   671  	p.RefreshReturn = &InstanceState{
   672  		ID:      "foo",
   673  		Tainted: true,
   674  	}
   675  
   676  	s, err := ctx.Refresh()
   677  	if err != nil {
   678  		t.Fatalf("err: %s", err)
   679  	}
   680  	if !p.RefreshCalled {
   681  		t.Fatal("refresh should be called")
   682  	}
   683  
   684  	actual := strings.TrimSpace(s.String())
   685  	expected := strings.TrimSpace(testContextRefreshTaintedStr)
   686  	if actual != expected {
   687  		t.Fatalf("bad:\n\n%s\n\n%s", actual, expected)
   688  	}
   689  }
   690  
   691  // Doing a Refresh (or any operation really, but Refresh usually
   692  // happens first) with a config with an unknown provider should result in
   693  // an error. The key bug this found was that this wasn't happening if
   694  // Providers was _empty_.
   695  func TestContext2Refresh_unknownProvider(t *testing.T) {
   696  	m := testModule(t, "refresh-unknown-provider")
   697  	p := testProvider("aws")
   698  	p.ApplyFn = testApplyFn
   699  	p.DiffFn = testDiffFn
   700  	ctx := testContext2(t, &ContextOpts{
   701  		Module:    m,
   702  		Providers: map[string]ResourceProviderFactory{},
   703  	})
   704  
   705  	if _, err := ctx.Refresh(); err == nil {
   706  		t.Fatal("should error")
   707  	}
   708  }
   709  
   710  func TestContext2Refresh_vars(t *testing.T) {
   711  	p := testProvider("aws")
   712  	m := testModule(t, "refresh-vars")
   713  	ctx := testContext2(t, &ContextOpts{
   714  		Module: m,
   715  		Providers: map[string]ResourceProviderFactory{
   716  			"aws": testProviderFuncFixed(p),
   717  		},
   718  		State: &State{
   719  
   720  			Modules: []*ModuleState{
   721  				&ModuleState{
   722  					Path: rootModulePath,
   723  					Resources: map[string]*ResourceState{
   724  						"aws_instance.web": &ResourceState{
   725  							Type: "aws_instance",
   726  							Primary: &InstanceState{
   727  								ID: "foo",
   728  							},
   729  						},
   730  					},
   731  				},
   732  			},
   733  		},
   734  	})
   735  
   736  	p.RefreshFn = nil
   737  	p.RefreshReturn = &InstanceState{
   738  		ID: "foo",
   739  	}
   740  
   741  	s, err := ctx.Refresh()
   742  	if err != nil {
   743  		t.Fatalf("err: %s", err)
   744  	}
   745  	mod := s.RootModule()
   746  	if !p.RefreshCalled {
   747  		t.Fatal("refresh should be called")
   748  	}
   749  	if p.RefreshState.ID != "foo" {
   750  		t.Fatalf("bad: %#v", p.RefreshState)
   751  	}
   752  	if !reflect.DeepEqual(mod.Resources["aws_instance.web"].Primary, p.RefreshReturn) {
   753  		t.Fatalf("bad: %#v", mod.Resources["aws_instance.web"])
   754  	}
   755  
   756  	for _, r := range mod.Resources {
   757  		if r.Type == "" {
   758  			t.Fatalf("no type: %#v", r)
   759  		}
   760  	}
   761  }
   762  
   763  func TestContext2Refresh_orphanModule(t *testing.T) {
   764  	p := testProvider("aws")
   765  	m := testModule(t, "refresh-module-orphan")
   766  
   767  	// Create a custom refresh function to track the order they were visited
   768  	var order []string
   769  	var orderLock sync.Mutex
   770  	p.RefreshFn = func(
   771  		info *InstanceInfo,
   772  		is *InstanceState) (*InstanceState, error) {
   773  		orderLock.Lock()
   774  		defer orderLock.Unlock()
   775  
   776  		order = append(order, is.ID)
   777  		return is, nil
   778  	}
   779  
   780  	state := &State{
   781  		Modules: []*ModuleState{
   782  			&ModuleState{
   783  				Path: rootModulePath,
   784  				Resources: map[string]*ResourceState{
   785  					"aws_instance.foo": &ResourceState{
   786  						Primary: &InstanceState{
   787  							ID: "i-abc123",
   788  							Attributes: map[string]string{
   789  								"childid":      "i-bcd234",
   790  								"grandchildid": "i-cde345",
   791  							},
   792  						},
   793  						Dependencies: []string{
   794  							"module.child",
   795  							"module.child",
   796  						},
   797  					},
   798  				},
   799  			},
   800  			&ModuleState{
   801  				Path: append(rootModulePath, "child"),
   802  				Resources: map[string]*ResourceState{
   803  					"aws_instance.bar": &ResourceState{
   804  						Primary: &InstanceState{
   805  							ID: "i-bcd234",
   806  							Attributes: map[string]string{
   807  								"grandchildid": "i-cde345",
   808  							},
   809  						},
   810  						Dependencies: []string{
   811  							"module.grandchild",
   812  						},
   813  					},
   814  				},
   815  				Outputs: map[string]*OutputState{
   816  					"id": &OutputState{
   817  						Value: "i-bcd234",
   818  						Type:  "string",
   819  					},
   820  					"grandchild_id": &OutputState{
   821  						Value: "i-cde345",
   822  						Type:  "string",
   823  					},
   824  				},
   825  			},
   826  			&ModuleState{
   827  				Path: append(rootModulePath, "child", "grandchild"),
   828  				Resources: map[string]*ResourceState{
   829  					"aws_instance.baz": &ResourceState{
   830  						Primary: &InstanceState{
   831  							ID: "i-cde345",
   832  						},
   833  					},
   834  				},
   835  				Outputs: map[string]*OutputState{
   836  					"id": &OutputState{
   837  						Value: "i-cde345",
   838  						Type:  "string",
   839  					},
   840  				},
   841  			},
   842  		},
   843  	}
   844  	ctx := testContext2(t, &ContextOpts{
   845  		Module: m,
   846  		Providers: map[string]ResourceProviderFactory{
   847  			"aws": testProviderFuncFixed(p),
   848  		},
   849  		State: state,
   850  	})
   851  
   852  	testCheckDeadlock(t, func() {
   853  		_, err := ctx.Refresh()
   854  		if err != nil {
   855  			t.Fatalf("err: %s", err)
   856  		}
   857  
   858  		// TODO: handle order properly for orphaned modules / resources
   859  		// expected := []string{"i-abc123", "i-bcd234", "i-cde345"}
   860  		// if !reflect.DeepEqual(order, expected) {
   861  		// 	t.Fatalf("expected: %#v, got: %#v", expected, order)
   862  		// }
   863  	})
   864  }
   865  
   866  func TestContext2Validate(t *testing.T) {
   867  	p := testProvider("aws")
   868  	m := testModule(t, "validate-good")
   869  	c := testContext2(t, &ContextOpts{
   870  		Module: m,
   871  		Providers: map[string]ResourceProviderFactory{
   872  			"aws": testProviderFuncFixed(p),
   873  		},
   874  	})
   875  
   876  	w, e := c.Validate()
   877  	if len(w) > 0 {
   878  		t.Fatalf("bad: %#v", w)
   879  	}
   880  	if len(e) > 0 {
   881  		t.Fatalf("bad: %s", e)
   882  	}
   883  }