|
| 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 lifecycletest |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "sync" |
| 20 | + "sync/atomic" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/blang/semver" |
| 24 | + "github.com/stretchr/testify/assert" |
| 25 | + "github.com/stretchr/testify/require" |
| 26 | + |
| 27 | + . "github.com/pulumi/pulumi/pkg/v3/engine" //nolint:revive |
| 28 | + lt "github.com/pulumi/pulumi/pkg/v3/engine/lifecycletest/framework" |
| 29 | + "github.com/pulumi/pulumi/pkg/v3/resource/deploy/deploytest" |
| 30 | + "github.com/pulumi/pulumi/sdk/v3/go/common/apitype" |
| 31 | + "github.com/pulumi/pulumi/sdk/v3/go/common/diag" |
| 32 | + "github.com/pulumi/pulumi/sdk/v3/go/common/resource/plugin" |
| 33 | + "github.com/pulumi/pulumi/sdk/v3/go/common/workspace" |
| 34 | +) |
| 35 | + |
| 36 | +// countingPluginManager records each plugin the engine considers for up-front installation. The |
| 37 | +// engine looks up every plugin in its install set via GetPluginPath; SkipPluginPreInstall |
| 38 | +// short-circuits that traversal, so the recorded set captures the gap between the two modes. |
| 39 | +type countingPluginManager struct { |
| 40 | + lt.NopPluginManager |
| 41 | + |
| 42 | + mu sync.Mutex |
| 43 | + considered []string |
| 44 | +} |
| 45 | + |
| 46 | +func (m *countingPluginManager) GetPluginPath( |
| 47 | + _ context.Context, _ diag.Sink, plug workspace.PluginDescriptor, _ []workspace.ProjectPlugin, |
| 48 | +) (string, error) { |
| 49 | + version := "" |
| 50 | + if plug.Version != nil { |
| 51 | + version = plug.Version.String() |
| 52 | + } |
| 53 | + m.mu.Lock() |
| 54 | + m.considered = append(m.considered, plug.Name+"@"+version) |
| 55 | + m.mu.Unlock() |
| 56 | + return "installed", nil |
| 57 | +} |
| 58 | + |
| 59 | +func (m *countingPluginManager) Considered() []string { |
| 60 | + m.mu.Lock() |
| 61 | + defer m.mu.Unlock() |
| 62 | + out := make([]string, len(m.considered)) |
| 63 | + copy(out, m.considered) |
| 64 | + return out |
| 65 | +} |
| 66 | + |
| 67 | +// TestSkipPluginPreInstallSkipsUnusedLanguagePackages demonstrates the over-inference that |
| 68 | +// SkipPluginPreInstall avoids: the language host reports every package the program imports as |
| 69 | +// "required", but only the packages whose resources the program actually registers are needed at |
| 70 | +// runtime. Without SkipPluginPreInstall the engine installs every reported package up-front; with |
| 71 | +// SkipPluginPreInstall it installs none, leaving the provider registry to load packages lazily as |
| 72 | +// resources are registered. |
| 73 | +func TestSkipPluginPreInstallSkipsUnusedLanguagePackages(t *testing.T) { |
| 74 | + t.Parallel() |
| 75 | + |
| 76 | + v1 := semver.Version{Major: 1} |
| 77 | + pkgA := workspace.PackageDescriptor{ |
| 78 | + PluginDescriptor: workspace.PluginDescriptor{ |
| 79 | + Name: "pkgA", |
| 80 | + Kind: apitype.ResourcePlugin, |
| 81 | + Version: &v1, |
| 82 | + }, |
| 83 | + } |
| 84 | + pkgB := workspace.PackageDescriptor{ |
| 85 | + PluginDescriptor: workspace.PluginDescriptor{ |
| 86 | + Name: "pkgB", |
| 87 | + Kind: apitype.ResourcePlugin, |
| 88 | + Version: &v1, |
| 89 | + }, |
| 90 | + } |
| 91 | + |
| 92 | + var pkgALoads, pkgBLoads atomic.Int32 |
| 93 | + loaders := []*deploytest.ProviderLoader{ |
| 94 | + deploytest.NewProviderLoader("pkgA", v1, func() (plugin.Provider, error) { |
| 95 | + pkgALoads.Add(1) |
| 96 | + return &deploytest.Provider{}, nil |
| 97 | + }), |
| 98 | + deploytest.NewProviderLoader("pkgB", v1, func() (plugin.Provider, error) { |
| 99 | + pkgBLoads.Add(1) |
| 100 | + return &deploytest.Provider{}, nil |
| 101 | + }), |
| 102 | + } |
| 103 | + |
| 104 | + program := func(_ plugin.RunInfo, monitor *deploytest.ResourceMonitor) error { |
| 105 | + _, err := monitor.RegisterResource("pkgA:m:typA", "resA", true) |
| 106 | + return err |
| 107 | + } |
| 108 | + |
| 109 | + // The language host advertises both pkgA and pkgB as required, even though the program only |
| 110 | + // ever registers a resource of pkgA. |
| 111 | + programF := deploytest.NewLanguageRuntimeF(program, pkgA, pkgB) |
| 112 | + hostF := deploytest.NewPluginHostF(nil, nil, programF, loaders...) |
| 113 | + |
| 114 | + runUpdate := func(skip bool) *countingPluginManager { |
| 115 | + // Reset counters |
| 116 | + pkgALoads.Store(0) |
| 117 | + pkgBLoads.Store(0) |
| 118 | + |
| 119 | + pm := &countingPluginManager{} |
| 120 | + p := <.TestPlan{ |
| 121 | + Options: lt.TestUpdateOptions{ |
| 122 | + UpdateOptions: UpdateOptions{SkipPluginPreInstall: skip}, |
| 123 | + T: t, |
| 124 | + HostF: hostF, |
| 125 | + PluginManager: pm, |
| 126 | + SkipDisplayTests: true, |
| 127 | + }, |
| 128 | + } |
| 129 | + _, err := lt.TestOp(Update).RunStep( |
| 130 | + p.GetProject(), p.GetTarget(t, nil), p.Options, false, p.BackendClient, nil, "") |
| 131 | + require.NoError(t, err) |
| 132 | + return pm |
| 133 | + } |
| 134 | + |
| 135 | + // Without SkipPluginPreInstall the engine considers both packages reported by the language host |
| 136 | + // for install, even though pkgB is never used at runtime. |
| 137 | + pm := runUpdate(false) |
| 138 | + assert.ElementsMatch(t, []string{"pkgA@1.0.0", "pkgB@1.0.0"}, pm.Considered()) |
| 139 | + assert.EqualValues(t, 1, pkgALoads.Load(), "pkgA provider must be loaded to register resA") |
| 140 | + assert.EqualValues(t, 0, pkgBLoads.Load(), "pkgB provider is never used") |
| 141 | + |
| 142 | + // With SkipPluginPreInstall the engine considers no plugins for up-front install. pkgA is still |
| 143 | + // loaded on demand by the provider registry when the program registers a resource of pkgA. |
| 144 | + pm = runUpdate(true) |
| 145 | + assert.Empty(t, pm.Considered()) |
| 146 | + assert.EqualValues(t, 1, pkgALoads.Load(), "pkgA must still be loaded lazily") |
| 147 | + assert.EqualValues(t, 0, pkgBLoads.Load(), "pkgB still unused") |
| 148 | +} |
0 commit comments