Skip to content

Commit

Permalink
Cross test inputs test cases (#1913)
Browse files Browse the repository at this point in the history
This one adds a bunch of test cases which point to discrepancies in the
bridge. Most were generated through the property based tests.

I'll open issues for each one.

Stacked on #1912
  • Loading branch information
VenelinMartinov authored May 3, 2024
1 parent 4f16ef0 commit 8bf39a6
Showing 1 changed file with 221 additions and 0 deletions.
221 changes: 221 additions & 0 deletions pkg/tests/cross-tests/input_cross_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
package crosstests

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-go/tftypes"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func TestInputsEqualStringBasic(t *testing.T) {
skipUnlessLinux(t)
// Test both config representations.
for _, tc := range []struct {
name string
config any
}{
{"any", map[string]any{"f0": "val"}},
{
"tftype",
tftypes.NewValue(tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"f0": tftypes.String,
},
}, map[string]tftypes.Value{
"f0": tftypes.NewValue(tftypes.String, "val"),
}),
},
} {
t.Run(tc.name, func(t *testing.T) {
runCreateInputCheck(t, inputTestCase{
Resource: &schema.Resource{
Schema: map[string]*schema.Schema{
"f0": {
Type: schema.TypeString,
Optional: true,
},
},
},
Config: tc.config,
})
})
}
}

func TestInputsEqualObjectBasic(t *testing.T) {
skipUnlessLinux(t)
t1 := tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"x": tftypes.String,
},
}
t0 := tftypes.List{ElementType: t1}

// Test both config representations.
for _, tc := range []struct {
name string
config any
}{
{"any", map[string]any{"f0": []any{map[string]any{"x": "ok"}}}},
{
"tftype",
tftypes.NewValue(
tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"f0": t0,
},
},
map[string]tftypes.Value{
"f0": tftypes.NewValue(t0,
[]tftypes.Value{
tftypes.NewValue(t1,
map[string]tftypes.Value{
"x": tftypes.NewValue(tftypes.String, "ok"),
}),
},
),
},
),
},
} {
t.Run(tc.name, func(t *testing.T) {
runCreateInputCheck(t, inputTestCase{
Resource: &schema.Resource{
Schema: map[string]*schema.Schema{
"f0": {
Required: true,
Type: schema.TypeList,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"x": {Optional: true, Type: schema.TypeString},
},
},
},
},
},
Config: tc.config,
})
})
}
}

// Isolated from rapid-generated tests
func TestInputsEmptySchema(t *testing.T) {
skipUnlessLinux(t)
t.Skip("We represent resources with empty schemas wrong.")
// TODO[pulumi/pulumi-terraform-bridge#1914]
runCreateInputCheck(
t, inputTestCase{
Resource: &schema.Resource{
Schema: map[string]*schema.Schema{},
},
Config: tftypes.NewValue(tftypes.Object{}, map[string]tftypes.Value{}),
},
)
}

func TestInputsEqualEmptyList(t *testing.T) {
skipUnlessLinux(t)
t.Skip("We misrepresent empty lists")
// TODO[pulumi/pulumi-terraform-bridge#1915]
for _, maxItems := range []int{0, 1} {
for _, configMode := range []schema.SchemaConfigMode{schema.SchemaConfigModeAuto, schema.SchemaConfigModeBlock, schema.SchemaConfigModeAttr} {
name := fmt.Sprintf("MaxItems: %v, ConfigMode: %v", maxItems, configMode)
t.Run(name, func(t *testing.T) {
t1 := tftypes.List{ElementType: tftypes.String}
t0 := tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"f0": t1,
},
}
runCreateInputCheck(t, inputTestCase{
Resource: &schema.Resource{
Schema: map[string]*schema.Schema{
"f0": {
Optional: true,
Type: schema.TypeList,
MaxItems: maxItems,
ConfigMode: configMode,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"x": {Optional: true, Type: schema.TypeString},
},
},
},
},
},
Config: tftypes.NewValue(
t0,
map[string]tftypes.Value{
"f0": tftypes.NewValue(t1, []tftypes.Value{}),
},
),
})
})
}
}
}

// Isolated from rapid-generated tests
func TestInputsEmptyString(t *testing.T) {
skipUnlessLinux(t)
t.Skip("Empty strings are misrepresented")
// TODO[pulumi/pulumi-terraform-bridge#1916]
runCreateInputCheck(t, inputTestCase{
Resource: &schema.Resource{
Schema: map[string]*schema.Schema{
"f0": {
Type: schema.TypeString,
Required: true,
},
},
},
Config: tftypes.NewValue(
tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"f0": tftypes.String,
},
},
map[string]tftypes.Value{
"f0": tftypes.NewValue(tftypes.String, ""),
}),
})
}

// Isolated from rapid-generated tests
func TestInputsEmptyConfigModeAttrSet(t *testing.T) {
skipUnlessLinux(t)
t.Skip("Our handling of ConfigModeAttr is wrong.")
// TODO[pulumi/pulumi-terraform-bridge#1762]
t2 := tftypes.Object{AttributeTypes: map[string]tftypes.Type{
"f0": tftypes.String,
}}
t0 := tftypes.Object{AttributeTypes: map[string]tftypes.Type{
"f1": tftypes.Set{ElementType: t2},
}}

runCreateInputCheck(t, inputTestCase{
Resource: &schema.Resource{
Schema: map[string]*schema.Schema{
"f1": {
Type: schema.TypeSet,
ConfigMode: schema.SchemaConfigMode(1),
Optional: true,
Elem: &schema.Resource{Schema: map[string]*schema.Schema{
"f0": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
}},
MaxItems: 1,
},
},
},
Config: tftypes.NewValue(t0, map[string]tftypes.Value{
"f1": tftypes.NewValue(tftypes.Set{ElementType: t2}, []tftypes.Value{}),
}),
})
}

0 comments on commit 8bf39a6

Please sign in to comment.