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

feat: Improve handling of ExcludeFromCopy configuration in Terragrunt #3816

Merged
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
6 changes: 3 additions & 3 deletions cli/commands/terraform/download_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,14 +211,14 @@ func readVersionFile(terraformSource *terraform.Source) (string, error) {
return util.ReadFileAsString(terraformSource.VersionFile)
}

// updateGetters returns the customized go-getter interfaces that Terragrunt relies on. Specifically:
// UpdateGetters returns the customized go-getter interfaces that Terragrunt relies on. Specifically:
// - Local file path getter is updated to copy the files instead of creating symlinks, which is what go-getter defaults
// to.
// - Include the customized getter for fetching sources from the Terraform Registry.
//
// This creates a closure that returns a function so that we have access to the terragrunt configuration, which is
// necessary for customizing the behavior of the file getter.
func updateGetters(terragruntOptions *options.TerragruntOptions, terragruntConfig *config.TerragruntConfig) func(*getter.Client) error {
func UpdateGetters(terragruntOptions *options.TerragruntOptions, terragruntConfig *config.TerragruntConfig) func(*getter.Client) error {
return func(client *getter.Client) error {
// We copy all the default getters from the go-getter library, but replace the "file" getter. We shallow clone the
// getter map here rather than using getter.Getters directly because (a) we shouldn't change the original,
Expand Down Expand Up @@ -270,7 +270,7 @@ func downloadSource(terraformSource *terraform.Source, terragruntOptions *option
canonicalSourceURL,
terraformSource.DownloadDir)

if err := getter.GetAny(terraformSource.DownloadDir, terraformSource.CanonicalSourceURL.String(), updateGetters(terragruntOptions, terragruntConfig)); err != nil {
if err := getter.GetAny(terraformSource.DownloadDir, terraformSource.CanonicalSourceURL.String(), UpdateGetters(terragruntOptions, terragruntConfig)); err != nil {
return errors.New(err)
}

Expand Down
55 changes: 55 additions & 0 deletions cli/commands/terraform/download_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/gruntwork-io/terragrunt/config"
"github.com/gruntwork-io/terragrunt/options"
"github.com/gruntwork-io/terragrunt/util"
"github.com/hashicorp/go-getter"
)

func TestAlreadyHaveLatestCodeLocalFilePathWithNoModifiedFiles(t *testing.T) {
Expand Down Expand Up @@ -502,3 +503,57 @@ func copyFolder(t *testing.T, src string, dest string) {
err := util.CopyFolderContents(logger, filepath.FromSlash(src), filepath.FromSlash(dest), ".terragrunt-test", nil, nil)
require.NoError(t, err)
}

// TestUpdateGettersExcludeFromCopy verifies the correct behavior of updateGetters with ExcludeFromCopy configuration
func TestUpdateGettersExcludeFromCopy(t *testing.T) {
t.Parallel()

testCases := []struct {
name string
terragruntConfig *config.TerragruntConfig
expectedExcludeFiles []string
}{
{
name: "Nil ExcludeFromCopy",
terragruntConfig: &config.TerragruntConfig{
Terraform: &config.TerraformConfig{
ExcludeFromCopy: nil,
},
},
expectedExcludeFiles: nil,
},
{
name: "Non-Nil ExcludeFromCopy",
terragruntConfig: &config.TerragruntConfig{
Terraform: &config.TerraformConfig{
ExcludeFromCopy: &[]string{"*.tfstate", "excluded_dir/"},
},
},
expectedExcludeFiles: []string{"*.tfstate", "excluded_dir/"},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()

terragruntOptions, err := options.NewTerragruntOptionsForTest("./test")
require.NoError(t, err)

client := &getter.Client{}

// Call updateGetters
updateGettersFunc := terraform.UpdateGetters(terragruntOptions, tc.terragruntConfig)
err = updateGettersFunc(client)
require.NoError(t, err)

// Find the file getter
fileGetter, ok := client.Getters["file"].(*terraform.FileCopyGetter)
require.True(t, ok, "File getter should be of type FileCopyGetter")

// Verify ExcludeFromCopy
assert.Equal(t, tc.expectedExcludeFiles, fileGetter.ExcludeFromCopy,
"ExcludeFromCopy should match expected value")
})
}
}