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