Skip to content

Commit d0009e2

Browse files
authored
Use ImportStatePersist to preserve state generated by import operation (#1052)
* Use ImportStatePersist to preserve state generated by import operation (#717) * Removing additional call to Init as this has already been called in runNewTest and when called at this point will overwrite the contents of the .terraform directory with the current version of the provider (#717) * Adding CHANGELOG entry (#717) * Adding test coverage (#717) * Linting (#717) * Appears that init needs to be run within testStepNewImportState if we're not persisting the state generated by the import (#717) * Fixing test and adding a couple of additional tests to verify that import test works when ImportStatePersist is false (#717) * Linting (#717)
1 parent 3951e14 commit d0009e2

File tree

4 files changed

+451
-8
lines changed

4 files changed

+451
-8
lines changed

.changelog/1052.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
helper/resource: Add ImportStatePersist to optionally persist state generated during import
3+
```

helper/resource/testing.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,12 @@ type TestStep struct {
564564
ImportStateVerify bool
565565
ImportStateVerifyIgnore []string
566566

567+
// ImportStatePersist, if true, will update the persisted state with the
568+
// state generated by the import operation (i.e., terraform import). When
569+
// false (default) the state generated by the import operation is discarded
570+
// at the end of the test step that is verifying import behavior.
571+
ImportStatePersist bool
572+
567573
// ProviderFactories can be specified for the providers that are valid for
568574
// this TestStep. When providers are specified at the TestStep level, all
569575
// TestStep within a TestCase must declare providers.

helper/resource/testing_new_import_state.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"strings"
88

99
"github.com/davecgh/go-spew/spew"
10-
testing "github.com/mitchellh/go-testing-interface"
10+
"github.com/mitchellh/go-testing-interface"
1111

1212
"github.com/hashicorp/terraform-plugin-sdk/v2/internal/logging"
1313
"github.com/hashicorp/terraform-plugin-sdk/v2/internal/plugintest"
@@ -86,20 +86,31 @@ func testStepNewImportState(ctx context.Context, t testing.T, helper *plugintest
8686
t.Fatal("Cannot import state with no specified config")
8787
}
8888
}
89-
importWd := helper.RequireNewWorkingDir(ctx, t)
90-
defer importWd.Close()
89+
90+
var importWd *plugintest.WorkingDir
91+
92+
// Use the same working directory to persist the state from import
93+
if step.ImportStatePersist {
94+
importWd = wd
95+
} else {
96+
importWd = helper.RequireNewWorkingDir(ctx, t)
97+
defer importWd.Close()
98+
}
99+
91100
err = importWd.SetConfig(ctx, step.Config)
92101
if err != nil {
93102
t.Fatalf("Error setting test config: %s", err)
94103
}
95104

96105
logging.HelperResourceDebug(ctx, "Running Terraform CLI init and import")
97106

98-
err = runProviderCommand(ctx, t, func() error {
99-
return importWd.Init(ctx)
100-
}, importWd, providers)
101-
if err != nil {
102-
t.Fatalf("Error running init: %s", err)
107+
if !step.ImportStatePersist {
108+
err = runProviderCommand(ctx, t, func() error {
109+
return importWd.Init(ctx)
110+
}, importWd, providers)
111+
if err != nil {
112+
t.Fatalf("Error running init: %s", err)
113+
}
103114
}
104115

105116
err = runProviderCommand(ctx, t, func() error {

0 commit comments

Comments
 (0)