Skip to content

Commit

Permalink
add cross test for nil vs empty maps and MakeTerraformResult test
Browse files Browse the repository at this point in the history
  • Loading branch information
VenelinMartinov committed Sep 24, 2024
1 parent 4965d17 commit 51dd086
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
33 changes: 33 additions & 0 deletions pkg/tests/cross-tests/diff_cross_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
})
}
57 changes: 57 additions & 0 deletions pkg/tests/schema_pulumi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})
}

0 comments on commit 51dd086

Please sign in to comment.