From 38be4eb138159f59ad25538c1a1af169d3d28d33 Mon Sep 17 00:00:00 2001 From: VenelinMartinov Date: Fri, 1 Nov 2024 15:22:35 +0000 Subject: [PATCH] Type checker integration tests schema in tests (#2536) Addresses a review comment on https://github.com/pulumi/pulumi-terraform-bridge/pull/2461 which I failed to commit in the original PR. https://github.com/pulumi/pulumi-terraform-bridge/pull/2461#pullrequestreview-2351869517 --- pkg/tests/type_checker_test.go | 147 +++++++++++++++++++++++++-------- 1 file changed, 112 insertions(+), 35 deletions(-) diff --git a/pkg/tests/type_checker_test.go b/pkg/tests/type_checker_test.go index 4d5b92dc2..a7b4bbd71 100644 --- a/pkg/tests/type_checker_test.go +++ b/pkg/tests/type_checker_test.go @@ -12,43 +12,14 @@ import ( func TestTypeChecker(t *testing.T) { t.Setenv("PULUMI_DEBUG_YAML_DISABLE_TYPE_CHECKING", "true") - resMap := map[string]*schema.Resource{ - "prov_test": { - Schema: map[string]*schema.Schema{ - "tags": { - Type: schema.TypeMap, - Elem: &schema.Schema{Type: schema.TypeString}, - Optional: true, - }, - "network_configuration": { - Type: schema.TypeList, - Optional: true, - MaxItems: 1, - Elem: &schema.Resource{ - Schema: map[string]*schema.Schema{ - "assign_public_ip": { - Type: schema.TypeBool, - Optional: true, - Default: false, - }, - "security_groups": { - Type: schema.TypeSet, - Optional: true, - Elem: &schema.Schema{Type: schema.TypeString}, - }, - "subnets": { - Type: schema.TypeSet, - Optional: true, - Elem: &schema.Schema{Type: schema.TypeString}, - }, - }, - }, - }, - }, - }, + makeResMap := func(sch map[string]*schema.Schema) map[string]*schema.Resource { + return map[string]*schema.Resource{ + "prov_test": {Schema: sch}, + } } runTest := func(t *testing.T, resMap map[string]*schema.Resource, props interface{}, expectedError string) { + t.Helper() propsJSON, err := json.Marshal(props) require.NoError(t, err) program := fmt.Sprintf(` @@ -68,33 +39,125 @@ resources: } t.Run("flat type instead of array", func(t *testing.T) { + resMap := makeResMap(map[string]*schema.Schema{ + "network_configuration": { + Type: schema.TypeList, + Optional: true, + MaxItems: 1, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "subnets": { + Type: schema.TypeSet, + Optional: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + }, + }, + }, + }) runTest(t, resMap, map[string]interface{}{"networkConfiguration": map[string]any{"subnets": "subnet"}}, "expected array type, got") }) t.Run("flat type instead of map", func(t *testing.T) { + resMap := makeResMap(map[string]*schema.Schema{ + "tags": { + Type: schema.TypeMap, + Elem: &schema.Schema{Type: schema.TypeString}, + Optional: true, + }, + }) runTest(t, resMap, map[string]interface{}{"tags": "tag"}, "expected object type, got") }) t.Run("flat type instead of object", func(t *testing.T) { t.Skip("This is caught by the YAML runtime, not the type checker") + resMap := makeResMap(map[string]*schema.Schema{ + "network_configuration": { + Type: schema.TypeList, + Optional: true, + MaxItems: 1, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "subnets": { + Type: schema.TypeSet, + Optional: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + }, + }, + }, + }) runTest(t, resMap, map[string]interface{}{"network_configuration": "config"}, "expected object type, got") }) t.Run("array instead of object", func(t *testing.T) { t.Skip("This is caught by the YAML runtime, not the type checker") + resMap := makeResMap(map[string]*schema.Schema{ + "network_configuration": { + Type: schema.TypeList, + Optional: true, + MaxItems: 1, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "subnets": { + Type: schema.TypeSet, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + }, + }, + }, + }) runTest(t, resMap, map[string]interface{}{"network_configuration": []string{"config"}}, "expected object type, got") }) t.Run("array instead of map", func(t *testing.T) { + resMap := makeResMap(map[string]*schema.Schema{ + "tags": { + Type: schema.TypeMap, + Elem: &schema.Schema{Type: schema.TypeString}, + Optional: true, + }, + }) runTest(t, resMap, map[string]interface{}{"tags": []string{"tag"}}, "expected object type, got") }) t.Run("array instead of flat type", func(t *testing.T) { t.Skip("This is caught by the YAML runtime, not the type checker") + resMap := makeResMap(map[string]*schema.Schema{ + "network_configuration": { + Type: schema.TypeList, + Optional: true, + MaxItems: 1, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "assign_public_ip": { + Type: schema.TypeBool, + Optional: true, + }, + }, + }, + }, + }) runTest(t, resMap, map[string]interface{}{"network_configuration": map[string]interface{}{"assign_public_ip": []any{true}}}, "expected array type, got") }) t.Run("map instead of array", func(t *testing.T) { + resMap := makeResMap(map[string]*schema.Schema{ + "network_configuration": { + Type: schema.TypeList, + Optional: true, + MaxItems: 1, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "subnets": { + Type: schema.TypeSet, + Optional: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + }, + }, + }, + }) runTest(t, resMap, map[string]interface{}{"networkConfiguration": map[string]any{"subnets": map[string]any{"sub": "sub"}}}, "expected array type, got") @@ -102,6 +165,20 @@ resources: t.Run("map instead of flat type", func(t *testing.T) { t.Skip("This is caught by the YAML runtime, not the type checker") + resMap := makeResMap(map[string]*schema.Schema{ + "network_configuration": { + Type: schema.TypeList, + Optional: true, + MaxItems: 1, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "assign_public_ip": { + Type: schema.TypeBool, + }, + }, + }, + }, + }) runTest(t, resMap, map[string]interface{}{"network_configuration": map[string]interface{}{"assign_public_ip": map[string]any{"val": true}}}, "expected array type, got") }) -} +} \ No newline at end of file