From c1171a037064551bcf35ff8b3f0c3cda0c4b6333 Mon Sep 17 00:00:00 2001 From: Ian Wahbe Date: Mon, 9 Dec 2024 14:48:05 +0100 Subject: [PATCH] Avoid a partially nil `host` as sink in test muxed providers This change prevents test providers from being initialized with partially nil logging sinks, which panicked in tests. Happily, the problem is restricted to `MakeMuxedServer`, a testing API. This was never present in production code. Fixes #2697 --- This is [my least favorite go feature](https://www.iwahbe.com/blog/go-pointers-considered-confusing/). --- pkg/pf/tests/logging_test.go | 35 +++++++++++++++++++++++++++++++++-- pkg/pf/tfbridge/main.go | 13 +++++++++++-- 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/pkg/pf/tests/logging_test.go b/pkg/pf/tests/logging_test.go index 53b8f0a46..daff0ec2a 100644 --- a/pkg/pf/tests/logging_test.go +++ b/pkg/pf/tests/logging_test.go @@ -8,23 +8,54 @@ import ( "github.com/hashicorp/terraform-plugin-framework/path" "github.com/hashicorp/terraform-plugin-framework/resource" "github.com/hashicorp/terraform-plugin-framework/resource/schema" + "github.com/pulumi/pulumi/pkg/v3/resource/provider" + presource "github.com/pulumi/pulumi/sdk/v3/go/common/resource" + pulumirpc "github.com/pulumi/pulumi/sdk/v3/proto/go" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/pf/tests/internal/providerbuilder" + "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/pf/tests/internal/testprovider" "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/pf/tests/pulcheck" - "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/pf/tfbridge" + pfbridge "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/pf/tfbridge" "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfbridge/info" "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfbridge/tokens" + shim "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfshim" ) +func TestRegress2699(t *testing.T) { + t.Parallel() + ctx := context.Background() + + var didLog bool + + info := testprovider.MuxedRandomProvider() + info.PreConfigureCallbackWithLogger = func( + ctx context.Context, + host *provider.HostClient, vars presource.PropertyMap, + config shim.ResourceConfig, + ) error { + log.Println("[DEBUG] Test") + didLog = true + return nil + } + info.UpstreamRepoPath = "." + + server, err := pfbridge.MakeMuxedServer(ctx, "muxedrandom", info, genSDKSchema(t, info))(nil) + require.NoError(t, err) + + _, err = server.CheckConfig(ctx, &pulumirpc.CheckRequest{}) + require.NoError(t, err) + require.True(t, didLog) +} + func TestLogCaputure(t *testing.T) { t.Setenv("TF_LOG", "WARN") provider := info.Provider{ Name: "test", Version: "0.0.1", - P: tfbridge.ShimProvider(providerbuilder.NewProvider(providerbuilder.NewProviderArgs{ + P: pfbridge.ShimProvider(providerbuilder.NewProvider(providerbuilder.NewProviderArgs{ TypeName: "test", AllResources: []providerbuilder.Resource{{ Name: "res", diff --git a/pkg/pf/tfbridge/main.go b/pkg/pf/tfbridge/main.go index 372588ce2..f628066dc 100644 --- a/pkg/pf/tfbridge/main.go +++ b/pkg/pf/tfbridge/main.go @@ -27,6 +27,7 @@ import ( "github.com/pulumi/pulumi/sdk/v3/go/common/util/contract" pulumirpc "github.com/pulumi/pulumi/sdk/v3/proto/go" + "github.com/pulumi/pulumi-terraform-bridge/v3/internal/logging" "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/pf" pfmuxer "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/pf/internal/muxer" "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfbridge" @@ -126,7 +127,7 @@ func MainWithMuxer(ctx context.Context, pkg string, info tfbridge.ProviderInfo, // of pulumi-terraform-bridge. This is an experimental API. func MakeMuxedServer( ctx context.Context, pkg string, info tfbridge.ProviderInfo, schema []byte, -) func(host *rprovider.HostClient) (pulumirpc.ResourceProviderServer, error) { +) func(*rprovider.HostClient) (pulumirpc.ResourceProviderServer, error) { shim, ok := info.P.(*pfmuxer.ProviderShim) contract.Assertf(ok, "MainWithMuxer must have a ProviderInfo.P created with AugmentShimWithPF") _, err := shim.ResolveDispatch(&info) @@ -172,7 +173,15 @@ func MakeMuxedServer( Server: func(host *rprovider.HostClient) (pulumirpc.ResourceProviderServer, error) { infoCopy := info infoCopy.P = prov - return NewProviderServer(ctx, host, + + // https://github.com/pulumi/pulumi-terraform-bridge/issues/2697: + // Avoid accidentally casting (*rprovider.HostClient)(nil) to + // (logging.Sink)((*rprovider.HostClient)(nil)). + var sink logging.Sink + if host != nil { + sink = host + } + return NewProviderServer(ctx, sink, infoCopy, ProviderMetadata{PackageSchema: schema}) }, })