|
| 1 | +package test_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/gruntwork-io/terragrunt/test/helpers" |
| 11 | + "github.com/gruntwork-io/terragrunt/util" |
| 12 | + "github.com/hashicorp/hcl/v2" |
| 13 | + "github.com/hashicorp/hcl/v2/hclwrite" |
| 14 | + "github.com/stretchr/testify/assert" |
| 15 | + "github.com/stretchr/testify/require" |
| 16 | +) |
| 17 | + |
| 18 | +const ( |
| 19 | + testFixtureProviderCacheConstraintIssue = "fixtures/provider-cache/constraint-issue" |
| 20 | +) |
| 21 | + |
| 22 | +// TestTerragruntProviderCacheConstraintIssue tests that provider cache preserves |
| 23 | +// module constraints instead of pinning exact versions in .terraform.lock.hcl files. |
| 24 | +// Reproduces and validates the fix for GitHub issue #4512. |
| 25 | +// |
| 26 | +//nolint:paralleltest,tparallel |
| 27 | +func TestTerragruntProviderCacheConstraintIssue(t *testing.T) { |
| 28 | + t.Parallel() |
| 29 | + |
| 30 | + helpers.CleanupTerraformFolder(t, testFixtureProviderCacheConstraintIssue) |
| 31 | + tmpEnvPath := helpers.CopyEnvironment(t, testFixtureProviderCacheConstraintIssue) |
| 32 | + rootPath := util.JoinPath(tmpEnvPath, testFixtureProviderCacheConstraintIssue) |
| 33 | + appPath := filepath.Join(rootPath, "app") |
| 34 | + |
| 35 | + providerCacheDir := t.TempDir() |
| 36 | + |
| 37 | + t.Run("initial_setup_preserves_module_constraints", func(t *testing.T) { |
| 38 | + helpers.RunTerragrunt(t, fmt.Sprintf("terragrunt init --provider-cache --provider-cache-dir %s --log-level trace --non-interactive --working-dir %s", providerCacheDir, appPath)) |
| 39 | + |
| 40 | + constraintsValue := extractConstraintsFromLockFile(t, appPath, "cloudflare/cloudflare") |
| 41 | + |
| 42 | + expectedConstraints := "~> 4.0" |
| 43 | + assert.Equal(t, expectedConstraints, constraintsValue, "Initial lock file should preserve module's required_providers constraints") |
| 44 | + }) |
| 45 | + |
| 46 | + t.Run("upgrade_updates_constraints_to_match_module", func(t *testing.T) { |
| 47 | + // Update the main.tf file to change cloudflare version constraint from "~> 4.0" to "~> 4.40" |
| 48 | + mainTfPath := filepath.Join(appPath, "main.tf") |
| 49 | + originalContent, err := os.ReadFile(mainTfPath) |
| 50 | + require.NoError(t, err) |
| 51 | + |
| 52 | + // Replace the version constraint |
| 53 | + updatedContent := strings.ReplaceAll(string(originalContent), `version = "~> 4.0"`, `version = "~> 4.40"`) |
| 54 | + require.NotEqual(t, string(originalContent), updatedContent, "Content should be different after replacement") |
| 55 | + |
| 56 | + err = os.WriteFile(mainTfPath, []byte(updatedContent), 0644) |
| 57 | + require.NoError(t, err) |
| 58 | + |
| 59 | + lockFilePreInit, err := os.ReadFile(filepath.Join(appPath, ".terraform.lock.hcl")) |
| 60 | + require.NoError(t, err) |
| 61 | + |
| 62 | + // Run terragrunt init and check that the lock file isn't updated |
| 63 | + helpers.RunTerragrunt(t, fmt.Sprintf("terragrunt init --provider-cache --provider-cache-dir %s --log-level trace --non-interactive --working-dir %s", providerCacheDir, appPath)) |
| 64 | + lockFilePostInit, err := os.ReadFile(filepath.Join(appPath, ".terraform.lock.hcl")) |
| 65 | + require.NoError(t, err) |
| 66 | + assert.Equal(t, string(lockFilePreInit), string(lockFilePostInit), "Lock file should not be updated") |
| 67 | + |
| 68 | + // Run terragrunt init -upgrade to update the lock file |
| 69 | + helpers.RunTerragrunt(t, fmt.Sprintf("terragrunt init -upgrade --provider-cache --provider-cache-dir %s --log-level trace --non-interactive --working-dir %s", providerCacheDir, appPath)) |
| 70 | + |
| 71 | + lockFilePostUpgrade, err := os.ReadFile(filepath.Join(appPath, ".terraform.lock.hcl")) |
| 72 | + require.NoError(t, err) |
| 73 | + assert.NotEqual(t, string(lockFilePostInit), string(lockFilePostUpgrade), "Lock file should be updated") |
| 74 | + |
| 75 | + // Verify the lock file constraints are updated to match the module |
| 76 | + constraintsValue := extractConstraintsFromLockFile(t, appPath, "cloudflare/cloudflare") |
| 77 | + |
| 78 | + expectedConstraints := "~> 4.40" |
| 79 | + assert.Equal(t, expectedConstraints, constraintsValue, "Constraints should be updated to match the module's required_providers") |
| 80 | + }) |
| 81 | + |
| 82 | + t.Run("fresh_start_uses_module_constraints", func(t *testing.T) { |
| 83 | + // Delete the lock file |
| 84 | + lockfilePath := filepath.Join(appPath, ".terraform.lock.hcl") |
| 85 | + err := os.Remove(lockfilePath) |
| 86 | + require.NoError(t, err) |
| 87 | + |
| 88 | + // Also clean up .terraform directory to ensure fresh start |
| 89 | + terraformDir := filepath.Join(appPath, ".terraform") |
| 90 | + if util.FileExists(terraformDir) { |
| 91 | + err = os.RemoveAll(terraformDir) |
| 92 | + require.NoError(t, err) |
| 93 | + } |
| 94 | + |
| 95 | + helpers.RunTerragrunt(t, fmt.Sprintf("terragrunt init --provider-cache --provider-cache-dir %s --log-level trace --non-interactive --working-dir %s", providerCacheDir, appPath)) |
| 96 | + |
| 97 | + constraintsValue := extractConstraintsFromLockFile(t, appPath, "cloudflare/cloudflare") |
| 98 | + |
| 99 | + expectedConstraints := "~> 4.40" |
| 100 | + assert.Equal(t, expectedConstraints, constraintsValue, "Fresh lock file should use module's required_providers constraints") |
| 101 | + }) |
| 102 | +} |
| 103 | + |
| 104 | +// Helper function to extract constraints value from lock file |
| 105 | +func extractConstraintsFromLockFile(t *testing.T, appPath string, providerName string) string { |
| 106 | + t.Helper() |
| 107 | + |
| 108 | + lockfilePath := filepath.Join(appPath, ".terraform.lock.hcl") |
| 109 | + require.True(t, util.FileExists(lockfilePath), "Lock file should exist") |
| 110 | + |
| 111 | + // Read and parse the lock file |
| 112 | + lockfileContent, err := os.ReadFile(lockfilePath) |
| 113 | + require.NoError(t, err) |
| 114 | + |
| 115 | + lockfile, diags := hclwrite.ParseConfig(lockfileContent, lockfilePath, hcl.Pos{Line: 1, Column: 1}) |
| 116 | + require.False(t, diags.HasErrors(), "Lock file should be valid HCL") |
| 117 | + |
| 118 | + // Find the provider block (handle both short and full provider names) |
| 119 | + var providerBlock *hclwrite.Block |
| 120 | + if strings.Contains(providerName, "/") { |
| 121 | + // Full name like "cloudflare/cloudflare" |
| 122 | + providerBlock = lockfile.Body().FirstMatchingBlock("provider", []string{"registry.terraform.io/" + providerName}) |
| 123 | + if providerBlock == nil { |
| 124 | + // Try OpenTofu registry as well |
| 125 | + providerBlock = lockfile.Body().FirstMatchingBlock("provider", []string{"registry.opentofu.org/" + providerName}) |
| 126 | + } |
| 127 | + } else { |
| 128 | + // Short name - search for matching block |
| 129 | + for _, block := range lockfile.Body().Blocks() { |
| 130 | + if block.Type() == "provider" && len(block.Labels()) > 0 { |
| 131 | + if strings.Contains(block.Labels()[0], providerName) { |
| 132 | + providerBlock = block |
| 133 | + break |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + require.NotNil(t, providerBlock, "Provider block should exist in lock file") |
| 139 | + |
| 140 | + // Get the constraints attribute |
| 141 | + constraintsAttr := providerBlock.Body().GetAttribute("constraints") |
| 142 | + require.NotNil(t, constraintsAttr, "Constraints attribute should exist") |
| 143 | + |
| 144 | + constraintsValue := strings.Trim(string(constraintsAttr.Expr().BuildTokens(nil).Bytes()), ` "`) |
| 145 | + return constraintsValue |
| 146 | +} |
0 commit comments