pulumi/pkg/codegen/testing/test/sdk_driver.go

627 lines
20 KiB
Go
Raw Permalink Normal View History

package test
import (
"flag"
"os"
"path/filepath"
"runtime"
"sort"
"strconv"
"strings"
"sync"
"testing"
"github.com/stretchr/testify/require"
"github.com/pulumi/pulumi/pkg/v3/codegen"
)
// Defines an extra check logic that accepts the directory with the
// generated code, typically `$TestDir/$test.Directory/$language`.
type CodegenCheck func(t *testing.T, codedir string)
type SDKTest struct {
Directory string
Description string
// Extra checks for this test. They keys of this map
// are of the form "$language/$check" such as "go/compile".
Checks map[string]CodegenCheck
// Skip checks, identified by "$language/$check".
// "$language/any" is special, skipping generating the
// code as well as all tests.
Skip codegen.StringSet
// Do not compile the generated code for the languages in this set.
// This is a helper form of `Skip`.
SkipCompileCheck codegen.StringSet
// Mutex to ensure only a single test operates on directory at a time
Mutex sync.Mutex
}
// ShouldSkipTest indicates if a given test for a given language should be run.
func (tt *SDKTest) ShouldSkipTest(language, test string) bool {
// Only language-specific checks.
if !strings.HasPrefix(test, language+"/") {
return true
}
// Obey SkipCompileCheck to skip compile and test targets.
if tt.SkipCompileCheck != nil &&
tt.SkipCompileCheck.Has(language) &&
Enable perfsprint linter (#14813) <!--- 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. --> Prompted by a comment in another review: https://github.com/pulumi/pulumi/pull/14654#discussion_r1419995945 This lints that we don't use `fmt.Errorf` when `errors.New` will suffice, it also covers a load of other cases where `Sprintf` is sub-optimal. Most of these edits were made by running `perfsprint --fix`. ## 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. --> - [ ] 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-12-12 12:19:42 +00:00
(test == language+"/compile" ||
test == language+"/test") {
return true
}
// Obey Skip.
if tt.Skip != nil && tt.Skip.Has(test) {
return true
}
return false
}
// ShouldSkipCodegen determines if codegen should be run. ShouldSkipCodegen=true
// further implies no other tests will be run.
func (tt *SDKTest) ShouldSkipCodegen(language string) bool {
return tt.Skip.Has(language + "/any")
}
const (
python = "python"
nodejs = "nodejs"
dotnet = "dotnet"
golang = "go"
)
var allLanguages = codegen.NewStringSet("python/any", "nodejs/any", "dotnet/any", "go/any", "docs/any")
var PulumiPulumiSDKTests = []*SDKTest{
{
Directory: "naming-collisions",
Description: "Schema with types that could potentially produce collisions.",
},
{
Directory: "dash-named-schema",
Description: "Simple schema with a two part name (foo-bar)",
},
{
Directory: "external-resource-schema",
Description: "External resource schema",
SkipCompileCheck: codegen.NewStringSet(golang),
},
{
Fix nested modules in dotnet codegen (#14297) <!--- 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/14296. ## 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. --> - [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: Thomas Gummerer <t.gummerer@gmail.com>
2023-10-20 22:21:58 +00:00
Directory: "nested-module",
Description: "Nested module",
},
{
Directory: "simplified-invokes",
Description: "Simplified invokes",
Skip: codegen.NewStringSet("python/any", "go/any"),
},
{
Fix nested modules in dotnet codegen (#14297) <!--- 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/14296. ## 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. --> - [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: Thomas Gummerer <t.gummerer@gmail.com>
2023-10-20 22:21:58 +00:00
Directory: "nested-module-thirdparty",
Description: "Third-party nested module",
},
{
Directory: "plain-schema-gh6957",
Description: "Repro for #6957",
},
{
Directory: "resource-args-python-case-insensitive",
Description: "Resource args with same named resource and type case insensitive",
},
{
Directory: "resource-args-python",
Description: "Resource args with same named resource and type",
},
{
Directory: "simple-enum-schema",
Description: "Simple schema with enum types",
},
{
Directory: "simple-plain-schema",
Description: "Simple schema with plain properties",
},
{
Directory: "simple-plain-schema-with-root-package",
Description: "Simple schema with root package set",
},
{
Directory: "simple-schema-pyproject",
Description: "A simple schema that generates a pyproject.toml file",
2023-05-26 18:28:10 +00:00
Skip: codegen.NewStringSet("go/any", "nodejs/any", "dotnet/any"),
},
{
Directory: "simple-resource-schema",
Description: "Simple schema with local resource properties",
},
{
Directory: "simple-resource-schema-custom-pypackage-name",
Description: "Simple schema with local resource properties and custom Python package name",
},
{
Directory: "simple-methods-schema",
Description: "Simple schema with methods",
SkipCompileCheck: codegen.NewStringSet(nodejs, golang),
},
{
Directory: "simple-methods-schema-single-value-returns",
Description: "Simple schema with methods that return single values",
},
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
{
Directory: "methods-return-plain-resource",
Description: "Test returning plain Resource objects from methods",
Skip: codegen.NewStringSet("dotnet/any"),
},
{
Directory: "simple-yaml-schema",
Description: "Simple schema encoded using YAML",
},
{
Directory: "provider-config-schema",
Description: "Simple provider config schema",
// For golang skip check, see https://github.com/pulumi/pulumi/issues/11567
SkipCompileCheck: codegen.NewStringSet(dotnet, golang),
},
{
Directory: "replace-on-change",
Description: "Simple use of replaceOnChange in schema",
},
{
Directory: "simple-resource-with-aliases",
Description: "Simple schema with a resource that has aliases",
},
{
Directory: "resource-property-overlap",
Description: "A resource with the same name as its property",
SkipCompileCheck: codegen.NewStringSet(dotnet, nodejs),
},
{
Directory: "hyphen-url",
Description: "A resource url with a hyphen in its path",
Skip: codegen.NewStringSet("go/any"),
},
{
Directory: "output-funcs",
Description: "Tests targeting the $fn_output helper code generation feature",
},
{
Directory: "output-funcs-edgeorder",
Description: "Regresses Node compilation issues on a subset of azure-native",
SkipCompileCheck: codegen.NewStringSet(golang, python),
Skip: codegen.NewStringSet("nodejs/test"),
},
{
Directory: "output-funcs-tfbridge20",
Description: "Similar to output-funcs, but with compatibility: tfbridge20, to simulate pulumi-aws use case",
SkipCompileCheck: codegen.NewStringSet(python),
},
{
Directory: "cyclic-types",
Description: "Cyclic object types",
},
{
Directory: "regress-node-8110",
Description: "Test the fix for pulumi/pulumi#8110 nodejs compilation error",
Skip: codegen.NewStringSet("go/test", "dotnet/test"),
},
{
Directory: "dashed-import-schema",
Description: "Ensure that we handle all valid go import paths",
Skip: codegen.NewStringSet("go/test", "dotnet/test"),
},
{
Directory: "plain-and-default",
Description: "Ensure that a resource with a plain default property works correctly",
},
{
Directory: "plain-object-defaults",
Description: "Ensure that object defaults are generated (repro #8132)",
[codegen/go] Call site defaults for Pulumi Object types (#8411) * Add test case * Fix tests * Add test dependencies correctly * Feed through error handling * Include test output * Get types to line up * Add remaining test files * Update changelog * Correctly find type paths * Handle transitive objects * Handle required fields * Add feature flag for go * Add required+default test case * Don't `<any>` cast known types. * Add more flags. I realize this should really wait for PR#8400 to merge. * Add plain object to env-helper test This test fails right now. My next problem is fixing it. * Handle plain types * Handle function inputs * Fix the indentation * Handle output types correctly * Remove unnecessary `!` * Add test case * Fix tests * Add test dependencies correctly * Feed through error handling * Include test output * Get types to line up * Add remaining test files * Update changelog * Correctly find type paths * Handle transitive objects * Handle required fields * Add required+default test case * Don't `<any>` cast known types. * Add plain object to env-helper test This test fails right now. My next problem is fixing it. * Handle plain types * Handle function inputs * Fix the indentation * Handle output types correctly * Remove unnecessary `!` * Start on `genPlainObjectDefaultFunc` * Add missing change to fix test * Run tests with merge * Refactor out assign * Merge in next _index.md diff * Change method name to `Defaults` * Handle enums correctly * Another attempt at _index.md * Make module generation deterministic * Add checks for old values * Insert defaults in resources * Fix docs generation Credit to @praneetloke * Progress on adding defaults to Resource arguments * Handle resource argument defaults * Don't create defaults if disableObjectDefaults * Rename test folder * Add test for disable flag * Fix disable test * Update docs * Abstract out nil comparisons * Use reflection to test for empty values * Simplify Ptr and pulumi.Any type handling * Remove unused function * Apply defaults to functions * Update new test with master codegen * Tests + nil check
2021-11-23 23:10:15 +00:00
},
{
Directory: "plain-object-disable-defaults",
Description: "Ensure that we can still compile safely when defaults are disabled",
},
{
Directory: "regress-8403",
Description: "Regress pulumi/pulumi#8403",
SkipCompileCheck: codegen.NewStringSet(python),
},
{
Directory: "different-package-name-conflict",
Description: "different packages with the same resource",
Skip: allLanguages,
},
{
Directory: "different-enum",
Description: "An enum in a different package namespace",
Skip: codegen.NewStringSet("dotnet/compile"),
},
{
Directory: "array-of-enum-map",
Description: "A schema with an array of maps where the values are enums. Issue pulumi/pulumi#14734",
Skip: allLanguages.Except("go/any"),
},
{
Directory: "azure-native-nested-types",
Description: "Condensed example of nested collection types from Azure Native",
Skip: codegen.NewStringSet("go/any"),
},
{
Directory: "regress-go-8664",
Description: "Regress pulumi/pulumi#8664 affecting Go",
Skip: allLanguages.Except("go/any"),
},
{
Directory: "regress-go-10527",
Description: "Regress pulumi/pulumi#10527 affecting Go",
Skip: allLanguages.Except("go/any"),
},
{
Directory: "regress-go-12971",
Description: "Regress pulumi/pulumi#12971 affecting Go",
Skip: allLanguages.Except("go/any"),
},
{
Directory: "regress-go-15478",
Description: "Regress pulumi/pulumi#15478 affecting Go ensuring SDK-gen doesn't panic",
Skip: allLanguages.Except("go/any"),
// skipping the compile step because the generated code emits nested types that are not supported by the Go SDK
SkipCompileCheck: codegen.NewStringSet(golang),
},
2023-06-09 14:53:20 +00:00
{
Directory: "regress-py-12546",
Description: "Regress pulumi/pulumi#12546 affecting Python",
Skip: allLanguages.Except("python/any"),
},
{
Directory: "docs-collision",
Description: "Tests that resources and functions with the same name do not clobber each other.",
Skip: allLanguages.Except("docs/any"),
},
[sdk-gen, dotnet/go] Fixes SDK-generation when referencing shared types in config variables (#15772) # Description Fixes #15751 - In .NET, non-primitive types such as objects used in config variables will be generated anyways under the `Types` namespace as plain-shaped objects inside of the `Config` class. This is because the types from `Outputs` or `Inputs` cannot be used directly from config. Also no longer marking objects used from config variables as "outputs" (doesn't make any sense) - In Go, disable adding imports into the `config.go` based on used shared types because these are not used/referenced in the config module anywhere anyways. ## 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. --> - [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. -->
2024-03-28 17:14:47 +00:00
{
Directory: "using-shared-types-in-config",
Description: "Tests that shared types can be used in config.",
},
{
Directory: "other-owned",
Description: "CSharp rootNamespaces",
// We only test in dotnet, because we are testing a change in a dotnet
// language property. Other tests should pass, but do not put the
// relevant feature under test. To save time, we skip them.
//
// We need to see dotnet changes (paths) in the docs too.
Skip: allLanguages.Except("dotnet/any").Except("docs/any"),
},
{
Directory: "external-node-compatibility",
// In this case, this test's schema has kubernetes20 set, but is referencing a type from Google Native
// which doesn't have any compatibility modes set, so the referenced type should be `AuditConfigArgs`
// (with the `Args` suffix) and not `AuditConfig`.
Description: "Ensure external package compatibility modes are used when referencing external types",
Skip: allLanguages.Except("nodejs/any"),
},
{
Directory: "external-go-import-aliases",
// Google Native has its own import aliases, so those should be respected, unless there are local aliases.
// AWS Classic doesn't have any import aliases, so none should be used, unless there are local aliases.
Description: "Ensure external import aliases are honored, and any local import aliases override them",
Skip: allLanguages.Except("go/any"),
},
{
Directory: "external-python-same-module-name",
Description: "Ensure referencing external types/resources with the same module name are referenced correctly",
Skip: allLanguages.Except("python/any"),
},
{
Directory: "enum-reference",
Description: "Ensure referencing external types/resources with referenced enums import correctly",
},
{
Directory: "enum-reference-python",
Description: "Ensure referencing external types/resources with referenced enums import correctly in Python",
Skip: allLanguages.Except("python/any"),
},
{
Directory: "external-enum",
Description: "Ensure we generate valid tokens for external enums",
Skip: codegen.NewStringSet("dotnet/any"),
},
{
Directory: "internal-dependencies-go",
Description: "Emit Go internal dependencies",
Skip: allLanguages.Except("go/any"),
},
{
Directory: "go-overridden-internal-module-name",
Description: "Go SDK where the internal module name is overridden to be 'utilities'",
Skip: allLanguages.Except("go/any"),
},
{
Directory: "go-plain-ref-repro",
Description: "Generate a resource that accepts a plain input type",
Skip: allLanguages.Except("go/any"),
},
{
Directory: "go-nested-collections",
Description: "Generate a resource that outputs [][][]Foo",
Skip: allLanguages.Except("go/any"),
},
{
Directory: "functions-secrets",
// Secret properties for non-Output<T> returning functions cannot be secret because they are plain.
Description: "functions that have properties that are secrets in the schema",
},
{
Directory: "secrets",
Description: "Generate a resource with secret properties",
SkipCompileCheck: codegen.NewStringSet(dotnet),
},
2022-10-05 19:19:29 +00:00
{
Directory: "regress-py-tfbridge-611",
Description: "Regresses pulumi/pulumi-terraform-bridge#611",
Skip: allLanguages.Except("python/any").Union(codegen.NewStringSet("python/test", "python/py_compile")),
},
{
Directory: "hyphenated-symbols",
Description: "Test that types can have names with hyphens in them",
Skip: allLanguages.Except("go/any").Except("python/any"),
},
{
Directory: "provider-type-schema",
Description: "A schema with a type called Provider schema",
},
{
Directory: "embedded-crd-types",
Description: "A schema with CRD types with package names different from the main package",
Skip: codegen.NewStringSet("dotnet/any"),
},
[sdk-gen/go] Generate non-plain type variants for types used as inputs inside unions (#14679) # Description When using types in union cases (using `oneOf` in the schema) then Go SDK-gen doesn't detect these types as being used as inputs and only emits the plain version of a type. That is unless `generateExtraInputTypes: true` is specified in which case Go SDK-gen will emit the non-plain types as well. In the case of azure-native modules, `generateExtraInputTypes` is currently set to `false` (default) as of latest v2.19.0 and is missing a few non-plain type variants (for example `FirewallPolicyFilterRuleCollection` from [this file](https://raw.githubusercontent.com/pulumi/pulumi-azure-native-sdk/master/network/pulumiTypes.go)). There no single plain type in azure-native and yet it is missing these non-plain variants. This PR fixes that by also traversing the element types of schema union definitions when determining plain-ness of types and how they are used. I added an example schema with an array of unions (common case in azure-native) and confirmed that the change actually results in the non-plain types being generated when `generateExtraInputTypes` is set to `false` Fixes https://github.com/pulumi/pulumi-azure-native/issues/1922 ## Checklist - [ ] 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. --> - [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 12:37:53 +00:00
{
Directory: "unions-inside-arrays",
Description: "A schema with a union type inside an array",
},
{
Directory: "assets-and-archives",
Description: "A schema with assets and archives",
},
[sdkgen/python] Fix error calling _configure when the value is None (#14014) We recently fixed an issue where defaults weren't set for nested objects when the nested objects are passed as dicts (#13825). Unfortunately, this introduced a regression when the nested object is optional, but it itself has required fields, and the nested object is not specified. In that case, an unintended error is raised. Consider a `Provider` resource with an optional `certmanager: ProviderCertmanagerArgs` argument, which itself has two required properties: `mtls_cert_pem` and `mtls_key_pem`. When creating a new `Provider` without specifying a `certmanager`, we get an error: ``` TypeError: ProviderCertmanagerArgs._configure() missing 2 required positional arguments: 'mtls_cert_pem' and 'mtls_key_pem' ``` The source of the problem is this check in the generated `Provider`'s constructor: ```python if not isinstance(certmanager, ProviderCertmanagerArgs): certmanager = certmanager or {} def _setter(key, value): certmanager[key] = value ProviderCertmanagerArgs._configure(_setter, **certmanager) ``` When `certmanager` is not specified, its value is `None`, which is also not an instance of `ProviderCertmanagerArgs`. So the code inside the `if` executes. `ProviderCertmanagerArgs._configure` is called on an empty dict, and the error is raised because there are two required positional arguments to `ProviderCertmanagerArgs._configure`. The fix is to add an additional check to ensure the value is not `None`. Fixes #14012
2023-09-22 18:28:25 +00:00
{
Directory: "regress-py-14012",
Description: "Regresses https://github.com/pulumi/pulumi/issues/14012",
Skip: allLanguages.Except("python/any"),
},
2023-11-28 16:59:41 +00:00
{
Directory: "regress-py-14539",
Description: "Regresses https://github.com/pulumi/pulumi/issues/14539",
Skip: allLanguages.Except("python/any"),
},
[go/sdk-gen] Fix generics-only option missing ToOutput(...) methods (#14584) # Description PR #14492 removed `ToOutput(...)` methods for output types when generating non-generic SDK variants. The fix there was added to only include these when `side-by-side` is enabled, which implicitly included `generics-only` except for a _single_ place where that wasn't the case 😓 (see comment below in `gen.go`) This PR fixes issue and includes tests for go sdkgen where `generics` setting is set to `generics-only`: - `output-funcs-go-generics-only` - `plain-and-default-go-generics-only` - `secrets-go-generics-only` - `simple-enum-schema-go-generics-only` - `simple-plain-schema-go-generics-only` ## Checklist - [ ] 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. --> - [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-16 18:41:57 +00:00
{
Directory: "output-funcs-go-generics-only",
Description: "Tests targeting the $fn_output code generation feature, only for Go generics == generics-only",
Skip: allLanguages.Except("go/any"),
},
{
Directory: "plain-and-default-go-generics-only",
Description: "resource with a plain default property works, only for Go generics == generics-only",
Skip: allLanguages.Except("go/any"),
},
{
Directory: "secrets-go-generics-only",
Description: "Generate a resource with secret properties, only for Go generics == generics-only",
Skip: allLanguages.Except("go/any"),
},
{
Directory: "simple-enum-schema-go-generics-only",
Description: "Simple schema with enum types, only for Go generics == generics-only",
Skip: allLanguages.Except("go/any"),
},
{
Directory: "simple-plain-schema-go-generics-only",
Description: "Simple schema with plain properties, only for Go generics == generics-only",
Skip: allLanguages.Except("go/any"),
},
[go/sdk-gen] Fixes plain and optional properties for generated types for Go SDKs using generics (#14616) # Description While working on on #14585 I tried to add a test schema for assets and archives that generate a `generics-only` go SDK but the the output didn't compile. At first I thought the issue was specific to asset and archive types but actually it was a broader issue where the function`pkg.genPlainType(InputObjectType)` would always reduce `Input[T]` and `Optional[T]` to just `T` on each property of `InputObjectType`. It is probably fine to reduce `Input[T]` because we are generating a plain type after all. However, reducing `Optional[T]` to `T` is incorrect because the generic variant of go sdks are more strict about optionality of types. > It probably works in non-generic SDKs today because it relies on a runtime cast Example type `TypeWithAssets` from schema that has a plain and optional property called `plainAsset`: ❌ Before it was the following and it didn't compile for generic go sdks ```go type TypeWithAssets struct { PlainAsset pulumi.AssetOrArchive `pulumi:"plainAsset"` } type TypeWithAssetsArgs struct { PlainAsset pulumix.Input[*pulumi.AssetOrArchive] `pulumi:"plainAsset"` } func (o TypeWithAssetsOutput) PlainAsset() pulumix.Output[*pulumi.AssetOrArchive] { return pulumix.Apply[TypeWithAssets](o, func(v TypeWithAssets) pulumi.AssetOrArchive { return v.PlainAsset }) } ``` ✅ Now it generates: ```go type TypeWithAssets struct { PlainAsset *pulumi.AssetOrArchive `pulumi:"plainAsset"` } type TypeWithAssetsArgs struct { PlainAsset *pulumi.AssetOrArchive `pulumi:"plainAsset"` } func (o TypeWithAssetsOutput) PlainAsset() pulumix.Output[*pulumi.AssetOrArchive] { return pulumix.Apply[TypeWithAssets](o, func(v TypeWithAssets) *pulumi.AssetOrArchive { return v.PlainAsset }) } ``` Which is correct and compiles The behavior for current non-generic SDKs remains unchanged ## Checklist - [ ] 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. --> - [ ] 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-21 16:43:51 +00:00
{
Directory: "assets-and-archives-go-generics-only",
Description: "Testing generating a schema with assets and archives for go using generics-only",
Skip: allLanguages.Except("go/any"),
},
Validate that resources don't use urn or id as output properties (#14637) <!--- 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/14631. This adds a couple of new tests to codegen. Firstly to check that if you use "urn" or "id" as a resource output property we fail schema binding. This is because these clash with the urn and id fields that every resource already has to have. Secondly a schema gen test that checks that urn and id _do_ work in other places, i.e. resource inputs, nested types, etc. ## 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. --> - [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-23 02:43:38 +00:00
{
Directory: "urn-id-properties",
Description: "Testing urn and id properties in valid locations",
},
Fix python cross module imports (#14832) # Description <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> For python we were generating bad imports for 1) members of the same module, and 2) for members imported from another module. When importing from a different module, we want to always reference members using a fully qualified name (`mymod.childmod.member.Member`) to avoid any naming clashes in case multiple modules have members of the same name. For these we need to import their top level python module (`from ${relPath} import mymod`). When importing members from the same module, we want to use relative imports (`from .some_member import SomeMember`). Fixes https://github.com/pulumi/pulumi/issues/12980 ## 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. -->
2024-01-05 09:05:41 +00:00
{
Directory: "regress-py-12980",
Description: "Import resources across modules",
Skip: allLanguages.Except("python/any"),
},
{
Directory: "unions-inline",
Description: "Testing the use of unions/oneOf in the schema inline with the property definition.",
},
{
Directory: "legacy-names",
Description: "Testing the use of snake_case names and tokens.",
Skip: codegen.NewStringSet("go/test"),
},
[dotnet] codegen fix for resources without constant input properties (#15488) <!--- 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. --> This PR fixes a bug in the dotnet codegen where the wrong "args" class name would be used, in the case that the resource has no constant inputs. This is an edge case because most resources do have at least one such input. For example, a new resource definition in p/k produced this output: ```csharp namespace Pulumi.Kubernetes.Yaml.V2 { public ConfigGroup(string name, Pulumi.Kubernetes.Types.Inputs.Yaml.V2.ConfigGroupArgs? args = null, CustomResourceOptions? options = null) : base("kubernetes:yaml/v2:ConfigGroup", name, args ?? new ConfigGroupArgs(), MakeResourceOptions(options, ""), remote: true) { } } ``` Which doesn't compile because `ConfigGroupArgs` is in a separate namespace. Should be: ```csharp public ConfigGroup(string name, Pulumi.Kubernetes.Types.Inputs.Yaml.V2.ConfigGroupArgs? args = null, CustomResourceOptions? options = null) : base("kubernetes:yaml/v2:ConfigGroup", name, args ?? new Pulumi.Kubernetes.Types.Inputs.Yaml.V2.ConfigGroupArgs(), MakeResourceOptions(options, ""), remote: true) { } ``` [Here's ](https://github.com/pulumi/pulumi/pull/15488/files#diff-18b12fabab20d68398aced2890b1ca3073cc32081bb62a022b77a5090c209e3bR45)where the fix manifests itself in the new test case. ## Testing A new SDK test case was added to cover the whole `kubernetes20` compatibility mode, based on a simplified schema from the pulumi-kubernetes provider. The schema contains a representative set of resources: 1. `kubernetes:core/v1:ConfigMap` - a non-overlay resource representing a Kubernetes kind. 2. `kubernetes:core/v1:ConfigMapList` - a Kubernetes list kind 3. `kubernetes:helm.sh/v3:Release` - a non-overlay, non-Kubernetes resource 4. `kubernetes:yaml:ConfigGroup` - an overlay component resource An important detail is whether a resource has any input properties that have a constant value, such as we see with `kind` and `apiVersion`. The `Release` resource intentionally has no such constant inputs. ## 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. --> - [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. -->
2024-03-02 06:01:31 +00:00
{
Directory: "kubernetes20",
Description: "Testing the kubernetes20 compatibility mode.",
Skip: codegen.NewStringSet("go/test"),
},
}
var genSDKOnly bool
func NoSDKCodegenChecks() bool {
return genSDKOnly
}
func init() {
noChecks := false
if env, ok := os.LookupEnv("PULUMI_TEST_SDK_NO_CHECKS"); ok {
noChecks, _ = strconv.ParseBool(env)
}
flag.BoolVar(&genSDKOnly, "sdk.no-checks", noChecks, "when set, skips all post-SDK-generation checks")
// NOTE: the testing package will call flag.Parse.
}
// SDKCodegenOptions describes the set of codegen tests for a language.
type SDKCodegenOptions struct {
// Name of the programming language.
Language string
// Language-aware code generator; such as `GeneratePackage`.
// from `codegen/dotnet`.
GenPackage GenPkgSignature
// Extra checks for all the tests. They keys of this map are
// of the form "$language/$check" such as "go/compile".
Checks map[string]CodegenCheck
// The tests to run. A testcase `tt` are assumed to be located at
// ../testing/test/testdata/${tt.Directory}
TestCases []*SDKTest
}
// TestSDKCodegen runs the complete set of SDK code generation tests
// against a particular language's code generator. It also verifies
// that the generated code is structurally sound.
//
// The test files live in `pkg/codegen/testing/test/testdata` and
// are registered in `var sdkTests` in `sdk_driver.go`.
//
// An SDK code generation test files consists of a schema and a set of
// expected outputs for each language. Each test is structured as a
// directory that contains that information:
//
2022-09-14 02:12:02 +00:00
// testdata/
// my-simple-schema/ # i.e. `simple-enum-schema`
// schema.(json|yaml)
// go/
// python/
// nodejs/
// dotnet/
// ...
//
// The schema is the only piece that *must* be manually authored.
//
// Once the schema has been written, the actual codegen outputs can be
// generated by running the following in `pkg/codegen` directory:
//
2022-09-14 02:12:02 +00:00
// PULUMI_ACCEPT=true go test ./...
//
// This will rebuild subfolders such as `go/` from scratch and store
// the set of code-generated file names in `go/codegen-manifest.json`.
// If these outputs look correct, they need to be checked into git and
// will then serve as the expected values for the normal test runs:
//
2022-09-14 02:12:02 +00:00
// go test ./...
//
// That is, the normal test runs will fail if changes to codegen or
// schema lead to a diff in the generated file set. If the diff is
// intentional, it can be accepted again via `PULUMI_ACCEPT=true`.
//
// To support running unit tests over the generated code, the tests
// also support mixing in manually written `$lang-extras` files into
// the generated tree. For example, given the following input:
//
2022-09-14 02:12:02 +00:00
// testdata/
// my-simple-schema/
// schema.json
// go/
// go-extras/
// tests/
// go_test.go
//
// The system will copy `go-extras/tests/go_test.go` into
// `go/tests/go_test.go` before performing compilation and unit test
// checks over the project generated in `go`.
func TestSDKCodegen(t *testing.T, opts *SDKCodegenOptions) { // revive:disable-line
if runtime.GOOS == "windows" {
t.Skip("TestSDKCodegen is skipped on Windows")
}
testDir := filepath.Join("..", "testing", "test", "testdata")
require.NotNil(t, opts.TestCases, "No test cases were provided. This was probably a mistake")
for _, tt := range opts.TestCases {
tt := tt // avoid capturing loop variable `sdkTest` in the closure
t.Run(tt.Directory, func(t *testing.T) {
t.Parallel()
tt.Mutex.Lock()
t.Cleanup(tt.Mutex.Unlock)
t.Log(tt.Description)
dirPath := filepath.Join(testDir, filepath.FromSlash(tt.Directory))
schemaPath := filepath.Join(dirPath, "schema.json")
if _, err := os.Stat(schemaPath); err != nil && os.IsNotExist(err) {
schemaPath = filepath.Join(dirPath, "schema.yaml")
}
if tt.ShouldSkipCodegen(opts.Language) {
t.Logf("Skipping generation + tests for %s", tt.Directory)
return
}
files, err := GeneratePackageFilesFromSchema(schemaPath, opts.GenPackage)
require.NoError(t, err)
if !RewriteFilesWhenPulumiAccept(t, dirPath, opts.Language, files) {
expectedFiles, err := LoadBaseline(dirPath, opts.Language)
require.NoError(t, err)
if !ValidateFileEquality(t, files, expectedFiles) {
t.Fail()
}
}
if genSDKOnly {
return
}
CopyExtraFiles(t, dirPath, opts.Language)
// Merge language-specific global and
// test-specific checks, with test-specific
// having precedence.
allChecks := make(map[string]CodegenCheck)
for k, v := range opts.Checks {
allChecks[k] = v
}
for k, v := range tt.Checks {
allChecks[k] = v
}
// Sort the checks in alphabetical order.
var checkOrder []string
for check := range allChecks {
checkOrder = append(checkOrder, check)
}
sort.Strings(checkOrder)
codeDir := filepath.Join(dirPath, opts.Language)
// Perform the checks.
//nolint:paralleltest // test functions are ordered
for _, check := range checkOrder {
check := check
t.Run(check, func(t *testing.T) {
if tt.ShouldSkipTest(opts.Language, check) {
t.Skip()
}
checkFun := allChecks[check]
checkFun(t, codeDir)
})
}
})
}
}