2022-11-03 20:30:35 +00:00
|
|
|
// 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.
|
|
|
|
|
2022-09-16 22:04:05 +00:00
|
|
|
//go:build python || all
|
|
|
|
|
|
|
|
package ints
|
|
|
|
|
|
|
|
import (
|
2023-01-06 22:39:16 +00:00
|
|
|
"os"
|
2022-09-16 22:04:05 +00:00
|
|
|
"path/filepath"
|
2024-06-17 17:10:55 +00:00
|
|
|
"runtime"
|
2022-09-16 22:04:05 +00:00
|
|
|
"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"
|
2022-09-16 22:04:05 +00:00
|
|
|
|
|
|
|
"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"
|
2024-06-03 13:52:27 +00:00
|
|
|
"github.com/pulumi/pulumi/sdk/v3/python/toolchain"
|
2022-09-16 22:04:05 +00:00
|
|
|
)
|
|
|
|
|
2022-12-05 17:23:37 +00:00
|
|
|
func boolPointer(b bool) *bool {
|
|
|
|
return &b
|
|
|
|
}
|
|
|
|
|
2022-09-16 22:04:05 +00:00
|
|
|
// TestEmptyPython simply tests that we can run an empty Python project.
|
2023-12-02 17:16:09 +00:00
|
|
|
//
|
|
|
|
//nolint:paralleltest // ProgramTest calls t.Parallel()
|
2022-09-16 22:04:05 +00:00
|
|
|
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.
|
2023-12-02 17:16:09 +00:00
|
|
|
//
|
|
|
|
//nolint:paralleltest // ProgramTest calls t.Parallel()
|
2022-09-16 22:04:05 +00:00
|
|
|
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{{
|
2023-12-06 13:29:40 +00:00
|
|
|
Dir: filepath.Join("dynamic", "python", "step1"),
|
2022-09-16 22:04:05 +00:00
|
|
|
Additive: true,
|
|
|
|
ExtraRuntimeValidation: func(t *testing.T, stack integration.RuntimeValidationStackInfo) {
|
|
|
|
assert.Equal(t, randomVal, stack.Outputs["random_val"].(string))
|
2023-06-29 08:28:19 +00:00
|
|
|
|
|
|
|
// 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"])
|
2023-11-01 22:19:37 +00:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
}
|
2022-09-16 22:04:05 +00:00
|
|
|
},
|
|
|
|
}},
|
2022-12-05 17:23:37 +00:00
|
|
|
UseSharedVirtualEnv: boolPointer(false),
|
2022-09-16 22:04:05 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-12-02 17:16:09 +00:00
|
|
|
//nolint:paralleltest // ProgramTest calls t.Parallel()
|
2022-09-16 22:04:05 +00:00
|
|
|
for _, test := range tests {
|
|
|
|
test := test
|
|
|
|
t.Run(test.componentDir, func(t *testing.T) {
|
2023-03-03 16:36:39 +00:00
|
|
|
localProviders := []integration.LocalDependency{
|
|
|
|
{Package: "testcomponent", Path: filepath.Join(testDir, test.componentDir)},
|
|
|
|
}
|
2022-09-16 22:04:05 +00:00
|
|
|
integration.ProgramTest(t,
|
2022-09-22 14:10:40 +00:00
|
|
|
optsForConstructPython(t, test.expectedResourceCount, localProviders, test.env...))
|
2022-09-16 22:04:05 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-28 16:43:48 +00:00
|
|
|
func optsForConstructPython(
|
|
|
|
t *testing.T, expectedResourceCount int, localProviders []integration.LocalDependency, env ...string,
|
|
|
|
) *integration.ProgramTestOptions {
|
2022-09-16 22:04:05 +00:00
|
|
|
return &integration.ProgramTestOptions{
|
|
|
|
Env: env,
|
|
|
|
Dir: filepath.Join("construct_component", "python"),
|
|
|
|
Dependencies: []string{
|
|
|
|
filepath.Join("..", "..", "sdk", "python", "env", "src"),
|
|
|
|
},
|
2022-09-22 14:10:40 +00:00
|
|
|
LocalProviders: localProviders,
|
2022-09-16 22:04:05 +00:00
|
|
|
Secrets: map[string]string{
|
|
|
|
"secret": "this super secret is encrypted",
|
|
|
|
},
|
2022-12-05 17:23:37 +00:00
|
|
|
Quick: true,
|
|
|
|
UseSharedVirtualEnv: boolPointer(false),
|
2022-09-16 22:04:05 +00:00
|
|
|
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)
|
|
|
|
|
2023-11-28 16:43:48 +00:00
|
|
|
urns[res.URN.Name()] = res.URN
|
2022-09-16 22:04:05 +00:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-22 15:32:59 +00:00
|
|
|
//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) {
|
2024-03-22 15:32:59 +00:00
|
|
|
// This uses the tls plugin so needs to be able to download it
|
|
|
|
t.Setenv("PULUMI_DISABLE_AUTOMATIC_PLUGIN_ACQUISITION", "false")
|
2023-12-02 17:16:09 +00:00
|
|
|
|
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},
|
2023-12-02 17:16:09 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-09-16 22:04:05 +00:00
|
|
|
// 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)
|
2024-07-23 10:37:01 +00:00
|
|
|
defer e.DeleteIfNotFailed()
|
2022-09-16 22:04:05 +00:00
|
|
|
|
|
|
|
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")
|
|
|
|
|
2023-01-06 22:39:16 +00:00
|
|
|
oldYaml, err := os.ReadFile(pulumiYaml)
|
2022-09-16 22:04:05 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
newYaml := []byte(strings.ReplaceAll(string(oldYaml),
|
|
|
|
"virtualenv: venv",
|
2024-04-19 06:20:33 +00:00
|
|
|
"virtualenv: >-\n "+venvPath))
|
2022-09-16 22:04:05 +00:00
|
|
|
|
2023-11-28 16:43:48 +00:00
|
|
|
if err := os.WriteFile(pulumiYaml, newYaml, 0o600); err != nil {
|
2022-09-16 22:04:05 +00:00
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Logf("Wrote Pulumi.yaml:\n%s\n", string(newYaml))
|
|
|
|
|
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
|
|
|
|
|
2022-09-16 22:04:05 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2024-06-03 13:52:27 +00:00
|
|
|
if !toolchain.IsVirtualEnv(absVenvPath) {
|
2022-09-16 22:04:05 +00:00
|
|
|
t.Errorf("Expected a virtual environment to be created at %s but it is not there",
|
|
|
|
absVenvPath)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("RelativePath", func(t *testing.T) {
|
2023-12-02 17:16:09 +00:00
|
|
|
t.Parallel()
|
2022-09-16 22:04:05 +00:00
|
|
|
check(t, "venv", filepath.Join("python", "venv"))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("AbsolutePath", func(t *testing.T) {
|
2023-12-02 17:16:09 +00:00
|
|
|
t.Parallel()
|
2022-09-16 22:04:05 +00:00
|
|
|
check(t, filepath.Join("${root}", "absvenv"), filepath.Join("python", "venv"))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("RelativePathWithMain", func(t *testing.T) {
|
2023-12-02 17:16:09 +00:00
|
|
|
t.Parallel()
|
2022-09-16 22:04:05 +00:00
|
|
|
check(t, "venv", filepath.Join("python", "venv-with-main"))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("AbsolutePathWithMain", func(t *testing.T) {
|
2023-12-02 17:16:09 +00:00
|
|
|
t.Parallel()
|
2022-09-16 22:04:05 +00:00
|
|
|
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)
|
2024-07-23 10:37:01 +00:00
|
|
|
defer e.DeleteIfNotFailed()
|
2022-09-16 22:04:05 +00:00
|
|
|
|
|
|
|
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")
|
|
|
|
})
|
|
|
|
}
|
2024-03-28 10:41:22 +00:00
|
|
|
|
2024-06-06 14:16:24 +00:00
|
|
|
func TestAutomaticVenvCreationPoetry(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
e := ptesting.NewEnvironment(t)
|
2024-07-23 10:37:01 +00:00
|
|
|
defer e.DeleteIfNotFailed()
|
2024-06-06 14:16:24 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
})
|
2024-07-01 15:17:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//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,
|
|
|
|
})
|
2024-03-28 10:41:22 +00:00
|
|
|
}
|
2024-06-17 17:10:55 +00:00
|
|
|
|
|
|
|
func TestNewPythonUsesPip(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
e := ptesting.NewEnvironment(t)
|
2024-07-23 10:37:01 +00:00
|
|
|
defer e.DeleteIfNotFailed()
|
2024-06-17 17:10:55 +00:00
|
|
|
|
|
|
|
e.RunCommand("pulumi", "login", "--cloud-url", e.LocalURL())
|
2024-06-20 10:27:41 +00:00
|
|
|
stdout, _ := e.RunCommand("pulumi", "new", "python", "--force", "--non-interactive", "--yes", "--generate-only")
|
|
|
|
|
|
|
|
require.Contains(t, stdout, "pulumi install")
|
2024-06-17 17:10:55 +00:00
|
|
|
|
|
|
|
expected := map[string]interface{}{
|
|
|
|
"toolchain": "pip",
|
|
|
|
"virtualenv": "venv",
|
|
|
|
}
|
2024-06-21 11:35:06 +00:00
|
|
|
integration.CheckRuntimeOptions(t, e.RootPath, expected)
|
2024-06-17 17:10:55 +00:00
|
|
|
}
|
|
|
|
|
2024-07-08 11:00:33 +00:00
|
|
|
//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)
|
2024-07-23 10:37:01 +00:00
|
|
|
defer e.DeleteIfNotFailed()
|
2024-07-08 11:00:33 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2024-06-17 17:10:55 +00:00
|
|
|
//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)
|
2024-07-23 10:37:01 +00:00
|
|
|
defer e.DeleteIfNotFailed()
|
2024-06-17 17:10:55 +00:00
|
|
|
|
|
|
|
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",
|
|
|
|
}
|
2024-06-21 11:35:06 +00:00
|
|
|
integration.CheckRuntimeOptions(t, e.RootPath, expected)
|
2024-06-17 17:10:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewPythonRuntimeOptions(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
e := ptesting.NewEnvironment(t)
|
2024-07-23 10:37:01 +00:00
|
|
|
defer e.DeleteIfNotFailed()
|
2024-06-17 17:10:55 +00:00
|
|
|
|
|
|
|
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",
|
|
|
|
}
|
2024-06-21 11:35:06 +00:00
|
|
|
integration.CheckRuntimeOptions(t, e.RootPath, expected)
|
2024-06-17 17:10:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewPythonConvertRequirementsTxt(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
e := ptesting.NewEnvironment(t)
|
2024-07-23 10:37:01 +00:00
|
|
|
defer e.DeleteIfNotFailed()
|
2024-06-17 17:10:55 +00:00
|
|
|
|
|
|
|
// 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")
|
2024-07-17 07:28:56 +00:00
|
|
|
|
|
|
|
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))
|
2024-06-17 17:10:55 +00:00
|
|
|
}
|