Skip to content

Commit

Permalink
Avoid a partially nil host as sink in test muxed providers
Browse files Browse the repository at this point in the history
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/).
  • Loading branch information
iwahbe committed Dec 9, 2024
1 parent 0852d64 commit c1171a0
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
35 changes: 33 additions & 2 deletions pkg/pf/tests/logging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
13 changes: 11 additions & 2 deletions pkg/pf/tfbridge/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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})
},
})
Expand Down

0 comments on commit c1171a0

Please sign in to comment.