Skip to content

Commit

Permalink
Fix typechecking booleans (#2310)
Browse files Browse the repository at this point in the history
The type-checking pass had a bug where it failed to flag passing a
string to a slot of type boolean. This is now fixed so that appropriate
warnings are generated.

Fixes  pulumi/pulumi-aws#4342 P1
  • Loading branch information
t0yv0 authored Aug 13, 2024
1 parent 5548bd6 commit e468215
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 4 deletions.
75 changes: 75 additions & 0 deletions pkg/tfbridge/tests/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/stretchr/testify/assert"

"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfbridge"
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfbridge/info"
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfgen"
shimv2 "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfshim/sdk-v2"
)
Expand Down Expand Up @@ -777,6 +778,80 @@ func TestValidateConfig(t *testing.T) {
})
}

// Assert that passing strings into fields of boolean type triggers a type-checking error message, even if some of the
// resource inputs are unknown. See also: https://github.com/pulumi/pulumi-aws/issues/4342
func TestTypeCheckingMistypedBooleansWithUnknowns(t *testing.T) {
t.Setenv("PULUMI_ERROR_TYPE_CHECKER", "true")
ctx := context.Background()
resourceID := "aws_ecs_service"
resourceSchema := 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,
Default: false,
},
"subnets": {
Type: schema.TypeSet,
Required: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
},
},
}

res := &schema.Resource{
Schema: resourceSchema,
}

resMap := map[string]*schema.Resource{resourceID: res}

schemaProvider := &schema.Provider{ResourcesMap: resMap}

p := newTestProvider(ctx, tfbridge.ProviderInfo{
P: shimv2.NewProvider(schemaProvider),
Name: "aws",
ResourcePrefix: "aws",
Resources: map[string]*info.Resource{
resourceID: {
Tok: "aws:ecs/service:Service",
},
},
}, newTestProviderOptions{})

reason := "expected boolean type, got string type. " +
"Examine values at 'my-ecs-service.networkConfiguration.assignPublicIp'."

// networkConfiguration.assignPublicIp has the wrong type intentionally.
replay.ReplaySequence(t, p, fmt.Sprintf(`
[
{
"method": "/pulumirpc.ResourceProvider/Check",
"request": {
"urn": "urn:pulumi:dev::aws-4342::aws:ecs/service:Service::my-ecs-service",
"olds": {},
"news": {
"networkConfiguration": {
"assignPublicIp": "DISABLED",
"subnets": "04da6b54-80e4-46f7-96ec-b56ff0331ba9"
}
},
"randomSeed": "7WaseITzLnMm7TGBDCYIbSUvAatQKt0rkmDuHUXxR9U="
},
"response": {
"inputs": "*",
"failures": [{"reason": "%s"}]
}
}
]`, reason))
}

func nilSink() diag.Sink {
nilSink := diag.DefaultSink(io.Discard, io.Discard, diag.FormatOptions{
Color: colors.Never,
Expand Down
5 changes: 2 additions & 3 deletions pkg/tfbridge/validate_input_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,8 @@ func (v *PulumiInputValidator) validatePropertyValue(
}

switch typeSpec.Type {
case "bool":
// The bridge permits coalescing strings to booleans, hence skip strings.
if !propertyValue.IsBool() && !propertyValue.IsString() {
case "boolean":
if !propertyValue.IsBool() {
return []TypeFailure{{
ResourcePath: propertyPath.String(),
Reason: fmt.Sprintf(
Expand Down
2 changes: 1 addition & 1 deletion pkg/tfbridge/validate_input_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1477,7 +1477,7 @@ func TestValidateInputType_toplevel(t *testing.T) {
TypeSpec: pschema.TypeSpec{
Type: "object",
AdditionalProperties: &pschema.TypeSpec{
Type: "bool",
Type: "boolean",
},
},
},
Expand Down

0 comments on commit e468215

Please sign in to comment.