Skip to content

Commit

Permalink
Add configuration option for terraform plugin dir (#787)
Browse files Browse the repository at this point in the history
* Add configuration option for terraform plugin dir

Add test for format function

* Update comment

* Add integration test for plugin dir

* Update test to require an err if plugins aren't available

* Move defers to make sure we always cleanup

* Update init test to support Terraform 0.14
  • Loading branch information
cbuto authored Mar 17, 2021
1 parent 9bd5d84 commit f8ed0b6
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 0 deletions.
10 changes: 10 additions & 0 deletions modules/terraform/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@ func FormatTerraformLockAsArgs(lockCheck bool, lockTimeout string) []string {
return lockArgs
}

// FormatTerraformPluginDirAsArgs formats the plugin-dir variable
// -plugin-dir
func FormatTerraformPluginDirAsArgs(pluginDir string) []string {
pluginArgs := []string{fmt.Sprintf("-plugin-dir=%v", pluginDir)}
if pluginDir == "" {
return nil
}
return pluginArgs
}

// FormatTerraformArgs will format multiple args with the arg name (e.g. "-var-file", []string{"foo.tfvars", "bar.tfvars"})
// returns "-var-file foo.tfvars -var-file bar.tfvars"
func FormatTerraformArgs(argName string, args []string) []string {
Expand Down
18 changes: 18 additions & 0 deletions modules/terraform/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,24 @@ func TestFormatTerraformPlanFileAsArgs(t *testing.T) {
}
}

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

testCases := []struct {
dir string
expected []string
}{
{"/some/plugin/dir", []string{"-plugin-dir=/some/plugin/dir"}},
{"", nil},
}

for _, testCase := range testCases {
checkResultWithRetry(t, 100, testCase.expected, fmt.Sprintf("FormatTerraformPluginDirAsArgs(%v)", testCase.dir), func() interface{} {
return FormatTerraformPluginDirAsArgs(testCase.dir)
})
}
}

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

Expand Down
1 change: 1 addition & 0 deletions modules/terraform/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ func Init(t testing.TestingT, options *Options) string {
func InitE(t testing.TestingT, options *Options) (string, error) {
args := []string{"init", fmt.Sprintf("-upgrade=%t", options.Upgrade)}
args = append(args, FormatTerraformBackendConfigAsArgs(options.BackendConfig)...)
args = append(args, FormatTerraformPluginDirAsArgs(options.PluginDir)...)
return RunTerraformCommandE(t, options, args...)
}
47 changes: 47 additions & 0 deletions modules/terraform/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package terraform

import (
"io/ioutil"
"os"
"path/filepath"
"testing"

"github.com/gruntwork-io/terratest/modules/files"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestInitBackendConfig(t *testing.T) {
Expand Down Expand Up @@ -35,3 +37,48 @@ func TestInitBackendConfig(t *testing.T) {

assert.FileExists(t, remoteStateFile)
}

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

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

terraformFixture := "../../test/fixtures/terraform-basic-configuration"

initializedFolder, err := files.CopyTerraformFolderToTemp(terraformFixture, t.Name())
require.NoError(t, err)
defer os.RemoveAll(initializedFolder)

testFolder, err := files.CopyTerraformFolderToTemp(terraformFixture, t.Name())
require.NoError(t, err)
defer os.RemoveAll(testFolder)

terraformOptions := &Options{
TerraformDir: initializedFolder,
}

terraformOptionsPluginDir := &Options{
TerraformDir: testFolder,
PluginDir: testingDir,
}

Init(t, terraformOptions)

_, err = InitE(t, terraformOptionsPluginDir)
require.Error(t, err)

// In Terraform 0.13, the directory is "plugins"
initializedPluginDir := initializedFolder + "/.terraform/plugins"

// In Terraform 0.14, the directory is "providers"
initializedProviderDir := initializedFolder + "/.terraform/providers"

files.CopyFolderContents(initializedPluginDir, testingDir)
files.CopyFolderContents(initializedProviderDir, testingDir)

initOutput := Init(t, terraformOptionsPluginDir)

assert.Contains(t, initOutput, "(unauthenticated)")
}
1 change: 1 addition & 0 deletions modules/terraform/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ type Options struct {
Logger *logger.Logger // Set a non-default logger that should be used. See the logger package for more info.
Parallelism int // Set the parallelism setting for Terraform
PlanFilePath string // The path to output a plan file to (for the plan command) or read one from (for the apply command)
PluginDir string // The path of downloaded plugins to pass to the terraform init command (-plugin-dir)
}

// Clone makes a deep copy of most fields on the Options object and returns it.
Expand Down

0 comments on commit f8ed0b6

Please sign in to comment.