2021-09-21 22:02:10 +00:00
|
|
|
//nolint:lll
|
|
|
|
package testing
|
|
|
|
|
|
|
|
import (
|
2021-11-20 01:39:11 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2021-09-21 22:02:10 +00:00
|
|
|
"pgregory.net/rapid"
|
|
|
|
|
|
|
|
"github.com/pulumi/pulumi/sdk/v3/go/common/resource"
|
Move assets and archives to their own package (#15157)
<!---
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
This PR is motivated by https://github.com/pulumi/pulumi/pull/15145.
`resource.*` should be built on top of `property.Value`,[^1] which means
that `resource`
needs to be able to import `property.Value`, and so `property` cannot
import
`resource`. Since Assets and Archives are both types of properties, they
must be moved out
of `resource`.
[^1]: For example:
https://github.com/pulumi/pulumi/blob/a1d686227cd7e3c70c51bd772450cb0cd57c1479/sdk/go/common/resource/resource_state.go#L35-L36
## Open Question
This PR moves them to their own sub-folders in `resource`. Should
`asset` and `archive`
live somewhere more high level, like `sdk/go/property/{asset,archive}`?
<!--- Please include a summary of the change and which issue is fixed.
Please also include relevant motivation and context. -->
## 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. -->
2024-01-25 20:39:31 +00:00
|
|
|
"github.com/pulumi/pulumi/sdk/v3/go/common/resource/archive"
|
|
|
|
"github.com/pulumi/pulumi/sdk/v3/go/common/resource/asset"
|
2021-09-21 22:02:10 +00:00
|
|
|
"github.com/pulumi/pulumi/sdk/v3/go/common/tokens"
|
|
|
|
)
|
|
|
|
|
2021-09-30 21:39:09 +00:00
|
|
|
// A StackContext provides context for generating URNs and references to resources.
|
|
|
|
type StackContext struct {
|
|
|
|
projectName string
|
|
|
|
stackName string
|
|
|
|
resources []*resource.State
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewStackContext creates a new stack context with the given project name, stack name, and list of resources.
|
|
|
|
func NewStackContext(projectName, stackName string, resources ...*resource.State) *StackContext {
|
|
|
|
ctx := &StackContext{projectName: projectName, stackName: stackName}
|
|
|
|
return ctx.Append(resources...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ProjectName returns the context's project name.
|
|
|
|
func (ctx *StackContext) ProjectName() string {
|
|
|
|
return ctx.projectName
|
|
|
|
}
|
|
|
|
|
|
|
|
// StackName returns the context's stack name.
|
|
|
|
func (ctx *StackContext) StackName() string {
|
|
|
|
return ctx.stackName
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resources returns the context's resources.
|
|
|
|
func (ctx *StackContext) Resources() []*resource.State {
|
|
|
|
return ctx.resources
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append creates a new context that contains the current context's resources and the given list of resources.
|
|
|
|
func (ctx *StackContext) Append(r ...*resource.State) *StackContext {
|
|
|
|
rs := make([]*resource.State, len(ctx.resources)+len(r))
|
|
|
|
copy(rs, ctx.resources)
|
|
|
|
copy(rs[len(ctx.resources):], r)
|
|
|
|
return &StackContext{ctx.projectName, ctx.stackName, rs}
|
|
|
|
}
|
|
|
|
|
|
|
|
// URNGenerator generates URNs that are valid within the context (i.e. the project name and stack name portions of the
|
|
|
|
// generated URNs will always be taken from the context).
|
2023-02-16 19:56:45 +00:00
|
|
|
func (ctx *StackContext) URNGenerator() *rapid.Generator[resource.URN] {
|
2021-09-30 21:39:09 +00:00
|
|
|
return urnGenerator(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// URNSampler samples URNs from the stack's resources.
|
2023-02-16 19:56:45 +00:00
|
|
|
func (ctx *StackContext) URNSampler() *rapid.Generator[resource.URN] {
|
2021-09-30 21:39:09 +00:00
|
|
|
return rapid.Custom(func(t *rapid.T) resource.URN {
|
2023-02-16 19:56:45 +00:00
|
|
|
return rapid.SampledFrom(ctx.Resources()).Draw(t, "referenced resource").URN
|
2021-09-30 21:39:09 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResourceReferenceGenerator generates resource.ResourceReference values. The referenced resource is
|
2023-02-16 19:56:45 +00:00
|
|
|
func (ctx *StackContext) ResourceReferenceGenerator() *rapid.Generator[resource.ResourceReference] {
|
2021-09-30 21:39:09 +00:00
|
|
|
if len(ctx.Resources()) == 0 {
|
|
|
|
panic("cannot generate resource references: stack context has no resources")
|
|
|
|
}
|
|
|
|
return resourceReferenceGenerator(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResourceReferencePropertyGenerator generates resource reference resource.PropertyValues.
|
2023-02-16 19:56:45 +00:00
|
|
|
func (ctx *StackContext) ResourceReferencePropertyGenerator() *rapid.Generator[resource.PropertyValue] {
|
2021-09-30 21:39:09 +00:00
|
|
|
return resourceReferencePropertyGenerator(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ArrayPropertyGenerator generates array resource.PropertyValues. The maxDepth parameter controls the maximum
|
|
|
|
// depth of the elements of the array.
|
2023-02-16 19:56:45 +00:00
|
|
|
func (ctx *StackContext) ArrayPropertyGenerator(maxDepth int) *rapid.Generator[resource.PropertyValue] {
|
2021-09-30 21:39:09 +00:00
|
|
|
return arrayPropertyGenerator(ctx, maxDepth)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PropertyMapGenerator generates resource.PropertyMap values. The maxDepth parameter controls the maximum
|
|
|
|
// depth of the elements of the map.
|
2023-02-16 19:56:45 +00:00
|
|
|
func (ctx *StackContext) PropertyMapGenerator(maxDepth int) *rapid.Generator[resource.PropertyMap] {
|
2021-09-30 21:39:09 +00:00
|
|
|
return propertyMapGenerator(ctx, maxDepth)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ObjectPropertyGenerator generates object resource.PropertyValues. The maxDepth parameter controls the maximum
|
|
|
|
// depth of the elements of the object.
|
2023-02-16 19:56:45 +00:00
|
|
|
func (ctx *StackContext) ObjectPropertyGenerator(maxDepth int) *rapid.Generator[resource.PropertyValue] {
|
2021-09-30 21:39:09 +00:00
|
|
|
return objectPropertyGenerator(ctx, maxDepth)
|
|
|
|
}
|
|
|
|
|
|
|
|
// OutputPropertyGenerator generates output resource.PropertyValues. The maxDepth parameter controls the maximum
|
|
|
|
// depth of the resolved value of the output, if any. The output's dependencies will only refer to resources in
|
|
|
|
// the context.
|
2023-02-16 19:56:45 +00:00
|
|
|
func (ctx *StackContext) OutputPropertyGenerator(maxDepth int) *rapid.Generator[resource.PropertyValue] {
|
2021-09-30 21:39:09 +00:00
|
|
|
return outputPropertyGenerator(ctx, maxDepth)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SecretPropertyGenerator generates secret resource.PropertyValues. The maxDepth parameter controls the maximum
|
|
|
|
// depth of the plaintext value of the secret, if any.
|
2023-02-16 19:56:45 +00:00
|
|
|
func (ctx *StackContext) SecretPropertyGenerator(maxDepth int) *rapid.Generator[resource.PropertyValue] {
|
2021-09-30 21:39:09 +00:00
|
|
|
return secretPropertyGenerator(ctx, maxDepth)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PropertyValueGenerator generates arbitrary resource.PropertyValues. The maxDepth parameter controls the maximum
|
|
|
|
// number of times the generator may recur.
|
2023-02-16 19:56:45 +00:00
|
|
|
func (ctx *StackContext) PropertyValueGenerator(maxDepth int) *rapid.Generator[resource.PropertyValue] {
|
2021-09-30 21:39:09 +00:00
|
|
|
return propertyValueGenerator(ctx, maxDepth)
|
|
|
|
}
|
|
|
|
|
2021-09-21 22:02:10 +00:00
|
|
|
// TypeGenerator generates legal tokens.Type values.
|
2023-02-16 19:56:45 +00:00
|
|
|
func TypeGenerator() *rapid.Generator[tokens.Type] {
|
2021-09-21 22:02:10 +00:00
|
|
|
return rapid.Custom(func(t *rapid.T) tokens.Type {
|
2023-02-16 19:56:45 +00:00
|
|
|
return tokens.Type(rapid.StringMatching(`^[a-zA-Z][-a-zA-Z0-9_]*:([^0-9][a-zA-Z0-9._/]*)?:[^0-9][a-zA-Z0-9._/]*$`).Draw(t, "type token"))
|
2021-09-21 22:02:10 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// URNGenerator generates legal resource.URN values.
|
2023-02-16 19:56:45 +00:00
|
|
|
func URNGenerator() *rapid.Generator[resource.URN] {
|
2021-09-30 21:39:09 +00:00
|
|
|
return urnGenerator(nil)
|
|
|
|
}
|
|
|
|
|
2023-02-16 19:56:45 +00:00
|
|
|
func urnGenerator(ctx *StackContext) *rapid.Generator[resource.URN] {
|
|
|
|
var stackNameGenerator, projectNameGenerator *rapid.Generator[string]
|
2021-09-30 21:39:09 +00:00
|
|
|
if ctx == nil {
|
|
|
|
stackNameGenerator = rapid.StringMatching(`^((:[^:])[^:]*)*:?$`)
|
|
|
|
projectNameGenerator = rapid.StringMatching(`^((:[^:])[^:]*)*:?$`)
|
|
|
|
} else {
|
|
|
|
stackNameGenerator = rapid.Just(ctx.StackName())
|
|
|
|
projectNameGenerator = rapid.Just(ctx.ProjectName())
|
|
|
|
}
|
|
|
|
|
2021-09-21 22:02:10 +00:00
|
|
|
return rapid.Custom(func(t *rapid.T) resource.URN {
|
2023-02-16 19:56:45 +00:00
|
|
|
stackName := tokens.QName(stackNameGenerator.Draw(t, "stack name"))
|
|
|
|
projectName := tokens.PackageName(projectNameGenerator.Draw(t, "project name"))
|
|
|
|
parentType := TypeGenerator().Draw(t, "parent type")
|
|
|
|
resourceType := TypeGenerator().Draw(t, "resource type")
|
2023-11-20 08:59:00 +00:00
|
|
|
resourceName := rapid.StringMatching(`^((:[^:])[^:]*)*:?$`).Draw(t, "resource name")
|
2021-09-21 22:02:10 +00:00
|
|
|
return resource.NewURN(stackName, projectName, parentType, resourceType, resourceName)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// IDGenerator generates legal resource.ID values.
|
2023-02-16 19:56:45 +00:00
|
|
|
func IDGenerator() *rapid.Generator[resource.ID] {
|
2021-09-21 22:02:10 +00:00
|
|
|
return rapid.Custom(func(t *rapid.T) resource.ID {
|
2023-02-16 19:56:45 +00:00
|
|
|
return resource.ID(rapid.StringMatching(`..*`).Draw(t, "ids"))
|
2021-09-21 22:02:10 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// SemverStringGenerator generates legal semver strings.
|
2023-02-16 19:56:45 +00:00
|
|
|
func SemverStringGenerator() *rapid.Generator[string] {
|
2021-09-21 22:02:10 +00:00
|
|
|
return rapid.StringMatching(`^v?(?P<major>0|[1-9]\d*)\.(?P<minor>0|[1-9]\d*)\.(?P<patch>0|[1-9]\d*)(?:-(?P<prerelease>(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?P<buildmetadata>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$`)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnknownPropertyGenerator generates the unknown resource.PropertyValue.
|
2023-02-16 19:56:45 +00:00
|
|
|
func UnknownPropertyGenerator() *rapid.Generator[resource.PropertyValue] {
|
2021-09-21 22:02:10 +00:00
|
|
|
return rapid.Custom(func(t *rapid.T) resource.PropertyValue {
|
2023-02-16 19:56:45 +00:00
|
|
|
return rapid.Just(resource.MakeComputed(resource.NewStringProperty(""))).Draw(t, "unknowns")
|
2021-09-21 22:02:10 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// NullPropertyGenerator generates the null resource.PropertyValue.
|
2023-02-16 19:56:45 +00:00
|
|
|
func NullPropertyGenerator() *rapid.Generator[resource.PropertyValue] {
|
2021-09-21 22:02:10 +00:00
|
|
|
return rapid.Custom(func(t *rapid.T) resource.PropertyValue {
|
2023-02-16 19:56:45 +00:00
|
|
|
return rapid.Just(resource.NewNullProperty()).Draw(t, "nulls")
|
2021-09-21 22:02:10 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// BoolPropertyGenerator generates boolean resource.PropertyValues.
|
2023-02-16 19:56:45 +00:00
|
|
|
func BoolPropertyGenerator() *rapid.Generator[resource.PropertyValue] {
|
2021-09-21 22:02:10 +00:00
|
|
|
return rapid.Custom(func(t *rapid.T) resource.PropertyValue {
|
2023-02-16 19:56:45 +00:00
|
|
|
return resource.NewBoolProperty(rapid.Bool().Draw(t, "booleans"))
|
2021-09-21 22:02:10 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// NumberPropertyGenerator generates numeric resource.PropertyValues.
|
2023-02-16 19:56:45 +00:00
|
|
|
func NumberPropertyGenerator() *rapid.Generator[resource.PropertyValue] {
|
2021-09-21 22:02:10 +00:00
|
|
|
return rapid.Custom(func(t *rapid.T) resource.PropertyValue {
|
2023-02-16 19:56:45 +00:00
|
|
|
return resource.NewNumberProperty(rapid.Float64().Draw(t, "numbers"))
|
2021-09-21 22:02:10 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// StringPropertyGenerator generates string resource.PropertyValues.
|
2023-02-16 19:56:45 +00:00
|
|
|
func StringPropertyGenerator() *rapid.Generator[resource.PropertyValue] {
|
2021-09-21 22:02:10 +00:00
|
|
|
return rapid.Custom(func(t *rapid.T) resource.PropertyValue {
|
2023-02-16 19:56:45 +00:00
|
|
|
return resource.NewStringProperty(rapid.String().Draw(t, "strings"))
|
2021-09-21 22:02:10 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
Move assets and archives to their own package (#15157)
<!---
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
This PR is motivated by https://github.com/pulumi/pulumi/pull/15145.
`resource.*` should be built on top of `property.Value`,[^1] which means
that `resource`
needs to be able to import `property.Value`, and so `property` cannot
import
`resource`. Since Assets and Archives are both types of properties, they
must be moved out
of `resource`.
[^1]: For example:
https://github.com/pulumi/pulumi/blob/a1d686227cd7e3c70c51bd772450cb0cd57c1479/sdk/go/common/resource/resource_state.go#L35-L36
## Open Question
This PR moves them to their own sub-folders in `resource`. Should
`asset` and `archive`
live somewhere more high level, like `sdk/go/property/{asset,archive}`?
<!--- Please include a summary of the change and which issue is fixed.
Please also include relevant motivation and context. -->
## 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. -->
2024-01-25 20:39:31 +00:00
|
|
|
// TextAssetGenerator generates textual *asset.Asset values.
|
|
|
|
func TextAssetGenerator() *rapid.Generator[*asset.Asset] {
|
|
|
|
return rapid.Custom(func(t *rapid.T) *asset.Asset {
|
|
|
|
asset, err := asset.FromText(rapid.String().Draw(t, "text asset contents"))
|
2021-11-20 01:39:11 +00:00
|
|
|
assert.NoError(t, err)
|
2021-09-21 22:02:10 +00:00
|
|
|
return asset
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
Move assets and archives to their own package (#15157)
<!---
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
This PR is motivated by https://github.com/pulumi/pulumi/pull/15145.
`resource.*` should be built on top of `property.Value`,[^1] which means
that `resource`
needs to be able to import `property.Value`, and so `property` cannot
import
`resource`. Since Assets and Archives are both types of properties, they
must be moved out
of `resource`.
[^1]: For example:
https://github.com/pulumi/pulumi/blob/a1d686227cd7e3c70c51bd772450cb0cd57c1479/sdk/go/common/resource/resource_state.go#L35-L36
## Open Question
This PR moves them to their own sub-folders in `resource`. Should
`asset` and `archive`
live somewhere more high level, like `sdk/go/property/{asset,archive}`?
<!--- Please include a summary of the change and which issue is fixed.
Please also include relevant motivation and context. -->
## 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. -->
2024-01-25 20:39:31 +00:00
|
|
|
// AssetGenerator generates *asset.Asset values.
|
|
|
|
func AssetGenerator() *rapid.Generator[*asset.Asset] {
|
2021-09-21 22:02:10 +00:00
|
|
|
return TextAssetGenerator()
|
|
|
|
}
|
|
|
|
|
|
|
|
// AssetPropertyGenerator generates asset resource.PropertyValues.
|
2023-02-16 19:56:45 +00:00
|
|
|
func AssetPropertyGenerator() *rapid.Generator[resource.PropertyValue] {
|
2021-09-21 22:02:10 +00:00
|
|
|
return rapid.Custom(func(t *rapid.T) resource.PropertyValue {
|
2023-02-16 19:56:45 +00:00
|
|
|
return resource.NewAssetProperty(AssetGenerator().Draw(t, "assets"))
|
2021-09-21 22:02:10 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
Move assets and archives to their own package (#15157)
<!---
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
This PR is motivated by https://github.com/pulumi/pulumi/pull/15145.
`resource.*` should be built on top of `property.Value`,[^1] which means
that `resource`
needs to be able to import `property.Value`, and so `property` cannot
import
`resource`. Since Assets and Archives are both types of properties, they
must be moved out
of `resource`.
[^1]: For example:
https://github.com/pulumi/pulumi/blob/a1d686227cd7e3c70c51bd772450cb0cd57c1479/sdk/go/common/resource/resource_state.go#L35-L36
## Open Question
This PR moves them to their own sub-folders in `resource`. Should
`asset` and `archive`
live somewhere more high level, like `sdk/go/property/{asset,archive}`?
<!--- Please include a summary of the change and which issue is fixed.
Please also include relevant motivation and context. -->
## 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. -->
2024-01-25 20:39:31 +00:00
|
|
|
// LiteralArchiveGenerator generates *archive.Archive values with literal archive contents.
|
|
|
|
func LiteralArchiveGenerator(maxDepth int) *rapid.Generator[*archive.Archive] {
|
|
|
|
return rapid.Custom(func(t *rapid.T) *archive.Archive {
|
2023-02-16 19:56:45 +00:00
|
|
|
var contentsGenerator *rapid.Generator[map[string]interface{}]
|
2021-09-21 22:02:10 +00:00
|
|
|
if maxDepth > 0 {
|
2023-02-16 19:56:45 +00:00
|
|
|
contentsGenerator = rapid.MapOfN(
|
|
|
|
rapid.StringMatching(`^(/[^[:cntrl:]/]+)*/?[^[:cntrl:]/]+$`),
|
|
|
|
rapid.OneOf(AssetGenerator().AsAny(), ArchiveGenerator(maxDepth-1).AsAny()),
|
|
|
|
0, // min length
|
|
|
|
16, // max length
|
|
|
|
)
|
2021-09-21 22:02:10 +00:00
|
|
|
} else {
|
|
|
|
contentsGenerator = rapid.Just(map[string]interface{}{})
|
|
|
|
}
|
Move assets and archives to their own package (#15157)
<!---
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
This PR is motivated by https://github.com/pulumi/pulumi/pull/15145.
`resource.*` should be built on top of `property.Value`,[^1] which means
that `resource`
needs to be able to import `property.Value`, and so `property` cannot
import
`resource`. Since Assets and Archives are both types of properties, they
must be moved out
of `resource`.
[^1]: For example:
https://github.com/pulumi/pulumi/blob/a1d686227cd7e3c70c51bd772450cb0cd57c1479/sdk/go/common/resource/resource_state.go#L35-L36
## Open Question
This PR moves them to their own sub-folders in `resource`. Should
`asset` and `archive`
live somewhere more high level, like `sdk/go/property/{asset,archive}`?
<!--- Please include a summary of the change and which issue is fixed.
Please also include relevant motivation and context. -->
## 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. -->
2024-01-25 20:39:31 +00:00
|
|
|
archive, err := archive.FromAssets(contentsGenerator.Draw(t, "literal archive contents"))
|
2021-11-20 01:39:11 +00:00
|
|
|
assert.NoError(t, err)
|
2021-09-21 22:02:10 +00:00
|
|
|
return archive
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
Move assets and archives to their own package (#15157)
<!---
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
This PR is motivated by https://github.com/pulumi/pulumi/pull/15145.
`resource.*` should be built on top of `property.Value`,[^1] which means
that `resource`
needs to be able to import `property.Value`, and so `property` cannot
import
`resource`. Since Assets and Archives are both types of properties, they
must be moved out
of `resource`.
[^1]: For example:
https://github.com/pulumi/pulumi/blob/a1d686227cd7e3c70c51bd772450cb0cd57c1479/sdk/go/common/resource/resource_state.go#L35-L36
## Open Question
This PR moves them to their own sub-folders in `resource`. Should
`asset` and `archive`
live somewhere more high level, like `sdk/go/property/{asset,archive}`?
<!--- Please include a summary of the change and which issue is fixed.
Please also include relevant motivation and context. -->
## 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. -->
2024-01-25 20:39:31 +00:00
|
|
|
// ArchiveGenerator generates *archive.Archive values.
|
|
|
|
func ArchiveGenerator(maxDepth int) *rapid.Generator[*archive.Archive] {
|
2021-09-21 22:02:10 +00:00
|
|
|
return LiteralArchiveGenerator(maxDepth)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ArchivePropertyGenerator generates archive resource.PropertyValues.
|
2023-02-16 19:56:45 +00:00
|
|
|
func ArchivePropertyGenerator(maxDepth int) *rapid.Generator[resource.PropertyValue] {
|
2021-09-21 22:02:10 +00:00
|
|
|
return rapid.Custom(func(t *rapid.T) resource.PropertyValue {
|
2023-02-16 19:56:45 +00:00
|
|
|
return resource.NewArchiveProperty(ArchiveGenerator(maxDepth).Draw(t, "archives"))
|
2021-09-21 22:02:10 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResourceReferenceGenerator generates resource.ResourceReference values.
|
2023-02-16 19:56:45 +00:00
|
|
|
func ResourceReferenceGenerator() *rapid.Generator[resource.ResourceReference] {
|
2021-09-30 21:39:09 +00:00
|
|
|
return resourceReferenceGenerator(nil)
|
|
|
|
}
|
|
|
|
|
2023-02-16 19:56:45 +00:00
|
|
|
func resourceReferenceGenerator(ctx *StackContext) *rapid.Generator[resource.ResourceReference] {
|
|
|
|
var resourceGenerator *rapid.Generator[*resource.State]
|
2021-09-30 21:39:09 +00:00
|
|
|
if ctx == nil {
|
|
|
|
resourceGenerator = rapid.Custom(func(t *rapid.T) *resource.State {
|
|
|
|
id := resource.ID("")
|
2023-02-16 19:56:45 +00:00
|
|
|
custom := !rapid.Bool().Draw(t, "component")
|
2021-09-30 21:39:09 +00:00
|
|
|
if custom {
|
2023-02-16 19:56:45 +00:00
|
|
|
id = IDGenerator().Draw(t, "resource ID")
|
2021-09-30 21:39:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &resource.State{
|
2023-02-16 19:56:45 +00:00
|
|
|
URN: URNGenerator().Draw(t, "resource URN"),
|
2021-09-30 21:39:09 +00:00
|
|
|
Custom: custom,
|
|
|
|
ID: id,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
resourceGenerator = rapid.SampledFrom(ctx.Resources())
|
|
|
|
}
|
|
|
|
|
2021-09-21 22:02:10 +00:00
|
|
|
return rapid.Custom(func(t *rapid.T) resource.ResourceReference {
|
2023-02-16 19:56:45 +00:00
|
|
|
r := resourceGenerator.Draw(t, "referenced resource")
|
2021-09-30 21:39:09 +00:00
|
|
|
|
|
|
|
// Only pull the resource's ID if it is a custom resource. Component resources do not have IDs.
|
|
|
|
var id resource.PropertyValue
|
|
|
|
if r.Custom {
|
2023-02-16 19:56:45 +00:00
|
|
|
id = rapid.OneOf(
|
|
|
|
UnknownPropertyGenerator(),
|
|
|
|
rapid.Just(resource.NewStringProperty(string(r.ID))),
|
|
|
|
).Draw(t, "referenced ID")
|
2021-09-21 22:02:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return resource.ResourceReference{
|
2021-09-30 21:39:09 +00:00
|
|
|
URN: r.URN,
|
2021-09-21 22:02:10 +00:00
|
|
|
ID: id,
|
2023-02-16 19:56:45 +00:00
|
|
|
PackageVersion: SemverStringGenerator().Draw(t, "package version"),
|
2021-09-21 22:02:10 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-09-30 21:39:09 +00:00
|
|
|
// ResourceReferencePropertyGenerator generates resource reference resource.PropertyValues.
|
2023-02-16 19:56:45 +00:00
|
|
|
func ResourceReferencePropertyGenerator() *rapid.Generator[resource.PropertyValue] {
|
2021-09-30 21:39:09 +00:00
|
|
|
return resourceReferencePropertyGenerator(nil)
|
|
|
|
}
|
|
|
|
|
2023-02-16 19:56:45 +00:00
|
|
|
func resourceReferencePropertyGenerator(ctx *StackContext) *rapid.Generator[resource.PropertyValue] {
|
2021-09-21 22:02:10 +00:00
|
|
|
return rapid.Custom(func(t *rapid.T) resource.PropertyValue {
|
2023-02-16 19:56:45 +00:00
|
|
|
return resource.NewResourceReferenceProperty(resourceReferenceGenerator(ctx).Draw(t, "resource reference"))
|
2021-09-21 22:02:10 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// ArrayPropertyGenerator generates array resource.PropertyValues. The maxDepth parameter controls the maximum
|
|
|
|
// depth of the elements of the array.
|
2023-02-16 19:56:45 +00:00
|
|
|
func ArrayPropertyGenerator(maxDepth int) *rapid.Generator[resource.PropertyValue] {
|
2021-09-30 21:39:09 +00:00
|
|
|
return arrayPropertyGenerator(nil, maxDepth)
|
|
|
|
}
|
|
|
|
|
2023-02-16 19:56:45 +00:00
|
|
|
func arrayPropertyGenerator(ctx *StackContext, maxDepth int) *rapid.Generator[resource.PropertyValue] {
|
2021-09-21 22:02:10 +00:00
|
|
|
return rapid.Custom(func(t *rapid.T) resource.PropertyValue {
|
2023-02-16 19:56:45 +00:00
|
|
|
return resource.NewArrayProperty(
|
|
|
|
rapid.SliceOfN(propertyValueGenerator(ctx, maxDepth-1), 0, 32).
|
|
|
|
Draw(t, "array elements"))
|
2021-09-21 22:02:10 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// PropertyKeyGenerator generates legal resource.PropertyKey values.
|
2023-02-16 19:56:45 +00:00
|
|
|
func PropertyKeyGenerator() *rapid.Generator[resource.PropertyKey] {
|
2021-09-21 22:02:10 +00:00
|
|
|
return rapid.Custom(func(t *rapid.T) resource.PropertyKey {
|
2023-02-16 19:56:45 +00:00
|
|
|
return resource.PropertyKey(rapid.String().Draw(t, "property key"))
|
2021-09-21 22:02:10 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// PropertyMapGenerator generates resource.PropertyMap values. The maxDepth parameter controls the maximum
|
|
|
|
// depth of the elements of the map.
|
2023-02-16 19:56:45 +00:00
|
|
|
func PropertyMapGenerator(maxDepth int) *rapid.Generator[resource.PropertyMap] {
|
2021-09-30 21:39:09 +00:00
|
|
|
return propertyMapGenerator(nil, maxDepth)
|
|
|
|
}
|
|
|
|
|
2023-02-16 19:56:45 +00:00
|
|
|
func propertyMapGenerator(ctx *StackContext, maxDepth int) *rapid.Generator[resource.PropertyMap] {
|
2021-09-21 22:02:10 +00:00
|
|
|
return rapid.Custom(func(t *rapid.T) resource.PropertyMap {
|
2023-02-16 19:56:45 +00:00
|
|
|
return resource.PropertyMap(
|
|
|
|
rapid.MapOfN(
|
|
|
|
PropertyKeyGenerator(),
|
|
|
|
propertyValueGenerator(ctx, maxDepth-1),
|
|
|
|
0, // min length
|
|
|
|
32, // max length
|
|
|
|
).Draw(t, "property map"))
|
2021-09-21 22:02:10 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// ObjectPropertyGenerator generates object resource.PropertyValues. The maxDepth parameter controls the maximum
|
|
|
|
// depth of the elements of the object.
|
2023-02-16 19:56:45 +00:00
|
|
|
func ObjectPropertyGenerator(maxDepth int) *rapid.Generator[resource.PropertyValue] {
|
2021-09-30 21:39:09 +00:00
|
|
|
return objectPropertyGenerator(nil, maxDepth)
|
|
|
|
}
|
|
|
|
|
2023-02-16 19:56:45 +00:00
|
|
|
func objectPropertyGenerator(ctx *StackContext, maxDepth int) *rapid.Generator[resource.PropertyValue] {
|
2021-09-21 22:02:10 +00:00
|
|
|
return rapid.Custom(func(t *rapid.T) resource.PropertyValue {
|
2023-02-16 19:56:45 +00:00
|
|
|
return resource.NewObjectProperty(propertyMapGenerator(ctx, maxDepth).Draw(t, "object contents"))
|
2021-09-21 22:02:10 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// OutputPropertyGenerator generates output resource.PropertyValues. The maxDepth parameter controls the maximum
|
2021-09-30 21:39:09 +00:00
|
|
|
// depth of the resolved value of the output, if any. If a StackContext, the output's dependencies will only refer to
|
|
|
|
// resources in the context.
|
2023-02-16 19:56:45 +00:00
|
|
|
func OutputPropertyGenerator(maxDepth int) *rapid.Generator[resource.PropertyValue] {
|
2021-09-30 21:39:09 +00:00
|
|
|
return outputPropertyGenerator(nil, maxDepth)
|
|
|
|
}
|
|
|
|
|
2023-02-16 19:56:45 +00:00
|
|
|
func outputPropertyGenerator(ctx *StackContext, maxDepth int) *rapid.Generator[resource.PropertyValue] {
|
|
|
|
var urnGenerator *rapid.Generator[resource.URN]
|
2021-09-30 21:39:09 +00:00
|
|
|
var dependenciesUpperBound int
|
|
|
|
if ctx == nil {
|
|
|
|
urnGenerator, dependenciesUpperBound = URNGenerator(), 32
|
|
|
|
} else {
|
|
|
|
urnGenerator = ctx.URNSampler()
|
|
|
|
dependenciesUpperBound = len(ctx.Resources())
|
|
|
|
if dependenciesUpperBound > 32 {
|
|
|
|
dependenciesUpperBound = 32
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-21 22:02:10 +00:00
|
|
|
return rapid.Custom(func(t *rapid.T) resource.PropertyValue {
|
|
|
|
var element resource.PropertyValue
|
|
|
|
|
2023-02-16 19:56:45 +00:00
|
|
|
known := rapid.Bool().Draw(t, "known")
|
2021-09-21 22:02:10 +00:00
|
|
|
if known {
|
2023-02-16 19:56:45 +00:00
|
|
|
element = propertyValueGenerator(ctx, maxDepth-1).Draw(t, "output element")
|
2021-09-21 22:02:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return resource.NewOutputProperty(resource.Output{
|
|
|
|
Element: element,
|
|
|
|
Known: known,
|
2023-02-16 19:56:45 +00:00
|
|
|
Secret: rapid.Bool().Draw(t, "secret"),
|
|
|
|
Dependencies: rapid.SliceOfN(urnGenerator, 0, dependenciesUpperBound).Draw(t, "dependencies"),
|
2021-09-21 22:02:10 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// SecretPropertyGenerator generates secret resource.PropertyValues. The maxDepth parameter controls the maximum
|
|
|
|
// depth of the plaintext value of the secret, if any.
|
2023-02-16 19:56:45 +00:00
|
|
|
func SecretPropertyGenerator(maxDepth int) *rapid.Generator[resource.PropertyValue] {
|
2021-09-30 21:39:09 +00:00
|
|
|
return secretPropertyGenerator(nil, maxDepth)
|
|
|
|
}
|
|
|
|
|
2023-02-16 19:56:45 +00:00
|
|
|
func secretPropertyGenerator(ctx *StackContext, maxDepth int) *rapid.Generator[resource.PropertyValue] {
|
2021-09-21 22:02:10 +00:00
|
|
|
return rapid.Custom(func(t *rapid.T) resource.PropertyValue {
|
|
|
|
return resource.NewSecretProperty(&resource.Secret{
|
2023-02-16 19:56:45 +00:00
|
|
|
Element: propertyValueGenerator(ctx, maxDepth-1).Draw(t, "secret element"),
|
2021-09-21 22:02:10 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// PropertyValueGenerator generates arbitrary resource.PropertyValues. The maxDepth parameter controls the maximum
|
|
|
|
// number of times the generator may recur.
|
2023-02-16 19:56:45 +00:00
|
|
|
func PropertyValueGenerator(maxDepth int) *rapid.Generator[resource.PropertyValue] {
|
2021-09-30 21:39:09 +00:00
|
|
|
return propertyValueGenerator(nil, maxDepth)
|
|
|
|
}
|
|
|
|
|
2023-02-16 19:56:45 +00:00
|
|
|
func propertyValueGenerator(ctx *StackContext, maxDepth int) *rapid.Generator[resource.PropertyValue] {
|
|
|
|
choices := []*rapid.Generator[resource.PropertyValue]{
|
2021-09-21 22:02:10 +00:00
|
|
|
UnknownPropertyGenerator(),
|
|
|
|
NullPropertyGenerator(),
|
|
|
|
BoolPropertyGenerator(),
|
|
|
|
NumberPropertyGenerator(),
|
|
|
|
StringPropertyGenerator(),
|
|
|
|
AssetPropertyGenerator(),
|
|
|
|
}
|
2021-09-30 21:39:09 +00:00
|
|
|
|
|
|
|
if ctx == nil || len(ctx.Resources()) > 0 {
|
|
|
|
choices = append(choices, resourceReferencePropertyGenerator(ctx))
|
|
|
|
}
|
|
|
|
|
2021-09-21 22:02:10 +00:00
|
|
|
if maxDepth > 0 {
|
|
|
|
choices = append(choices,
|
|
|
|
ArchivePropertyGenerator(maxDepth),
|
2021-09-30 21:39:09 +00:00
|
|
|
arrayPropertyGenerator(ctx, maxDepth),
|
|
|
|
objectPropertyGenerator(ctx, maxDepth),
|
|
|
|
outputPropertyGenerator(ctx, maxDepth),
|
|
|
|
secretPropertyGenerator(ctx, maxDepth))
|
2021-09-21 22:02:10 +00:00
|
|
|
}
|
|
|
|
return rapid.OneOf(choices...)
|
|
|
|
}
|