From 51dd086ea50354006ac7e202cfb5cdcf8ccea354 Mon Sep 17 00:00:00 2001 From: Venelin Date: Tue, 24 Sep 2024 15:44:54 +0300 Subject: [PATCH] add cross test for nil vs empty maps and MakeTerraformResult test --- pkg/tests/cross-tests/diff_cross_test.go | 33 ++++++++++++++ pkg/tests/schema_pulumi_test.go | 57 ++++++++++++++++++++++++ 2 files changed, 90 insertions(+) diff --git a/pkg/tests/cross-tests/diff_cross_test.go b/pkg/tests/cross-tests/diff_cross_test.go index 3302dea6b..63ebb3839 100644 --- a/pkg/tests/cross-tests/diff_cross_test.go +++ b/pkg/tests/cross-tests/diff_cross_test.go @@ -974,3 +974,36 @@ func TestNilVsEmptyListProperty(t *testing.T) { }) }) } + +func TestNilVsEmptyMapProperty(t *testing.T) { + cfgEmpty := map[string]any{"f0": map[string]any{}} + cfgNil := map[string]any{} + + res := &schema.Resource{ + Schema: map[string]*schema.Schema{ + "f0": { + Type: schema.TypeMap, + Optional: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + }, + } + + t.Run("nil to empty", func(t *testing.T) { + runDiffCheck(t, diffTestCase{ + Resource: res, + Config1: cfgNil, + Config2: cfgEmpty, + }) + }) + + t.Run("empty to nil", func(t *testing.T) { + runDiffCheck(t, diffTestCase{ + Resource: res, + Config1: cfgEmpty, + Config2: cfgNil, + }) + }) +} diff --git a/pkg/tests/schema_pulumi_test.go b/pkg/tests/schema_pulumi_test.go index eb6630528..f5eb36795 100644 --- a/pkg/tests/schema_pulumi_test.go +++ b/pkg/tests/schema_pulumi_test.go @@ -4197,3 +4197,60 @@ Resources: `)) }) } + +func TestMakeTerraformResultNilVsEmptyMap(t *testing.T) { + // Nil and empty maps are not equal + nilMap := resource.NewObjectProperty(nil) + emptyMap := resource.NewObjectProperty(resource.PropertyMap{}) + + assert.True(t, nilMap.DeepEquals(emptyMap)) + assert.NotEqual(t, emptyMap.ObjectValue(), nilMap.ObjectValue()) + + // Check that MakeTerraformResult maintains that difference + const resName = "prov_test" + resMap := map[string]*schema.Resource{ + "prov_test": { + Schema: map[string]*schema.Schema{ + "test": { + Type: schema.TypeMap, + Optional: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + }, + }, + } + + prov := &schema.Provider{ + ResourcesMap: resMap, + } + bridgedProvider := pulcheck.BridgedProvider(t, "prov", prov) + + ctx := context.Background() + shimProv := bridgedProvider.P + + res := shimProv.ResourcesMap().Get(resName) + + t.Run("NilMap", func(t *testing.T) { + // Create a resource with a nil map + state, err := res.InstanceState("0", map[string]interface{}{}, map[string]interface{}{}) + assert.NoError(t, err) + + props, err := tfbridge.MakeTerraformResult(ctx, shimProv, state, res.Schema(), nil, nil, true) + assert.NoError(t, err) + assert.NotNil(t, props) + assert.True(t, props["test"].DeepEquals(nilMap)) + }) + + t.Run("EmptyMap", func(t *testing.T) { + // Create a resource with an empty map + state, err := res.InstanceState("0", map[string]interface{}{"test": map[string]interface{}{}}, map[string]interface{}{}) + assert.NoError(t, err) + + props, err := tfbridge.MakeTerraformResult(ctx, shimProv, state, res.Schema(), nil, nil, true) + assert.NoError(t, err) + assert.NotNil(t, props) + assert.True(t, props["test"].DeepEquals(emptyMap)) + }) +} \ No newline at end of file