pulumi/tests
Abhinav Gupta 33b5ad6527
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-24 09:49:16 -07:00
..
benchmarks/go-alias-norm Bump go-git to v5.6.0 to remove cgo dependency fixing 2023-02-28 16:01:31 -08:00
examples Rename "Smoke" test to "Acceptance" tests 2023-01-30 15:38:37 -05:00
integration feat(go/host): Support vendored dependencies 2023-04-24 09:49:16 -07:00
testprovider all: Reformat with gofumpt 2023-03-03 09:00:24 -08:00
.gitignore ci: Use reduced smoke testing on Windows & macOS targets 2022-09-21 09:55:06 -07:00
README.md Rename "Smoke" test to "Acceptance" tests 2023-01-30 15:38:37 -05:00
about_test.go test: fix regex used to test Go version output in about command. (#10499) 2022-08-29 11:53:03 -07:00
config_test.go filestate: Re-add project support 2023-03-31 13:21:36 -07:00
go.mod feat(go/host): Support vendored dependencies 2023-04-24 09:49:16 -07:00
go.sum feat(go/host): Support vendored dependencies 2023-04-24 09:49:16 -07:00
history_test.go all: Reformat with gofumpt 2023-03-03 09:00:24 -08:00
login_test.go ci: radical idea - what if slow tests & no stdout makes GH consider runner dead? 2022-03-06 14:52:13 -08:00
main_test.go [breaking] Changing the version of go.mod in sdk / pkg to be v3 2021-04-14 19:32:18 +01:00
remote_test.go all: Reformat with gofumpt 2023-03-03 09:00:24 -08:00
roundtrip_test.go all: Reformat with gofumpt 2023-03-03 09:00:24 -08:00
stack_test.go filestate: Re-add project support 2023-03-31 13:21:36 -07:00

README.md

Integration Tests

This module provides integration tests for the Pulumi CLI.

The tests can be run via:

make test_all

Usage of Go build tags

In order to speed up integration tests in GitHub actions, Go build tags are used to conditionally compile the desired test cases.

// integration_nodejs_test.go
//go:build (nodejs || all) && !xplatform-acceptance

// integration_nodejs_acceptance_test.go
//go:build nodejs || all