|
| 1 | +// Copyright 2026, Pulumi Corporation. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package schema |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "os" |
| 20 | + "os/exec" |
| 21 | + "path/filepath" |
| 22 | + "runtime" |
| 23 | + "testing" |
| 24 | + |
| 25 | + "github.com/stretchr/testify/assert" |
| 26 | + "github.com/stretchr/testify/require" |
| 27 | + |
| 28 | + pkghost "github.com/pulumi/pulumi/pkg/v3/host" |
| 29 | + "github.com/pulumi/pulumi/sdk/v3/go/common/apitype" |
| 30 | + "github.com/pulumi/pulumi/sdk/v3/go/common/env" |
| 31 | + "github.com/pulumi/pulumi/sdk/v3/go/common/resource" |
| 32 | + "github.com/pulumi/pulumi/sdk/v3/go/common/resource/plugin" |
| 33 | + "github.com/pulumi/pulumi/sdk/v3/go/common/testing/diagtest" |
| 34 | + "github.com/pulumi/pulumi/sdk/v3/go/common/tokens" |
| 35 | + "github.com/pulumi/pulumi/sdk/v3/go/common/workspace" |
| 36 | +) |
| 37 | + |
| 38 | +// TestLoaderServerFromContext_RealProvider verifies the full loader handshake loop against a real provider binary, |
| 39 | +// and that the loader target stays usable for ordinary resource operations rather than just at handshake time: |
| 40 | +// |
| 41 | +// 1. The host boots the test provider and sends it a loader target as part of the handshake. |
| 42 | +// 2. A Create on the provider dials that target and asks the engine's loader for the "loadtest" schema. |
| 43 | +// 3. The engine's loader boots a second instance of the same provider and retrieves the schema it serves via |
| 44 | +// GetSchema, which the first instance returns as the created resource's state. |
| 45 | +// |
| 46 | +// Uses t.Setenv, so it cannot be parallel. |
| 47 | +func TestLoaderServerFromContext_RealProvider(t *testing.T) { |
| 48 | + // Build the test provider into the plugin layout of a fake PULUMI_HOME, where both the host's plugin loader and |
| 49 | + // the schema loader's plugin resolution will find it. |
| 50 | + home := t.TempDir() |
| 51 | + binName := "pulumi-resource-loadtest" |
| 52 | + if runtime.GOOS == "windows" { |
| 53 | + binName += ".exe" |
| 54 | + } |
| 55 | + pluginDir := filepath.Join(home, "plugins", "resource-loadtest-v1.0.0") |
| 56 | + require.NoError(t, os.MkdirAll(pluginDir, 0o755)) |
| 57 | + |
| 58 | + build := exec.Command("go", "build", "-o", filepath.Join(pluginDir, binName), "./testdata/loader-provider") |
| 59 | + build.Env = append(os.Environ(), "CGO_ENABLED=0") |
| 60 | + out, err := build.CombinedOutput() |
| 61 | + require.NoError(t, err, "building test provider: %s", out) |
| 62 | + |
| 63 | + t.Setenv("PULUMI_HOME", home) |
| 64 | + |
| 65 | + sink := diagtest.LogSink(t) |
| 66 | + pluginHost, err := pkghost.New(context.WithoutCancel(t.Context()), sink, sink, nil, nil, |
| 67 | + NewLoaderServerFromContext, nil) |
| 68 | + require.NoError(t, err) |
| 69 | + defer func() { require.NoError(t, pluginHost.Close()) }() |
| 70 | + pctx, err := plugin.NewContext(t.Context(), sink, sink, pluginHost, nil, t.TempDir(), nil, false, nil) |
| 71 | + require.NoError(t, err) |
| 72 | + defer pctx.Close() |
| 73 | + |
| 74 | + p, err := pctx.Host.Provider(pctx, workspace.PluginDescriptor{ |
| 75 | + Name: "loadtest", |
| 76 | + Kind: apitype.ResourcePlugin, |
| 77 | + }, env.Global()) |
| 78 | + require.NoError(t, err) |
| 79 | + |
| 80 | + typ := tokens.Type("pulumi:providers:loadtest") |
| 81 | + _, err = p.Configure(t.Context(), plugin.ConfigureRequest{Type: &typ}) |
| 82 | + require.NoError(t, err) |
| 83 | + |
| 84 | + res, err := p.Create(t.Context(), plugin.CreateRequest{ |
| 85 | + URN: resource.NewURN("test", "test", "", "loadtest:index:Res", "res"), |
| 86 | + Properties: resource.PropertyMap{}, |
| 87 | + }) |
| 88 | + require.NoError(t, err) |
| 89 | + assert.Equal(t, resource.PropertyMap{ |
| 90 | + "schema": resource.NewProperty(`{"name":"loadtest","version":"1.0.0"}`), |
| 91 | + }, res.Properties) |
| 92 | +} |
0 commit comments