Skip to content

Commit

Permalink
Merge pull request #923 from wasfree/f/tf-init-options
Browse files Browse the repository at this point in the history
[terraform] add init options -reconfigure and -migrate-state
  • Loading branch information
brikis98 authored Jun 25, 2021
2 parents 0122e76 + 5a3d9d7 commit 22ada23
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
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

0 comments on commit 22ada23

Please sign in to comment.