2021-12-02 00:12:28 +00:00
|
|
|
// Copyright 2016-2021, Pulumi Corporation.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2020-04-06 19:30:40 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-04-21 00:26:18 +00:00
|
|
|
"context"
|
2023-04-18 22:47:13 +00:00
|
|
|
"flag"
|
2021-12-02 00:12:28 +00:00
|
|
|
"os"
|
feat(go/host): Support vendored dependencies
The Go language host cannot resolve dependencies or plugins if a Pulumi
program vendors its dependencies.
BACKGROUND
The GetRequiredPlugins and GetProgramDependencies methods of the Go
language host rely on the following two commands:
go list -m -mod=mod all
go list -m -mod=mod ...
# '...' means current module and its descendants
GetRequiredPlugins additionally searches the source directories for each
returned module for pulumi-plugin.json files at a pre-determined paths.
$module/pulumi-plugin.json
$module/go/pulumi-plugin.json
$module/go/*/pulumi-plugin.json
This works for most Pulumi programs, except those that vendor private
dependencies with 'go mod vendor'.
For those programs, the above commands fail because -mod=mod forces them
to run in module mode, and their private dependencies are not accessible
in module mode (because they are not exposed publicly).
We use the -mod=mod flag to force 'go list' to run in module mode
because otherwise, it will automatically use vendor mode if a vendor
directory is present. However, in vendor mode, the two 'go list'
commands above are not supported.
The following links add more context on why, but in short:
vendor does not have enough information for the general 'go list'.
- https://stackoverflow.com/a/60660593,
- https://github.com/golang/go/issues/35589#issuecomment-554488544
In short,
- list all with -mod=mod fails because the dependency is private
- list without -mod=mod will use vendor mode
- vendor mode doesn't support the listing all
SOLUTION
Drop the -mod=mod flag so that 'go list' can decide whether to run in
module mode or vendor mode.
However, instead of running it with 'all' or '...',
pass in a list of dependencies extracted from the go.mod.
go list -m import/path1 import/path2 # ...
This operation is completely offline in vendor mode
so it can list information about private dependencies too.
This alone isn't enough though because in vendor mode,
the JSON output does not include the module root directory.
E.g.
% go list -mod=vendor -json -m github.com/pulumi/pulumi/sdk/v3
{
"Path": "github.com/pulumi/pulumi/sdk/v3",
"Version": "v3.55.0",
"GoVersion": "1.18"
}
# Versus
% go list -mod=mod -json -m github.com/pulumi/pulumi/sdk/v3
{
"Path": "github.com/pulumi/pulumi/sdk/v3",
"Version": "v3.55.0",
"Time": "2023-02-14T11:04:22Z",
"Dir": "[...]/go/pkg/mod/github.com/pulumi/pulumi/sdk/v3@v3.55.0",
"GoMod": "[...]/go/pkg/mod/cache/download/github.com/pulumi/pulumi/sdk/v3/@v/v3.55.0.mod",
"GoVersion": "1.18"
}
Therefore, we have to manually calculate the path for each module root.
That's easy enough: vendor/$importPath.
Lastly, since GetProgramDependencies only needs a dependency list,
it now extracts information from the go.mod without calling 'go list'.
TESTING
Adds a variant of the test added in #12715 that verifies the
functionality with vendoring. It removes the sources for the
dependencies to simulate private dependencies. The new test fails
without the accompanying change.
The fix was further manually verified against the reproduction included
in #12526.
% cd go-output
% pulumi plugin rm -a -y
% pulumi preview
Previewing update (abhinav):
Downloading plugin: 15.19 MiB / 15.19 MiB [=========================] 100.00% 0s
[resource plugin random-4.8.2] installing
Type Name Plan
+ pulumi:pulumi:Stack go-output-abhinav create
+ └─ random:index:RandomId rrr create
Resources:
+ 2 to create
% pulumi plugin ls
NAME KIND VERSION SIZE INSTALLED LAST USED
random resource 4.8.2 33 MB 26 seconds ago 26 seconds ago
TOTAL plugin cache size: 33 MB
Note that the version of random (4.8.2) is what's specified in the
go.mod, not the latest release (v4.12.1).
% grep pulumi-random go.mod
github.com/pulumi/pulumi-random/sdk/v4 v4.8.2
With the plugin downloaded, I ran this again without an internet
connection.
% pulumi preview
Previewing update (abhinav):
Type Name Plan
+ pulumi:pulumi:Stack go-output-abhinav create
+ └─ random:index:RandomId rrr create
Resources:
+ 2 to create
This means that if the dependencies are vendored, and the plugin is
already available, we won't make additional network requests, which also
addresses #7089.
Resolves #12526
Resolves #7089
2023-04-20 23:27:39 +00:00
|
|
|
"os/exec"
|
2021-12-02 00:12:28 +00:00
|
|
|
"path/filepath"
|
2020-04-06 19:30:40 +00:00
|
|
|
"testing"
|
2023-04-21 00:26:18 +00:00
|
|
|
"time"
|
2020-04-06 19:30:40 +00:00
|
|
|
|
2021-12-02 00:12:28 +00:00
|
|
|
"github.com/pulumi/pulumi/sdk/v3/go/common/resource/plugin"
|
2023-04-18 22:47:13 +00:00
|
|
|
"github.com/pulumi/pulumi/sdk/v3/go/common/testing/iotest"
|
2023-04-21 00:26:18 +00:00
|
|
|
"github.com/pulumi/pulumi/sdk/v3/go/common/util/fsutil"
|
2023-11-08 18:52:28 +00:00
|
|
|
pulumirpc "github.com/pulumi/pulumi/sdk/v3/go/proto/go"
|
2020-04-06 19:30:40 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2023-04-21 00:26:18 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-04-06 19:30:40 +00:00
|
|
|
)
|
|
|
|
|
2023-04-18 22:47:13 +00:00
|
|
|
func TestParseRunParams(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
desc string
|
|
|
|
give []string
|
|
|
|
want runParams
|
|
|
|
wantErr string // non-empty if we expect an error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "no arguments",
|
|
|
|
wantErr: "missing required engine RPC address argument",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "no options",
|
|
|
|
give: []string{"localhost:1234"},
|
|
|
|
want: runParams{
|
|
|
|
engineAddress: "localhost:1234",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "binary buildTarget exclusivity",
|
|
|
|
give: []string{"-binary", "foo", "-buildTarget=bar"},
|
|
|
|
wantErr: "binary and buildTarget cannot both be specified",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "tracing",
|
|
|
|
give: []string{"-tracing", "foo.trace", "localhost:1234"},
|
|
|
|
want: runParams{
|
|
|
|
tracing: "foo.trace",
|
|
|
|
engineAddress: "localhost:1234",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "binary",
|
|
|
|
give: []string{"-binary", "foo", "localhost:1234"},
|
|
|
|
want: runParams{
|
|
|
|
binary: "foo",
|
|
|
|
engineAddress: "localhost:1234",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "buildTarget",
|
|
|
|
give: []string{"-buildTarget", "foo", "localhost:1234"},
|
|
|
|
want: runParams{
|
|
|
|
buildTarget: "foo",
|
|
|
|
engineAddress: "localhost:1234",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "root",
|
|
|
|
give: []string{"-root", "path/to/root", "localhost:1234"},
|
|
|
|
want: runParams{
|
|
|
|
root: "path/to/root",
|
|
|
|
engineAddress: "localhost:1234",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "unknown option",
|
|
|
|
give: []string{"-unknown-option", "bar", "localhost:1234"},
|
|
|
|
wantErr: "flag provided but not defined: -unknown-option",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
tt := tt
|
|
|
|
t.Run(tt.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
// Use a FlagSet with ContinueOnError for each case
|
|
|
|
// instead of using the global flag set.
|
|
|
|
//
|
|
|
|
// The global flag set uses flag.ExitOnError,
|
|
|
|
// so it cannot validate error cases during tests.
|
|
|
|
fset := flag.NewFlagSet(t.Name(), flag.ContinueOnError)
|
|
|
|
fset.SetOutput(iotest.LogWriter(t))
|
|
|
|
|
|
|
|
got, err := parseRunParams(fset, tt.give)
|
|
|
|
if tt.wantErr != "" {
|
|
|
|
assert.ErrorContains(t, err, tt.wantErr)
|
|
|
|
} else {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, &tt.want, got)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-06 19:30:40 +00:00
|
|
|
func TestGetPlugin(t *testing.T) {
|
2022-03-04 08:17:41 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2021-12-02 00:12:28 +00:00
|
|
|
cases := []struct {
|
|
|
|
Name string
|
|
|
|
Mod *modInfo
|
|
|
|
Expected *pulumirpc.PluginDependency
|
|
|
|
ShouldErr bool
|
2021-12-06 21:10:30 +00:00
|
|
|
JSON *plugin.PulumiPluginJSON
|
|
|
|
JSONPath string
|
2021-12-02 00:12:28 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
Name: "valid-pulumi-mod",
|
|
|
|
Mod: &modInfo{
|
|
|
|
Path: "github.com/pulumi/pulumi-aws/sdk",
|
|
|
|
Version: "v1.29.0",
|
|
|
|
},
|
|
|
|
Expected: &pulumirpc.PluginDependency{
|
|
|
|
Name: "aws",
|
|
|
|
Version: "v1.29.0",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "pulumi-pseduo-version-plugin",
|
|
|
|
Mod: &modInfo{
|
|
|
|
Path: "github.com/pulumi/pulumi-aws/sdk",
|
|
|
|
Version: "v1.29.1-0.20200403140640-efb5e2a48a86",
|
|
|
|
},
|
|
|
|
Expected: &pulumirpc.PluginDependency{
|
|
|
|
Name: "aws",
|
|
|
|
Version: "v1.29.0",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "non-pulumi-mod",
|
|
|
|
Mod: &modInfo{
|
|
|
|
Path: "github.com/moolumi/pulumi-aws/sdk",
|
|
|
|
Version: "v1.29.0",
|
|
|
|
},
|
|
|
|
ShouldErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "invalid-version-module",
|
|
|
|
Mod: &modInfo{
|
|
|
|
Path: "github.com/pulumi/pulumi-aws/sdk",
|
|
|
|
Version: "42-42-42",
|
|
|
|
},
|
|
|
|
ShouldErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "pulumi-pulumi-mod",
|
|
|
|
Mod: &modInfo{
|
|
|
|
Path: "github.com/pulumi/pulumi/sdk",
|
|
|
|
Version: "v1.14.0",
|
|
|
|
},
|
|
|
|
ShouldErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "beta-pulumi-module",
|
|
|
|
Mod: &modInfo{
|
|
|
|
Path: "github.com/pulumi/pulumi-aws/sdk",
|
|
|
|
Version: "v2.0.0-beta.1",
|
|
|
|
},
|
|
|
|
Expected: &pulumirpc.PluginDependency{
|
|
|
|
Name: "aws",
|
|
|
|
Version: "v2.0.0-beta.1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "non-zero-patch-module", Mod: &modInfo{
|
|
|
|
Path: "github.com/pulumi/pulumi-kubernetes/sdk",
|
|
|
|
Version: "v1.5.8",
|
|
|
|
},
|
|
|
|
Expected: &pulumirpc.PluginDependency{
|
|
|
|
Name: "kubernetes",
|
|
|
|
Version: "v1.5.8",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "pulumiplugin",
|
|
|
|
Mod: &modInfo{
|
|
|
|
Path: "github.com/me/myself/i",
|
|
|
|
Version: "invalid-Version",
|
|
|
|
},
|
|
|
|
Expected: &pulumirpc.PluginDependency{
|
|
|
|
Name: "thing1",
|
|
|
|
Version: "v1.2.3",
|
|
|
|
Server: "myserver.com",
|
|
|
|
},
|
2021-12-06 21:10:30 +00:00
|
|
|
JSON: &plugin.PulumiPluginJSON{
|
2021-12-02 00:12:28 +00:00
|
|
|
Resource: true,
|
|
|
|
Name: "thing1",
|
|
|
|
Version: "v1.2.3",
|
|
|
|
Server: "myserver.com",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "non-resource",
|
|
|
|
Mod: &modInfo{},
|
|
|
|
ShouldErr: true,
|
2021-12-06 21:10:30 +00:00
|
|
|
JSON: &plugin.PulumiPluginJSON{
|
2021-12-02 00:12:28 +00:00
|
|
|
Resource: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "missing-pulumiplugin",
|
|
|
|
Mod: &modInfo{
|
|
|
|
Dir: "/not/real",
|
|
|
|
},
|
|
|
|
ShouldErr: true,
|
2021-12-06 21:10:30 +00:00
|
|
|
JSON: &plugin.PulumiPluginJSON{
|
2021-12-02 00:12:28 +00:00
|
|
|
Name: "thing2",
|
|
|
|
Version: "v1.2.3",
|
|
|
|
},
|
|
|
|
},
|
2021-12-06 21:10:30 +00:00
|
|
|
{
|
|
|
|
Name: "pulumiplugin-go-lookup",
|
|
|
|
Mod: &modInfo{
|
|
|
|
Path: "github.com/me/myself",
|
|
|
|
Version: "v1.2.3",
|
|
|
|
},
|
|
|
|
JSON: &plugin.PulumiPluginJSON{
|
|
|
|
Name: "name",
|
|
|
|
Resource: true,
|
|
|
|
},
|
|
|
|
JSONPath: "go",
|
|
|
|
Expected: &pulumirpc.PluginDependency{
|
|
|
|
Name: "name",
|
|
|
|
Version: "v1.2.3",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "pulumiplugin-go-name-lookup",
|
|
|
|
Mod: &modInfo{
|
|
|
|
Path: "github.com/me/myself",
|
|
|
|
Version: "v1.2.3",
|
|
|
|
},
|
|
|
|
JSON: &plugin.PulumiPluginJSON{
|
|
|
|
Name: "name",
|
|
|
|
Resource: true,
|
|
|
|
},
|
|
|
|
JSONPath: filepath.Join("go", "name"),
|
|
|
|
Expected: &pulumirpc.PluginDependency{
|
|
|
|
Name: "name",
|
|
|
|
Version: "v1.2.3",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "pulumiplugin-nested-too-deep",
|
|
|
|
Mod: &modInfo{
|
|
|
|
Path: "path.com/here",
|
|
|
|
Version: "v0.0",
|
|
|
|
},
|
|
|
|
JSONPath: filepath.Join("go", "valid", "invalid"),
|
|
|
|
JSON: &plugin.PulumiPluginJSON{
|
|
|
|
Name: "name",
|
|
|
|
Resource: true,
|
|
|
|
},
|
|
|
|
ShouldErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "nested-wrong-folder",
|
|
|
|
Mod: &modInfo{
|
|
|
|
Path: "path.com/here",
|
|
|
|
Version: "v0.0",
|
|
|
|
},
|
|
|
|
JSONPath: filepath.Join("invalid", "valid"),
|
|
|
|
JSON: &plugin.PulumiPluginJSON{
|
|
|
|
Name: "name",
|
|
|
|
Resource: true,
|
|
|
|
},
|
|
|
|
ShouldErr: true,
|
|
|
|
},
|
2020-04-06 19:30:40 +00:00
|
|
|
}
|
|
|
|
|
2021-12-02 00:12:28 +00:00
|
|
|
for _, c := range cases {
|
2022-03-04 08:17:41 +00:00
|
|
|
c := c
|
2021-12-02 00:12:28 +00:00
|
|
|
t.Run(c.Name, func(t *testing.T) {
|
2022-03-04 08:17:41 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2021-12-02 00:12:28 +00:00
|
|
|
cwd := t.TempDir()
|
2023-07-11 22:00:40 +00:00
|
|
|
if c.Mod.Dir == "" {
|
|
|
|
c.Mod.Dir = cwd
|
|
|
|
}
|
2021-12-06 21:10:30 +00:00
|
|
|
if c.JSON != nil {
|
|
|
|
path := filepath.Join(cwd, c.JSONPath)
|
2023-03-03 16:36:39 +00:00
|
|
|
err := os.MkdirAll(path, 0o700)
|
2022-10-09 14:58:33 +00:00
|
|
|
assert.NoErrorf(t, err, "Failed to setup test folder %s", path)
|
2021-12-06 21:10:30 +00:00
|
|
|
bytes, err := c.JSON.JSON()
|
2021-12-15 18:41:44 +00:00
|
|
|
assert.NoError(t, err, "Failed to setup test pulumi-plugin.json")
|
2023-03-03 16:36:39 +00:00
|
|
|
err = os.WriteFile(filepath.Join(path, "pulumi-plugin.json"), bytes, 0o600)
|
2021-12-15 18:41:44 +00:00
|
|
|
assert.NoError(t, err, "Failed to write pulumi-plugin.json")
|
2021-12-02 00:12:28 +00:00
|
|
|
}
|
feat(go/host): Support vendored dependencies
The Go language host cannot resolve dependencies or plugins if a Pulumi
program vendors its dependencies.
BACKGROUND
The GetRequiredPlugins and GetProgramDependencies methods of the Go
language host rely on the following two commands:
go list -m -mod=mod all
go list -m -mod=mod ...
# '...' means current module and its descendants
GetRequiredPlugins additionally searches the source directories for each
returned module for pulumi-plugin.json files at a pre-determined paths.
$module/pulumi-plugin.json
$module/go/pulumi-plugin.json
$module/go/*/pulumi-plugin.json
This works for most Pulumi programs, except those that vendor private
dependencies with 'go mod vendor'.
For those programs, the above commands fail because -mod=mod forces them
to run in module mode, and their private dependencies are not accessible
in module mode (because they are not exposed publicly).
We use the -mod=mod flag to force 'go list' to run in module mode
because otherwise, it will automatically use vendor mode if a vendor
directory is present. However, in vendor mode, the two 'go list'
commands above are not supported.
The following links add more context on why, but in short:
vendor does not have enough information for the general 'go list'.
- https://stackoverflow.com/a/60660593,
- https://github.com/golang/go/issues/35589#issuecomment-554488544
In short,
- list all with -mod=mod fails because the dependency is private
- list without -mod=mod will use vendor mode
- vendor mode doesn't support the listing all
SOLUTION
Drop the -mod=mod flag so that 'go list' can decide whether to run in
module mode or vendor mode.
However, instead of running it with 'all' or '...',
pass in a list of dependencies extracted from the go.mod.
go list -m import/path1 import/path2 # ...
This operation is completely offline in vendor mode
so it can list information about private dependencies too.
This alone isn't enough though because in vendor mode,
the JSON output does not include the module root directory.
E.g.
% go list -mod=vendor -json -m github.com/pulumi/pulumi/sdk/v3
{
"Path": "github.com/pulumi/pulumi/sdk/v3",
"Version": "v3.55.0",
"GoVersion": "1.18"
}
# Versus
% go list -mod=mod -json -m github.com/pulumi/pulumi/sdk/v3
{
"Path": "github.com/pulumi/pulumi/sdk/v3",
"Version": "v3.55.0",
"Time": "2023-02-14T11:04:22Z",
"Dir": "[...]/go/pkg/mod/github.com/pulumi/pulumi/sdk/v3@v3.55.0",
"GoMod": "[...]/go/pkg/mod/cache/download/github.com/pulumi/pulumi/sdk/v3/@v/v3.55.0.mod",
"GoVersion": "1.18"
}
Therefore, we have to manually calculate the path for each module root.
That's easy enough: vendor/$importPath.
Lastly, since GetProgramDependencies only needs a dependency list,
it now extracts information from the go.mod without calling 'go list'.
TESTING
Adds a variant of the test added in #12715 that verifies the
functionality with vendoring. It removes the sources for the
dependencies to simulate private dependencies. The new test fails
without the accompanying change.
The fix was further manually verified against the reproduction included
in #12526.
% cd go-output
% pulumi plugin rm -a -y
% pulumi preview
Previewing update (abhinav):
Downloading plugin: 15.19 MiB / 15.19 MiB [=========================] 100.00% 0s
[resource plugin random-4.8.2] installing
Type Name Plan
+ pulumi:pulumi:Stack go-output-abhinav create
+ └─ random:index:RandomId rrr create
Resources:
+ 2 to create
% pulumi plugin ls
NAME KIND VERSION SIZE INSTALLED LAST USED
random resource 4.8.2 33 MB 26 seconds ago 26 seconds ago
TOTAL plugin cache size: 33 MB
Note that the version of random (4.8.2) is what's specified in the
go.mod, not the latest release (v4.12.1).
% grep pulumi-random go.mod
github.com/pulumi/pulumi-random/sdk/v4 v4.8.2
With the plugin downloaded, I ran this again without an internet
connection.
% pulumi preview
Previewing update (abhinav):
Type Name Plan
+ pulumi:pulumi:Stack go-output-abhinav create
+ └─ random:index:RandomId rrr create
Resources:
+ 2 to create
This means that if the dependencies are vendored, and the plugin is
already available, we won't make additional network requests, which also
addresses #7089.
Resolves #12526
Resolves #7089
2023-04-20 23:27:39 +00:00
|
|
|
|
|
|
|
actual, err := c.Mod.getPlugin(t.TempDir())
|
2021-12-02 00:12:28 +00:00
|
|
|
if c.ShouldErr {
|
|
|
|
assert.Error(t, err)
|
|
|
|
} else {
|
|
|
|
// Kind must be resource. We can thus exclude it from the test.
|
|
|
|
if c.Expected.Kind == "" {
|
|
|
|
c.Expected.Kind = "resource"
|
|
|
|
}
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, c.Expected, actual)
|
|
|
|
}
|
|
|
|
})
|
2020-04-06 19:30:40 +00:00
|
|
|
}
|
|
|
|
}
|
2023-04-21 00:26:18 +00:00
|
|
|
|
|
|
|
func TestPluginsAndDependencies_moduleMode(t *testing.T) {
|
2023-04-21 00:48:46 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2023-04-21 00:26:18 +00:00
|
|
|
root := t.TempDir()
|
|
|
|
require.NoError(t,
|
|
|
|
fsutil.CopyFile(root, filepath.Join("testdata", "sample"), nil),
|
|
|
|
"copy test data")
|
|
|
|
|
|
|
|
testPluginsAndDependencies(t, filepath.Join(root, "prog"))
|
|
|
|
}
|
|
|
|
|
feat(go/host): Support vendored dependencies
The Go language host cannot resolve dependencies or plugins if a Pulumi
program vendors its dependencies.
BACKGROUND
The GetRequiredPlugins and GetProgramDependencies methods of the Go
language host rely on the following two commands:
go list -m -mod=mod all
go list -m -mod=mod ...
# '...' means current module and its descendants
GetRequiredPlugins additionally searches the source directories for each
returned module for pulumi-plugin.json files at a pre-determined paths.
$module/pulumi-plugin.json
$module/go/pulumi-plugin.json
$module/go/*/pulumi-plugin.json
This works for most Pulumi programs, except those that vendor private
dependencies with 'go mod vendor'.
For those programs, the above commands fail because -mod=mod forces them
to run in module mode, and their private dependencies are not accessible
in module mode (because they are not exposed publicly).
We use the -mod=mod flag to force 'go list' to run in module mode
because otherwise, it will automatically use vendor mode if a vendor
directory is present. However, in vendor mode, the two 'go list'
commands above are not supported.
The following links add more context on why, but in short:
vendor does not have enough information for the general 'go list'.
- https://stackoverflow.com/a/60660593,
- https://github.com/golang/go/issues/35589#issuecomment-554488544
In short,
- list all with -mod=mod fails because the dependency is private
- list without -mod=mod will use vendor mode
- vendor mode doesn't support the listing all
SOLUTION
Drop the -mod=mod flag so that 'go list' can decide whether to run in
module mode or vendor mode.
However, instead of running it with 'all' or '...',
pass in a list of dependencies extracted from the go.mod.
go list -m import/path1 import/path2 # ...
This operation is completely offline in vendor mode
so it can list information about private dependencies too.
This alone isn't enough though because in vendor mode,
the JSON output does not include the module root directory.
E.g.
% go list -mod=vendor -json -m github.com/pulumi/pulumi/sdk/v3
{
"Path": "github.com/pulumi/pulumi/sdk/v3",
"Version": "v3.55.0",
"GoVersion": "1.18"
}
# Versus
% go list -mod=mod -json -m github.com/pulumi/pulumi/sdk/v3
{
"Path": "github.com/pulumi/pulumi/sdk/v3",
"Version": "v3.55.0",
"Time": "2023-02-14T11:04:22Z",
"Dir": "[...]/go/pkg/mod/github.com/pulumi/pulumi/sdk/v3@v3.55.0",
"GoMod": "[...]/go/pkg/mod/cache/download/github.com/pulumi/pulumi/sdk/v3/@v/v3.55.0.mod",
"GoVersion": "1.18"
}
Therefore, we have to manually calculate the path for each module root.
That's easy enough: vendor/$importPath.
Lastly, since GetProgramDependencies only needs a dependency list,
it now extracts information from the go.mod without calling 'go list'.
TESTING
Adds a variant of the test added in #12715 that verifies the
functionality with vendoring. It removes the sources for the
dependencies to simulate private dependencies. The new test fails
without the accompanying change.
The fix was further manually verified against the reproduction included
in #12526.
% cd go-output
% pulumi plugin rm -a -y
% pulumi preview
Previewing update (abhinav):
Downloading plugin: 15.19 MiB / 15.19 MiB [=========================] 100.00% 0s
[resource plugin random-4.8.2] installing
Type Name Plan
+ pulumi:pulumi:Stack go-output-abhinav create
+ └─ random:index:RandomId rrr create
Resources:
+ 2 to create
% pulumi plugin ls
NAME KIND VERSION SIZE INSTALLED LAST USED
random resource 4.8.2 33 MB 26 seconds ago 26 seconds ago
TOTAL plugin cache size: 33 MB
Note that the version of random (4.8.2) is what's specified in the
go.mod, not the latest release (v4.12.1).
% grep pulumi-random go.mod
github.com/pulumi/pulumi-random/sdk/v4 v4.8.2
With the plugin downloaded, I ran this again without an internet
connection.
% pulumi preview
Previewing update (abhinav):
Type Name Plan
+ pulumi:pulumi:Stack go-output-abhinav create
+ └─ random:index:RandomId rrr create
Resources:
+ 2 to create
This means that if the dependencies are vendored, and the plugin is
already available, we won't make additional network requests, which also
addresses #7089.
Resolves #12526
Resolves #7089
2023-04-20 23:27:39 +00:00
|
|
|
// Test for https://github.com/pulumi/pulumi/issues/12526.
|
|
|
|
// Validates that if a Pulumi program has vendored its dependencies,
|
|
|
|
// the language host can still find the plugin and run the program.
|
|
|
|
func TestPluginsAndDependencies_vendored(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
root := t.TempDir()
|
|
|
|
require.NoError(t,
|
|
|
|
fsutil.CopyFile(root, filepath.Join("testdata", "sample"), nil),
|
|
|
|
"copy test data")
|
|
|
|
|
|
|
|
progDir := filepath.Join(root, "prog")
|
|
|
|
|
|
|
|
// Vendor the dependencies and nuke the sources
|
|
|
|
// to ensure that the language host can only use the vendored version.
|
|
|
|
cmd := exec.Command("go", "mod", "vendor")
|
|
|
|
cmd.Dir = progDir
|
|
|
|
cmd.Stdout = iotest.LogWriter(t)
|
|
|
|
cmd.Stderr = iotest.LogWriter(t)
|
|
|
|
require.NoError(t, cmd.Run(), "vendor dependencies")
|
|
|
|
require.NoError(t, os.RemoveAll(filepath.Join(root, "plugin")))
|
|
|
|
require.NoError(t, os.RemoveAll(filepath.Join(root, "dep")))
|
|
|
|
require.NoError(t, os.RemoveAll(filepath.Join(root, "indirect-dep")))
|
|
|
|
|
|
|
|
testPluginsAndDependencies(t, progDir)
|
|
|
|
}
|
|
|
|
|
2023-05-17 17:40:18 +00:00
|
|
|
// Regression test for https://github.com/pulumi/pulumi/issues/12963.
|
|
|
|
// Verifies that the language host can find plugins and dependencies
|
|
|
|
// when the Pulumi program is in a subdirectory of the project root.
|
|
|
|
func TestPluginsAndDependencies_subdir(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
t.Run("moduleMode", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
root := t.TempDir()
|
|
|
|
require.NoError(t,
|
|
|
|
fsutil.CopyFile(root, filepath.Join("testdata", "sample"), nil),
|
|
|
|
"copy test data")
|
|
|
|
|
|
|
|
testPluginsAndDependencies(t, filepath.Join(root, "prog-subdir", "infra"))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("vendored", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
root := t.TempDir()
|
|
|
|
require.NoError(t,
|
|
|
|
fsutil.CopyFile(root, filepath.Join("testdata", "sample"), nil),
|
|
|
|
"copy test data")
|
|
|
|
|
|
|
|
progDir := filepath.Join(root, "prog-subdir", "infra")
|
|
|
|
|
|
|
|
// Vendor the dependencies and nuke the sources
|
|
|
|
// to ensure that the language host can only use the vendored version.
|
|
|
|
cmd := exec.Command("go", "mod", "vendor")
|
|
|
|
cmd.Dir = progDir
|
|
|
|
cmd.Stdout = iotest.LogWriter(t)
|
|
|
|
cmd.Stderr = iotest.LogWriter(t)
|
|
|
|
require.NoError(t, cmd.Run(), "vendor dependencies")
|
|
|
|
require.NoError(t, os.RemoveAll(filepath.Join(root, "plugin")))
|
|
|
|
require.NoError(t, os.RemoveAll(filepath.Join(root, "dep")))
|
|
|
|
require.NoError(t, os.RemoveAll(filepath.Join(root, "indirect-dep")))
|
|
|
|
|
|
|
|
testPluginsAndDependencies(t, progDir)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-04-21 00:26:18 +00:00
|
|
|
func testPluginsAndDependencies(t *testing.T, progDir string) {
|
2023-04-21 00:48:46 +00:00
|
|
|
host := newLanguageHost("0.0.0.0:0", progDir, "", "", "")
|
2023-04-21 00:26:18 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
t.Run("GetRequiredPlugins", func(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
res, err := host.GetRequiredPlugins(ctx, &pulumirpc.GetRequiredPluginsRequest{
|
|
|
|
Project: "prog",
|
|
|
|
Pwd: progDir,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Len(t, res.Plugins, 1)
|
|
|
|
plug := res.Plugins[0]
|
|
|
|
|
|
|
|
assert.Equal(t, "example", plug.Name, "plugin name")
|
|
|
|
assert.Equal(t, "v1.2.3", plug.Version, "plugin version")
|
|
|
|
assert.Equal(t, "resource", plug.Kind, "plugin kind")
|
|
|
|
assert.Equal(t, "example.com/download", plug.Server, "plugin server")
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("GetProgramDependencies", func(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
res, err := host.GetProgramDependencies(ctx, &pulumirpc.GetProgramDependenciesRequest{
|
|
|
|
Project: "prog",
|
|
|
|
Pwd: progDir,
|
|
|
|
TransitiveDependencies: true,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
gotDeps := make(map[string]string) // name => version
|
|
|
|
for _, dep := range res.Dependencies {
|
|
|
|
gotDeps[dep.Name] = dep.Version
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, map[string]string{
|
|
|
|
"example.com/plugin": "v1.2.3",
|
|
|
|
"example.com/dep": "v1.5.0",
|
|
|
|
"example.com/indirect-dep/v2": "v2.1.0",
|
|
|
|
}, gotDeps)
|
|
|
|
})
|
|
|
|
}
|