From 9fc9f9e8c5f67c168804689d641d0eb7445a9ec3 Mon Sep 17 00:00:00 2001 From: corymhall <43035978+corymhall@users.noreply.github.com> Date: Mon, 8 Jul 2024 15:41:34 -0400 Subject: [PATCH] pf resources show diff for empty map re https://github.com/pulumi/pulumi-aws/issues/4080 I've noticed different behavior between sdkv2 and pf resources. Sometimes if you do not provide a value for a map property it will write an empty value to the outputs (i.e. `{ "tagsAll": {} }`). When diff is called, the `olds` show the empty object value and the news show no entry at all. sdkv2 resources handle this and do not display any diff whereas pf resources show a diff. --- pf/tests/provider_diff_test.go | 36 +++++++++++++++++++++ pkg/tfbridge/tests/provider_test.go | 49 +++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) diff --git a/pf/tests/provider_diff_test.go b/pf/tests/provider_diff_test.go index 4ea93d7b7..d3072e206 100644 --- a/pf/tests/provider_diff_test.go +++ b/pf/tests/provider_diff_test.go @@ -195,3 +195,39 @@ func TestDiffVersionUpgrade(t *testing.T) { }` testutils.Replay(t, server, testCase) } + +// Test shows that an empty object shows a diff if it is not provided +func TestEmptyObjectDiff(t *testing.T) { + server := newProviderServer(t, testprovider.SyntheticTestBridgeProvider()) + testCase := ` + { + "method": "/pulumirpc.ResourceProvider/Diff", + "request": { + "id": "0", + "urn": "urn:pulumi:test-stack::basicprogram::testbridge:index/testres:Testres::testres1", + "olds": { + "id": "none", + "statedir": "res-461023c", + "requiredInputString": "abc", + "requiredInputStringCopy": "abc", + "optionalInputStringMap": {} + }, + "news": { + "statedir": "res-461023c", + "requiredInputString": "abc" + }, + "oldInputs": { + "statedir": "res-461023c", + "requiredInputString": "abc" + } + }, + "response": { + "changes": "DIFF_SOME", + "diffs": [ + "optionalInputStringMap" + ] + } + } + ` + testutils.Replay(t, server, testCase) +} diff --git a/pkg/tfbridge/tests/provider_test.go b/pkg/tfbridge/tests/provider_test.go index 91d9c4da6..2833d284e 100644 --- a/pkg/tfbridge/tests/provider_test.go +++ b/pkg/tfbridge/tests/provider_test.go @@ -70,6 +70,55 @@ func TestWithNewTestProvider(t *testing.T) { `) } +// An empty object does not show a diff if it is not provided +func TestEmptyMapNoDiff(t *testing.T) { + ctx := context.Background() + p := newTestProvider(ctx, tfbridge.ProviderInfo{ + P: shimv2.NewProvider(&schema.Provider{ + Schema: map[string]*schema.Schema{}, + ResourcesMap: map[string]*schema.Resource{ + "example_resource": { + Schema: map[string]*schema.Schema{ + "optional_input_string_map": { + Type: schema.TypeMap, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + Optional: true, + }, + }, + }, + }, + }), + Name: "testprov", + ResourcePrefix: "example", + Resources: map[string]*tfbridge.ResourceInfo{ + "example_resource": {Tok: "testprov:index:ExampleResource"}, + }, + }, newTestProviderOptions{}) + + replay.Replay(t, p, ` + { + "method": "/pulumirpc.ResourceProvider/Diff", + "request": { + "id": "0", + "urn": "urn:pulumi:dev::teststack::testprov:index:ExampleResource::exres", + "olds": { + "optionalInputStringMap": {} + }, + "news": { + }, + "oldInputs": { + } + }, + "response": { + "changes": "DIFF_NONE", + "hasDetailedDiff": true + } + } + `) +} + // TestRegress1932 tests that we can have a list with different types (string & unknown) func TestRegress1932(t *testing.T) { ctx := context.Background()