2021-09-21 17:00:44 +00:00
|
|
|
//nolint:revive
|
2020-10-15 17:35:09 +00:00
|
|
|
package lifecycletest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"reflect"
|
2022-07-12 16:39:07 +00:00
|
|
|
"sync"
|
2020-10-15 17:35:09 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"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"
|
|
|
|
"github.com/pulumi/pulumi/sdk/v3/go/common/resource"
|
|
|
|
"github.com/pulumi/pulumi/sdk/v3/go/common/resource/config"
|
|
|
|
"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 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2022-07-12 16:39:07 +00:00
|
|
|
var wg sync.WaitGroup
|
2020-10-15 17:35:09 +00:00
|
|
|
var firedEvents []Event
|
2022-07-12 16:39:07 +00:00
|
|
|
wg.Add(1)
|
2020-10-15 17:35:09 +00:00
|
|
|
go func() {
|
|
|
|
for e := range events {
|
|
|
|
firedEvents = append(firedEvents, e)
|
|
|
|
}
|
2022-07-12 16:39:07 +00:00
|
|
|
wg.Done()
|
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)
|
|
|
|
wg.Wait()
|
2020-10-15 17:35:09 +00:00
|
|
|
contract.IgnoreClose(journal)
|
|
|
|
|
|
|
|
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
|
|
|
}
|
2021-11-24 22:13:29 +00:00
|
|
|
if dryRun {
|
2023-10-11 14:44:09 +00:00
|
|
|
return plan, nil, opErr
|
2021-11-24 22:13:29 +00:00
|
|
|
}
|
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 := journal.Snap(target.Snapshot)
|
2023-10-11 14:44:09 +00:00
|
|
|
if opErr == nil && err != nil {
|
|
|
|
opErr = err
|
|
|
|
} else if opErr == nil && snap != nil {
|
|
|
|
opErr = snap.VerifyIntegrity()
|
2020-10-15 17:35:09 +00:00
|
|
|
}
|
2023-10-11 14:44:09 +00:00
|
|
|
return nil, snap, opErr
|
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
|
|
|
|
}
|
|
|
|
|
2022-03-17 21:37:11 +00:00
|
|
|
func (p *TestPlan) getNames() (stack tokens.Name, 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"
|
|
|
|
}
|
2022-03-17 21:37:11 +00:00
|
|
|
stack = tokens.Name(p.Stack)
|
2020-10-15 17:35:09 +00:00
|
|
|
if stack == "" {
|
|
|
|
stack = "test"
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
2022-03-17 21:37:11 +00:00
|
|
|
return resource.NewURN(stack.Q(), project, pt, typ, tokens.QName(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
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|