2021-09-21 17:00:44 +00:00
|
|
|
//nolint:revive
|
2020-10-15 17:35:09 +00:00
|
|
|
package lifecycletest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-01-03 17:32:13 +00:00
|
|
|
"errors"
|
2020-10-15 17:35:09 +00:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
2023-12-10 21:29:37 +00:00
|
|
|
"github.com/blang/semver"
|
2020-10-15 17:35:09 +00:00
|
|
|
"github.com/mitchellh/copystructure"
|
|
|
|
"github.com/stretchr/testify/assert"
|
[engine] Only record a resource's chosen alias. (#9288)
As we discovered when removing aliases from the state entirely, the
snapshotter needs to be alias-aware so that it can fix up references to
resources that were aliased. After a resource operation finishes, the
snapshotter needs to write out a new copy of the snapshot. However, at
the time we write the snapshot, there may be resources that have not yet
been registered that refer to the just-registered resources by a
different URN due to aliasing. Those references need to be fixed up
prior to writing the snapshot in order to preserve the snapshot's
integrity (in particular, the property that all URNs refer to resources
that exist in the snapshot).
For example, consider the following simple dependency graph: A <-- B.
When that graph is serialized, B will contain a reference to A in its
dependency list. Let the next run of the program produces the graph A'
<-- B where A' is aliased to A. After A' is registered, the snapshotter
needs to write a snapshot that contains its state, but B must also be
updated so it references A' instead of A, which will no longer be in the
snapshot.
These changes take advantage of the fact that although a resource can
provide multiple aliases, it can only ever resolve those aliases to a
single resource in the existing state. Therefore, at the time the
statefile is fixed up, each resource in the statefile could only have
been aliased to a single old resource, and it is sufficient to store
only the URN of the chosen resource rather than all possible aliases. In
addition to preserving the ability to fix up references to aliased
resources, retaining the chosen alias allows the history of a logical
resource to be followed across aliases.
2022-03-28 15:36:08 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-10-15 17:35:09 +00:00
|
|
|
|
2023-09-18 11:01:28 +00:00
|
|
|
"github.com/pulumi/pulumi/pkg/v3/display"
|
2021-03-17 13:20:05 +00:00
|
|
|
. "github.com/pulumi/pulumi/pkg/v3/engine"
|
|
|
|
"github.com/pulumi/pulumi/pkg/v3/resource/deploy"
|
2023-09-28 21:50:18 +00:00
|
|
|
"github.com/pulumi/pulumi/pkg/v3/resource/deploy/deploytest"
|
2021-03-17 13:20:05 +00:00
|
|
|
"github.com/pulumi/pulumi/pkg/v3/resource/deploy/providers"
|
|
|
|
"github.com/pulumi/pulumi/pkg/v3/util/cancel"
|
2023-11-15 14:53:12 +00:00
|
|
|
"github.com/pulumi/pulumi/sdk/v3/go/common/promise"
|
2021-03-17 13:20:05 +00:00
|
|
|
"github.com/pulumi/pulumi/sdk/v3/go/common/resource"
|
|
|
|
"github.com/pulumi/pulumi/sdk/v3/go/common/resource/config"
|
2023-12-10 21:29:37 +00:00
|
|
|
"github.com/pulumi/pulumi/sdk/v3/go/common/resource/plugin"
|
|
|
|
"github.com/pulumi/pulumi/sdk/v3/go/common/slice"
|
2021-03-17 13:20:05 +00:00
|
|
|
"github.com/pulumi/pulumi/sdk/v3/go/common/tokens"
|
|
|
|
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
|
|
|
|
"github.com/pulumi/pulumi/sdk/v3/go/common/util/result"
|
|
|
|
"github.com/pulumi/pulumi/sdk/v3/go/common/workspace"
|
2020-10-15 17:35:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type updateInfo struct {
|
|
|
|
project workspace.Project
|
|
|
|
target deploy.Target
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *updateInfo) GetRoot() string {
|
2024-01-25 23:28:58 +00:00
|
|
|
// These tests run in-memory, so we don't have a real root. Just pretend we're at the filesystem root.
|
|
|
|
return "/"
|
2020-10-15 17:35:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (u *updateInfo) GetProject() *workspace.Project {
|
|
|
|
return &u.project
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *updateInfo) GetTarget() *deploy.Target {
|
|
|
|
return &u.target
|
|
|
|
}
|
|
|
|
|
2020-11-11 05:11:30 +00:00
|
|
|
func ImportOp(imports []deploy.Import) TestOp {
|
2022-01-31 10:31:51 +00:00
|
|
|
return TestOp(func(info UpdateInfo, ctx *Context, opts UpdateOptions,
|
2023-03-03 16:36:39 +00:00
|
|
|
dryRun bool,
|
2023-10-11 14:44:09 +00:00
|
|
|
) (*deploy.Plan, display.ResourceChanges, error) {
|
2020-11-11 05:11:30 +00:00
|
|
|
return Import(info, ctx, opts, imports, dryRun)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-10-11 14:44:09 +00:00
|
|
|
type TestOp func(UpdateInfo, *Context, UpdateOptions, bool) (*deploy.Plan, display.ResourceChanges, error)
|
2020-11-11 05:11:30 +00:00
|
|
|
|
2020-10-15 17:35:09 +00:00
|
|
|
type ValidateFunc func(project workspace.Project, target deploy.Target, entries JournalEntries,
|
2023-10-11 14:44:09 +00:00
|
|
|
events []Event, err error) error
|
2020-10-15 17:35:09 +00:00
|
|
|
|
2023-09-28 21:50:18 +00:00
|
|
|
func (op TestOp) Plan(project workspace.Project, target deploy.Target, opts TestUpdateOptions,
|
2023-03-03 16:36:39 +00:00
|
|
|
backendClient deploy.BackendClient, validate ValidateFunc,
|
2023-10-11 14:44:09 +00:00
|
|
|
) (*deploy.Plan, error) {
|
|
|
|
plan, _, err := op.runWithContext(context.Background(), project, target, opts, true, backendClient, validate)
|
|
|
|
return plan, err
|
2022-01-31 10:31:51 +00:00
|
|
|
}
|
|
|
|
|
2023-09-28 21:50:18 +00:00
|
|
|
func (op TestOp) Run(project workspace.Project, target deploy.Target, opts TestUpdateOptions,
|
2023-03-03 16:36:39 +00:00
|
|
|
dryRun bool, backendClient deploy.BackendClient, validate ValidateFunc,
|
2023-10-11 14:44:09 +00:00
|
|
|
) (*deploy.Snapshot, error) {
|
2020-10-15 17:35:09 +00:00
|
|
|
return op.RunWithContext(context.Background(), project, target, opts, dryRun, backendClient, validate)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (op TestOp) RunWithContext(
|
|
|
|
callerCtx context.Context, project workspace.Project,
|
2023-09-28 21:50:18 +00:00
|
|
|
target deploy.Target, opts TestUpdateOptions, dryRun bool,
|
2023-03-03 16:36:39 +00:00
|
|
|
backendClient deploy.BackendClient, validate ValidateFunc,
|
2023-10-11 14:44:09 +00:00
|
|
|
) (*deploy.Snapshot, error) {
|
|
|
|
_, snap, err := op.runWithContext(callerCtx, project, target, opts, dryRun, backendClient, validate)
|
|
|
|
return snap, err
|
2022-01-31 10:31:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (op TestOp) runWithContext(
|
|
|
|
callerCtx context.Context, project workspace.Project,
|
2023-09-28 21:50:18 +00:00
|
|
|
target deploy.Target, opts TestUpdateOptions, dryRun bool,
|
2023-03-03 16:36:39 +00:00
|
|
|
backendClient deploy.BackendClient, validate ValidateFunc,
|
2023-10-11 14:44:09 +00:00
|
|
|
) (*deploy.Plan, *deploy.Snapshot, error) {
|
2020-10-15 17:35:09 +00:00
|
|
|
// Create an appropriate update info and context.
|
|
|
|
info := &updateInfo{project: project, target: target}
|
|
|
|
|
|
|
|
cancelCtx, cancelSrc := cancel.NewContext(context.Background())
|
|
|
|
done := make(chan bool)
|
|
|
|
defer close(done)
|
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case <-callerCtx.Done():
|
|
|
|
cancelSrc.Cancel()
|
|
|
|
case <-done:
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
events := make(chan Event)
|
|
|
|
journal := NewJournal()
|
|
|
|
|
|
|
|
ctx := &Context{
|
|
|
|
Cancel: cancelCtx,
|
|
|
|
Events: events,
|
|
|
|
SnapshotManager: journal,
|
|
|
|
BackendClient: backendClient,
|
|
|
|
}
|
|
|
|
|
2023-09-28 21:50:18 +00:00
|
|
|
updateOpts := opts.Options()
|
|
|
|
defer func() {
|
|
|
|
if updateOpts.Host != nil {
|
|
|
|
contract.IgnoreClose(updateOpts.Host)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-10-15 17:35:09 +00:00
|
|
|
// Begin draining events.
|
2023-11-15 14:53:12 +00:00
|
|
|
firedEventsPromise := promise.Run(func() ([]Event, error) {
|
|
|
|
var firedEvents []Event
|
2020-10-15 17:35:09 +00:00
|
|
|
for e := range events {
|
|
|
|
firedEvents = append(firedEvents, e)
|
|
|
|
}
|
2023-11-15 14:53:12 +00:00
|
|
|
return firedEvents, nil
|
|
|
|
})
|
2020-10-15 17:35:09 +00:00
|
|
|
|
|
|
|
// Run the step and its validator.
|
2023-10-11 14:44:09 +00:00
|
|
|
plan, _, opErr := op(info, ctx, updateOpts, dryRun)
|
2022-07-12 16:39:07 +00:00
|
|
|
close(events)
|
2024-01-03 17:32:13 +00:00
|
|
|
closeErr := journal.Close()
|
2020-10-15 17:35:09 +00:00
|
|
|
|
2023-11-16 09:58:30 +00:00
|
|
|
// Wait for the events to finish. You'd think this would cancel with the callerCtx but tests explicitly use that for
|
|
|
|
// the deployment context, not expecting it to have any effect on the test code here. See
|
|
|
|
// https://github.com/pulumi/pulumi/issues/14588 for what happens if you try to use callerCtx here.
|
|
|
|
firedEvents, err := firedEventsPromise.Result(context.Background())
|
2023-11-15 14:53:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2020-10-15 17:35:09 +00:00
|
|
|
if validate != nil {
|
2023-10-11 14:44:09 +00:00
|
|
|
opErr = validate(project, target, journal.Entries(), firedEvents, opErr)
|
2020-10-15 17:35:09 +00:00
|
|
|
}
|
2024-01-03 17:32:13 +00:00
|
|
|
errs := []error{opErr, closeErr}
|
2021-11-24 22:13:29 +00:00
|
|
|
if dryRun {
|
2024-01-03 17:32:13 +00:00
|
|
|
return plan, nil, errors.Join(errs...)
|
2021-11-24 22:13:29 +00:00
|
|
|
}
|
2020-10-15 17:35:09 +00:00
|
|
|
|
2024-01-05 23:16:40 +00:00
|
|
|
entries := journal.Entries()
|
|
|
|
// Check that each possible snapshot we could have created is valid
|
|
|
|
var snap *deploy.Snapshot
|
|
|
|
for i := 0; i <= len(entries); i++ {
|
|
|
|
var err error
|
|
|
|
snap, err = entries[0:i].Snap(target.Snapshot)
|
|
|
|
if err != nil {
|
|
|
|
// if any snapshot fails to create just return this error, don't keep going
|
|
|
|
errs = append(errs, err)
|
|
|
|
snap = nil
|
|
|
|
break
|
|
|
|
}
|
2024-01-03 17:32:13 +00:00
|
|
|
err = snap.VerifyIntegrity()
|
|
|
|
if err != nil {
|
2024-01-05 23:16:40 +00:00
|
|
|
// Likewise as soon as one snapshot fails to validate stop checking
|
2024-01-03 17:32:13 +00:00
|
|
|
errs = append(errs, err)
|
2024-01-05 23:16:40 +00:00
|
|
|
snap = nil
|
|
|
|
break
|
2024-01-03 17:32:13 +00:00
|
|
|
}
|
2020-10-15 17:35:09 +00:00
|
|
|
}
|
2024-01-03 17:32:13 +00:00
|
|
|
|
|
|
|
return nil, snap, errors.Join(errs...)
|
2020-10-15 17:35:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type TestStep struct {
|
|
|
|
Op TestOp
|
|
|
|
ExpectFailure bool
|
|
|
|
SkipPreview bool
|
|
|
|
Validate ValidateFunc
|
|
|
|
}
|
|
|
|
|
2021-12-17 22:52:01 +00:00
|
|
|
func (t *TestStep) ValidateAnd(f ValidateFunc) {
|
|
|
|
o := t.Validate
|
|
|
|
t.Validate = func(project workspace.Project, target deploy.Target, entries JournalEntries,
|
2023-10-11 14:44:09 +00:00
|
|
|
events []Event, err error,
|
|
|
|
) error {
|
|
|
|
r := o(project, target, entries, events, err)
|
2021-12-17 22:52:01 +00:00
|
|
|
if r != nil {
|
|
|
|
return r
|
|
|
|
}
|
2023-10-11 14:44:09 +00:00
|
|
|
return f(project, target, entries, events, err)
|
2021-12-17 22:52:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-28 21:50:18 +00:00
|
|
|
// TestUpdateOptions is UpdateOptions for a TestPlan.
|
|
|
|
type TestUpdateOptions struct {
|
|
|
|
UpdateOptions
|
|
|
|
// a factory to produce a plugin host for an update operation.
|
|
|
|
HostF deploytest.PluginHostFactory
|
|
|
|
}
|
|
|
|
|
|
|
|
// Options produces UpdateOptions for an update operation.
|
|
|
|
func (o TestUpdateOptions) Options() UpdateOptions {
|
|
|
|
opts := o.UpdateOptions
|
|
|
|
if o.HostF != nil {
|
|
|
|
opts.Host = o.HostF()
|
|
|
|
}
|
|
|
|
return opts
|
|
|
|
}
|
|
|
|
|
2020-10-15 17:35:09 +00:00
|
|
|
type TestPlan struct {
|
|
|
|
Project string
|
|
|
|
Stack string
|
|
|
|
Runtime string
|
|
|
|
RuntimeOptions map[string]interface{}
|
|
|
|
Config config.Map
|
|
|
|
Decrypter config.Decrypter
|
|
|
|
BackendClient deploy.BackendClient
|
2023-09-28 21:50:18 +00:00
|
|
|
Options TestUpdateOptions
|
2020-10-15 17:35:09 +00:00
|
|
|
Steps []TestStep
|
|
|
|
}
|
|
|
|
|
Add tokens.StackName (#14487)
<!---
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 adds a new type `tokens.StackName` which is a relatively strongly
typed container for a stack name. The only weakly typed aspect of it is
Go will always allow the "zero" value to be created for a struct, which
for a stack name is the empty string which is invalid. To prevent
introducing unexpected empty strings when working with stack names the
`String()` method will panic for zero initialized stack names.
Apart from the zero value, all other instances of `StackName` are via
`ParseStackName` which returns a descriptive error if the string is not
valid.
This PR only updates "pkg/" to use this type. There are a number of
places in "sdk/" which could do with this type as well, but there's no
harm in doing a staggered roll out, and some parts of "sdk/" are user
facing and will probably have to stay on the current `tokens.Name` and
`tokens.QName` types.
There are two places in the system where we panic on invalid stack
names, both in the http backend. This _should_ be fine as we've had long
standing validation that stacks created in the service are valid stack
names.
Just in case people have managed to introduce invalid stack names, there
is the `PULUMI_DISABLE_VALIDATION` environment variable which will turn
off the validation _and_ panicing for stack names. Users can use that to
temporarily disable the validation and continue working, but it should
only be seen as a temporary measure. If they have invalid names they
should rename them, or if they think they should be valid raise an issue
with us to change the validation code.
## 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.
-->
- [ ] 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-15 07:44:54 +00:00
|
|
|
func (p *TestPlan) getNames() (stack tokens.StackName, project tokens.PackageName, runtime string) {
|
2020-10-15 17:35:09 +00:00
|
|
|
project = tokens.PackageName(p.Project)
|
|
|
|
if project == "" {
|
|
|
|
project = "test"
|
|
|
|
}
|
|
|
|
runtime = p.Runtime
|
|
|
|
if runtime == "" {
|
|
|
|
runtime = "test"
|
|
|
|
}
|
Add tokens.StackName (#14487)
<!---
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 adds a new type `tokens.StackName` which is a relatively strongly
typed container for a stack name. The only weakly typed aspect of it is
Go will always allow the "zero" value to be created for a struct, which
for a stack name is the empty string which is invalid. To prevent
introducing unexpected empty strings when working with stack names the
`String()` method will panic for zero initialized stack names.
Apart from the zero value, all other instances of `StackName` are via
`ParseStackName` which returns a descriptive error if the string is not
valid.
This PR only updates "pkg/" to use this type. There are a number of
places in "sdk/" which could do with this type as well, but there's no
harm in doing a staggered roll out, and some parts of "sdk/" are user
facing and will probably have to stay on the current `tokens.Name` and
`tokens.QName` types.
There are two places in the system where we panic on invalid stack
names, both in the http backend. This _should_ be fine as we've had long
standing validation that stacks created in the service are valid stack
names.
Just in case people have managed to introduce invalid stack names, there
is the `PULUMI_DISABLE_VALIDATION` environment variable which will turn
off the validation _and_ panicing for stack names. Users can use that to
temporarily disable the validation and continue working, but it should
only be seen as a temporary measure. If they have invalid names they
should rename them, or if they think they should be valid raise an issue
with us to change the validation code.
## 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.
-->
- [ ] 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-15 07:44:54 +00:00
|
|
|
stack = tokens.MustParseStackName("test")
|
|
|
|
if p.Stack != "" {
|
|
|
|
stack = tokens.MustParseStackName(p.Stack)
|
2020-10-15 17:35:09 +00:00
|
|
|
}
|
|
|
|
return stack, project, runtime
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *TestPlan) NewURN(typ tokens.Type, name string, parent resource.URN) resource.URN {
|
|
|
|
stack, project, _ := p.getNames()
|
|
|
|
var pt tokens.Type
|
|
|
|
if parent != "" {
|
2023-09-14 19:52:27 +00:00
|
|
|
pt = parent.QualifiedType()
|
2020-10-15 17:35:09 +00:00
|
|
|
}
|
2023-11-20 08:59:00 +00:00
|
|
|
return resource.NewURN(stack.Q(), project, pt, typ, name)
|
2020-10-15 17:35:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *TestPlan) NewProviderURN(pkg tokens.Package, name string, parent resource.URN) resource.URN {
|
|
|
|
return p.NewURN(providers.MakeProviderType(pkg), name, parent)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *TestPlan) GetProject() workspace.Project {
|
|
|
|
_, projectName, runtime := p.getNames()
|
|
|
|
|
|
|
|
return workspace.Project{
|
|
|
|
Name: projectName,
|
|
|
|
Runtime: workspace.NewProjectRuntimeInfo(runtime, p.RuntimeOptions),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-04 23:28:49 +00:00
|
|
|
func (p *TestPlan) GetTarget(t testing.TB, snapshot *deploy.Snapshot) deploy.Target {
|
2020-10-15 17:35:09 +00:00
|
|
|
stack, _, _ := p.getNames()
|
|
|
|
|
|
|
|
cfg := p.Config
|
|
|
|
if cfg == nil {
|
|
|
|
cfg = config.Map{}
|
|
|
|
}
|
|
|
|
|
|
|
|
return deploy.Target{
|
|
|
|
Name: stack,
|
|
|
|
Config: cfg,
|
|
|
|
Decrypter: p.Decrypter,
|
2021-12-09 09:09:48 +00:00
|
|
|
// note: it's really important that the preview and update operate on different snapshots. the engine can and
|
|
|
|
// does mutate the snapshot in-place, even in previews, and sharing a snapshot between preview and update can
|
|
|
|
// cause state changes from the preview to persist even when doing an update.
|
|
|
|
Snapshot: CloneSnapshot(t, snapshot),
|
2020-10-15 17:35:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// CloneSnapshot makes a deep copy of the given snapshot and returns a pointer to the clone.
|
2023-05-04 23:28:49 +00:00
|
|
|
func CloneSnapshot(t testing.TB, snap *deploy.Snapshot) *deploy.Snapshot {
|
2020-10-15 17:35:09 +00:00
|
|
|
t.Helper()
|
|
|
|
if snap != nil {
|
|
|
|
copiedSnap := copystructure.Must(copystructure.Copy(*snap)).(deploy.Snapshot)
|
|
|
|
assert.True(t, reflect.DeepEqual(*snap, copiedSnap))
|
|
|
|
return &copiedSnap
|
|
|
|
}
|
|
|
|
|
|
|
|
return snap
|
|
|
|
}
|
|
|
|
|
2023-05-04 23:28:49 +00:00
|
|
|
func (p *TestPlan) Run(t testing.TB, snapshot *deploy.Snapshot) *deploy.Snapshot {
|
2020-10-15 17:35:09 +00:00
|
|
|
project := p.GetProject()
|
|
|
|
snap := snapshot
|
|
|
|
for _, step := range p.Steps {
|
|
|
|
// note: it's really important that the preview and update operate on different snapshots. the engine can and
|
|
|
|
// does mutate the snapshot in-place, even in previews, and sharing a snapshot between preview and update can
|
|
|
|
// cause state changes from the preview to persist even when doing an update.
|
2021-12-09 09:09:48 +00:00
|
|
|
// GetTarget ALWAYS clones the snapshot, so the previewTarget.Snapshot != target.Snapshot
|
2020-10-15 17:35:09 +00:00
|
|
|
if !step.SkipPreview {
|
2021-12-09 09:09:48 +00:00
|
|
|
previewTarget := p.GetTarget(t, snap)
|
2021-11-24 22:13:29 +00:00
|
|
|
// Don't run validate on the preview step
|
2023-10-11 14:44:09 +00:00
|
|
|
_, err := step.Op.Run(project, previewTarget, p.Options, true, p.BackendClient, nil)
|
2020-10-15 17:35:09 +00:00
|
|
|
if step.ExpectFailure {
|
2023-10-13 09:46:07 +00:00
|
|
|
assert.Error(t, err)
|
2020-10-15 17:35:09 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-10-13 09:46:07 +00:00
|
|
|
assert.NoError(t, err)
|
2020-10-15 17:35:09 +00:00
|
|
|
}
|
|
|
|
|
2023-10-11 14:44:09 +00:00
|
|
|
var err error
|
2021-12-09 09:09:48 +00:00
|
|
|
target := p.GetTarget(t, snap)
|
2023-10-11 14:44:09 +00:00
|
|
|
snap, err = step.Op.Run(project, target, p.Options, false, p.BackendClient, step.Validate)
|
2020-10-15 17:35:09 +00:00
|
|
|
if step.ExpectFailure {
|
2023-10-13 09:46:07 +00:00
|
|
|
assert.Error(t, err)
|
2020-10-15 17:35:09 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-10-11 14:44:09 +00:00
|
|
|
if err != nil {
|
|
|
|
if result.IsBail(err) {
|
2023-10-13 09:46:07 +00:00
|
|
|
t.Logf("Got unexpected bail result: %v", err)
|
2020-10-15 17:35:09 +00:00
|
|
|
t.FailNow()
|
|
|
|
} else {
|
2023-10-11 14:44:09 +00:00
|
|
|
t.Logf("Got unexpected error result: %v", err)
|
2020-10-15 17:35:09 +00:00
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-13 09:46:07 +00:00
|
|
|
assert.NoError(t, err)
|
2020-10-15 17:35:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return snap
|
|
|
|
}
|
|
|
|
|
2022-06-23 00:30:01 +00:00
|
|
|
// resCount is the expected number of resources registered during this test.
|
2020-10-15 17:35:09 +00:00
|
|
|
func MakeBasicLifecycleSteps(t *testing.T, resCount int) []TestStep {
|
|
|
|
return []TestStep{
|
|
|
|
// Initial update
|
|
|
|
{
|
|
|
|
Op: Update,
|
|
|
|
Validate: func(project workspace.Project, target deploy.Target, entries JournalEntries,
|
2023-10-11 14:44:09 +00:00
|
|
|
_ []Event, err error,
|
|
|
|
) error {
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-09-29 23:05:45 +00:00
|
|
|
// Should see only creates or reads.
|
2020-10-15 17:35:09 +00:00
|
|
|
for _, entry := range entries {
|
2021-09-29 23:05:45 +00:00
|
|
|
op := entry.Step.Op()
|
|
|
|
assert.True(t, op == deploy.OpCreate || op == deploy.OpRead)
|
2020-10-15 17:35:09 +00:00
|
|
|
}
|
[engine] Only record a resource's chosen alias. (#9288)
As we discovered when removing aliases from the state entirely, the
snapshotter needs to be alias-aware so that it can fix up references to
resources that were aliased. After a resource operation finishes, the
snapshotter needs to write out a new copy of the snapshot. However, at
the time we write the snapshot, there may be resources that have not yet
been registered that refer to the just-registered resources by a
different URN due to aliasing. Those references need to be fixed up
prior to writing the snapshot in order to preserve the snapshot's
integrity (in particular, the property that all URNs refer to resources
that exist in the snapshot).
For example, consider the following simple dependency graph: A <-- B.
When that graph is serialized, B will contain a reference to A in its
dependency list. Let the next run of the program produces the graph A'
<-- B where A' is aliased to A. After A' is registered, the snapshotter
needs to write a snapshot that contains its state, but B must also be
updated so it references A' instead of A, which will no longer be in the
snapshot.
These changes take advantage of the fact that although a resource can
provide multiple aliases, it can only ever resolve those aliases to a
single resource in the existing state. Therefore, at the time the
statefile is fixed up, each resource in the statefile could only have
been aliased to a single old resource, and it is sufficient to store
only the URN of the chosen resource rather than all possible aliases. In
addition to preserving the ability to fix up references to aliased
resources, retaining the chosen alias allows the history of a logical
resource to be followed across aliases.
2022-03-28 15:36:08 +00:00
|
|
|
snap, err := entries.Snap(target.Snapshot)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Len(t, snap.Resources, resCount)
|
2023-10-11 14:44:09 +00:00
|
|
|
return err
|
2020-10-15 17:35:09 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
// No-op refresh
|
|
|
|
{
|
|
|
|
Op: Refresh,
|
|
|
|
Validate: func(project workspace.Project, target deploy.Target, entries JournalEntries,
|
2023-10-11 14:44:09 +00:00
|
|
|
_ []Event, err error,
|
|
|
|
) error {
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-10-15 17:35:09 +00:00
|
|
|
// Should see only refresh-sames.
|
|
|
|
for _, entry := range entries {
|
|
|
|
assert.Equal(t, deploy.OpRefresh, entry.Step.Op())
|
|
|
|
assert.Equal(t, deploy.OpSame, entry.Step.(*deploy.RefreshStep).ResultOp())
|
|
|
|
}
|
[engine] Only record a resource's chosen alias. (#9288)
As we discovered when removing aliases from the state entirely, the
snapshotter needs to be alias-aware so that it can fix up references to
resources that were aliased. After a resource operation finishes, the
snapshotter needs to write out a new copy of the snapshot. However, at
the time we write the snapshot, there may be resources that have not yet
been registered that refer to the just-registered resources by a
different URN due to aliasing. Those references need to be fixed up
prior to writing the snapshot in order to preserve the snapshot's
integrity (in particular, the property that all URNs refer to resources
that exist in the snapshot).
For example, consider the following simple dependency graph: A <-- B.
When that graph is serialized, B will contain a reference to A in its
dependency list. Let the next run of the program produces the graph A'
<-- B where A' is aliased to A. After A' is registered, the snapshotter
needs to write a snapshot that contains its state, but B must also be
updated so it references A' instead of A, which will no longer be in the
snapshot.
These changes take advantage of the fact that although a resource can
provide multiple aliases, it can only ever resolve those aliases to a
single resource in the existing state. Therefore, at the time the
statefile is fixed up, each resource in the statefile could only have
been aliased to a single old resource, and it is sufficient to store
only the URN of the chosen resource rather than all possible aliases. In
addition to preserving the ability to fix up references to aliased
resources, retaining the chosen alias allows the history of a logical
resource to be followed across aliases.
2022-03-28 15:36:08 +00:00
|
|
|
snap, err := entries.Snap(target.Snapshot)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Len(t, snap.Resources, resCount)
|
2023-10-11 14:44:09 +00:00
|
|
|
return err
|
2020-10-15 17:35:09 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
// No-op update
|
|
|
|
{
|
|
|
|
Op: Update,
|
|
|
|
Validate: func(project workspace.Project, target deploy.Target, entries JournalEntries,
|
2023-10-11 14:44:09 +00:00
|
|
|
_ []Event, err error,
|
|
|
|
) error {
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-10-15 17:35:09 +00:00
|
|
|
// Should see only sames.
|
|
|
|
for _, entry := range entries {
|
2021-09-29 23:05:45 +00:00
|
|
|
op := entry.Step.Op()
|
|
|
|
assert.True(t, op == deploy.OpSame || op == deploy.OpRead)
|
2020-10-15 17:35:09 +00:00
|
|
|
}
|
[engine] Only record a resource's chosen alias. (#9288)
As we discovered when removing aliases from the state entirely, the
snapshotter needs to be alias-aware so that it can fix up references to
resources that were aliased. After a resource operation finishes, the
snapshotter needs to write out a new copy of the snapshot. However, at
the time we write the snapshot, there may be resources that have not yet
been registered that refer to the just-registered resources by a
different URN due to aliasing. Those references need to be fixed up
prior to writing the snapshot in order to preserve the snapshot's
integrity (in particular, the property that all URNs refer to resources
that exist in the snapshot).
For example, consider the following simple dependency graph: A <-- B.
When that graph is serialized, B will contain a reference to A in its
dependency list. Let the next run of the program produces the graph A'
<-- B where A' is aliased to A. After A' is registered, the snapshotter
needs to write a snapshot that contains its state, but B must also be
updated so it references A' instead of A, which will no longer be in the
snapshot.
These changes take advantage of the fact that although a resource can
provide multiple aliases, it can only ever resolve those aliases to a
single resource in the existing state. Therefore, at the time the
statefile is fixed up, each resource in the statefile could only have
been aliased to a single old resource, and it is sufficient to store
only the URN of the chosen resource rather than all possible aliases. In
addition to preserving the ability to fix up references to aliased
resources, retaining the chosen alias allows the history of a logical
resource to be followed across aliases.
2022-03-28 15:36:08 +00:00
|
|
|
snap, err := entries.Snap(target.Snapshot)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Len(t, snap.Resources, resCount)
|
2023-10-11 14:44:09 +00:00
|
|
|
return err
|
2020-10-15 17:35:09 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
// No-op refresh
|
|
|
|
{
|
|
|
|
Op: Refresh,
|
|
|
|
Validate: func(project workspace.Project, target deploy.Target, entries JournalEntries,
|
2023-10-11 14:44:09 +00:00
|
|
|
_ []Event, err error,
|
|
|
|
) error {
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-10-15 17:35:09 +00:00
|
|
|
// Should see only refresh-sames.
|
|
|
|
for _, entry := range entries {
|
|
|
|
assert.Equal(t, deploy.OpRefresh, entry.Step.Op())
|
|
|
|
assert.Equal(t, deploy.OpSame, entry.Step.(*deploy.RefreshStep).ResultOp())
|
|
|
|
}
|
[engine] Only record a resource's chosen alias. (#9288)
As we discovered when removing aliases from the state entirely, the
snapshotter needs to be alias-aware so that it can fix up references to
resources that were aliased. After a resource operation finishes, the
snapshotter needs to write out a new copy of the snapshot. However, at
the time we write the snapshot, there may be resources that have not yet
been registered that refer to the just-registered resources by a
different URN due to aliasing. Those references need to be fixed up
prior to writing the snapshot in order to preserve the snapshot's
integrity (in particular, the property that all URNs refer to resources
that exist in the snapshot).
For example, consider the following simple dependency graph: A <-- B.
When that graph is serialized, B will contain a reference to A in its
dependency list. Let the next run of the program produces the graph A'
<-- B where A' is aliased to A. After A' is registered, the snapshotter
needs to write a snapshot that contains its state, but B must also be
updated so it references A' instead of A, which will no longer be in the
snapshot.
These changes take advantage of the fact that although a resource can
provide multiple aliases, it can only ever resolve those aliases to a
single resource in the existing state. Therefore, at the time the
statefile is fixed up, each resource in the statefile could only have
been aliased to a single old resource, and it is sufficient to store
only the URN of the chosen resource rather than all possible aliases. In
addition to preserving the ability to fix up references to aliased
resources, retaining the chosen alias allows the history of a logical
resource to be followed across aliases.
2022-03-28 15:36:08 +00:00
|
|
|
snap, err := entries.Snap(target.Snapshot)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Len(t, snap.Resources, resCount)
|
2023-10-11 14:44:09 +00:00
|
|
|
return err
|
2020-10-15 17:35:09 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
// Destroy
|
|
|
|
{
|
|
|
|
Op: Destroy,
|
|
|
|
Validate: func(project workspace.Project, target deploy.Target, entries JournalEntries,
|
2023-10-11 14:44:09 +00:00
|
|
|
_ []Event, err error,
|
|
|
|
) error {
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-10-15 17:35:09 +00:00
|
|
|
// Should see only deletes.
|
|
|
|
for _, entry := range entries {
|
|
|
|
switch entry.Step.Op() {
|
|
|
|
case deploy.OpDelete, deploy.OpReadDiscard:
|
|
|
|
// ok
|
|
|
|
default:
|
|
|
|
assert.Fail(t, "expected OpDelete or OpReadDiscard")
|
|
|
|
}
|
|
|
|
}
|
[engine] Only record a resource's chosen alias. (#9288)
As we discovered when removing aliases from the state entirely, the
snapshotter needs to be alias-aware so that it can fix up references to
resources that were aliased. After a resource operation finishes, the
snapshotter needs to write out a new copy of the snapshot. However, at
the time we write the snapshot, there may be resources that have not yet
been registered that refer to the just-registered resources by a
different URN due to aliasing. Those references need to be fixed up
prior to writing the snapshot in order to preserve the snapshot's
integrity (in particular, the property that all URNs refer to resources
that exist in the snapshot).
For example, consider the following simple dependency graph: A <-- B.
When that graph is serialized, B will contain a reference to A in its
dependency list. Let the next run of the program produces the graph A'
<-- B where A' is aliased to A. After A' is registered, the snapshotter
needs to write a snapshot that contains its state, but B must also be
updated so it references A' instead of A, which will no longer be in the
snapshot.
These changes take advantage of the fact that although a resource can
provide multiple aliases, it can only ever resolve those aliases to a
single resource in the existing state. Therefore, at the time the
statefile is fixed up, each resource in the statefile could only have
been aliased to a single old resource, and it is sufficient to store
only the URN of the chosen resource rather than all possible aliases. In
addition to preserving the ability to fix up references to aliased
resources, retaining the chosen alias allows the history of a logical
resource to be followed across aliases.
2022-03-28 15:36:08 +00:00
|
|
|
snap, err := entries.Snap(target.Snapshot)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Len(t, snap.Resources, 0)
|
2023-10-11 14:44:09 +00:00
|
|
|
return err
|
2020-10-15 17:35:09 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
// No-op refresh
|
|
|
|
{
|
|
|
|
Op: Refresh,
|
|
|
|
Validate: func(project workspace.Project, target deploy.Target, entries JournalEntries,
|
2023-10-11 14:44:09 +00:00
|
|
|
_ []Event, err error,
|
|
|
|
) error {
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-10-15 17:35:09 +00:00
|
|
|
assert.Len(t, entries, 0)
|
[engine] Only record a resource's chosen alias. (#9288)
As we discovered when removing aliases from the state entirely, the
snapshotter needs to be alias-aware so that it can fix up references to
resources that were aliased. After a resource operation finishes, the
snapshotter needs to write out a new copy of the snapshot. However, at
the time we write the snapshot, there may be resources that have not yet
been registered that refer to the just-registered resources by a
different URN due to aliasing. Those references need to be fixed up
prior to writing the snapshot in order to preserve the snapshot's
integrity (in particular, the property that all URNs refer to resources
that exist in the snapshot).
For example, consider the following simple dependency graph: A <-- B.
When that graph is serialized, B will contain a reference to A in its
dependency list. Let the next run of the program produces the graph A'
<-- B where A' is aliased to A. After A' is registered, the snapshotter
needs to write a snapshot that contains its state, but B must also be
updated so it references A' instead of A, which will no longer be in the
snapshot.
These changes take advantage of the fact that although a resource can
provide multiple aliases, it can only ever resolve those aliases to a
single resource in the existing state. Therefore, at the time the
statefile is fixed up, each resource in the statefile could only have
been aliased to a single old resource, and it is sufficient to store
only the URN of the chosen resource rather than all possible aliases. In
addition to preserving the ability to fix up references to aliased
resources, retaining the chosen alias allows the history of a logical
resource to be followed across aliases.
2022-03-28 15:36:08 +00:00
|
|
|
snap, err := entries.Snap(target.Snapshot)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Len(t, snap.Resources, 0)
|
2023-10-11 14:44:09 +00:00
|
|
|
return err
|
2020-10-15 17:35:09 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2023-12-10 21:29:37 +00:00
|
|
|
|
|
|
|
type testBuilder struct {
|
|
|
|
t *testing.T
|
|
|
|
loaders []*deploytest.ProviderLoader
|
|
|
|
snap *deploy.Snapshot
|
|
|
|
}
|
|
|
|
|
|
|
|
func newTestBuilder(t *testing.T, snap *deploy.Snapshot) *testBuilder {
|
|
|
|
return &testBuilder{
|
|
|
|
t: t,
|
|
|
|
snap: snap,
|
|
|
|
loaders: slice.Prealloc[*deploytest.ProviderLoader](1),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *testBuilder) WithProvider(name string, version string, prov *deploytest.Provider) *testBuilder {
|
2024-01-26 16:20:45 +00:00
|
|
|
loader := deploytest.NewProviderLoader(
|
|
|
|
tokens.Package(name), semver.MustParse(version), func() (plugin.Provider, error) {
|
|
|
|
return prov, nil
|
|
|
|
})
|
2023-12-10 21:29:37 +00:00
|
|
|
b.loaders = append(b.loaders, loader)
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
type Result struct {
|
|
|
|
snap *deploy.Snapshot
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *testBuilder) RunUpdate(program func(info plugin.RunInfo, monitor *deploytest.ResourceMonitor) error) *Result {
|
|
|
|
programF := deploytest.NewLanguageRuntimeF(program)
|
|
|
|
hostF := deploytest.NewPluginHostF(nil, nil, programF, b.loaders...)
|
|
|
|
|
|
|
|
p := &TestPlan{
|
|
|
|
Options: TestUpdateOptions{HostF: hostF},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run an update for initial state.
|
|
|
|
var err error
|
|
|
|
snap, err := TestOp(Update).Run(
|
|
|
|
p.GetProject(), p.GetTarget(b.t, b.snap), p.Options, false, p.BackendClient, nil)
|
|
|
|
return &Result{
|
|
|
|
snap: snap,
|
|
|
|
err: err,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Then() is used to convey dependence between program runs via program structure.
|
|
|
|
func (res *Result) Then(do func(snap *deploy.Snapshot, err error)) {
|
|
|
|
do(res.snap, res.err)
|
|
|
|
}
|