Skip to content

Commit

Permalink
Type checker integration tests schema in tests (#2536)
Browse files Browse the repository at this point in the history
Addresses a review comment on
#2461 which I
failed to commit in the original PR.


#2461 (review)
  • Loading branch information
VenelinMartinov authored Nov 1, 2024
1 parent 3efb90b commit 38be4eb
Showing 1 changed file with 112 additions and 35 deletions.
147 changes: 112 additions & 35 deletions pkg/tests/type_checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(`
Expand All @@ -68,40 +39,146 @@ 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")
})

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")
})
}
}

0 comments on commit 38be4eb

Please sign in to comment.