github.com/adrian-bl/terraform@v0.7.0-rc2.0.20160705220747-de0a34fc3517/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  	ctx := testContext2(t, &ContextOpts{
    12  		Providers: map[string]ResourceProviderFactory{
    13  			"aws": testProviderFuncFixed(p),
    14  		},
    15  	})
    16  
    17  	p.ImportStateReturn = []*InstanceState{
    18  		&InstanceState{
    19  			ID:        "foo",
    20  			Ephemeral: EphemeralState{Type: "aws_instance"},
    21  		},
    22  	}
    23  
    24  	state, err := ctx.Import(&ImportOpts{
    25  		Targets: []*ImportTarget{
    26  			&ImportTarget{
    27  				Addr: "aws_instance.foo",
    28  				ID:   "bar",
    29  			},
    30  		},
    31  	})
    32  	if err != nil {
    33  		t.Fatalf("err: %s", err)
    34  	}
    35  
    36  	actual := strings.TrimSpace(state.String())
    37  	expected := strings.TrimSpace(testImportStr)
    38  	if actual != expected {
    39  		t.Fatalf("bad: \n%s", actual)
    40  	}
    41  }
    42  
    43  func TestContextImport_collision(t *testing.T) {
    44  	p := testProvider("aws")
    45  	ctx := testContext2(t, &ContextOpts{
    46  		Providers: map[string]ResourceProviderFactory{
    47  			"aws": testProviderFuncFixed(p),
    48  		},
    49  
    50  		State: &State{
    51  			Modules: []*ModuleState{
    52  				&ModuleState{
    53  					Path: []string{"root"},
    54  					Resources: map[string]*ResourceState{
    55  						"aws_instance.foo": &ResourceState{
    56  							Type: "aws_instance",
    57  							Primary: &InstanceState{
    58  								ID: "bar",
    59  							},
    60  						},
    61  					},
    62  				},
    63  			},
    64  		},
    65  	})
    66  
    67  	p.ImportStateReturn = []*InstanceState{
    68  		&InstanceState{
    69  			ID:        "foo",
    70  			Ephemeral: EphemeralState{Type: "aws_instance"},
    71  		},
    72  	}
    73  
    74  	state, err := ctx.Import(&ImportOpts{
    75  		Targets: []*ImportTarget{
    76  			&ImportTarget{
    77  				Addr: "aws_instance.foo",
    78  				ID:   "bar",
    79  			},
    80  		},
    81  	})
    82  	if err == nil {
    83  		t.Fatalf("err: %s", err)
    84  	}
    85  
    86  	actual := strings.TrimSpace(state.String())
    87  	expected := strings.TrimSpace(testImportCollisionStr)
    88  	if actual != expected {
    89  		t.Fatalf("bad: \n%s", actual)
    90  	}
    91  }
    92  
    93  func TestContextImport_missingType(t *testing.T) {
    94  	p := testProvider("aws")
    95  	ctx := testContext2(t, &ContextOpts{
    96  		Providers: map[string]ResourceProviderFactory{
    97  			"aws": testProviderFuncFixed(p),
    98  		},
    99  	})
   100  
   101  	p.ImportStateReturn = []*InstanceState{
   102  		&InstanceState{
   103  			ID: "foo",
   104  		},
   105  	}
   106  
   107  	state, err := ctx.Import(&ImportOpts{
   108  		Targets: []*ImportTarget{
   109  			&ImportTarget{
   110  				Addr: "aws_instance.foo",
   111  				ID:   "bar",
   112  			},
   113  		},
   114  	})
   115  	if err == nil {
   116  		t.Fatal("should error")
   117  	}
   118  
   119  	actual := strings.TrimSpace(state.String())
   120  	expected := "<no state>"
   121  	if actual != expected {
   122  		t.Fatalf("bad: \n%s", actual)
   123  	}
   124  }
   125  
   126  func TestContextImport_moduleProvider(t *testing.T) {
   127  	p := testProvider("aws")
   128  	ctx := testContext2(t, &ContextOpts{
   129  		Providers: map[string]ResourceProviderFactory{
   130  			"aws": testProviderFuncFixed(p),
   131  		},
   132  	})
   133  
   134  	p.ImportStateReturn = []*InstanceState{
   135  		&InstanceState{
   136  			ID:        "foo",
   137  			Ephemeral: EphemeralState{Type: "aws_instance"},
   138  		},
   139  	}
   140  
   141  	configured := false
   142  	p.ConfigureFn = func(c *ResourceConfig) error {
   143  		configured = true
   144  
   145  		if v, ok := c.Get("foo"); !ok || v.(string) != "bar" {
   146  			return fmt.Errorf("bad")
   147  		}
   148  
   149  		return nil
   150  	}
   151  
   152  	m := testModule(t, "import-provider")
   153  
   154  	state, err := ctx.Import(&ImportOpts{
   155  		Module: m,
   156  		Targets: []*ImportTarget{
   157  			&ImportTarget{
   158  				Addr: "aws_instance.foo",
   159  				ID:   "bar",
   160  			},
   161  		},
   162  	})
   163  	if err != nil {
   164  		t.Fatalf("err: %s", err)
   165  	}
   166  
   167  	if !configured {
   168  		t.Fatal("didn't configure provider")
   169  	}
   170  
   171  	actual := strings.TrimSpace(state.String())
   172  	expected := strings.TrimSpace(testImportStr)
   173  	if actual != expected {
   174  		t.Fatalf("bad: \n%s", actual)
   175  	}
   176  }
   177  
   178  func TestContextImport_refresh(t *testing.T) {
   179  	p := testProvider("aws")
   180  	ctx := testContext2(t, &ContextOpts{
   181  		Providers: map[string]ResourceProviderFactory{
   182  			"aws": testProviderFuncFixed(p),
   183  		},
   184  	})
   185  
   186  	p.ImportStateReturn = []*InstanceState{
   187  		&InstanceState{
   188  			ID:        "foo",
   189  			Ephemeral: EphemeralState{Type: "aws_instance"},
   190  		},
   191  	}
   192  
   193  	p.RefreshFn = func(info *InstanceInfo, s *InstanceState) (*InstanceState, error) {
   194  		return &InstanceState{
   195  			ID:         "foo",
   196  			Attributes: map[string]string{"foo": "bar"},
   197  		}, nil
   198  	}
   199  
   200  	state, err := ctx.Import(&ImportOpts{
   201  		Targets: []*ImportTarget{
   202  			&ImportTarget{
   203  				Addr: "aws_instance.foo",
   204  				ID:   "bar",
   205  			},
   206  		},
   207  	})
   208  	if err != nil {
   209  		t.Fatalf("err: %s", err)
   210  	}
   211  
   212  	actual := strings.TrimSpace(state.String())
   213  	expected := strings.TrimSpace(testImportRefreshStr)
   214  	if actual != expected {
   215  		t.Fatalf("bad: \n%s", actual)
   216  	}
   217  }
   218  
   219  func TestContextImport_refreshNil(t *testing.T) {
   220  	p := testProvider("aws")
   221  	ctx := testContext2(t, &ContextOpts{
   222  		Providers: map[string]ResourceProviderFactory{
   223  			"aws": testProviderFuncFixed(p),
   224  		},
   225  	})
   226  
   227  	p.ImportStateReturn = []*InstanceState{
   228  		&InstanceState{
   229  			ID:        "foo",
   230  			Ephemeral: EphemeralState{Type: "aws_instance"},
   231  		},
   232  	}
   233  
   234  	p.RefreshFn = func(info *InstanceInfo, s *InstanceState) (*InstanceState, error) {
   235  		return nil, nil
   236  	}
   237  
   238  	state, err := ctx.Import(&ImportOpts{
   239  		Targets: []*ImportTarget{
   240  			&ImportTarget{
   241  				Addr: "aws_instance.foo",
   242  				ID:   "bar",
   243  			},
   244  		},
   245  	})
   246  	if err == nil {
   247  		t.Fatal("should error")
   248  	}
   249  
   250  	actual := strings.TrimSpace(state.String())
   251  	expected := "<no state>"
   252  	if actual != expected {
   253  		t.Fatalf("bad: \n%s", actual)
   254  	}
   255  }
   256  
   257  func TestContextImport_module(t *testing.T) {
   258  	p := testProvider("aws")
   259  	ctx := testContext2(t, &ContextOpts{
   260  		Providers: map[string]ResourceProviderFactory{
   261  			"aws": testProviderFuncFixed(p),
   262  		},
   263  	})
   264  
   265  	p.ImportStateReturn = []*InstanceState{
   266  		&InstanceState{
   267  			ID:        "foo",
   268  			Ephemeral: EphemeralState{Type: "aws_instance"},
   269  		},
   270  	}
   271  
   272  	state, err := ctx.Import(&ImportOpts{
   273  		Targets: []*ImportTarget{
   274  			&ImportTarget{
   275  				Addr: "module.foo.aws_instance.foo",
   276  				ID:   "bar",
   277  			},
   278  		},
   279  	})
   280  	if err != nil {
   281  		t.Fatalf("err: %s", err)
   282  	}
   283  
   284  	actual := strings.TrimSpace(state.String())
   285  	expected := strings.TrimSpace(testImportModuleStr)
   286  	if actual != expected {
   287  		t.Fatalf("bad: \n%s", actual)
   288  	}
   289  }
   290  
   291  func TestContextImport_moduleDepth2(t *testing.T) {
   292  	p := testProvider("aws")
   293  	ctx := testContext2(t, &ContextOpts{
   294  		Providers: map[string]ResourceProviderFactory{
   295  			"aws": testProviderFuncFixed(p),
   296  		},
   297  	})
   298  
   299  	p.ImportStateReturn = []*InstanceState{
   300  		&InstanceState{
   301  			ID:        "foo",
   302  			Ephemeral: EphemeralState{Type: "aws_instance"},
   303  		},
   304  	}
   305  
   306  	state, err := ctx.Import(&ImportOpts{
   307  		Targets: []*ImportTarget{
   308  			&ImportTarget{
   309  				Addr: "module.a.module.b.aws_instance.foo",
   310  				ID:   "bar",
   311  			},
   312  		},
   313  	})
   314  	if err != nil {
   315  		t.Fatalf("err: %s", err)
   316  	}
   317  
   318  	actual := strings.TrimSpace(state.String())
   319  	expected := strings.TrimSpace(testImportModuleDepth2Str)
   320  	if actual != expected {
   321  		t.Fatalf("bad: \n%s", actual)
   322  	}
   323  }
   324  
   325  func TestContextImport_moduleDiff(t *testing.T) {
   326  	p := testProvider("aws")
   327  	ctx := testContext2(t, &ContextOpts{
   328  		Providers: map[string]ResourceProviderFactory{
   329  			"aws": testProviderFuncFixed(p),
   330  		},
   331  
   332  		State: &State{
   333  			Modules: []*ModuleState{
   334  				&ModuleState{
   335  					Path: []string{"root", "bar"},
   336  					Resources: map[string]*ResourceState{
   337  						"aws_instance.bar": &ResourceState{
   338  							Type: "aws_instance",
   339  							Primary: &InstanceState{
   340  								ID: "bar",
   341  							},
   342  						},
   343  					},
   344  				},
   345  			},
   346  		},
   347  	})
   348  
   349  	p.ImportStateReturn = []*InstanceState{
   350  		&InstanceState{
   351  			ID:        "foo",
   352  			Ephemeral: EphemeralState{Type: "aws_instance"},
   353  		},
   354  	}
   355  
   356  	state, err := ctx.Import(&ImportOpts{
   357  		Targets: []*ImportTarget{
   358  			&ImportTarget{
   359  				Addr: "module.foo.aws_instance.foo",
   360  				ID:   "bar",
   361  			},
   362  		},
   363  	})
   364  	if err != nil {
   365  		t.Fatalf("err: %s", err)
   366  	}
   367  
   368  	actual := strings.TrimSpace(state.String())
   369  	expected := strings.TrimSpace(testImportModuleDiffStr)
   370  	if actual != expected {
   371  		t.Fatalf("bad: \n%s", actual)
   372  	}
   373  }
   374  
   375  func TestContextImport_moduleExisting(t *testing.T) {
   376  	p := testProvider("aws")
   377  	ctx := testContext2(t, &ContextOpts{
   378  		Providers: map[string]ResourceProviderFactory{
   379  			"aws": testProviderFuncFixed(p),
   380  		},
   381  
   382  		State: &State{
   383  			Modules: []*ModuleState{
   384  				&ModuleState{
   385  					Path: []string{"root", "foo"},
   386  					Resources: map[string]*ResourceState{
   387  						"aws_instance.bar": &ResourceState{
   388  							Type: "aws_instance",
   389  							Primary: &InstanceState{
   390  								ID: "bar",
   391  							},
   392  						},
   393  					},
   394  				},
   395  			},
   396  		},
   397  	})
   398  
   399  	p.ImportStateReturn = []*InstanceState{
   400  		&InstanceState{
   401  			ID:        "foo",
   402  			Ephemeral: EphemeralState{Type: "aws_instance"},
   403  		},
   404  	}
   405  
   406  	state, err := ctx.Import(&ImportOpts{
   407  		Targets: []*ImportTarget{
   408  			&ImportTarget{
   409  				Addr: "module.foo.aws_instance.foo",
   410  				ID:   "bar",
   411  			},
   412  		},
   413  	})
   414  	if err != nil {
   415  		t.Fatalf("err: %s", err)
   416  	}
   417  
   418  	actual := strings.TrimSpace(state.String())
   419  	expected := strings.TrimSpace(testImportModuleExistingStr)
   420  	if actual != expected {
   421  		t.Fatalf("bad: \n%s", actual)
   422  	}
   423  }
   424  
   425  func TestContextImport_multiState(t *testing.T) {
   426  	p := testProvider("aws")
   427  	ctx := testContext2(t, &ContextOpts{
   428  		Providers: map[string]ResourceProviderFactory{
   429  			"aws": testProviderFuncFixed(p),
   430  		},
   431  	})
   432  
   433  	p.ImportStateReturn = []*InstanceState{
   434  		&InstanceState{
   435  			ID:        "foo",
   436  			Ephemeral: EphemeralState{Type: "aws_instance"},
   437  		},
   438  		&InstanceState{
   439  			ID:        "bar",
   440  			Ephemeral: EphemeralState{Type: "aws_instance_thing"},
   441  		},
   442  	}
   443  
   444  	state, err := ctx.Import(&ImportOpts{
   445  		Targets: []*ImportTarget{
   446  			&ImportTarget{
   447  				Addr: "aws_instance.foo",
   448  				ID:   "bar",
   449  			},
   450  		},
   451  	})
   452  	if err != nil {
   453  		t.Fatalf("err: %s", err)
   454  	}
   455  
   456  	actual := strings.TrimSpace(state.String())
   457  	expected := strings.TrimSpace(testImportMultiStr)
   458  	if actual != expected {
   459  		t.Fatalf("bad: \n%s", actual)
   460  	}
   461  }
   462  
   463  func TestContextImport_multiStateSame(t *testing.T) {
   464  	p := testProvider("aws")
   465  	ctx := testContext2(t, &ContextOpts{
   466  		Providers: map[string]ResourceProviderFactory{
   467  			"aws": testProviderFuncFixed(p),
   468  		},
   469  	})
   470  
   471  	p.ImportStateReturn = []*InstanceState{
   472  		&InstanceState{
   473  			ID:        "foo",
   474  			Ephemeral: EphemeralState{Type: "aws_instance"},
   475  		},
   476  		&InstanceState{
   477  			ID:        "bar",
   478  			Ephemeral: EphemeralState{Type: "aws_instance_thing"},
   479  		},
   480  		&InstanceState{
   481  			ID:        "qux",
   482  			Ephemeral: EphemeralState{Type: "aws_instance_thing"},
   483  		},
   484  	}
   485  
   486  	state, err := ctx.Import(&ImportOpts{
   487  		Targets: []*ImportTarget{
   488  			&ImportTarget{
   489  				Addr: "aws_instance.foo",
   490  				ID:   "bar",
   491  			},
   492  		},
   493  	})
   494  	if err != nil {
   495  		t.Fatalf("err: %s", err)
   496  	}
   497  
   498  	actual := strings.TrimSpace(state.String())
   499  	expected := strings.TrimSpace(testImportMultiSameStr)
   500  	if actual != expected {
   501  		t.Fatalf("bad: \n%s", actual)
   502  	}
   503  }
   504  
   505  const testImportStr = `
   506  aws_instance.foo:
   507    ID = foo
   508    provider = aws
   509  `
   510  
   511  const testImportCollisionStr = `
   512  aws_instance.foo:
   513    ID = bar
   514  `
   515  
   516  const testImportModuleStr = `
   517  <no state>
   518  module.foo:
   519    aws_instance.foo:
   520      ID = foo
   521      provider = aws
   522  `
   523  
   524  const testImportModuleDepth2Str = `
   525  <no state>
   526  module.a.b:
   527    aws_instance.foo:
   528      ID = foo
   529      provider = aws
   530  `
   531  
   532  const testImportModuleDiffStr = `
   533  module.bar:
   534    aws_instance.bar:
   535      ID = bar
   536  module.foo:
   537    aws_instance.foo:
   538      ID = foo
   539      provider = aws
   540  `
   541  
   542  const testImportModuleExistingStr = `
   543  module.foo:
   544    aws_instance.bar:
   545      ID = bar
   546    aws_instance.foo:
   547      ID = foo
   548      provider = aws
   549  `
   550  
   551  const testImportMultiStr = `
   552  aws_instance.foo:
   553    ID = foo
   554    provider = aws
   555  aws_instance_thing.foo:
   556    ID = bar
   557    provider = aws
   558  `
   559  
   560  const testImportMultiSameStr = `
   561  aws_instance.foo:
   562    ID = foo
   563    provider = aws
   564  aws_instance_thing.foo:
   565    ID = bar
   566    provider = aws
   567  aws_instance_thing.foo-1:
   568    ID = qux
   569    provider = aws
   570  `
   571  
   572  const testImportRefreshStr = `
   573  aws_instance.foo:
   574    ID = foo
   575    provider = aws
   576    foo = bar
   577  `