Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[terraform] add init options -reconfigure and -migrate-state #923

Merged
merged 5 commits into from
Jun 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions modules/terraform/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ func Init(t testing.TestingT, options *Options) string {
// InitE calls terraform init and return stdout/stderr.
func InitE(t testing.TestingT, options *Options) (string, error) {
args := []string{"init", fmt.Sprintf("-upgrade=%t", options.Upgrade)}

// Append reconfigure option if specified
if options.Reconfigure {
args = append(args, "-reconfigure")
}
// Append combination of migrate-state and force-copy to suppress answer prompt
if options.MigrateState {
args = append(args, "-migrate-state", "-force-copy")
}

args = append(args, FormatTerraformBackendConfigAsArgs(options.BackendConfig)...)
args = append(args, FormatTerraformPluginDirAsArgs(options.PluginDir)...)
return RunTerraformCommandE(t, options, args...)
Expand Down
60 changes: 60 additions & 0 deletions modules/terraform/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,63 @@ func TestInitPluginDir(t *testing.T) {

assert.Contains(t, initOutput, "(unauthenticated)")
}

func TestInitReconfigureBackend(t *testing.T) {
t.Parallel()

stateDirectory, err := ioutil.TempDir("", t.Name())
require.NoError(t, err)
defer os.RemoveAll(stateDirectory)

testFolder, err := files.CopyTerraformFolderToTemp("../../test/fixtures/terraform-backend", t.Name())
require.NoError(t, err)
defer os.RemoveAll(testFolder)

options := &Options{
TerraformDir: testFolder,
BackendConfig: map[string]interface{}{
"path": filepath.Join(stateDirectory, "backend.tfstate"),
"workspace_dir": "current",
},
}

Init(t, options)

options.BackendConfig["workspace_dir"] = "new"
_, err = InitE(t, options)
assert.Error(t, err, "Backend initialization with changed configuration should fail without -reconfigure option")

options.Reconfigure = true
_, err = InitE(t, options)
assert.NoError(t, err, "Backend initialization with changed configuration should success with -reconfigure option")
}

func TestInitBackendMigration(t *testing.T) {
t.Parallel()

stateDirectory, err := ioutil.TempDir("", t.Name())
require.NoError(t, err)
defer os.RemoveAll(stateDirectory)

testFolder, err := files.CopyTerraformFolderToTemp("../../test/fixtures/terraform-backend", t.Name())
require.NoError(t, err)
defer os.RemoveAll(testFolder)

options := &Options{
TerraformDir: testFolder,
BackendConfig: map[string]interface{}{
"path": filepath.Join(stateDirectory, "backend.tfstate"),
"workspace_dir": "current",
},
}

Init(t, options)

options.BackendConfig["workspace_dir"] = "new"
_, err = InitE(t, options)
assert.Error(t, err, "Backend initialization with changed configuration should fail without -migrate-state option")

options.MigrateState = true
_, err = InitE(t, options)
assert.NoError(t, err, "Backend initialization with changed configuration should success with -migrate-state option")
}
2 changes: 2 additions & 0 deletions modules/terraform/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ type Options struct {
MaxRetries int // Maximum number of times to retry errors matching RetryableTerraformErrors
TimeBetweenRetries time.Duration // The amount of time to wait between retries
Upgrade bool // Whether the -upgrade flag of the terraform init command should be set to true or not
Reconfigure bool // Set the -reconfigure flag to the terraform init command
MigrateState bool // Set the -migrate-state and -force-copy (suppress 'yes' answer prompt) flag to the terraform init command
NoColor bool // Whether the -no-color flag will be set for any Terraform command or not
SshAgent *ssh.SshAgent // Overrides local SSH agent with the given in-process agent
NoStderr bool // Disable stderr redirection
Expand Down