pulumi/sdk/python/cmd/pulumi-language-python/main_test.go

282 lines
7.7 KiB
Go
Raw Normal View History

// 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.
package main
import (
"context"
"encoding/json"
"os"
"path/filepath"
"testing"
"github.com/pulumi/pulumi/sdk/v3/python/toolchain"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestDeterminePluginVersion(t *testing.T) {
t.Parallel()
tests := []struct {
input string
expected string
err string
}{
{
input: "0.1",
expected: "0.1.0",
},
{
input: "1.0",
expected: "1.0.0",
},
{
input: "1.0.0",
expected: "1.0.0",
},
{
input: "",
err: "cannot parse empty string",
},
{
input: "4.3.2.1",
expected: "4.3.2.1",
},
{
input: " 1 . 2 . 3 ",
err: `' 1 . 2 . 3 ' still unparsed`,
},
{
input: "2.1a123456789",
expected: "2.1.0-alpha.123456789",
},
{
input: "2.14.0a1605583329",
expected: "2.14.0-alpha.1605583329",
},
{
input: "1.2.3b123456",
expected: "1.2.3-beta.123456",
},
{
input: "3.2.1rc654321",
expected: "3.2.1-rc.654321",
},
{
input: "1.2.3dev7890",
err: "'dev7890' still unparsed",
},
{
input: "1.2.3.dev456",
expected: "1.2.3+dev456",
},
{
input: "1.",
err: "'.' still unparsed",
},
{
input: "3.2.post32",
expected: "3.2.0+post32",
},
{
input: "0.3.0b8",
expected: "0.3.0-beta.8",
},
{
input: "10!3.2.1",
err: "epochs are not supported",
},
{
input: "3.2.post1.dev0",
expected: "3.2.0+post1dev0",
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.input, func(t *testing.T) {
t.Parallel()
result, err := determinePluginVersion(tt.input)
if tt.err != "" {
assert.EqualError(t, err, tt.err)
return
}
assert.NoError(t, err)
assert.Equal(t, tt.expected, result)
})
}
}
func TestDeterminePulumiPackages(t *testing.T) {
t.Parallel()
t.Run("empty", func(t *testing.T) {
t.Parallel()
cwd := t.TempDir()
_, err := runPythonModuleCommand(t, "", cwd, "venv", "venv")
assert.NoError(t, err)
packages, err := determinePulumiPackages(context.Background(), toolchain.PythonOptions{
Toolchain: toolchain.Pip,
Root: cwd,
Virtualenv: "venv",
})
assert.NoError(t, err)
assert.Empty(t, packages)
})
t.Run("non-empty", func(t *testing.T) {
t.Parallel()
cwd := t.TempDir()
_, err := runPythonModuleCommand(t, "", cwd, "venv", "venv")
assert.NoError(t, err)
// Install the local Pulumi SDK into the virtual environment.
sdkDir, err := filepath.Abs(filepath.Join("..", "..", "env", "src"))
assert.NoError(t, err)
_, err = runPythonModuleCommand(t, "venv", cwd, "pip", "install", "-e", sdkDir)
assert.NoError(t, err)
_, err = runPythonModuleCommand(t, "venv", cwd, "pip", "install", "pulumi-random")
assert.NoError(t, err)
_, err = runPythonModuleCommand(t, "venv", cwd, "pip", "install", "pip-install-test")
assert.NoError(t, err)
packages, err := determinePulumiPackages(context.Background(), toolchain.PythonOptions{
Toolchain: toolchain.Pip,
Root: cwd,
Virtualenv: "venv",
})
assert.NoError(t, err)
assert.NotEmpty(t, packages)
assert.Equal(t, 1, len(packages))
random := packages[0]
assert.Equal(t, "pulumi_random", random.Name)
assert.NotEmpty(t, random.Location)
})
t.Run("pulumiplugin", func(t *testing.T) {
t.Parallel()
cwd := t.TempDir()
_, err := runPythonModuleCommand(t, "", cwd, "venv", "venv")
require.NoError(t, err)
_, err = runPythonModuleCommand(t, "venv", cwd, "pip", "install", "pip-install-test")
require.NoError(t, err)
// Find sitePackages folder in Python that contains pip_install_test subfolder.
var sitePackages string
possibleSitePackages, err := runPythonCommand(t, "venv", cwd, "-c",
"import site; import json; print(json.dumps(site.getsitepackages()))")
require.NoError(t, err)
var possibleSitePackagePaths []string
err = json.Unmarshal(possibleSitePackages, &possibleSitePackagePaths)
require.NoError(t, err)
for _, dir := range possibleSitePackagePaths {
_, err := os.Stat(filepath.Join(dir, "pip_install_test"))
if os.IsNotExist(err) {
continue
}
require.NoError(t, err)
sitePackages = dir
}
if sitePackages == "" {
t.Error("None of Python site.getsitepackages() folders contain a pip_install_test subfolder")
t.FailNow()
}
path := filepath.Join(sitePackages, "pip_install_test", "pulumi-plugin.json")
bytes := []byte(`{ "name": "thing1", "version": "thing2", "server": "thing3", "resource": true }` + "\n")
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
err = os.WriteFile(path, bytes, 0o600)
require.NoError(t, err)
t.Logf("Wrote pulumipluing.json file: %s", path)
packages, err := determinePulumiPackages(context.Background(), toolchain.PythonOptions{
Toolchain: toolchain.Pip,
Root: cwd,
Virtualenv: "venv",
})
require.NoError(t, err)
assert.Equal(t, 1, len(packages))
pipInstallTest := packages[0]
assert.Equal(t, "pip-install-test", pipInstallTest.Name)
assert.NotEmpty(t, pipInstallTest.Location)
plugin, err := determinePluginDependency(pipInstallTest)
assert.NoError(t, err)
assert.NotNil(t, plugin)
assert.Equal(t, "thing1", plugin.Name)
assert.Equal(t, "vthing2", plugin.Version)
assert.Equal(t, "thing3", plugin.Server)
assert.Equal(t, "resource", plugin.Kind)
})
t.Run("no-pulumiplugin.json-file", func(t *testing.T) {
t.Parallel()
cwd := t.TempDir()
_, err := runPythonModuleCommand(t, "", cwd, "venv", "venv")
assert.NoError(t, err)
_, err = runPythonModuleCommand(t,
"venv", cwd, "pip", "install", "--upgrade", "pip", "setuptools")
assert.NoError(t, err)
// Install the local Pulumi SDK into the virtual environment.
sdkDir, err := filepath.Abs(filepath.Join("..", "..", "env", "src"))
assert.NoError(t, err)
_, err = runPythonModuleCommand(t, "venv", cwd, "pip", "install", "-e", sdkDir)
assert.NoError(t, err)
// Install a local old provider SDK that does not have a pulumi-plugin.json file.
oldSdkDir, err := filepath.Abs(filepath.Join("testdata", "sdks", "old-1.0.0"))
assert.NoError(t, err)
_, err = runPythonModuleCommand(t, "venv", cwd, "pip", "install", "-e", oldSdkDir)
assert.NoError(t, err)
// The package should be considered a Pulumi package since its name is prefixed with "pulumi_".
packages, err := determinePulumiPackages(context.Background(), toolchain.PythonOptions{
Toolchain: toolchain.Pip,
Root: cwd,
Virtualenv: "venv",
})
assert.NoError(t, err)
assert.NotEmpty(t, packages)
assert.Equal(t, 1, len(packages))
old := packages[0]
assert.Equal(t, "pulumi_old", old.Name)
assert.NotEmpty(t, old.Location)
})
}
func runPythonModuleCommand(t *testing.T, virtualenv, cwd, module string, args ...string) ([]byte, error) {
return runPythonCommand(t, virtualenv, cwd, append([]string{"-m", module}, args...)...)
}
func runPythonCommand(t *testing.T, virtualenv, cwd string, args ...string) ([]byte, error) {
t.Helper()
tc, err := toolchain.ResolveToolchain(toolchain.PythonOptions{
Toolchain: toolchain.Pip,
Root: cwd,
Virtualenv: virtualenv,
})
require.NoError(t, err)
cmd, err := tc.Command(context.Background(), args...)
require.NoError(t, err)
cmd.Dir = cwd
output, err := cmd.Output()
if err != nil {
return nil, err
}
return output, err
}