Skip to content

Commit

Permalink
Enable PRC by default (#2380)
Browse files Browse the repository at this point in the history
This PR enables PlanResourceChange by default in the bridge. A bunch of
tests had to be fixed up to work with PRC.

fixes #1785
  • Loading branch information
VenelinMartinov authored Sep 19, 2024
1 parent 83d2f45 commit be3b5e6
Show file tree
Hide file tree
Showing 10 changed files with 460 additions and 636 deletions.
3 changes: 2 additions & 1 deletion pf/tests/provider_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ func TestMuxedAliasCreate(t *testing.T) {
"id": "4",
"fair": true,
"number": 4,
"suggestionUpdated": false
"suggestionUpdated": false,
"suggestion": "*"
}
},
"metadata": {
Expand Down
13 changes: 2 additions & 11 deletions pkg/tests/regress_1020_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,8 @@ func TestRegress1020(t *testing.T) {
"addresses": [
"2001:0db8:85a3:0000:0000:8a2e:0370:7334/32"
],
"id": "",
"id": "04da6b54-80e4-46f7-96ec-b56ff0331ba9",
"rules": [],
"ipAddressVersion": "IPV6",
"name": "ip6_sample-e8442ad",
"scope": "CLOUDFRONT"
Expand All @@ -201,11 +202,6 @@ func TestRegress1020(t *testing.T) {
testutils.Replay(t, server(p), createTestCase)
})

t.Run("can preview Create when using PlanState", func(t *testing.T) {
p := shimv2.NewProvider(tfProvider, shimv2.WithDiffStrategy(shimv2.PlanState))
testutils.Replay(t, server(p), createTestCase)
})

diffTestCase := `
{
"method": "/pulumirpc.ResourceProvider/Diff",
Expand Down Expand Up @@ -250,9 +246,4 @@ func TestRegress1020(t *testing.T) {
p := shimv2.NewProvider(tfProvider)
testutils.Replay(t, server(p), diffTestCase)
})

t.Run("can compute an Update plan in Diff when using PlanState", func(t *testing.T) {
p := shimv2.NewProvider(tfProvider, shimv2.WithDiffStrategy(shimv2.PlanState))
testutils.Replay(t, server(p), diffTestCase)
})
}
5 changes: 4 additions & 1 deletion pkg/tests/regress_hcloud_175_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,10 @@ func TestRegressHCloud175(t *testing.T) {
"id": "*",
"ipRange": "10.0.1.0/24",
"networkZone": "eu-central",
"type": "cloud"
"type": "cloud",
"gateway": "*",
"vswitchId": "*",
"networkId": "*"
}
}
}`
Expand Down
124 changes: 124 additions & 0 deletions pkg/tests/schema_pulumi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4199,3 +4199,127 @@ outputs:
assert.Equal(t, "val", out.Outputs["keyValue"].Value)
assert.Equal(t, "", out.Outputs["emptyValue"].Value)
}

func TestUnknownSetElementDiff(t *testing.T) {
resMap := map[string]*schema.Resource{
"prov_test": {
Schema: map[string]*schema.Schema{
"test": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
},
"prov_aux": {
Schema: map[string]*schema.Schema{
"aux": {
Type: schema.TypeString,
Computed: true,
Optional: true,
},
},
CreateContext: func(_ context.Context, d *schema.ResourceData, _ interface{}) diag.Diagnostics {
d.SetId("aux")
err := d.Set("aux", "aux")
require.NoError(t, err)
return nil
},
},
}
tfp := &schema.Provider{ResourcesMap: resMap}

runTest := func(t *testing.T, PRC bool, expectedOutput autogold.Value) {
opts := []pulcheck.BridgedProviderOpt{}
if !PRC {
opts = append(opts, pulcheck.DisablePlanResourceChange())
}
bridgedProvider := pulcheck.BridgedProvider(t, "prov", tfp, opts...)
originalProgram := `
name: test
runtime: yaml
resources:
mainRes:
type: prov:index:Test
outputs:
testOut: ${mainRes.tests}
`

programWithUnknown := `
name: test
runtime: yaml
resources:
auxRes:
type: prov:index:Aux
mainRes:
type: prov:index:Test
properties:
tests:
- ${auxRes.aux}
outputs:
testOut: ${mainRes.tests}
`
pt := pulcheck.PulCheck(t, bridgedProvider, originalProgram)
pt.Up()
pulumiYamlPath := filepath.Join(pt.CurrentStack().Workspace().WorkDir(), "Pulumi.yaml")

err := os.WriteFile(pulumiYamlPath, []byte(programWithUnknown), 0o600)
require.NoError(t, err)

res := pt.Preview(optpreview.Diff())
// Test that the test property is unknown at preview time
expectedOutput.Equal(t, res.StdOut)
resUp := pt.Up()
// assert that the property gets resolved
require.Equal(t,
[]interface{}{"aux"},
resUp.Outputs["testOut"].Value,
)
}

t.Run("PRC enabled", func(t *testing.T) {
// TODO[pulumi/pulumi-terraform-bridge#2427]: Incorrect detailed diff with unknown elements
t.Skip("Skipping until pulumi/pulumi-terraform-bridge#2427 is resolved")
runTest(t, true, autogold.Expect(`Previewing update (test):
pulumi:pulumi:Stack: (same)
[urn=urn:pulumi:test::test::pulumi:pulumi:Stack::test-test]
+ prov:index/aux:Aux: (create)
[urn=urn:pulumi:test::test::prov:index/aux:Aux::auxRes]
~ prov:index/test:Test: (update)
[id=newid]
[urn=urn:pulumi:test::test::prov:index/test:Test::mainRes]
+ tests: [
+ [0]: output<string>
]
--outputs:--
+ testOut: output<string>
Resources:
+ 1 to create
~ 1 to update
2 changes. 1 unchanged
`))
})

t.Run("PRC disabled", func(t *testing.T) {
runTest(t, false, autogold.Expect(`Previewing update (test):
pulumi:pulumi:Stack: (same)
[urn=urn:pulumi:test::test::pulumi:pulumi:Stack::test-test]
+ prov:index/aux:Aux: (create)
[urn=urn:pulumi:test::test::prov:index/aux:Aux::auxRes]
~ prov:index/test:Test: (update)
[id=newid]
[urn=urn:pulumi:test::test::prov:index/test:Test::mainRes]
+ tests: [
+ [0]: output<string>
]
--outputs:--
+ testOut: output<string>
Resources:
+ 1 to create
~ 1 to update
2 changes. 1 unchanged
`))
})
}
27 changes: 17 additions & 10 deletions pkg/tfbridge/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,16 @@ func TestCustomizeDiff(t *testing.T) {
return err
},
}
provider := shimv2.NewProvider(&v2Schema.Provider{
v2Provider := v2Schema.Provider{
ResourcesMap: map[string]*v2Schema.Resource{
"resource": customDiffRes,
},
})
}
provider := shimv2.NewProvider(&v2Provider)

// Convert the inputs and state to TF config and resource attributes.
r := Resource{
TF: shimv2.NewResource(customDiffRes),
TF: provider.ResourcesMap().Get("resource"),
Schema: &ResourceInfo{Fields: info},
}
tfState, err := makeTerraformStateWithOpts(ctx, r, "id", stateMap,
Expand Down Expand Up @@ -118,15 +119,16 @@ func TestCustomizeDiff(t *testing.T) {
noCustomDiffRes := &v2Schema.Resource{
Schema: tfs,
}
provider := shimv2.NewProvider(&v2Schema.Provider{
v2Provider := v2Schema.Provider{
ResourcesMap: map[string]*v2Schema.Resource{
"resource": noCustomDiffRes,
},
})
}
provider := shimv2.NewProvider(&v2Provider)

// Convert the inputs and state to TF config and resource attributes.
r := Resource{
TF: shimv2.NewResource(noCustomDiffRes),
TF: provider.ResourcesMap().Get("resource"),
Schema: &ResourceInfo{Fields: info},
}
tfState, err := makeTerraformStateWithOpts(ctx, r, "id", stateMap,
Expand Down Expand Up @@ -180,7 +182,7 @@ func TestCustomizeDiff(t *testing.T) {

// Convert the inputs and state to TF config and resource attributes.
r := Resource{
TF: shimv2.NewResource(customDiffRes),
TF: provider.ResourcesMap().Get("resource"),
Schema: &ResourceInfo{Fields: info},
}
tfState, err := makeTerraformStateWithOpts(ctx, r, "id", stateMap,
Expand Down Expand Up @@ -245,16 +247,17 @@ func v2Setup(tfs map[string]*v2Schema.Schema) (
return d.SetNewComputed("outp")
},
}
provider := shimv2.NewProvider(&v2Schema.Provider{
v2Provider := v2Schema.Provider{
ResourcesMap: map[string]*v2Schema.Resource{
"resource": res,
},
})
}
provider := shimv2.NewProvider(&v2Provider)
info := map[string]*SchemaInfo{}

// Convert the inputs and state to TF config and resource attributes.
r := Resource{
TF: shimv2.NewResource(res),
TF: provider.ResourcesMap().Get("resource"),
Schema: &ResourceInfo{Fields: info},
}

Expand Down Expand Up @@ -1464,6 +1467,8 @@ func TestNestedComputedSetUpdate(t *testing.T) {
}

func TestNestedComputedSetAdd(t *testing.T) {
// TODO[pulumi/pulumi-terraform-bridge#2427]: Incorrect detailed diff with unknown elements
t.Skip("Skipping until pulumi/pulumi-terraform-bridge#2427 is resolved")
diffTest(t,
map[string]*v2Schema.Schema{
"prop": {Type: v2Schema.TypeSet, Elem: &v2Schema.Schema{Type: v2Schema.TypeString}},
Expand Down Expand Up @@ -1539,6 +1544,8 @@ func TestNestedComputedSetIntUpdateReplace(t *testing.T) {
}

func TestNestedComputedSetIntAdd(t *testing.T) {
// TODO[pulumi/pulumi-terraform-bridge#2427]: Incorrect detailed diff with unknown elements
t.Skip("Skipping until pulumi/pulumi-terraform-bridge#2427 is resolved")
diffTest(t,
map[string]*v2Schema.Schema{
"prop": {Type: v2Schema.TypeSet, Elem: &v2Schema.Schema{Type: v2Schema.TypeInt}},
Expand Down
Loading

0 comments on commit be3b5e6

Please sign in to comment.