pulumi/pkg/resource/edit/operations_test.go

693 lines
18 KiB
Go
Raw Permalink Normal View History

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
// Copyright 2016-2023, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package edit
import (
"testing"
"time"
"github.com/pulumi/pulumi/pkg/v3/secrets/b64"
"github.com/pulumi/pulumi/pkg/v3/resource/deploy"
"github.com/pulumi/pulumi/pkg/v3/resource/deploy/providers"
"github.com/pulumi/pulumi/pkg/v3/version"
Rewrite filestate.RenameStack logic in terms of checkpoints (#13927) <!--- 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. --> The RenameStack function in filestate was one of the few places outside of "cmd" still using `stack.DefaultSecretsProvider`, which it was only using because the stack rename function was written in terms of deserialised snapshots instead of serialised deployments. This change rewrites the RenameStack function to work on the raw deployment data rather than deserialised snapshot. This allows us to remove the use of `stack.DefaultSecretsProvider` from filestate. This would also allow the service to update their use of RenameStack to work on the raw deployment as well, saving them the use of their NoopSecretsProvider. ## 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. --> - [ ] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [ ] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2023-12-22 12:28:41 +00:00
"github.com/pulumi/pulumi/sdk/v3/go/common/apitype"
"github.com/pulumi/pulumi/sdk/v3/go/common/resource"
"github.com/pulumi/pulumi/sdk/v3/go/common/tokens"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func NewResource(name string, provider *resource.State, deps ...resource.URN) *resource.State {
prov := ""
if provider != nil {
p, err := providers.NewReference(provider.URN, provider.ID)
if err != nil {
panic(err)
}
prov = p.String()
}
t := tokens.Type("a:b:c")
return &resource.State{
Type: t,
Allow anything in resource names (#14107) <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> Fixes https://github.com/pulumi/pulumi/issues/13968. Fixes https://github.com/pulumi/pulumi/issues/8949. This requires changing the parsing of URN's slightly, it is _very_ likely that providers will need to update to handle URNs like this correctly. This changes resource names to be `string` not `QName`. We never validated this before and it turns out that users have put all manner of text for resource names so we just updating the system to correctly reflect that. ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [x] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [x] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2023-11-20 08:59:00 +00:00
URN: resource.NewURN("test", "test", "", t, name),
Inputs: resource.PropertyMap{},
Outputs: resource.PropertyMap{},
Dependencies: deps,
Provider: prov,
}
}
func NewProviderResource(pkg, name, id string, deps ...resource.URN) *resource.State {
t := providers.MakeProviderType(tokens.Package(pkg))
return &resource.State{
Type: t,
Allow anything in resource names (#14107) <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> Fixes https://github.com/pulumi/pulumi/issues/13968. Fixes https://github.com/pulumi/pulumi/issues/8949. This requires changing the parsing of URN's slightly, it is _very_ likely that providers will need to update to handle URNs like this correctly. This changes resource names to be `string` not `QName`. We never validated this before and it turns out that users have put all manner of text for resource names so we just updating the system to correctly reflect that. ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [x] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [x] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2023-11-20 08:59:00 +00:00
URN: resource.NewURN("test", "test", "", t, name),
ID: resource.ID(id),
Inputs: resource.PropertyMap{},
Outputs: resource.PropertyMap{},
Dependencies: deps,
}
}
func NewSnapshot(resources []*resource.State) *deploy.Snapshot {
return deploy.NewSnapshot(deploy.Manifest{
Time: time.Now(),
Version: version.Version,
Plugins: nil,
}, b64.NewBase64SecretsManager(), resources, nil)
}
func TestDeletion(t *testing.T) {
t.Parallel()
pA := NewProviderResource("a", "p1", "0")
a := NewResource("a", pA)
b := NewResource("b", pA)
c := NewResource("c", pA)
snap := NewSnapshot([]*resource.State{
pA,
a,
b,
c,
})
err := DeleteResource(snap, b, nil, false)
assert.NoError(t, err)
assert.Len(t, snap.Resources, 3)
assert.Equal(t, []*resource.State{pA, a, c}, snap.Resources)
}
func TestDeletingDuplicateURNs(t *testing.T) {
t.Parallel()
pA := NewProviderResource("a", "p1", "0")
a := NewResource("a", pA)
// Create duplicate resources.
b1 := NewResource("b", pA)
b2 := NewResource("b", pA)
b3 := NewResource("b", pA)
// ensure b1, b2, and b3 must have the same URN.
bURN := b1.URN
assert.Equal(t, bURN, b1.URN)
assert.Equal(t, bURN, b2.URN)
assert.Equal(t, bURN, b3.URN)
// c exists to check behavior on b's dependents.
c := NewResource("c", pA, bURN)
// This test ensures that when targeting dependent resources, deleting a
// resource with a redundant URN will not delete dependent resources in
// state as it's ambiguous since another URN can satisfy the dependency.
t.Run("do-target-dependents", func(t *testing.T) {
t.Parallel()
snap := NewSnapshot([]*resource.State{
pA, a, b1, b2, b3, c,
})
err := DeleteResource(snap, b1, nil, true /* targetDependents */)
require.NoError(t, err)
assert.Equal(t, []*resource.State{
pA, a, b2, b3, c,
}, snap.Resources)
// Ensure that a pointer to b1 is not in the list.
for _, s := range snap.Resources {
assert.False(t, s == b1)
}
})
// This test ensures that when targeting a resource with a redundant URN,
// dependency checks should not block the resource from being deleted from state.
t.Run("do-not-target-dependents", func(t *testing.T) {
t.Parallel()
snap := NewSnapshot([]*resource.State{
pA, a, b1, b2, b3, c,
})
err := DeleteResource(snap, b1, nil, false /* targetDependents */)
require.NoError(t, err)
assert.Equal(t, []*resource.State{
pA, a, b2, b3, c,
}, snap.Resources)
// Ensure that a pointer to b1 is not in the list.
for _, s := range snap.Resources {
assert.False(t, s == b1)
}
})
}
State: fix panic when deleting non-unique Provider (#15322) # Description In https://github.com/pulumi/pulumi/commit/30f59eb30a9b519766aa0e3d3e463a2b4667466b, we made sure that we don't delete multiple resources, when a user is prompted to choose between multiple resources with the same URN. If there are non-unique URNs, we currently assume that we can just go ahead and delete it, as there's still another resource that will fulfill the requirements. However that is not true if the dependent is a provider. Providers are identified by a {URN, ID} tuple. Therefore even though after the deletion of a provider with a specific URN there's still another one with the same URN left, this is not good enough to fulfill the dependency requirements. If we don't have a unique URN, check again to make sure there's no provider dependency on the condemned resource. If there is one, we either error out, or delete the dependencies depending on the options passed in. Note that I haven't managed to reproduce this by just issuing pulumi commands, but I did by editing the state in a way to reproduce the same issue. One unanswered question for me is how the deleted cluster in the original issue ended up lingering in the state. While this PR fixes the panic, and will make it easier for the user to actually delete the cluster from the state (it's going to suggest `--target-dependents` which will do the right thing), it doesn't address that bit. Fixes https://github.com/pulumi/pulumi/issues/15166 ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [x] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [x] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. --> --------- Co-authored-by: Fraser Waters <fraser@pulumi.com>
2024-02-05 16:21:38 +00:00
func TestDeletingDuplicateProviderURN(t *testing.T) {
t.Parallel()
// Create duplicate provider resources
pA0 := NewProviderResource("a", "p1", "0")
pA1 := NewProviderResource("a", "p1", "1")
// Create a resource that depends on the duplicate Provider.
b0 := NewResource("b", pA0)
b1 := NewResource("b", pA1)
assert.Equal(t, b0.URN, b1.URN)
c := NewResource("c", pA1, b0.URN)
t.Run("do-target-dependents", func(t *testing.T) {
t.Parallel()
snap := NewSnapshot([]*resource.State{
pA0, pA1, b0, b1, c,
})
err := DeleteResource(snap, pA0, nil, true /* targetDependents */)
require.NoError(t, err)
assert.Equal(t, []*resource.State{
pA1, b1, c,
}, snap.Resources)
})
t.Run("do-not-target-dependents", func(t *testing.T) {
t.Parallel()
snap := NewSnapshot([]*resource.State{
pA0, pA1, b0, b1, c,
})
err := DeleteResource(snap, pA0, nil, false /* targetDependents */)
require.ErrorContains(t, err,
"Can't delete resource \"urn:pulumi:test::test::pulumi:providers:a::p1\" due to dependent resources")
})
t.Run("do-target-dependents-one-intermediate", func(t *testing.T) {
t.Parallel()
snap := NewSnapshot([]*resource.State{
pA0, pA1, b0, c,
})
err := DeleteResource(snap, pA0, nil, true /* targetDependents */)
require.NoError(t, err)
assert.Equal(t, []*resource.State{
pA1,
}, snap.Resources)
})
t.Run("do-target-dependents-one-intermediate", func(t *testing.T) {
t.Parallel()
snap := NewSnapshot([]*resource.State{
pA0, pA1, b0, c,
})
err := DeleteResource(snap, pA0, nil, false /* targetDependents */)
require.ErrorContains(t, err,
"Can't delete resource \"urn:pulumi:test::test::pulumi:providers:a::p1\" due to dependent resources")
})
}
func TestDeletingDuplicateProviderURNWithDependents(t *testing.T) {
t.Parallel()
// Create duplicate provider resources
pA0 := NewProviderResource("a", "p1", "0")
pA1 := NewProviderResource("a", "p1", "1")
// Create a resource that depends on the duplicate Provider.
b0 := NewResource("b", pA0)
c0 := NewProviderResource("c", "p1", "0", b0.URN)
c1 := NewProviderResource("c", "p1", "1")
d0 := NewResource("d", c0)
d1 := NewResource("d", c1)
t.Run("do-target-dependents", func(t *testing.T) {
t.Parallel()
snap := NewSnapshot([]*resource.State{
pA0, pA1, b0, c0, c1, d0, d1,
})
err := DeleteResource(snap, pA0, nil, true /* targetDependents */)
require.NoError(t, err)
assert.Equal(t, []*resource.State{
pA1, c1, d1,
}, snap.Resources)
})
t.Run("do-not-target-dependents", func(t *testing.T) {
t.Parallel()
snap := NewSnapshot([]*resource.State{
pA0, pA1, b0, c0, c1, d0, d1,
})
err := DeleteResource(snap, pA0, nil, false /* targetDependents */)
require.ErrorContains(t, err,
"Can't delete resource \"urn:pulumi:test::test::pulumi:providers:a::p1\" due to dependent resources")
})
}
func TestDeletingDependencies(t *testing.T) {
t.Parallel()
pA := NewProviderResource("a", "p1", "0")
a := NewResource("a", pA)
b := NewResource("b", pA)
c := NewResource("c", pA, a.URN)
d := NewResource("d", pA, c.URN)
snap := NewSnapshot([]*resource.State{
pA, a, b, c, d,
})
err := DeleteResource(snap, a, nil, true)
require.NoError(t, err)
assert.Equal(t, snap.Resources, []*resource.State{pA, b})
}
func TestFailedDeletionProviderDependency(t *testing.T) {
t.Parallel()
pA := NewProviderResource("a", "p1", "0")
a := NewResource("a", pA)
b := NewResource("b", pA)
c := NewResource("c", pA)
snap := NewSnapshot([]*resource.State{
pA,
a,
b,
c,
})
err := DeleteResource(snap, pA, nil, false)
assert.Error(t, err)
depErr, ok := err.(ResourceHasDependenciesError)
if !assert.True(t, ok) {
t.FailNow()
}
assert.Contains(t, depErr.Dependencies, a)
assert.Contains(t, depErr.Dependencies, b)
assert.Contains(t, depErr.Dependencies, c)
assert.Len(t, snap.Resources, 4)
assert.Equal(t, []*resource.State{pA, a, b, c}, snap.Resources)
}
func TestFailedDeletionRegularDependency(t *testing.T) {
t.Parallel()
pA := NewProviderResource("a", "p1", "0")
a := NewResource("a", pA)
b := NewResource("b", pA, a.URN)
c := NewResource("c", pA)
snap := NewSnapshot([]*resource.State{
pA,
a,
b,
c,
})
err := DeleteResource(snap, a, nil, false)
assert.Error(t, err)
depErr, ok := err.(ResourceHasDependenciesError)
if !assert.True(t, ok) {
t.FailNow()
}
assert.NotContains(t, depErr.Dependencies, pA)
assert.NotContains(t, depErr.Dependencies, a)
assert.Contains(t, depErr.Dependencies, b)
assert.NotContains(t, depErr.Dependencies, c)
assert.Len(t, snap.Resources, 4)
assert.Equal(t, []*resource.State{pA, a, b, c}, snap.Resources)
}
func TestFailedDeletionProtected(t *testing.T) {
t.Parallel()
pA := NewProviderResource("a", "p1", "0")
a := NewResource("a", pA)
a.Protect = true
snap := NewSnapshot([]*resource.State{
pA,
a,
})
err := DeleteResource(snap, a, nil, false)
assert.Error(t, err)
_, ok := err.(ResourceProtectedError)
assert.True(t, ok)
}
func TestDeleteProtected(t *testing.T) {
t.Parallel()
tests := []struct {
name string
test func(t *testing.T, pA, a, b, c *resource.State, snap *deploy.Snapshot)
}{
{
"root-protected",
func(t *testing.T, pA, a, b, c *resource.State, snap *deploy.Snapshot) {
a.Protect = true
protectedCount := 0
err := DeleteResource(snap, a, func(s *resource.State) error {
s.Protect = false
protectedCount++
return nil
}, false)
assert.NoError(t, err)
assert.Equal(t, protectedCount, 1)
assert.Equal(t, snap.Resources, []*resource.State{pA, b, c})
},
},
{
"root-and-branch",
func(t *testing.T, pA, a, b, c *resource.State, snap *deploy.Snapshot) {
a.Protect = true
b.Protect = true
c.Protect = true
protectedCount := 0
err := DeleteResource(snap, b, func(s *resource.State) error {
s.Protect = false
protectedCount++
return nil
}, true)
assert.NoError(t, err)
// 2 because we only plan to delete b and c. a is protected but not
// scheduled for deletion, so we don't call the onProtect handler.
assert.Equal(t, protectedCount, 2)
assert.Equal(t, snap.Resources, []*resource.State{pA, a})
},
},
{
"branch",
func(t *testing.T, pA, a, b, c *resource.State, snap *deploy.Snapshot) {
b.Protect = true
c.Protect = true
protectedCount := 0
err := DeleteResource(snap, c, func(s *resource.State) error {
s.Protect = false
protectedCount++
return nil
}, false)
assert.NoError(t, err)
assert.Equal(t, protectedCount, 1)
assert.Equal(t, snap.Resources, []*resource.State{pA, a, b})
},
},
{
"no-permission-root",
func(t *testing.T, pA, a, b, c *resource.State, snap *deploy.Snapshot) {
c.Protect = true
err := DeleteResource(snap, c, nil, false).(ResourceProtectedError)
assert.Equal(t, ResourceProtectedError{
Condemned: c,
}, err)
},
},
{
"no-permission-branch",
func(t *testing.T, pA, a, b, c *resource.State, snap *deploy.Snapshot) {
c.Protect = true
err := DeleteResource(snap, b, nil, true).(ResourceProtectedError)
assert.Equal(t, ResourceProtectedError{
Condemned: c,
}, err)
},
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
pA := NewProviderResource("a", "p1", "0")
a := NewResource("a", pA)
b := NewResource("b", pA)
c := NewResource("c", pA, b.URN)
snap := NewSnapshot([]*resource.State{
pA,
a,
b,
c,
})
tt.test(t, pA, a, b, c, snap)
})
}
}
func TestFailedDeletionParentDependency(t *testing.T) {
t.Parallel()
pA := NewProviderResource("a", "p1", "0")
a := NewResource("a", pA)
b := NewResource("b", pA)
b.Parent = a.URN
c := NewResource("c", pA)
c.Parent = a.URN
snap := NewSnapshot([]*resource.State{
pA,
a,
b,
c,
})
err := DeleteResource(snap, a, nil, false)
assert.Error(t, err)
depErr, ok := err.(ResourceHasDependenciesError)
if !assert.True(t, ok) {
t.FailNow()
}
assert.NotContains(t, depErr.Dependencies, pA)
assert.NotContains(t, depErr.Dependencies, a)
assert.Contains(t, depErr.Dependencies, b)
assert.Contains(t, depErr.Dependencies, c)
assert.Len(t, snap.Resources, 4)
assert.Equal(t, []*resource.State{pA, a, b, c}, snap.Resources)
}
func TestUnprotectResource(t *testing.T) {
t.Parallel()
pA := NewProviderResource("a", "p1", "0")
a := NewResource("a", pA)
a.Protect = true
b := NewResource("b", pA)
c := NewResource("c", pA)
snap := NewSnapshot([]*resource.State{
pA,
a,
b,
c,
})
err := UnprotectResource(snap, a)
assert.NoError(t, err)
assert.Len(t, snap.Resources, 4)
assert.Equal(t, []*resource.State{pA, a, b, c}, snap.Resources)
assert.False(t, a.Protect)
}
func TestLocateResourceNotFound(t *testing.T) {
t.Parallel()
pA := NewProviderResource("a", "p1", "0")
a := NewResource("a", pA)
b := NewResource("b", pA)
c := NewResource("c", pA)
snap := NewSnapshot([]*resource.State{
pA,
a,
b,
c,
})
ty := tokens.Type("a:b:c")
urn := resource.NewURN("test", "test", "", ty, "not-present")
resList := LocateResource(snap, urn)
assert.Nil(t, resList)
}
func TestLocateResourceAmbiguous(t *testing.T) {
t.Parallel()
pA := NewProviderResource("a", "p1", "0")
a := NewResource("a", pA)
b := NewResource("b", pA)
aPending := NewResource("a", pA)
aPending.Delete = true
snap := NewSnapshot([]*resource.State{
pA,
a,
b,
aPending,
})
resList := LocateResource(snap, a.URN)
assert.Len(t, resList, 2)
assert.Contains(t, resList, a)
assert.Contains(t, resList, aPending)
assert.NotContains(t, resList, pA)
assert.NotContains(t, resList, b)
}
func TestLocateResourceExact(t *testing.T) {
t.Parallel()
pA := NewProviderResource("a", "p1", "0")
a := NewResource("a", pA)
b := NewResource("b", pA)
c := NewResource("c", pA)
snap := NewSnapshot([]*resource.State{
pA,
a,
b,
c,
})
resList := LocateResource(snap, a.URN)
assert.Len(t, resList, 1)
assert.Contains(t, resList, a)
}
func TestRenameStack(t *testing.T) {
t.Parallel()
Rewrite filestate.RenameStack logic in terms of checkpoints (#13927) <!--- 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. --> The RenameStack function in filestate was one of the few places outside of "cmd" still using `stack.DefaultSecretsProvider`, which it was only using because the stack rename function was written in terms of deserialised snapshots instead of serialised deployments. This change rewrites the RenameStack function to work on the raw deployment data rather than deserialised snapshot. This allows us to remove the use of `stack.DefaultSecretsProvider` from filestate. This would also allow the service to update their use of RenameStack to work on the raw deployment as well, saving them the use of their NoopSecretsProvider. ## 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. --> - [ ] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [ ] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2023-12-22 12:28:41 +00:00
locateResource := func(deployment *apitype.DeploymentV3, urn resource.URN) []apitype.ResourceV3 {
if deployment == nil {
return nil
}
var resources []apitype.ResourceV3
for _, res := range deployment.Resources {
if res.URN == urn {
resources = append(resources, res)
}
}
return resources
}
newResource := func(name string, provider *apitype.ResourceV3, deps ...resource.URN) apitype.ResourceV3 {
prov := ""
if provider != nil {
p, err := providers.NewReference(provider.URN, provider.ID)
if err != nil {
panic(err)
}
prov = p.String()
}
t := tokens.Type("a:b:c")
return apitype.ResourceV3{
Type: t,
URN: resource.NewURN("test", "test", "", t, name),
Inputs: map[string]interface{}{},
Outputs: map[string]interface{}{},
Dependencies: deps,
Provider: prov,
}
}
newProviderResource := func(pkg, name, id string, deps ...resource.URN) apitype.ResourceV3 {
t := providers.MakeProviderType(tokens.Package(pkg))
return apitype.ResourceV3{
Type: t,
URN: resource.NewURN("test", "test", "", t, name),
ID: resource.ID(id),
Inputs: map[string]interface{}{},
Outputs: map[string]interface{}{},
Dependencies: deps,
}
}
newDeployment := func(resources []apitype.ResourceV3) *apitype.DeploymentV3 {
return &apitype.DeploymentV3{
Manifest: apitype.ManifestV1{
Time: time.Now(),
Version: version.Version,
Plugins: nil,
},
Resources: resources,
}
}
pA := newProviderResource("a", "p1", "0")
a := newResource("a", &pA)
b := newResource("b", &pA)
c := newResource("c", &pA)
deployment := newDeployment([]apitype.ResourceV3{
pA,
a,
b,
c,
})
// Baseline. Can locate resource A.
Rewrite filestate.RenameStack logic in terms of checkpoints (#13927) <!--- 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. --> The RenameStack function in filestate was one of the few places outside of "cmd" still using `stack.DefaultSecretsProvider`, which it was only using because the stack rename function was written in terms of deserialised snapshots instead of serialised deployments. This change rewrites the RenameStack function to work on the raw deployment data rather than deserialised snapshot. This allows us to remove the use of `stack.DefaultSecretsProvider` from filestate. This would also allow the service to update their use of RenameStack to work on the raw deployment as well, saving them the use of their NoopSecretsProvider. ## 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. --> - [ ] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [ ] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2023-12-22 12:28:41 +00:00
resList := locateResource(deployment, a.URN)
assert.Len(t, resList, 1)
assert.Contains(t, resList, a)
if t.Failed() {
Rewrite filestate.RenameStack logic in terms of checkpoints (#13927) <!--- 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. --> The RenameStack function in filestate was one of the few places outside of "cmd" still using `stack.DefaultSecretsProvider`, which it was only using because the stack rename function was written in terms of deserialised snapshots instead of serialised deployments. This change rewrites the RenameStack function to work on the raw deployment data rather than deserialised snapshot. This allows us to remove the use of `stack.DefaultSecretsProvider` from filestate. This would also allow the service to update their use of RenameStack to work on the raw deployment as well, saving them the use of their NoopSecretsProvider. ## 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. --> - [ ] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [ ] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2023-12-22 12:28:41 +00:00
t.Fatal("Unable to find expected resource in initial checkpoint.")
}
baselineResourceURN := resList[0].URN
// The stack name and project are hard-coded in NewResource(...)
assert.EqualValues(t, "test", baselineResourceURN.Stack())
assert.EqualValues(t, "test", baselineResourceURN.Project())
// Rename just the stack.
//nolint:paralleltest // uses shared stack
t.Run("JustTheStack", func(t *testing.T) {
Rewrite filestate.RenameStack logic in terms of checkpoints (#13927) <!--- 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. --> The RenameStack function in filestate was one of the few places outside of "cmd" still using `stack.DefaultSecretsProvider`, which it was only using because the stack rename function was written in terms of deserialised snapshots instead of serialised deployments. This change rewrites the RenameStack function to work on the raw deployment data rather than deserialised snapshot. This allows us to remove the use of `stack.DefaultSecretsProvider` from filestate. This would also allow the service to update their use of RenameStack to work on the raw deployment as well, saving them the use of their NoopSecretsProvider. ## 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. --> - [ ] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [ ] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2023-12-22 12:28:41 +00:00
err := RenameStack(deployment, tokens.MustParseStackName("new-stack"), tokens.PackageName(""))
if err != nil {
t.Fatalf("Error renaming stack: %v", err)
}
// Confirm the previous resource by URN isn't found.
Rewrite filestate.RenameStack logic in terms of checkpoints (#13927) <!--- 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. --> The RenameStack function in filestate was one of the few places outside of "cmd" still using `stack.DefaultSecretsProvider`, which it was only using because the stack rename function was written in terms of deserialised snapshots instead of serialised deployments. This change rewrites the RenameStack function to work on the raw deployment data rather than deserialised snapshot. This allows us to remove the use of `stack.DefaultSecretsProvider` from filestate. This would also allow the service to update their use of RenameStack to work on the raw deployment as well, saving them the use of their NoopSecretsProvider. ## 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. --> - [ ] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [ ] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2023-12-22 12:28:41 +00:00
assert.Len(t, locateResource(deployment, baselineResourceURN), 0)
// Confirm the resource has been renamed.
updatedResourceURN := resource.NewURN(
tokens.QName("new-stack"),
"test", // project name stayed the same
"" /*parent type*/, baselineResourceURN.Type(),
baselineResourceURN.Name())
Rewrite filestate.RenameStack logic in terms of checkpoints (#13927) <!--- 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. --> The RenameStack function in filestate was one of the few places outside of "cmd" still using `stack.DefaultSecretsProvider`, which it was only using because the stack rename function was written in terms of deserialised snapshots instead of serialised deployments. This change rewrites the RenameStack function to work on the raw deployment data rather than deserialised snapshot. This allows us to remove the use of `stack.DefaultSecretsProvider` from filestate. This would also allow the service to update their use of RenameStack to work on the raw deployment as well, saving them the use of their NoopSecretsProvider. ## 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. --> - [ ] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [ ] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2023-12-22 12:28:41 +00:00
assert.Len(t, locateResource(deployment, updatedResourceURN), 1)
})
// Rename the stack and project.
//nolint:paralleltest // uses shared stack
t.Run("StackAndProject", func(t *testing.T) {
Rewrite filestate.RenameStack logic in terms of checkpoints (#13927) <!--- 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. --> The RenameStack function in filestate was one of the few places outside of "cmd" still using `stack.DefaultSecretsProvider`, which it was only using because the stack rename function was written in terms of deserialised snapshots instead of serialised deployments. This change rewrites the RenameStack function to work on the raw deployment data rather than deserialised snapshot. This allows us to remove the use of `stack.DefaultSecretsProvider` from filestate. This would also allow the service to update their use of RenameStack to work on the raw deployment as well, saving them the use of their NoopSecretsProvider. ## 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. --> - [ ] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [ ] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2023-12-22 12:28:41 +00:00
err := RenameStack(deployment, tokens.MustParseStackName("new-stack2"), tokens.PackageName("new-project"))
if err != nil {
t.Fatalf("Error renaming stack: %v", err)
}
// Lookup the resource by URN, with both stack and project updated.
updatedResourceURN := resource.NewURN(
tokens.QName("new-stack2"),
"new-project",
"" /*parent type*/, baselineResourceURN.Type(),
baselineResourceURN.Name())
Rewrite filestate.RenameStack logic in terms of checkpoints (#13927) <!--- 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. --> The RenameStack function in filestate was one of the few places outside of "cmd" still using `stack.DefaultSecretsProvider`, which it was only using because the stack rename function was written in terms of deserialised snapshots instead of serialised deployments. This change rewrites the RenameStack function to work on the raw deployment data rather than deserialised snapshot. This allows us to remove the use of `stack.DefaultSecretsProvider` from filestate. This would also allow the service to update their use of RenameStack to work on the raw deployment as well, saving them the use of their NoopSecretsProvider. ## 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. --> - [ ] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [ ] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2023-12-22 12:28:41 +00:00
assert.Len(t, locateResource(deployment, updatedResourceURN), 1)
})
}