Skip to content

Commit

Permalink
Prevent EnsureProviderValid from mutating
Browse files Browse the repository at this point in the history
This function has caused repeated test failures with concurrent map mutations, the most
recent of which is
https://github.com/pulumi/pulumi-terraform-bridge/actions/runs/12772071950/job/35600764628?pr=2832. This
commit converts the function to return a copy instead of mutating.
  • Loading branch information
iwahbe committed Jan 14, 2025
1 parent 5ed2021 commit 0fc8d8a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
37 changes: 35 additions & 2 deletions pkg/internal/tests/pulcheck/pulcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,39 @@ func resourceNeedsUpdate(res *schema.Resource) bool {
return false
}

func copyMap[K comparable, V any](m map[K]V, cp func(V) V) map[K]V {
dst := make(map[K]V, len(m))
for k, v := range m {
dst[k] = cp(v)
}
return dst
}

// WithValidProvider returns a copy of tfp, with all required fields filled with default
// values.
//
// This is an experimental API.
func EnsureProviderValid(t T, tfp *schema.Provider) {
func WithValidProvider(t T, tfp *schema.Provider) *schema.Provider {
if tfp == nil {
return nil
}

// Copy tfp as deep as we will mutate.
{
dst := *tfp // memcopy
dst.ResourcesMap = copyMap(tfp.ResourcesMap, func(v *schema.Resource) *schema.Resource {
cp := *v // memcopy
cp.Schema = copyMap(v.Schema, func(s *schema.Schema) *schema.Schema {
cp := *s
return &cp
})
return &cp
})
tfp = &dst
}

// Now ensure that tfp is valid

for _, r := range tfp.ResourcesMap {
if r.Schema["id"] == nil {
r.Schema["id"] = &schema.Schema{
Expand Down Expand Up @@ -108,6 +139,8 @@ func EnsureProviderValid(t T, tfp *schema.Provider) {
}
}
require.NoError(t, tfp.InternalValidate())

return tfp
}

func ProviderServerFromInfo(
Expand Down Expand Up @@ -206,7 +239,7 @@ func BridgedProvider(t T, providerName string, tfp *schema.Provider, opts ...Bri
opt(&options)
}

EnsureProviderValid(t, tfp)
tfp = WithValidProvider(t, tfp)

// If the PULUMI_ACCURATE_BRIDGE_PREVIEWS environment variable is set, use it to enable
// accurate bridge previews.
Expand Down
2 changes: 1 addition & 1 deletion pkg/tests/tfcheck/tfcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func NewTfDriver(t pulcheck.T, dir, providerName string, prov any) *TFDriver {
}

func newTfDriverSDK(t pulcheck.T, dir, providerName string, prov *schema.Provider) *TFDriver {
pulcheck.EnsureProviderValid(t, prov)
prov = pulcheck.WithValidProvider(t, prov)
v6server, err := tf5to6server.UpgradeServer(context.Background(),
func() tfprotov5.ProviderServer { return prov.GRPCProvider() })
require.NoError(t, err)
Expand Down

0 comments on commit 0fc8d8a

Please sign in to comment.