|
| 1 | +package discovery_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/gruntwork-io/terragrunt/internal/component" |
| 11 | + "github.com/gruntwork-io/terragrunt/internal/discovery" |
| 12 | + "github.com/gruntwork-io/terragrunt/internal/filter" |
| 13 | + "github.com/gruntwork-io/terragrunt/pkg/options" |
| 14 | + "github.com/gruntwork-io/terragrunt/test/helpers" |
| 15 | + "github.com/gruntwork-io/terragrunt/test/helpers/logger" |
| 16 | + "github.com/stretchr/testify/require" |
| 17 | +) |
| 18 | + |
| 19 | +// TestDiscovery_GraphConcurrentConfigAccessWithRacing reproduces, through the |
| 20 | +// public Discover entry point, the data race the per-Unit cfg/reading locks |
| 21 | +// guard against: the graph phase reaches a shared unit from several goroutines |
| 22 | +// at once, so one goroutine's parse stores the config while others read it. |
| 23 | +// |
| 24 | +// The shared unit gets a large config and many dependents, repeated across |
| 25 | +// several Discover calls, so the read/write overlap is reliably observable; a |
| 26 | +// smaller config or fewer iterations make the race intermittent. |
| 27 | +// |
| 28 | +// To confirm the locks are load-bearing, drop the lock/unlock calls from Unit's |
| 29 | +// Config, StoreConfig, Reading, and SetReading and run with -race. |
| 30 | +func TestDiscovery_GraphConcurrentConfigAccessWithRacing(t *testing.T) { |
| 31 | + t.Parallel() |
| 32 | + |
| 33 | + tmpDir := helpers.TmpDirWOSymlinks(t) |
| 34 | + |
| 35 | + // remote_state is partially decoded during discovery, so a large block is |
| 36 | + // walked during parse rather than skipped, which lengthens the parse. |
| 37 | + var sharedConfig strings.Builder |
| 38 | + |
| 39 | + sharedConfig.WriteString("remote_state {\n backend = \"local\"\n") |
| 40 | + sharedConfig.WriteString(" generate = { path = \"backend.tf\", if_exists = \"overwrite\" }\n config = {\n") |
| 41 | + |
| 42 | + for i := range 8000 { |
| 43 | + fmt.Fprintf(&sharedConfig, " k%d = \"v%d\"\n", i, i) |
| 44 | + } |
| 45 | + |
| 46 | + sharedConfig.WriteString(" }\n}\n") |
| 47 | + |
| 48 | + vpcDir := filepath.Join(tmpDir, "vpc") |
| 49 | + require.NoError(t, os.MkdirAll(vpcDir, 0755)) |
| 50 | + require.NoError(t, os.WriteFile(filepath.Join(vpcDir, "terragrunt.hcl"), []byte(sharedConfig.String()), 0644)) |
| 51 | + |
| 52 | + const leaves = 8 |
| 53 | + |
| 54 | + for i := range leaves { |
| 55 | + leafDir := filepath.Join(tmpDir, fmt.Sprintf("app%d", i)) |
| 56 | + require.NoError(t, os.MkdirAll(leafDir, 0755)) |
| 57 | + require.NoError(t, os.WriteFile(filepath.Join(leafDir, "terragrunt.hcl"), []byte(` |
| 58 | + dependency "vpc" { |
| 59 | + config_path = "../vpc" |
| 60 | + } |
| 61 | + `), 0644)) |
| 62 | + } |
| 63 | + |
| 64 | + l := logger.CreateLogger() |
| 65 | + opts := &options.TerragruntOptions{ |
| 66 | + WorkingDir: tmpDir, |
| 67 | + RootWorkingDir: tmpDir, |
| 68 | + } |
| 69 | + |
| 70 | + filters, err := filter.ParseFilterQueries(l, []string{"{./**}..."}) |
| 71 | + require.NoError(t, err) |
| 72 | + |
| 73 | + for range 8 { |
| 74 | + d := discovery.NewDiscovery(tmpDir). |
| 75 | + WithDiscoveryContext(&component.DiscoveryContext{WorkingDir: tmpDir}). |
| 76 | + WithFilters(filters) |
| 77 | + |
| 78 | + _, err := d.Discover(t.Context(), l, opts) |
| 79 | + require.NoError(t, err) |
| 80 | + } |
| 81 | +} |
0 commit comments