Skip to content

Commit

Permalink
Ensure datasource aliasing is deterministic (#1287)
Browse files Browse the repository at this point in the history
  • Loading branch information
iwahbe authored Jul 11, 2023
1 parent 7cfaa9a commit fecaef1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
20 changes: 13 additions & 7 deletions pkg/tfbridge/auto_aliasing.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,20 +155,24 @@ func (info *ProviderInfo) ApplyAutoAliases() error {
// Applying resource aliases adds new resources to providerInfo.Resources. To keep
// this process deterministic, we don't apply resource aliases until all resources
// have been examined.
applyResourceAliases := []func(){}
//
// The same logic applies to datasources.
applyAliases := []func(){}

for tfToken, computed := range info.Resources {
r, _ := rMap.GetOk(tfToken)
aliasResource(info, r, &applyResourceAliases, hist.Resources,
aliasResource(info, r, &applyAliases, hist.Resources,
computed, tfToken, currentVersion)
}
for _, f := range applyResourceAliases {
f()
}

for tfToken, computed := range info.DataSources {
ds, _ := dMap.GetOk(tfToken)
aliasDataSource(info, ds, hist.DataSources, computed, tfToken, currentVersion)
aliasDataSource(info, ds, &applyAliases, hist.DataSources,
computed, tfToken, currentVersion)
}

for _, f := range applyAliases {
f()
}

if err := md.Set(artifact, aliasMetadataKey, hist); err != nil {
Expand Down Expand Up @@ -433,6 +437,7 @@ func aliasOrRenameResource(
func aliasDataSource(
p *ProviderInfo,
ds shim.Resource,
queue *[]func(),
hist map[string]*tokenHistory[tokens.ModuleMember],
computed *DataSourceInfo,
tfToken string,
Expand All @@ -447,7 +452,8 @@ func aliasDataSource(
MajorVersion: version,
}
} else {
aliasOrRenameDataSource(p, computed, tfToken, prev, version)
*queue = append(*queue,
func() { aliasOrRenameDataSource(p, computed, tfToken, prev, version) })
}

if ds == nil {
Expand Down
23 changes: 17 additions & 6 deletions pkg/tfbridge/tokens_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,16 @@ func makeAutoAliasing(t *testing.T) (
}

func TestTokenAliasing(t *testing.T) {
// We run this test multiple times to guard against nondeterminism.
//
// See https://github.com/pulumi/pulumi-terraform-bridge/issues/1286.
for i := 0; i < 500; i++ {
t.Run(fmt.Sprintf("%d", i), testTokenAliasing)
}
}

func testTokenAliasing(t *testing.T) {
t.Parallel()
provider := func() *tfbridge.ProviderInfo {
return &tfbridge.ProviderInfo{
P: (&schema.Provider{
Expand All @@ -463,11 +473,12 @@ func TestTokenAliasing(t *testing.T) {
"pkg_mod2_r1": nil,
},
DataSourcesMap: schema.ResourceMap{
"pkg_mod1_r1": nil,
"pkg_mod1_d1": nil,
},
}).Shim(),
}
}

simple := provider()

metadata, autoAliasing := makeAutoAliasing(t)
Expand Down Expand Up @@ -527,11 +538,11 @@ func TestTokenAliasing(t *testing.T) {
},
}, modules.Resources)
assert.Equal(t, map[string]*tfbridge.DataSourceInfo{
"pkg_mod1_r1": {Tok: "pkg:mod1/getR1:getR1"},
"pkg_mod1_r1_legacy": {
Tok: "pkg:index/getMod1R1:getMod1R1",
DeprecationMessage: "pkg.index/getmod1r1.getMod1R1 has been deprecated in favor of pkg.mod1/getr1.getR1",
Docs: &tfbridge.DocInfo{Source: "kg_mod1_r1.html.markdown"},
"pkg_mod1_d1": {Tok: "pkg:mod1/getD1:getD1"},
"pkg_mod1_d1_legacy": {
Tok: "pkg:index/getMod1D1:getMod1D1",
DeprecationMessage: "pkg.index/getmod1d1.getMod1D1 has been deprecated in favor of pkg.mod1/getd1.getD1",
Docs: &tfbridge.DocInfo{Source: "kg_mod1_d1.html.markdown"},
},
},
modules.DataSources)
Expand Down

0 comments on commit fecaef1

Please sign in to comment.