pulumi/pkg/testing/integration/program_test.go

201 lines
6.1 KiB
Go
Raw Permalink Normal View History

// Copyright 2016-2021, Pulumi Corporation.
2018-05-22 19:43:36 +00:00
//
// 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.
package integration
import (
2018-01-06 01:40:41 +00:00
"os"
"os/exec"
"path/filepath"
2022-12-13 20:21:36 +00:00
"runtime"
"testing"
"github.com/pulumi/pulumi/sdk/v3/go/common/testing/iotest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
2018-01-06 01:40:41 +00:00
// Test that RunCommand writes the command's output to a log file.
func TestRunCommandLog(t *testing.T) {
t.Parallel()
2018-01-06 01:40:41 +00:00
// Try to find node on the path. We need a program to run, and node is probably
// available on all platforms where we're testing. If it's not found, skip the test.
node, err := exec.LookPath("node")
if err != nil {
t.Skip("Couldn't find Node on PATH")
}
testw := iotest.LogWriter(t)
2018-01-06 01:40:41 +00:00
opts := &ProgramTestOptions{
Stdout: testw,
Stderr: testw,
2018-01-06 01:40:41 +00:00
}
tempdir := t.TempDir()
2018-01-06 01:40:41 +00:00
args := []string{node, "-e", "console.log('output from node');"}
err = RunCommand(t, "node", args, tempdir, opts)
assert.NoError(t, err)
2018-01-06 01:40:41 +00:00
matches, err := filepath.Glob(filepath.Join(tempdir, commandOutputFolderName, "node.*"))
assert.NoError(t, err)
2018-01-06 01:40:41 +00:00
assert.Equal(t, 1, len(matches))
output, err := os.ReadFile(matches[0])
assert.NoError(t, err)
2018-01-06 01:40:41 +00:00
assert.Equal(t, "output from node\n", string(output))
}
2020-08-31 23:08:15 +00:00
func TestSanitizedPkg(t *testing.T) {
t.Parallel()
2020-09-01 01:57:05 +00:00
v2 := getSanitizedModulePath("github.com/pulumi/pulumi-docker/sdk/v2")
2020-08-31 23:08:15 +00:00
assert.Equal(t, "github.com/pulumi/pulumi-docker/sdk", v2)
2020-09-01 01:57:05 +00:00
v3 := getSanitizedModulePath("github.com/pulumi/pulumi-aws/sdk/v3")
2020-08-31 23:08:15 +00:00
assert.Equal(t, "github.com/pulumi/pulumi-aws/sdk", v3)
2020-09-01 01:57:05 +00:00
nonVersion := getSanitizedModulePath("github.com/pulumi/pulumi-auth/sdk")
2020-08-31 23:08:15 +00:00
assert.Equal(t, "github.com/pulumi/pulumi-auth/sdk", nonVersion)
}
func TestDepRootCalc(t *testing.T) {
t.Parallel()
2020-08-31 23:08:15 +00:00
var dep string
dep = getRewritePath("github.com/pulumi/pulumi-docker/sdk/v2", "/gopath", "")
assert.Equal(t, "/gopath/src/github.com/pulumi/pulumi-docker/sdk", filepath.ToSlash(dep))
2020-08-31 23:08:15 +00:00
dep = getRewritePath("github.com/pulumi/pulumi-gcp/sdk/v3", "/gopath", "/my-go-src")
assert.Equal(t, "/my-go-src/pulumi-gcp/sdk", filepath.ToSlash(dep))
2020-08-31 23:08:15 +00:00
2020-09-01 01:54:14 +00:00
dep = getRewritePath("github.com/example/foo/pkg/v2", "/gopath", "/my-go-src")
assert.Equal(t, "/my-go-src/foo/pkg", filepath.ToSlash(dep))
2020-09-01 01:54:14 +00:00
dep = getRewritePath("github.com/example/foo/v2", "/gopath", "/my-go-src")
assert.Equal(t, "/my-go-src/foo", filepath.ToSlash(dep))
2020-09-01 01:54:14 +00:00
2020-09-01 02:01:23 +00:00
dep = getRewritePath("github.com/example/foo", "/gopath", "/my-go-src")
assert.Equal(t, "/my-go-src/foo", filepath.ToSlash(dep))
2020-09-01 02:01:23 +00:00
dep = getRewritePath("github.com/pulumi/pulumi-auth0/sdk", "gopath", "/my-go-src")
assert.Equal(t, "/my-go-src/pulumi-auth0/sdk", filepath.ToSlash(dep))
2020-08-31 23:08:15 +00:00
}
func TestGoModEdits(t *testing.T) {
t.Parallel()
cwd, err := os.Getwd()
require.NoError(t, err)
depRoot := filepath.Clean(filepath.Join(cwd, "../../../.."))
gopath, err := GoPath()
require.NoError(t, err)
// Were we to commit this go.mod file, `make tidy` would fail, and we should keep the complexity
// of tests constrained to the test itself.
// The dir must be a relative path as well, so we make it relative to cwd (which is absolute).
// If we're on Windows and the drive letters don't match, we can't use a relative path, so we skip the test.
var skipInvalidPathTest bool
badModDir := t.TempDir()
if runtime.GOOS == "windows" && cwd[0] != badModDir[0] {
skipInvalidPathTest = true
} else {
badModDir, err = filepath.Rel(cwd, badModDir)
require.NoError(t, err)
badModFile := filepath.Join(badModDir, "go.mod")
err = os.WriteFile(badModFile, []byte(`
# invalid go.mod
all: Reformat with gofumpt Per team discussion, switching to gofumpt. [gofumpt][1] is an alternative, stricter alternative to gofmt. It addresses other stylistic concerns that gofmt doesn't yet cover. [1]: https://github.com/mvdan/gofumpt See the full list of [Added rules][2], but it includes: - Dropping empty lines around function bodies - Dropping unnecessary variable grouping when there's only one variable - Ensuring an empty line between multi-line functions - simplification (`-s` in gofmt) is always enabled - Ensuring multi-line function signatures end with `) {` on a separate line. [2]: https://github.com/mvdan/gofumpt#Added-rules gofumpt is stricter, but there's no lock-in. All gofumpt output is valid gofmt output, so if we decide we don't like it, it's easy to switch back without any code changes. gofumpt support is built into the tooling we use for development so this won't change development workflows. - golangci-lint includes a gofumpt check (enabled in this PR) - gopls, the LSP for Go, includes a gofumpt option (see [installation instrutions][3]) [3]: https://github.com/mvdan/gofumpt#installation This change was generated by running: ```bash gofumpt -w $(rg --files -g '*.go' | rg -v testdata | rg -v compilation_error) ``` The following files were manually tweaked afterwards: - pkg/cmd/pulumi/stack_change_secrets_provider.go: one of the lines overflowed and had comments in an inconvenient place - pkg/cmd/pulumi/destroy.go: `var x T = y` where `T` wasn't necessary - pkg/cmd/pulumi/policy_new.go: long line because of error message - pkg/backend/snapshot_test.go: long line trying to assign three variables in the same assignment I have included mention of gofumpt in the CONTRIBUTING.md.
2023-03-03 16:36:39 +00:00
`), 0o600)
require.NoError(t, err)
}
2022-12-13 20:21:36 +00:00
errNotExists := "no such file or directory"
if runtime.GOOS == "windows" {
errNotExists = "The system cannot find the path specified"
}
tests := []struct {
name string
dep string
expectedValue string
expectedError string
skip bool
}{
{
name: "valid-path",
dep: "../../../sdk",
expectedValue: "github.com/pulumi/pulumi/sdk/v3=" + filepath.Join(cwd, "../../../sdk"),
},
{
name: "invalid-path-non-existent",
dep: "../../../.tmp.non-existent-dir",
2022-12-13 20:21:36 +00:00
expectedError: errNotExists,
},
{
name: "invalid-path-bad-go-mod",
dep: badModDir,
expectedError: "error parsing go.mod",
skip: skipInvalidPathTest,
},
{
name: "valid-module-name",
dep: "github.com/pulumi/pulumi/sdk/v3",
expectedValue: "github.com/pulumi/pulumi/sdk/v3=" + filepath.Join(cwd, "../../../sdk"),
},
{
name: "valid-module-name-version-skew",
dep: "github.com/pulumi/pulumi/sdk",
expectedValue: "github.com/pulumi/pulumi/sdk=" + filepath.Join(cwd, "../../../sdk"),
},
{
name: "valid-rel-path",
dep: "github.com/pulumi/pulumi/sdk/v3=../../../sdk",
expectedValue: "github.com/pulumi/pulumi/sdk/v3=" + filepath.Join(cwd, "../../../sdk"),
},
{
name: "valid-rel-path-version-skew",
dep: "github.com/pulumi/pulumi/sdk=../../../sdk",
expectedValue: "github.com/pulumi/pulumi/sdk=" + filepath.Join(cwd, "../../../sdk"),
},
{
name: "invalid-rel-path",
dep: "github.com/pulumi/pulumi/pkg=../../../sdk",
expectedError: "found module path with prefix github.com/pulumi/pulumi/sdk, expected github.com/pulumi/pulumi/pkg",
},
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
if test.skip {
t.Skip()
}
editStr, err := getEditStr(test.dep, gopath, depRoot)
if test.expectedError != "" {
assert.ErrorContains(t, err, test.expectedError)
} else {
assert.NoError(t, err)
assert.Equal(t, test.expectedValue, editStr)
}
})
}
}