From 62d3fb44087ee5d311751d1cbb3de7071d55eddc Mon Sep 17 00:00:00 2001 From: Nick Metz Date: Wed, 9 Jun 2021 19:24:58 +0200 Subject: [PATCH] Add terraform init options -reconfigure and -migrate-state including tests --- modules/terraform/init.go | 10 +++++ modules/terraform/init_test.go | 68 ++++++++++++++++++++++++++++++++++ modules/terraform/options.go | 2 + 3 files changed, 80 insertions(+) diff --git a/modules/terraform/init.go b/modules/terraform/init.go index e2921b2c2..e1cb17730 100644 --- a/modules/terraform/init.go +++ b/modules/terraform/init.go @@ -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...) diff --git a/modules/terraform/init_test.go b/modules/terraform/init_test.go index d87bddc1b..e985777c2 100644 --- a/modules/terraform/init_test.go +++ b/modules/terraform/init_test.go @@ -82,3 +82,71 @@ func TestInitPluginDir(t *testing.T) { assert.Contains(t, initOutput, "(unauthenticated)") } + +func TestInitReconfigureBackend(t *testing.T) { + t.Parallel() + + stateDirectory, err := ioutil.TempDir("", t.Name()) + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(stateDirectory) + + testFolder, err := files.CopyTerraformFolderToTemp("../../test/fixtures/terraform-backend", t.Name()) + if err != nil { + t.Fatal(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()) + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(stateDirectory) + + testFolder, err := files.CopyTerraformFolderToTemp("../../test/fixtures/terraform-backend", t.Name()) + if err != nil { + t.Fatal(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") +} \ No newline at end of file diff --git a/modules/terraform/options.go b/modules/terraform/options.go index b0fe85b9b..3e2906100 100644 --- a/modules/terraform/options.go +++ b/modules/terraform/options.go @@ -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