github.com/hs0210/hashicorp-terraform@v0.11.12-beta1/terraform/context_import_test.go (about)

     1  package terraform
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  )
     8  
     9  func TestContextImport_basic(t *testing.T) {
    10  	p := testProvider("aws")
    11  	m := testModule(t, "import-provider")
    12  	ctx := testContext2(t, &ContextOpts{
    13  		Module: m,
    14  		ProviderResolver: ResourceProviderResolverFixed(
    15  			map[string]ResourceProviderFactory{
    16  				"aws": testProviderFuncFixed(p),
    17  			},
    18  		),
    19  	})
    20  
    21  	p.ImportStateReturn = []*InstanceState{
    22  		&InstanceState{
    23  			ID:        "foo",
    24  			Ephemeral: EphemeralState{Type: "aws_instance"},
    25  		},
    26  	}
    27  
    28  	state, err := ctx.Import(&ImportOpts{
    29  		Targets: []*ImportTarget{
    30  			&ImportTarget{
    31  				Addr: "aws_instance.foo",
    32  				ID:   "bar",
    33  			},
    34  		},
    35  	})
    36  	if err != nil {
    37  		t.Fatalf("err: %s", err)
    38  	}
    39  	actual := strings.TrimSpace(state.String())
    40  	expected := strings.TrimSpace(testImportStr)
    41  	if actual != expected {
    42  		t.Fatalf("bad: \n%s", actual)
    43  	}
    44  }
    45  
    46  func TestContextImport_countIndex(t *testing.T) {
    47  	p := testProvider("aws")
    48  	m := testModule(t, "import-provider")
    49  	ctx := testContext2(t, &ContextOpts{
    50  		Module: m,
    51  		ProviderResolver: ResourceProviderResolverFixed(
    52  			map[string]ResourceProviderFactory{
    53  				"aws": testProviderFuncFixed(p),
    54  			},
    55  		),
    56  	})
    57  
    58  	p.ImportStateReturn = []*InstanceState{
    59  		&InstanceState{
    60  			ID:        "foo",
    61  			Ephemeral: EphemeralState{Type: "aws_instance"},
    62  		},
    63  	}
    64  
    65  	state, err := ctx.Import(&ImportOpts{
    66  		Targets: []*ImportTarget{
    67  			&ImportTarget{
    68  				Addr: "aws_instance.foo[0]",
    69  				ID:   "bar",
    70  			},
    71  		},
    72  	})
    73  	if err != nil {
    74  		t.Fatalf("err: %s", err)
    75  	}
    76  
    77  	actual := strings.TrimSpace(state.String())
    78  	expected := strings.TrimSpace(testImportCountIndexStr)
    79  	if actual != expected {
    80  		t.Fatalf("bad: \n%s", actual)
    81  	}
    82  }
    83  
    84  func TestContextImport_collision(t *testing.T) {
    85  	p := testProvider("aws")
    86  	m := testModule(t, "import-provider")
    87  	ctx := testContext2(t, &ContextOpts{
    88  		Module: m,
    89  		ProviderResolver: ResourceProviderResolverFixed(
    90  			map[string]ResourceProviderFactory{
    91  				"aws": testProviderFuncFixed(p),
    92  			},
    93  		),
    94  
    95  		State: &State{
    96  			Modules: []*ModuleState{
    97  				&ModuleState{
    98  					Path: []string{"root"},
    99  					Resources: map[string]*ResourceState{
   100  						"aws_instance.foo": &ResourceState{
   101  							Type: "aws_instance",
   102  							Primary: &InstanceState{
   103  								ID: "bar",
   104  							},
   105  						},
   106  					},
   107  				},
   108  			},
   109  		},
   110  	})
   111  
   112  	p.ImportStateReturn = []*InstanceState{
   113  		&InstanceState{
   114  			ID:        "foo",
   115  			Ephemeral: EphemeralState{Type: "aws_instance"},
   116  		},
   117  	}
   118  
   119  	state, err := ctx.Import(&ImportOpts{
   120  		Targets: []*ImportTarget{
   121  			&ImportTarget{
   122  				Addr: "aws_instance.foo",
   123  				ID:   "bar",
   124  			},
   125  		},
   126  	})
   127  	if err == nil {
   128  		t.Fatalf("err: %s", err)
   129  	}
   130  
   131  	actual := strings.TrimSpace(state.String())
   132  	expected := strings.TrimSpace(testImportCollisionStr)
   133  	if actual != expected {
   134  		t.Fatalf("bad: \n%s", actual)
   135  	}
   136  }
   137  
   138  func TestContextImport_missingType(t *testing.T) {
   139  	p := testProvider("aws")
   140  	m := testModule(t, "import-provider")
   141  	ctx := testContext2(t, &ContextOpts{
   142  		Module: m,
   143  		ProviderResolver: ResourceProviderResolverFixed(
   144  			map[string]ResourceProviderFactory{
   145  				"aws": testProviderFuncFixed(p),
   146  			},
   147  		),
   148  	})
   149  
   150  	p.ImportStateReturn = []*InstanceState{
   151  		&InstanceState{
   152  			ID: "foo",
   153  		},
   154  	}
   155  
   156  	state, err := ctx.Import(&ImportOpts{
   157  		Targets: []*ImportTarget{
   158  			&ImportTarget{
   159  				Addr: "aws_instance.foo",
   160  				ID:   "bar",
   161  			},
   162  		},
   163  	})
   164  	if err == nil {
   165  		t.Fatal("should error")
   166  	}
   167  
   168  	actual := strings.TrimSpace(state.String())
   169  	expected := "<no state>"
   170  	if actual != expected {
   171  		t.Fatalf("bad: \n%s", actual)
   172  	}
   173  }
   174  
   175  func TestContextImport_moduleProvider(t *testing.T) {
   176  	p := testProvider("aws")
   177  	m := testModule(t, "import-provider")
   178  	ctx := testContext2(t, &ContextOpts{
   179  		Module: m,
   180  		ProviderResolver: ResourceProviderResolverFixed(
   181  			map[string]ResourceProviderFactory{
   182  				"aws": testProviderFuncFixed(p),
   183  			},
   184  		),
   185  	})
   186  
   187  	p.ImportStateReturn = []*InstanceState{
   188  		&InstanceState{
   189  			ID:        "foo",
   190  			Ephemeral: EphemeralState{Type: "aws_instance"},
   191  		},
   192  	}
   193  
   194  	configured := false
   195  	p.ConfigureFn = func(c *ResourceConfig) error {
   196  		configured = true
   197  
   198  		if v, ok := c.Get("foo"); !ok || v.(string) != "bar" {
   199  			return fmt.Errorf("bad")
   200  		}
   201  
   202  		return nil
   203  	}
   204  
   205  	state, err := ctx.Import(&ImportOpts{
   206  		Module: m,
   207  		Targets: []*ImportTarget{
   208  			&ImportTarget{
   209  				Addr: "aws_instance.foo",
   210  				ID:   "bar",
   211  			},
   212  		},
   213  	})
   214  	if err != nil {
   215  		t.Fatalf("err: %s", err)
   216  	}
   217  
   218  	if !configured {
   219  		t.Fatal("didn't configure provider")
   220  	}
   221  
   222  	actual := strings.TrimSpace(state.String())
   223  	expected := strings.TrimSpace(testImportStr)
   224  	if actual != expected {
   225  		t.Fatalf("bad: \n%s", actual)
   226  	}
   227  }
   228  
   229  // Importing into a module requires a provider config in that module.
   230  func TestContextImport_providerModule(t *testing.T) {
   231  	p := testProvider("aws")
   232  	m := testModule(t, "import-provider-module")
   233  	ctx := testContext2(t, &ContextOpts{
   234  		Module: m,
   235  		ProviderResolver: ResourceProviderResolverFixed(
   236  			map[string]ResourceProviderFactory{
   237  				"aws": testProviderFuncFixed(p),
   238  			},
   239  		),
   240  	})
   241  
   242  	p.ImportStateReturn = []*InstanceState{
   243  		&InstanceState{
   244  			ID:        "foo",
   245  			Ephemeral: EphemeralState{Type: "aws_instance"},
   246  		},
   247  	}
   248  
   249  	configured := false
   250  	p.ConfigureFn = func(c *ResourceConfig) error {
   251  		configured = true
   252  
   253  		if v, ok := c.Get("foo"); !ok || v.(string) != "bar" {
   254  			return fmt.Errorf("bad")
   255  		}
   256  
   257  		return nil
   258  	}
   259  
   260  	_, err := ctx.Import(&ImportOpts{
   261  		Module: m,
   262  		Targets: []*ImportTarget{
   263  			&ImportTarget{
   264  				Addr: "module.child.aws_instance.foo",
   265  				ID:   "bar",
   266  			},
   267  		},
   268  	})
   269  	if err != nil {
   270  		t.Fatalf("err: %s", err)
   271  	}
   272  
   273  	if !configured {
   274  		t.Fatal("didn't configure provider")
   275  	}
   276  }
   277  
   278  // Test that import will interpolate provider configuration and use
   279  // that configuration for import.
   280  func TestContextImport_providerVarConfig(t *testing.T) {
   281  	p := testProvider("aws")
   282  	m := testModule(t, "import-provider-vars")
   283  	ctx := testContext2(t, &ContextOpts{
   284  		Module: m,
   285  		ProviderResolver: ResourceProviderResolverFixed(
   286  			map[string]ResourceProviderFactory{
   287  				"aws": testProviderFuncFixed(p),
   288  			},
   289  		),
   290  		Variables: map[string]interface{}{
   291  			"foo": "bar",
   292  		},
   293  	})
   294  
   295  	configured := false
   296  	p.ConfigureFn = func(c *ResourceConfig) error {
   297  		configured = true
   298  
   299  		if v, ok := c.Get("foo"); !ok || v.(string) != "bar" {
   300  			return fmt.Errorf("bad value: %#v", v)
   301  		}
   302  
   303  		return nil
   304  	}
   305  
   306  	p.ImportStateReturn = []*InstanceState{
   307  		&InstanceState{
   308  			ID:        "foo",
   309  			Ephemeral: EphemeralState{Type: "aws_instance"},
   310  		},
   311  	}
   312  
   313  	state, err := ctx.Import(&ImportOpts{
   314  		Targets: []*ImportTarget{
   315  			&ImportTarget{
   316  				Addr: "aws_instance.foo",
   317  				ID:   "bar",
   318  			},
   319  		},
   320  	})
   321  	if err != nil {
   322  		t.Fatalf("err: %s", err)
   323  	}
   324  
   325  	if !configured {
   326  		t.Fatal("didn't configure provider")
   327  	}
   328  
   329  	actual := strings.TrimSpace(state.String())
   330  	expected := strings.TrimSpace(testImportStr)
   331  	if actual != expected {
   332  		t.Fatalf("bad: \n%s", actual)
   333  	}
   334  }
   335  
   336  // Test that provider configs can't reference resources.
   337  func TestContextImport_providerNonVarConfig(t *testing.T) {
   338  	p := testProvider("aws")
   339  	m := testModule(t, "import-provider-non-vars")
   340  	ctx := testContext2(t, &ContextOpts{
   341  		Module: m,
   342  		ProviderResolver: ResourceProviderResolverFixed(
   343  			map[string]ResourceProviderFactory{
   344  				"aws": testProviderFuncFixed(p),
   345  			},
   346  		),
   347  	})
   348  
   349  	p.ImportStateReturn = []*InstanceState{
   350  		&InstanceState{
   351  			ID:        "foo",
   352  			Ephemeral: EphemeralState{Type: "aws_instance"},
   353  		},
   354  	}
   355  
   356  	_, err := ctx.Import(&ImportOpts{
   357  		Targets: []*ImportTarget{
   358  			&ImportTarget{
   359  				Addr: "aws_instance.foo",
   360  				ID:   "bar",
   361  			},
   362  		},
   363  	})
   364  	if err == nil {
   365  		t.Fatal("should error")
   366  	}
   367  }
   368  
   369  func TestContextImport_refresh(t *testing.T) {
   370  	p := testProvider("aws")
   371  	m := testModule(t, "import-provider")
   372  	ctx := testContext2(t, &ContextOpts{
   373  		Module: m,
   374  		ProviderResolver: ResourceProviderResolverFixed(
   375  			map[string]ResourceProviderFactory{
   376  				"aws": testProviderFuncFixed(p),
   377  			},
   378  		),
   379  	})
   380  
   381  	p.ImportStateReturn = []*InstanceState{
   382  		&InstanceState{
   383  			ID:        "foo",
   384  			Ephemeral: EphemeralState{Type: "aws_instance"},
   385  		},
   386  	}
   387  
   388  	p.RefreshFn = func(info *InstanceInfo, s *InstanceState) (*InstanceState, error) {
   389  		return &InstanceState{
   390  			ID:         "foo",
   391  			Attributes: map[string]string{"foo": "bar"},
   392  		}, nil
   393  	}
   394  
   395  	state, err := ctx.Import(&ImportOpts{
   396  		Targets: []*ImportTarget{
   397  			&ImportTarget{
   398  				Addr: "aws_instance.foo",
   399  				ID:   "bar",
   400  			},
   401  		},
   402  	})
   403  	if err != nil {
   404  		t.Fatalf("err: %s", err)
   405  	}
   406  
   407  	actual := strings.TrimSpace(state.String())
   408  	expected := strings.TrimSpace(testImportRefreshStr)
   409  	if actual != expected {
   410  		t.Fatalf("bad: \n%s", actual)
   411  	}
   412  }
   413  
   414  func TestContextImport_refreshNil(t *testing.T) {
   415  	p := testProvider("aws")
   416  	m := testModule(t, "import-provider")
   417  	ctx := testContext2(t, &ContextOpts{
   418  		Module: m,
   419  		ProviderResolver: ResourceProviderResolverFixed(
   420  			map[string]ResourceProviderFactory{
   421  				"aws": testProviderFuncFixed(p),
   422  			},
   423  		),
   424  	})
   425  
   426  	p.ImportStateReturn = []*InstanceState{
   427  		&InstanceState{
   428  			ID:        "foo",
   429  			Ephemeral: EphemeralState{Type: "aws_instance"},
   430  		},
   431  	}
   432  
   433  	p.RefreshFn = func(info *InstanceInfo, s *InstanceState) (*InstanceState, error) {
   434  		return nil, nil
   435  	}
   436  
   437  	state, err := ctx.Import(&ImportOpts{
   438  		Targets: []*ImportTarget{
   439  			&ImportTarget{
   440  				Addr: "aws_instance.foo",
   441  				ID:   "bar",
   442  			},
   443  		},
   444  	})
   445  	if err == nil {
   446  		t.Fatal("should error")
   447  	}
   448  
   449  	actual := strings.TrimSpace(state.String())
   450  	expected := "<no state>"
   451  	if actual != expected {
   452  		t.Fatalf("bad: \n%s", actual)
   453  	}
   454  }
   455  
   456  func TestContextImport_module(t *testing.T) {
   457  	p := testProvider("aws")
   458  	m := testModule(t, "import-provider")
   459  	ctx := testContext2(t, &ContextOpts{
   460  		Module: m,
   461  		ProviderResolver: ResourceProviderResolverFixed(
   462  			map[string]ResourceProviderFactory{
   463  				"aws": testProviderFuncFixed(p),
   464  			},
   465  		),
   466  	})
   467  
   468  	p.ImportStateReturn = []*InstanceState{
   469  		&InstanceState{
   470  			ID:        "foo",
   471  			Ephemeral: EphemeralState{Type: "aws_instance"},
   472  		},
   473  	}
   474  
   475  	state, err := ctx.Import(&ImportOpts{
   476  		Targets: []*ImportTarget{
   477  			&ImportTarget{
   478  				Addr: "module.foo.aws_instance.foo",
   479  				ID:   "bar",
   480  			},
   481  		},
   482  	})
   483  	if err != nil {
   484  		t.Fatalf("err: %s", err)
   485  	}
   486  
   487  	actual := strings.TrimSpace(state.String())
   488  	expected := strings.TrimSpace(testImportModuleStr)
   489  	if actual != expected {
   490  		t.Fatalf("bad: \n%s", actual)
   491  	}
   492  }
   493  
   494  func TestContextImport_moduleDepth2(t *testing.T) {
   495  	p := testProvider("aws")
   496  	m := testModule(t, "import-provider")
   497  	ctx := testContext2(t, &ContextOpts{
   498  		Module: m,
   499  		ProviderResolver: ResourceProviderResolverFixed(
   500  			map[string]ResourceProviderFactory{
   501  				"aws": testProviderFuncFixed(p),
   502  			},
   503  		),
   504  	})
   505  
   506  	p.ImportStateReturn = []*InstanceState{
   507  		&InstanceState{
   508  			ID:        "foo",
   509  			Ephemeral: EphemeralState{Type: "aws_instance"},
   510  		},
   511  	}
   512  
   513  	state, err := ctx.Import(&ImportOpts{
   514  		Targets: []*ImportTarget{
   515  			&ImportTarget{
   516  				Addr: "module.a.module.b.aws_instance.foo",
   517  				ID:   "bar",
   518  			},
   519  		},
   520  	})
   521  	if err != nil {
   522  		t.Fatalf("err: %s", err)
   523  	}
   524  
   525  	actual := strings.TrimSpace(state.String())
   526  	expected := strings.TrimSpace(testImportModuleDepth2Str)
   527  	if actual != expected {
   528  		t.Fatalf("bad: \n%s", actual)
   529  	}
   530  }
   531  
   532  func TestContextImport_moduleDiff(t *testing.T) {
   533  	p := testProvider("aws")
   534  	m := testModule(t, "import-provider")
   535  	ctx := testContext2(t, &ContextOpts{
   536  		Module: m,
   537  		ProviderResolver: ResourceProviderResolverFixed(
   538  			map[string]ResourceProviderFactory{
   539  				"aws": testProviderFuncFixed(p),
   540  			},
   541  		),
   542  
   543  		State: &State{
   544  			Modules: []*ModuleState{
   545  				&ModuleState{
   546  					Path: []string{"root", "bar"},
   547  					Resources: map[string]*ResourceState{
   548  						"aws_instance.bar": &ResourceState{
   549  							Type: "aws_instance",
   550  							Primary: &InstanceState{
   551  								ID: "bar",
   552  							},
   553  						},
   554  					},
   555  				},
   556  			},
   557  		},
   558  	})
   559  
   560  	p.ImportStateReturn = []*InstanceState{
   561  		&InstanceState{
   562  			ID:        "foo",
   563  			Ephemeral: EphemeralState{Type: "aws_instance"},
   564  		},
   565  	}
   566  
   567  	state, err := ctx.Import(&ImportOpts{
   568  		Targets: []*ImportTarget{
   569  			&ImportTarget{
   570  				Addr: "module.foo.aws_instance.foo",
   571  				ID:   "bar",
   572  			},
   573  		},
   574  	})
   575  	if err != nil {
   576  		t.Fatalf("err: %s", err)
   577  	}
   578  
   579  	actual := strings.TrimSpace(state.String())
   580  	expected := strings.TrimSpace(testImportModuleDiffStr)
   581  	if actual != expected {
   582  		t.Fatalf("bad: \n%s", actual)
   583  	}
   584  }
   585  
   586  func TestContextImport_moduleExisting(t *testing.T) {
   587  	p := testProvider("aws")
   588  	m := testModule(t, "import-provider")
   589  	ctx := testContext2(t, &ContextOpts{
   590  		Module: m,
   591  		ProviderResolver: ResourceProviderResolverFixed(
   592  			map[string]ResourceProviderFactory{
   593  				"aws": testProviderFuncFixed(p),
   594  			},
   595  		),
   596  
   597  		State: &State{
   598  			Modules: []*ModuleState{
   599  				&ModuleState{
   600  					Path: []string{"root", "foo"},
   601  					Resources: map[string]*ResourceState{
   602  						"aws_instance.bar": &ResourceState{
   603  							Type: "aws_instance",
   604  							Primary: &InstanceState{
   605  								ID: "bar",
   606  							},
   607  						},
   608  					},
   609  				},
   610  			},
   611  		},
   612  	})
   613  
   614  	p.ImportStateReturn = []*InstanceState{
   615  		&InstanceState{
   616  			ID:        "foo",
   617  			Ephemeral: EphemeralState{Type: "aws_instance"},
   618  		},
   619  	}
   620  
   621  	state, err := ctx.Import(&ImportOpts{
   622  		Targets: []*ImportTarget{
   623  			&ImportTarget{
   624  				Addr: "module.foo.aws_instance.foo",
   625  				ID:   "bar",
   626  			},
   627  		},
   628  	})
   629  	if err != nil {
   630  		t.Fatalf("err: %s", err)
   631  	}
   632  
   633  	actual := strings.TrimSpace(state.String())
   634  	expected := strings.TrimSpace(testImportModuleExistingStr)
   635  	if actual != expected {
   636  		t.Fatalf("bad: \n%s", actual)
   637  	}
   638  }
   639  
   640  func TestContextImport_multiState(t *testing.T) {
   641  	p := testProvider("aws")
   642  	m := testModule(t, "import-provider")
   643  	ctx := testContext2(t, &ContextOpts{
   644  		Module: m,
   645  		ProviderResolver: ResourceProviderResolverFixed(
   646  			map[string]ResourceProviderFactory{
   647  				"aws": testProviderFuncFixed(p),
   648  			},
   649  		),
   650  	})
   651  
   652  	p.ImportStateReturn = []*InstanceState{
   653  		&InstanceState{
   654  			ID:        "foo",
   655  			Ephemeral: EphemeralState{Type: "aws_instance"},
   656  		},
   657  		&InstanceState{
   658  			ID:        "bar",
   659  			Ephemeral: EphemeralState{Type: "aws_instance_thing"},
   660  		},
   661  	}
   662  
   663  	state, err := ctx.Import(&ImportOpts{
   664  		Targets: []*ImportTarget{
   665  			&ImportTarget{
   666  				Addr: "aws_instance.foo",
   667  				ID:   "bar",
   668  			},
   669  		},
   670  	})
   671  	if err != nil {
   672  		t.Fatalf("err: %s", err)
   673  	}
   674  
   675  	actual := strings.TrimSpace(state.String())
   676  	expected := strings.TrimSpace(testImportMultiStr)
   677  	if actual != expected {
   678  		t.Fatalf("bad: \n%s", actual)
   679  	}
   680  }
   681  
   682  func TestContextImport_multiStateSame(t *testing.T) {
   683  	p := testProvider("aws")
   684  	m := testModule(t, "import-provider")
   685  	ctx := testContext2(t, &ContextOpts{
   686  		Module: m,
   687  		ProviderResolver: ResourceProviderResolverFixed(
   688  			map[string]ResourceProviderFactory{
   689  				"aws": testProviderFuncFixed(p),
   690  			},
   691  		),
   692  	})
   693  
   694  	p.ImportStateReturn = []*InstanceState{
   695  		&InstanceState{
   696  			ID:        "foo",
   697  			Ephemeral: EphemeralState{Type: "aws_instance"},
   698  		},
   699  		&InstanceState{
   700  			ID:        "bar",
   701  			Ephemeral: EphemeralState{Type: "aws_instance_thing"},
   702  		},
   703  		&InstanceState{
   704  			ID:        "qux",
   705  			Ephemeral: EphemeralState{Type: "aws_instance_thing"},
   706  		},
   707  	}
   708  
   709  	state, err := ctx.Import(&ImportOpts{
   710  		Targets: []*ImportTarget{
   711  			&ImportTarget{
   712  				Addr: "aws_instance.foo",
   713  				ID:   "bar",
   714  			},
   715  		},
   716  	})
   717  	if err != nil {
   718  		t.Fatalf("err: %s", err)
   719  	}
   720  
   721  	actual := strings.TrimSpace(state.String())
   722  	expected := strings.TrimSpace(testImportMultiSameStr)
   723  	if actual != expected {
   724  		t.Fatalf("bad: \n%s", actual)
   725  	}
   726  }
   727  
   728  // import missing a provider alias should fail
   729  func TestContextImport_customProviderMissing(t *testing.T) {
   730  	p := testProvider("aws")
   731  	m := testModule(t, "import-provider")
   732  	ctx := testContext2(t, &ContextOpts{
   733  		Module: m,
   734  		ProviderResolver: ResourceProviderResolverFixed(
   735  			map[string]ResourceProviderFactory{
   736  				"aws": testProviderFuncFixed(p),
   737  			},
   738  		),
   739  	})
   740  
   741  	p.ImportStateReturn = []*InstanceState{
   742  		&InstanceState{
   743  			ID:        "foo",
   744  			Ephemeral: EphemeralState{Type: "aws_instance"},
   745  		},
   746  	}
   747  
   748  	_, err := ctx.Import(&ImportOpts{
   749  		Targets: []*ImportTarget{
   750  			&ImportTarget{
   751  				Addr:     "aws_instance.foo",
   752  				ID:       "bar",
   753  				Provider: "aws.alias",
   754  			},
   755  		},
   756  	})
   757  	if err == nil {
   758  		t.Fatal("expected error")
   759  	}
   760  }
   761  
   762  func TestContextImport_customProvider(t *testing.T) {
   763  	p := testProvider("aws")
   764  	m := testModule(t, "import-provider-alias")
   765  	ctx := testContext2(t, &ContextOpts{
   766  		Module: m,
   767  		ProviderResolver: ResourceProviderResolverFixed(
   768  			map[string]ResourceProviderFactory{
   769  				"aws": testProviderFuncFixed(p),
   770  			},
   771  		),
   772  	})
   773  
   774  	p.ImportStateReturn = []*InstanceState{
   775  		&InstanceState{
   776  			ID:        "foo",
   777  			Ephemeral: EphemeralState{Type: "aws_instance"},
   778  		},
   779  	}
   780  
   781  	state, err := ctx.Import(&ImportOpts{
   782  		Targets: []*ImportTarget{
   783  			&ImportTarget{
   784  				Addr:     "aws_instance.foo",
   785  				ID:       "bar",
   786  				Provider: "aws.alias",
   787  			},
   788  		},
   789  	})
   790  	if err != nil {
   791  		t.Fatalf("err: %s", err)
   792  	}
   793  
   794  	actual := strings.TrimSpace(state.String())
   795  	expected := strings.TrimSpace(testImportCustomProviderStr)
   796  	if actual != expected {
   797  		t.Fatalf("bad: \n%s", actual)
   798  	}
   799  }
   800  
   801  const testImportStr = `
   802  aws_instance.foo:
   803    ID = foo
   804    provider = provider.aws
   805  `
   806  
   807  const testImportCountIndexStr = `
   808  aws_instance.foo.0:
   809    ID = foo
   810    provider = provider.aws
   811  `
   812  
   813  const testImportCollisionStr = `
   814  aws_instance.foo:
   815    ID = bar
   816  `
   817  
   818  const testImportModuleStr = `
   819  <no state>
   820  module.foo:
   821    aws_instance.foo:
   822      ID = foo
   823      provider = provider.aws
   824  `
   825  
   826  const testImportModuleDepth2Str = `
   827  <no state>
   828  module.a.b:
   829    aws_instance.foo:
   830      ID = foo
   831      provider = provider.aws
   832  `
   833  
   834  const testImportModuleDiffStr = `
   835  module.bar:
   836    aws_instance.bar:
   837      ID = bar
   838  module.foo:
   839    aws_instance.foo:
   840      ID = foo
   841      provider = provider.aws
   842  `
   843  
   844  const testImportModuleExistingStr = `
   845  module.foo:
   846    aws_instance.bar:
   847      ID = bar
   848    aws_instance.foo:
   849      ID = foo
   850      provider = provider.aws
   851  `
   852  
   853  const testImportMultiStr = `
   854  aws_instance.foo:
   855    ID = foo
   856    provider = provider.aws
   857  aws_instance_thing.foo:
   858    ID = bar
   859    provider = provider.aws
   860  `
   861  
   862  const testImportMultiSameStr = `
   863  aws_instance.foo:
   864    ID = foo
   865    provider = provider.aws
   866  aws_instance_thing.foo:
   867    ID = bar
   868    provider = provider.aws
   869  aws_instance_thing.foo-1:
   870    ID = qux
   871    provider = provider.aws
   872  `
   873  
   874  const testImportRefreshStr = `
   875  aws_instance.foo:
   876    ID = foo
   877    provider = provider.aws
   878    foo = bar
   879  `
   880  
   881  const testImportCustomProviderStr = `
   882  aws_instance.foo:
   883    ID = foo
   884    provider = provider.aws.alias
   885  `