pulumi/tests/integration/integration_python_acceptan...

551 lines
18 KiB
Go
Raw Normal View History

// Copyright 2016-2022, 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.
//go:build python || all
package ints
import (
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"github.com/stretchr/testify/assert"
Support returning plain values from methods (#13592) Support returning plain values from methods. Implements Node, Python and Go support. Remaining: - [x] test receiving unknowns - [x] acceptance tests written and passing locally for Node, Python, Go clients against a Go server - [x] acceptance tests passing in CI - [x] tickets filed for remaining languages - [x] https://github.com/pulumi/pulumi-yaml/issues/499 - [x] https://github.com/pulumi/pulumi-java/issues/1193 - [x] https://github.com/pulumi/pulumi-dotnet/issues/170 Known limitations: - this is technically a breaking change in case there is code out there that already uses methods that return Plain: true - struct-wrapping limitation: the provider for the component resource needs to still wrap the plain-returning Method response with a 1-arg struct; by convention the field is named "res", and this is how it travels through the plumbing - resources cannot return plain values yet - the provider for the component resource cannot have unknown configuration, if it does, the methods will not be called - Per Luke https://github.com/pulumi/pulumi/issues/11520 this might not be supported/realizable yet <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> Fixes https://github.com/pulumi/pulumi/issues/12709 ## Checklist - [ ] I have run `make tidy` to update any new dependencies - [ ] I have run `make lint` to verify my code passes the lint check - [ ] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [ ] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [ ] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2023-11-18 06:02:06 +00:00
"github.com/stretchr/testify/require"
"github.com/pulumi/pulumi/pkg/v3/testing/integration"
"github.com/pulumi/pulumi/sdk/v3/go/common/resource"
ptesting "github.com/pulumi/pulumi/sdk/v3/go/common/testing"
"github.com/pulumi/pulumi/sdk/v3/python/toolchain"
)
func boolPointer(b bool) *bool {
return &b
}
// TestEmptyPython simply tests that we can run an empty Python project.
//
//nolint:paralleltest // ProgramTest calls t.Parallel()
func TestEmptyPython(t *testing.T) {
integration.ProgramTest(t, &integration.ProgramTestOptions{
Dir: filepath.Join("empty", "python"),
Dependencies: []string{
filepath.Join("..", "..", "sdk", "python", "env", "src"),
},
Quick: true,
})
}
// Tests dynamic provider in Python.
//
//nolint:paralleltest // ProgramTest calls t.Parallel()
func TestDynamicPython(t *testing.T) {
var randomVal string
integration.ProgramTest(t, &integration.ProgramTestOptions{
Dir: filepath.Join("dynamic", "python"),
Dependencies: []string{
filepath.Join("..", "..", "sdk", "python", "env", "src"),
},
ExtraRuntimeValidation: func(t *testing.T, stack integration.RuntimeValidationStackInfo) {
randomVal = stack.Outputs["random_val"].(string)
},
EditDirs: []integration.EditDir{{
Don't swallow file not found in fsutil.FileCopy (#14695) # Description `FileCopy` silently swallows file not found errors which leads to hard to debug issues. For instance, as described in #14694, tests with edit steps unexpectedly run twice against the first step. Fixes #14694 Fixes #5230 Merging this will break plenty of existing tests if they're not fixed first. [This query](https://github.com/search?q=org%3Apulumi+path%3A_test.go+%2F%5E%5Ct*Dir%3A+*%22%2F&type=code) is a rough indication. ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [x] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [ ] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. --> --------- Co-authored-by: Fraser Waters <fraser@pulumi.com>
2023-12-06 13:29:40 +00:00
Dir: filepath.Join("dynamic", "python", "step1"),
Additive: true,
ExtraRuntimeValidation: func(t *testing.T, stack integration.RuntimeValidationStackInfo) {
assert.Equal(t, randomVal, stack.Outputs["random_val"].(string))
// Regression testing the workaround for https://github.com/pulumi/pulumi/issues/8265
// Ensure the __provider input and output was marked secret
assertIsSecret := func(v interface{}) {
switch v := v.(type) {
case string:
assert.Fail(t, "__provider was not a secret")
case map[string]interface{}:
assert.Equal(t, resource.SecretSig, v[resource.SigKey])
}
}
dynRes := stack.Deployment.Resources[2]
assertIsSecret(dynRes.Inputs["__provider"])
assertIsSecret(dynRes.Outputs["__provider"])
// Ensure there are no diagnostic events other than debug.
for _, event := range stack.Events {
if event.DiagnosticEvent != nil {
assert.Equal(t, "debug", event.DiagnosticEvent.Severity,
"unexpected diagnostic event: %#v", event.DiagnosticEvent)
}
}
},
}},
UseSharedVirtualEnv: boolPointer(false),
})
}
// Test remote component construction in Python.
func TestConstructPython(t *testing.T) {
t.Parallel()
testDir := "construct_component"
runComponentSetup(t, testDir)
tests := []struct {
componentDir string
expectedResourceCount int
env []string
}{
{
componentDir: "testcomponent",
expectedResourceCount: 9,
// TODO[pulumi/pulumi#5455]: Dynamic providers fail to load when used from multi-lang components.
// Until we've addressed this, set PULUMI_TEST_YARN_LINK_PULUMI, which tells the integration test
// module to run `yarn install && yarn link @pulumi/pulumi` in the Go program's directory, allowing
// the Node.js dynamic provider plugin to load.
// When the underlying issue has been fixed, the use of this environment variable inside the integration
// test module should be removed.
env: []string{"PULUMI_TEST_YARN_LINK_PULUMI=true"},
},
{
componentDir: "testcomponent-python",
expectedResourceCount: 9,
},
{
componentDir: "testcomponent-go",
expectedResourceCount: 8, // One less because no dynamic provider.
},
}
//nolint:paralleltest // ProgramTest calls t.Parallel()
for _, test := range tests {
test := test
t.Run(test.componentDir, func(t *testing.T) {
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
localProviders := []integration.LocalDependency{
{Package: "testcomponent", Path: filepath.Join(testDir, test.componentDir)},
}
integration.ProgramTest(t,
optsForConstructPython(t, test.expectedResourceCount, localProviders, test.env...))
})
}
}
func optsForConstructPython(
t *testing.T, expectedResourceCount int, localProviders []integration.LocalDependency, env ...string,
) *integration.ProgramTestOptions {
return &integration.ProgramTestOptions{
Env: env,
Dir: filepath.Join("construct_component", "python"),
Dependencies: []string{
filepath.Join("..", "..", "sdk", "python", "env", "src"),
},
LocalProviders: localProviders,
Secrets: map[string]string{
"secret": "this super secret is encrypted",
},
Quick: true,
UseSharedVirtualEnv: boolPointer(false),
ExtraRuntimeValidation: func(t *testing.T, stackInfo integration.RuntimeValidationStackInfo) {
assert.NotNil(t, stackInfo.Deployment)
if assert.Equal(t, expectedResourceCount, len(stackInfo.Deployment.Resources)) {
stackRes := stackInfo.Deployment.Resources[0]
assert.NotNil(t, stackRes)
assert.Equal(t, resource.RootStackType, stackRes.Type)
assert.Equal(t, "", string(stackRes.Parent))
// Check that dependencies flow correctly between the originating program and the remote component
// plugin.
urns := make(map[string]resource.URN)
for _, res := range stackInfo.Deployment.Resources[1:] {
assert.NotNil(t, res)
urns[res.URN.Name()] = res.URN
switch res.URN.Name() {
case "child-a":
for _, deps := range res.PropertyDependencies {
assert.Empty(t, deps)
}
case "child-b":
expected := []resource.URN{urns["a"]}
assert.ElementsMatch(t, expected, res.Dependencies)
assert.ElementsMatch(t, expected, res.PropertyDependencies["echo"])
case "child-c":
expected := []resource.URN{urns["a"], urns["child-a"]}
assert.ElementsMatch(t, expected, res.Dependencies)
assert.ElementsMatch(t, expected, res.PropertyDependencies["echo"])
case "a", "b", "c":
secretPropValue, ok := res.Outputs["secret"].(map[string]interface{})
assert.Truef(t, ok, "secret output was not serialized as a secret")
assert.Equal(t, resource.SecretSig, secretPropValue[resource.SigKey].(string))
}
}
}
},
}
}
//nolint:paralleltest // Sets env vars
Support returning plain values from methods (#13592) Support returning plain values from methods. Implements Node, Python and Go support. Remaining: - [x] test receiving unknowns - [x] acceptance tests written and passing locally for Node, Python, Go clients against a Go server - [x] acceptance tests passing in CI - [x] tickets filed for remaining languages - [x] https://github.com/pulumi/pulumi-yaml/issues/499 - [x] https://github.com/pulumi/pulumi-java/issues/1193 - [x] https://github.com/pulumi/pulumi-dotnet/issues/170 Known limitations: - this is technically a breaking change in case there is code out there that already uses methods that return Plain: true - struct-wrapping limitation: the provider for the component resource needs to still wrap the plain-returning Method response with a 1-arg struct; by convention the field is named "res", and this is how it travels through the plumbing - resources cannot return plain values yet - the provider for the component resource cannot have unknown configuration, if it does, the methods will not be called - Per Luke https://github.com/pulumi/pulumi/issues/11520 this might not be supported/realizable yet <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> Fixes https://github.com/pulumi/pulumi/issues/12709 ## Checklist - [ ] I have run `make tidy` to update any new dependencies - [ ] I have run `make lint` to verify my code passes the lint check - [ ] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [ ] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [ ] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2023-11-18 06:02:06 +00:00
func TestConstructComponentConfigureProviderPython(t *testing.T) {
// This uses the tls plugin so needs to be able to download it
t.Setenv("PULUMI_DISABLE_AUTOMATIC_PLUGIN_ACQUISITION", "false")
Support returning plain values from methods (#13592) Support returning plain values from methods. Implements Node, Python and Go support. Remaining: - [x] test receiving unknowns - [x] acceptance tests written and passing locally for Node, Python, Go clients against a Go server - [x] acceptance tests passing in CI - [x] tickets filed for remaining languages - [x] https://github.com/pulumi/pulumi-yaml/issues/499 - [x] https://github.com/pulumi/pulumi-java/issues/1193 - [x] https://github.com/pulumi/pulumi-dotnet/issues/170 Known limitations: - this is technically a breaking change in case there is code out there that already uses methods that return Plain: true - struct-wrapping limitation: the provider for the component resource needs to still wrap the plain-returning Method response with a 1-arg struct; by convention the field is named "res", and this is how it travels through the plumbing - resources cannot return plain values yet - the provider for the component resource cannot have unknown configuration, if it does, the methods will not be called - Per Luke https://github.com/pulumi/pulumi/issues/11520 this might not be supported/realizable yet <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> Fixes https://github.com/pulumi/pulumi/issues/12709 ## Checklist - [ ] I have run `make tidy` to update any new dependencies - [ ] I have run `make lint` to verify my code passes the lint check - [ ] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [ ] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [ ] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2023-11-18 06:02:06 +00:00
const testDir = "construct_component_configure_provider"
runComponentSetup(t, testDir)
pulumiRoot, err := filepath.Abs("../..")
require.NoError(t, err)
pulumiPySDK := filepath.Join("..", "..", "sdk", "python", "env", "src")
componentSDK := filepath.Join(pulumiRoot, "pkg/codegen/testing/test/testdata/methods-return-plain-resource/python")
opts := testConstructComponentConfigureProviderCommonOptions()
opts = opts.With(integration.ProgramTestOptions{
Dir: filepath.Join(testDir, "python"),
Dependencies: []string{pulumiPySDK, componentSDK},
NoParallel: true,
Support returning plain values from methods (#13592) Support returning plain values from methods. Implements Node, Python and Go support. Remaining: - [x] test receiving unknowns - [x] acceptance tests written and passing locally for Node, Python, Go clients against a Go server - [x] acceptance tests passing in CI - [x] tickets filed for remaining languages - [x] https://github.com/pulumi/pulumi-yaml/issues/499 - [x] https://github.com/pulumi/pulumi-java/issues/1193 - [x] https://github.com/pulumi/pulumi-dotnet/issues/170 Known limitations: - this is technically a breaking change in case there is code out there that already uses methods that return Plain: true - struct-wrapping limitation: the provider for the component resource needs to still wrap the plain-returning Method response with a 1-arg struct; by convention the field is named "res", and this is how it travels through the plumbing - resources cannot return plain values yet - the provider for the component resource cannot have unknown configuration, if it does, the methods will not be called - Per Luke https://github.com/pulumi/pulumi/issues/11520 this might not be supported/realizable yet <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> Fixes https://github.com/pulumi/pulumi/issues/12709 ## Checklist - [ ] I have run `make tidy` to update any new dependencies - [ ] I have run `make lint` to verify my code passes the lint check - [ ] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [ ] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [ ] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2023-11-18 06:02:06 +00:00
})
integration.ProgramTest(t, &opts)
}
// Regresses https://github.com/pulumi/pulumi/issues/6471
func TestAutomaticVenvCreation(t *testing.T) {
t.Parallel()
// Do not use integration.ProgramTest to avoid automatic venv
// handling by test harness; we actually are testing venv
// handling by the pulumi CLI itself.
check := func(t *testing.T, venvPathTemplate string, dir string) {
e := ptesting.NewEnvironment(t)
defer e.DeleteIfNotFailed()
venvPath := strings.ReplaceAll(venvPathTemplate, "${root}", e.RootPath)
t.Logf("venvPath = %s (IsAbs = %v)", venvPath, filepath.IsAbs(venvPath))
e.ImportDirectory(dir)
// replace "virtualenv: venv" with "virtualenv: ${venvPath}" in Pulumi.yaml
pulumiYaml := filepath.Join(e.RootPath, "Pulumi.yaml")
oldYaml, err := os.ReadFile(pulumiYaml)
if err != nil {
t.Error(err)
return
}
newYaml := []byte(strings.ReplaceAll(string(oldYaml),
"virtualenv: venv",
"virtualenv: >-\n "+venvPath))
if err := os.WriteFile(pulumiYaml, newYaml, 0o600); err != nil {
t.Error(err)
return
}
t.Logf("Wrote Pulumi.yaml:\n%s\n", string(newYaml))
Actually save root on plugin.Context (#14684) <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> Pretty sure this has been broken since forever. Pretty sure it's only _mostly_ worked because we nearly always run pulumi from the directory with the Pulumi.yaml file and there's a bit of code in `buildArgsForNewPlugin` that calls `filepath.Abs(root)` before passing it to the language plugins. `Abs("")` will just resolve to the working directory, which as above is nearly always the one with the Pulumi.yaml file in it. ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [ ] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [x] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2023-11-29 14:30:52 +00:00
// Make a subdir and change to it to ensure paths aren't just relative to the working directory.
subdir := filepath.Join(e.RootPath, "subdir")
err = os.Mkdir(subdir, 0o755)
require.NoError(t, err)
e.CWD = subdir
e.RunCommand("pulumi", "login", "--cloud-url", e.LocalURL())
e.RunCommand("pulumi", "stack", "init", "teststack")
e.RunCommand("pulumi", "preview")
var absVenvPath string
if filepath.IsAbs(venvPath) {
absVenvPath = venvPath
} else {
absVenvPath = filepath.Join(e.RootPath, venvPath)
}
if !toolchain.IsVirtualEnv(absVenvPath) {
t.Errorf("Expected a virtual environment to be created at %s but it is not there",
absVenvPath)
}
}
t.Run("RelativePath", func(t *testing.T) {
t.Parallel()
check(t, "venv", filepath.Join("python", "venv"))
})
t.Run("AbsolutePath", func(t *testing.T) {
t.Parallel()
check(t, filepath.Join("${root}", "absvenv"), filepath.Join("python", "venv"))
})
t.Run("RelativePathWithMain", func(t *testing.T) {
t.Parallel()
check(t, "venv", filepath.Join("python", "venv-with-main"))
})
t.Run("AbsolutePathWithMain", func(t *testing.T) {
t.Parallel()
check(t, filepath.Join("${root}", "absvenv"), filepath.Join("python", "venv-with-main"))
})
t.Run("TestInitVirtualEnvBeforePythonVersionCheck", func(t *testing.T) {
t.Parallel()
e := ptesting.NewEnvironment(t)
defer e.DeleteIfNotFailed()
dir := filepath.Join("python", "venv")
e.ImportDirectory(dir)
e.RunCommand("pulumi", "login", "--cloud-url", e.LocalURL())
e.RunCommand("pulumi", "stack", "init", "teststack")
stdout, stderr, _ := e.GetCommandResults("pulumi", "preview")
// pulumi/pulumi#9175
// Ensures this error message doesn't show up for uninitialized
// virtualenv
// `Failed to resolve python version command: ` +
// `fork/exec <path>/venv/bin/python: ` +
// `no such file or directory`
assert.NotContains(t, stdout, "fork/exec")
assert.NotContains(t, stderr, "fork/exec")
})
}
Add 'typechecker' option to python runtime (#15725) <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> Fixes https://github.com/pulumi/pulumi/issues/15724. This adds a new runtime option to the python language host `typeChecker` which can be set to either "mypy" or "pyright". If it's set the language host will run the mypy/pyright module before running the program. ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [x] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [x] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [ ] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2024-03-28 10:41:22 +00:00
func TestAutomaticVenvCreationPoetry(t *testing.T) {
t.Parallel()
e := ptesting.NewEnvironment(t)
defer e.DeleteIfNotFailed()
e.ImportDirectory(filepath.Join("python", "poetry"))
// Make a subdir and change to it to ensure paths aren't just relative to the working directory.
subdir := filepath.Join(e.RootPath, "subdir")
err := os.Mkdir(subdir, 0o755)
require.NoError(t, err)
e.CWD = subdir
e.RunCommand("pulumi", "login", "--cloud-url", e.LocalURL())
e.RunCommand("pulumi", "stack", "init", "teststack")
e.RunCommand("pulumi", "preview")
localPoetryVenv := filepath.Join(e.RootPath, ".venv")
if !toolchain.IsVirtualEnv(localPoetryVenv) {
t.Errorf("Expected a virtual environment to be created at %s but it is not there", localPoetryVenv)
}
}
Add 'typechecker' option to python runtime (#15725) <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> Fixes https://github.com/pulumi/pulumi/issues/15724. This adds a new runtime option to the python language host `typeChecker` which can be set to either "mypy" or "pyright". If it's set the language host will run the mypy/pyright module before running the program. ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [x] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [x] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [ ] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2024-03-28 10:41:22 +00:00
//nolint:paralleltest // ProgramTest calls t.Parallel()
func TestMypySupport(t *testing.T) {
validation := func(t *testing.T, stack integration.RuntimeValidationStackInfo) {
// Should get an event for the mypy failure.
messages := []string{}
for _, event := range stack.Events {
if event.DiagnosticEvent != nil {
messages = append(messages,
strings.Replace(event.DiagnosticEvent.Message, "\r\n", "\n", -1))
}
}
expected := "__main__.py:8: error: " +
"Argument 1 to \"export\" has incompatible type \"int\"; expected \"str\"" +
" [arg-type]\n\n"
assert.Contains(t, messages, expected, "Did not find expected mypy diagnostic event")
}
integration.ProgramTest(t, &integration.ProgramTestOptions{
Dir: filepath.Join("python", "mypy"),
// mypy doesn't really support editable packages, so we install pulumi normally from pip for this test.
Quick: true,
ExpectFailure: true,
ExtraRuntimeValidation: validation,
})
}
//nolint:paralleltest // ProgramTest calls t.Parallel()
func TestPyrightSupport(t *testing.T) {
validation := func(t *testing.T, stack integration.RuntimeValidationStackInfo) {
// Should get an event for the pyright failure.
found := false
expected := "__main__.py:8:15 - error:" +
" Argument of type \"Literal[42]\" cannot be assigned to parameter \"name\"" +
" of type \"str\" in function \"export\"\n\n"
for _, event := range stack.Events {
if event.DiagnosticEvent != nil {
message := strings.Replace(event.DiagnosticEvent.Message, "\r\n", "\n", -1)
if strings.HasSuffix(message, expected) {
found = true
}
}
}
assert.True(t, found, "Did not find expected pyright diagnostic event")
}
integration.ProgramTest(t, &integration.ProgramTestOptions{
Dir: filepath.Join("python", "pyright"),
// to match the mypy test we install pulumi normally from pip for this test.
Quick: true,
ExpectFailure: true,
ExtraRuntimeValidation: validation,
})
}
//nolint:paralleltest // ProgramTest calls t.Parallel()
func TestTypecheckerMissingError(t *testing.T) {
validation := func(t *testing.T, stack integration.RuntimeValidationStackInfo) {
// Should get an event for the pyright failure.
found := false
expected := "The typechecker option is set to pyright, but pyright is not installed." +
" Please add an entry for pyright to requirements.txt"
for _, event := range stack.Events {
if event.DiagnosticEvent != nil {
if strings.Contains(event.DiagnosticEvent.Message, expected) {
found = true
}
}
}
assert.True(t, found, "Did not find expected pyright diagnostic event")
}
integration.ProgramTest(t, &integration.ProgramTestOptions{
Dir: filepath.Join("python", "pyright-missing"),
Quick: true,
ExpectFailure: true,
ExtraRuntimeValidation: validation,
})
Add 'typechecker' option to python runtime (#15725) <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> Fixes https://github.com/pulumi/pulumi/issues/15724. This adds a new runtime option to the python language host `typeChecker` which can be set to either "mypy" or "pyright". If it's set the language host will run the mypy/pyright module before running the program. ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [x] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [x] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [ ] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2024-03-28 10:41:22 +00:00
}
func TestNewPythonUsesPip(t *testing.T) {
t.Parallel()
e := ptesting.NewEnvironment(t)
defer e.DeleteIfNotFailed()
e.RunCommand("pulumi", "login", "--cloud-url", e.LocalURL())
stdout, _ := e.RunCommand("pulumi", "new", "python", "--force", "--non-interactive", "--yes", "--generate-only")
require.Contains(t, stdout, "pulumi install")
expected := map[string]interface{}{
"toolchain": "pip",
"virtualenv": "venv",
}
integration.CheckRuntimeOptions(t, e.RootPath, expected)
}
//nolint:paralleltest // Modifies env
func TestNewPythonUsesPipNonInteractive(t *testing.T) {
// Force interactive mode to properly test `--yes`.
t.Setenv("PULUMI_TEST_INTERACTIVE", "1")
e := ptesting.NewEnvironment(t)
defer e.DeleteIfNotFailed()
e.RunCommand("pulumi", "login", "--cloud-url", e.LocalURL())
stdout, _ := e.RunCommand("pulumi", "new", "python", "--force", "--yes", "--generate-only")
require.Contains(t, stdout, "pulumi install")
expected := map[string]interface{}{
"toolchain": "pip",
"virtualenv": "venv",
}
integration.CheckRuntimeOptions(t, e.RootPath, expected)
}
//nolint:paralleltest // Modifies env
func TestNewPythonChoosePoetry(t *testing.T) {
// The windows acceptance tests are run using git bash, but the survey library does not support this
// https://github.com/AlecAivazis/survey/issues/148
if runtime.GOOS == "windows" {
t.Skip("Skipping: survey library does not support git bash on Windows")
}
t.Setenv("PULUMI_TEST_INTERACTIVE", "1")
e := ptesting.NewEnvironment(t)
defer e.DeleteIfNotFailed()
e.RunCommand("pulumi", "login", "--cloud-url", e.LocalURL())
e.Stdin = strings.NewReader("poetry\n")
e.RunCommand("pulumi", "new", "python", "--force", "--generate-only",
"--name", "test_project",
"--description", "A python test using poetry as toolchain",
"--stack", "test",
)
expected := map[string]interface{}{
"toolchain": "poetry",
}
integration.CheckRuntimeOptions(t, e.RootPath, expected)
}
func TestNewPythonRuntimeOptions(t *testing.T) {
t.Parallel()
e := ptesting.NewEnvironment(t)
defer e.DeleteIfNotFailed()
e.RunCommand("pulumi", "login", "--cloud-url", e.LocalURL())
e.RunCommand("pulumi", "new", "python", "--force", "--non-interactive", "--yes", "--generate-only",
"--name", "test_project",
"--description", "A python test using poetry as toolchain",
"--stack", "test",
"--runtime-options", "toolchain=pip,virtualenv=mytestenv",
)
expected := map[string]interface{}{
"toolchain": "pip",
"virtualenv": "mytestenv",
}
integration.CheckRuntimeOptions(t, e.RootPath, expected)
}
func TestNewPythonConvertRequirementsTxt(t *testing.T) {
t.Parallel()
e := ptesting.NewEnvironment(t)
defer e.DeleteIfNotFailed()
// Add a poetry.toml to make poetry create the virtualenv inside the temp
// directory. That way it gets cleaned up with the test.
poetryToml := `[virtualenvs]
in-project = true`
err := os.WriteFile(filepath.Join(e.RootPath, "poetry.toml"), []byte(poetryToml), 0o600)
require.NoError(t, err)
template := "python"
e.RunCommand("pulumi", "login", "--cloud-url", e.LocalURL())
e.RunCommand("pulumi", "new", template, "--force", "--non-interactive", "--yes",
"--name", "test_project",
"--description", "A python test using poetry as toolchain",
"--stack", "test",
"--runtime-options", "toolchain=poetry",
)
require.True(t, e.PathExists("pyproject.toml"), "pyproject.toml was created")
require.False(t, e.PathExists("requirements.txt"), "requirements.txt was removed")
b, err := os.ReadFile(filepath.Join(e.RootPath, "pyproject.toml"))
require.NoError(t, err)
require.Equal(t, `[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
[tool]
[tool.poetry]
package-mode = false
[tool.poetry.dependencies]
pulumi = ">=3.0.0,<4.0.0"
python = "^3.8"
`, string(b))
}