filestate: Introduce referenceStore to control layout
Adds a referenceStore abstraction to control the layout of the storage,
and a legacyReferenceStore implementation based on the current layout
(that does not support projects).
This allows us to move code to determine file paths of stacks, their
histories, and their backups, all into a single component that we can
swap out for project support.
localBackendReferences keep track of the referenceStore that built them.
The primary reason for this is that when we add support for migrating a
stack state from legacy to project mode, `backend.store` will become
mutable.
For references created before the store for a backend was changed, we
still need to be able to access their original file paths, so we need to
hold onto the original referenceStore.
However, as a side-effect of this,
it's more convenient to acess paths from `ref.Foo()` rather than
`backend.foo(ref)` or `backend.store.Foo(ref)`.
In the future, we may also move stackPath to the store,
since right now the .json/.json.gz logic is duplicated in a couple
places.
Extracted from #12134
2023-03-14 00:49:05 +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.
|
|
|
|
|
2024-01-30 15:53:10 +00:00
|
|
|
package diy
|
filestate: Introduce referenceStore to control layout
Adds a referenceStore abstraction to control the layout of the storage,
and a legacyReferenceStore implementation based on the current layout
(that does not support projects).
This allows us to move code to determine file paths of stacks, their
histories, and their backups, all into a single component that we can
swap out for project support.
localBackendReferences keep track of the referenceStore that built them.
The primary reason for this is that when we add support for migrating a
stack state from legacy to project mode, `backend.store` will become
mutable.
For references created before the store for a backend was changed, we
still need to be able to access their original file paths, so we need to
hold onto the original referenceStore.
However, as a side-effect of this,
it's more convenient to acess paths from `ref.Foo()` rather than
`backend.foo(ref)` or `backend.store.Foo(ref)`.
In the future, we may also move stackPath to the store,
since right now the .json/.json.gz logic is duplicated in a couple
places.
Extracted from #12134
2023-03-14 00:49:05 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/pulumi/pulumi/sdk/v3/go/common/tokens"
|
2023-02-10 12:24:28 +00:00
|
|
|
"github.com/pulumi/pulumi/sdk/v3/go/common/workspace"
|
filestate: Introduce referenceStore to control layout
Adds a referenceStore abstraction to control the layout of the storage,
and a legacyReferenceStore implementation based on the current layout
(that does not support projects).
This allows us to move code to determine file paths of stacks, their
histories, and their backups, all into a single component that we can
swap out for project support.
localBackendReferences keep track of the referenceStore that built them.
The primary reason for this is that when we add support for migrating a
stack state from legacy to project mode, `backend.store` will become
mutable.
For references created before the store for a backend was changed, we
still need to be able to access their original file paths, so we need to
hold onto the original referenceStore.
However, as a side-effect of this,
it's more convenient to acess paths from `ref.Foo()` rather than
`backend.foo(ref)` or `backend.store.Foo(ref)`.
In the future, we may also move stackPath to the store,
since right now the .json/.json.gz logic is duplicated in a couple
places.
Extracted from #12134
2023-03-14 00:49:05 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"gocloud.dev/blob/memblob"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestLegacyReferenceStore_referencePaths(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
bucket := memblob.OpenBucket(nil)
|
|
|
|
store := newLegacyReferenceStore(bucket)
|
|
|
|
|
|
|
|
ref, err := store.ParseReference("foo")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
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
|
|
|
assert.Equal(t, tokens.MustParseStackName("foo"), ref.Name())
|
filestate: Introduce referenceStore to control layout
Adds a referenceStore abstraction to control the layout of the storage,
and a legacyReferenceStore implementation based on the current layout
(that does not support projects).
This allows us to move code to determine file paths of stacks, their
histories, and their backups, all into a single component that we can
swap out for project support.
localBackendReferences keep track of the referenceStore that built them.
The primary reason for this is that when we add support for migrating a
stack state from legacy to project mode, `backend.store` will become
mutable.
For references created before the store for a backend was changed, we
still need to be able to access their original file paths, so we need to
hold onto the original referenceStore.
However, as a side-effect of this,
it's more convenient to acess paths from `ref.Foo()` rather than
`backend.foo(ref)` or `backend.store.Foo(ref)`.
In the future, we may also move stackPath to the store,
since right now the .json/.json.gz logic is duplicated in a couple
places.
Extracted from #12134
2023-03-14 00:49:05 +00:00
|
|
|
assert.Equal(t, tokens.QName("foo"), ref.FullyQualifiedName())
|
|
|
|
assert.Equal(t, ".pulumi/stacks/foo", ref.StackBasePath())
|
|
|
|
assert.Equal(t, ".pulumi/history/foo", ref.HistoryDir())
|
|
|
|
assert.Equal(t, ".pulumi/backups/foo", ref.BackupDir())
|
|
|
|
}
|
|
|
|
|
2023-02-10 12:24:28 +00:00
|
|
|
func TestProjectReferenceStore_referencePaths(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
bucket := memblob.OpenBucket(nil)
|
|
|
|
store := newProjectReferenceStore(bucket, func() *workspace.Project {
|
|
|
|
return &workspace.Project{Name: "test"}
|
|
|
|
})
|
|
|
|
|
|
|
|
ref, err := store.ParseReference("organization/myproject/mystack")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, ".pulumi/stacks/myproject/mystack", ref.StackBasePath())
|
|
|
|
assert.Equal(t, ".pulumi/history/myproject/mystack", ref.HistoryDir())
|
|
|
|
assert.Equal(t, ".pulumi/backups/myproject/mystack", ref.BackupDir())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProjectReferenceStore_ParseReference(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
bucket := memblob.OpenBucket(nil)
|
|
|
|
store := newProjectReferenceStore(bucket, func() *workspace.Project {
|
|
|
|
return &workspace.Project{Name: "currentProject"}
|
|
|
|
})
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
desc string
|
|
|
|
give string
|
|
|
|
|
|
|
|
fqname tokens.QName
|
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
|
|
|
name string
|
2023-02-10 12:24:28 +00:00
|
|
|
project tokens.Name
|
|
|
|
str string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "simple",
|
|
|
|
give: "foo",
|
|
|
|
fqname: "organization/currentProject/foo",
|
|
|
|
name: "foo",
|
|
|
|
project: "currentProject",
|
|
|
|
str: "foo",
|
|
|
|
// truncated because project name is the same as current project
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "organization",
|
|
|
|
give: "organization/foo",
|
|
|
|
fqname: "organization/currentProject/foo",
|
|
|
|
name: "foo",
|
|
|
|
project: "currentProject",
|
|
|
|
str: "foo",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "fully qualified",
|
|
|
|
give: "organization/project/foo",
|
|
|
|
fqname: "organization/project/foo",
|
|
|
|
name: "foo",
|
|
|
|
project: "project",
|
|
|
|
str: "organization/project/foo", // doesn't match current project
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
tt := tt
|
|
|
|
t.Run(tt.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
ref, err := store.ParseReference(tt.give)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, tt.fqname, ref.FullyQualifiedName())
|
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
|
|
|
assert.Equal(t, tokens.MustParseStackName(tt.name), ref.Name())
|
2023-04-27 08:13:08 +00:00
|
|
|
proj, has := ref.Project()
|
|
|
|
assert.True(t, has)
|
|
|
|
assert.Equal(t, tt.project, proj)
|
2023-02-10 12:24:28 +00:00
|
|
|
assert.Equal(t, tt.str, ref.String())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
filestate: Introduce referenceStore to control layout
Adds a referenceStore abstraction to control the layout of the storage,
and a legacyReferenceStore implementation based on the current layout
(that does not support projects).
This allows us to move code to determine file paths of stacks, their
histories, and their backups, all into a single component that we can
swap out for project support.
localBackendReferences keep track of the referenceStore that built them.
The primary reason for this is that when we add support for migrating a
stack state from legacy to project mode, `backend.store` will become
mutable.
For references created before the store for a backend was changed, we
still need to be able to access their original file paths, so we need to
hold onto the original referenceStore.
However, as a side-effect of this,
it's more convenient to acess paths from `ref.Foo()` rather than
`backend.foo(ref)` or `backend.store.Foo(ref)`.
In the future, we may also move stackPath to the store,
since right now the .json/.json.gz logic is duplicated in a couple
places.
Extracted from #12134
2023-03-14 00:49:05 +00:00
|
|
|
func TestLegacyReferenceStore_ParseReference_errors(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
bucket := memblob.OpenBucket(nil)
|
|
|
|
store := newLegacyReferenceStore(bucket)
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
desc string
|
|
|
|
give string
|
|
|
|
}{
|
|
|
|
{desc: "empty", give: ""},
|
|
|
|
{desc: "invalid name", give: "foo/bar"},
|
|
|
|
{desc: "too many parts", give: "foo/bar/baz"},
|
|
|
|
{
|
|
|
|
desc: "over 100 characters",
|
|
|
|
give: strings.Repeat("a", 101),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
tt := tt
|
|
|
|
t.Run(tt.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
_, err := store.ParseReference(tt.give)
|
|
|
|
assert.Error(t, err)
|
|
|
|
// If we ever make error messages here more specific,
|
|
|
|
// we can add assert.ErrorContains here.
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-10 12:24:28 +00:00
|
|
|
func TestProjectReferenceStore_ParseReference_errors(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
bucket := memblob.OpenBucket(nil)
|
|
|
|
store := newProjectReferenceStore(bucket, func() *workspace.Project {
|
|
|
|
return nil // current project is not set
|
|
|
|
})
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
desc string
|
|
|
|
give string
|
|
|
|
wantErr string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "empty",
|
|
|
|
wantErr: "must not be empty",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "bad organization",
|
|
|
|
give: "foo/bar/baz",
|
|
|
|
wantErr: "organization name must be 'organization'",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "long project name",
|
|
|
|
give: "organization/" + strings.Repeat("a", 101) + "/foo",
|
|
|
|
wantErr: "project names are limited to 100 characters",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "long project stack name",
|
|
|
|
give: "organization/foo/" + strings.Repeat("a", 101),
|
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
|
|
|
wantErr: "a stack name cannot exceed 100 characters",
|
2023-02-10 12:24:28 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "no current project",
|
|
|
|
give: "organization/foo",
|
|
|
|
wantErr: "pass the fully qualified name",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "invalid project name",
|
|
|
|
give: "organization/foo:bar/baz",
|
|
|
|
wantErr: "may only contain alphanumeric",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "invalid stack name",
|
|
|
|
give: "organization/foo/baz:qux",
|
|
|
|
wantErr: "may only contain alphanumeric",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
tt := tt
|
|
|
|
t.Run(tt.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
require.NotEmpty(t, tt.wantErr,
|
|
|
|
"bad test case: wantErr must be non-empty")
|
|
|
|
|
|
|
|
_, err := store.ParseReference(tt.give)
|
|
|
|
assert.ErrorContains(t, err, tt.wantErr)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
filestate: Introduce referenceStore to control layout
Adds a referenceStore abstraction to control the layout of the storage,
and a legacyReferenceStore implementation based on the current layout
(that does not support projects).
This allows us to move code to determine file paths of stacks, their
histories, and their backups, all into a single component that we can
swap out for project support.
localBackendReferences keep track of the referenceStore that built them.
The primary reason for this is that when we add support for migrating a
stack state from legacy to project mode, `backend.store` will become
mutable.
For references created before the store for a backend was changed, we
still need to be able to access their original file paths, so we need to
hold onto the original referenceStore.
However, as a side-effect of this,
it's more convenient to acess paths from `ref.Foo()` rather than
`backend.foo(ref)` or `backend.store.Foo(ref)`.
In the future, we may also move stackPath to the store,
since right now the .json/.json.gz logic is duplicated in a couple
places.
Extracted from #12134
2023-03-14 00:49:05 +00:00
|
|
|
func TestLegacyReferenceStore_ListReferences(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
desc string
|
|
|
|
|
|
|
|
// List of file paths relative to the storage root
|
|
|
|
// that should exist before ListReferences is called.
|
|
|
|
files []string
|
|
|
|
|
|
|
|
// List of fully-qualified stack names that should be returned
|
|
|
|
// by ListReferences.
|
|
|
|
want []tokens.QName
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "empty",
|
|
|
|
want: []tokens.QName{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "json",
|
|
|
|
files: []string{
|
|
|
|
".pulumi/stacks/foo.json",
|
|
|
|
},
|
|
|
|
want: []tokens.QName{"foo"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "gzipped",
|
|
|
|
files: []string{
|
|
|
|
".pulumi/stacks/foo.json.gz",
|
|
|
|
},
|
|
|
|
want: []tokens.QName{"foo"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "multiple",
|
|
|
|
files: []string{
|
|
|
|
".pulumi/stacks/foo.json",
|
|
|
|
".pulumi/stacks/bar.json.gz",
|
|
|
|
".pulumi/stacks/baz.json",
|
|
|
|
},
|
|
|
|
want: []tokens.QName{"bar", "baz", "foo"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "extraneous directories",
|
|
|
|
files: []string{
|
|
|
|
".pulumi/stacks/foo.json",
|
|
|
|
".pulumi/stacks/bar.json/baz.json", // not a file
|
|
|
|
},
|
|
|
|
want: []tokens.QName{"foo"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
tt := tt
|
|
|
|
t.Run(tt.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
bucket := memblob.OpenBucket(nil)
|
|
|
|
store := newLegacyReferenceStore(bucket)
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
for _, f := range tt.files {
|
|
|
|
require.NoError(t, bucket.WriteAll(ctx, f, []byte{}, nil))
|
|
|
|
}
|
|
|
|
|
2023-04-03 21:36:44 +00:00
|
|
|
refs, err := store.ListReferences(ctx)
|
filestate: Introduce referenceStore to control layout
Adds a referenceStore abstraction to control the layout of the storage,
and a legacyReferenceStore implementation based on the current layout
(that does not support projects).
This allows us to move code to determine file paths of stacks, their
histories, and their backups, all into a single component that we can
swap out for project support.
localBackendReferences keep track of the referenceStore that built them.
The primary reason for this is that when we add support for migrating a
stack state from legacy to project mode, `backend.store` will become
mutable.
For references created before the store for a backend was changed, we
still need to be able to access their original file paths, so we need to
hold onto the original referenceStore.
However, as a side-effect of this,
it's more convenient to acess paths from `ref.Foo()` rather than
`backend.foo(ref)` or `backend.store.Foo(ref)`.
In the future, we may also move stackPath to the store,
since right now the .json/.json.gz logic is duplicated in a couple
places.
Extracted from #12134
2023-03-14 00:49:05 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
got := make([]tokens.QName, len(refs))
|
|
|
|
for i, ref := range refs {
|
|
|
|
got[i] = ref.FullyQualifiedName()
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, tt.want, got)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2023-02-10 12:24:28 +00:00
|
|
|
|
|
|
|
func TestProjectReferenceStore_List(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
desc string
|
|
|
|
|
|
|
|
// List of file paths relative to the storage root
|
|
|
|
// that should exist before ListReferences is called.
|
|
|
|
files []string
|
|
|
|
|
|
|
|
// List of fully-qualified stack names that should be returned
|
|
|
|
// by ListReferences.
|
|
|
|
stacks []tokens.QName
|
|
|
|
|
|
|
|
// List of project names that should be returned by ListProjects.
|
|
|
|
projects []tokens.Name
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "empty",
|
|
|
|
stacks: []tokens.QName{},
|
2023-06-28 16:02:04 +00:00
|
|
|
projects: nil,
|
2023-02-10 12:24:28 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "json",
|
|
|
|
files: []string{
|
|
|
|
".pulumi/stacks/proj/foo.json",
|
|
|
|
},
|
|
|
|
stacks: []tokens.QName{"organization/proj/foo"},
|
|
|
|
projects: []tokens.Name{"proj"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "gzipped",
|
|
|
|
files: []string{
|
|
|
|
".pulumi/stacks/foo/bar.json.gz",
|
|
|
|
},
|
|
|
|
stacks: []tokens.QName{"organization/foo/bar"},
|
|
|
|
projects: []tokens.Name{"foo"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "multiple",
|
|
|
|
files: []string{
|
|
|
|
".pulumi/stacks/a/foo.json",
|
|
|
|
".pulumi/stacks/b/bar.json.gz",
|
|
|
|
".pulumi/stacks/c/baz.json",
|
|
|
|
},
|
|
|
|
stacks: []tokens.QName{
|
|
|
|
"organization/a/foo",
|
|
|
|
"organization/b/bar",
|
|
|
|
"organization/c/baz",
|
|
|
|
},
|
|
|
|
projects: []tokens.Name{"a", "b", "c"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "extraneous files and directories",
|
|
|
|
files: []string{
|
|
|
|
".pulumi/stacks/a/foo.json",
|
|
|
|
".pulumi/stacks/foo.json",
|
|
|
|
".pulumi/stacks/bar/baz/qux.json", // nested too deep
|
|
|
|
".pulumi/stacks/a b/c.json", // bad project name
|
|
|
|
},
|
|
|
|
stacks: []tokens.QName{"organization/a/foo"},
|
|
|
|
projects: []tokens.Name{"a", "bar"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
tt := tt
|
|
|
|
t.Run(tt.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
bucket := memblob.OpenBucket(nil)
|
|
|
|
store := newProjectReferenceStore(bucket, func() *workspace.Project {
|
|
|
|
return &workspace.Project{Name: "test"}
|
|
|
|
})
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
for _, f := range tt.files {
|
|
|
|
require.NoError(t, bucket.WriteAll(ctx, f, []byte{}, nil))
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("Projects", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2023-04-03 21:36:44 +00:00
|
|
|
projects, err := store.ListProjects(ctx)
|
2023-02-10 12:24:28 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, tt.projects, projects)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("References", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2023-04-03 21:36:44 +00:00
|
|
|
refs, err := store.ListReferences(ctx)
|
2023-02-10 12:24:28 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
got := make([]tokens.QName, len(refs))
|
|
|
|
for i, ref := range refs {
|
|
|
|
got[i] = ref.FullyQualifiedName()
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, tt.stacks, got)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2023-04-29 15:52:04 +00:00
|
|
|
|
|
|
|
func TestProjectReferenceStore_ProjectExists(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
desc string
|
|
|
|
|
|
|
|
// List of file paths relative to the storage root
|
|
|
|
// that should exist before ListReferences is called.
|
|
|
|
files []string
|
|
|
|
|
|
|
|
// Project name that should exist before ProjectExists is called.
|
|
|
|
projectName string
|
|
|
|
|
|
|
|
// Result that should be returned by ProjectExists.
|
|
|
|
exist bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "project exists",
|
|
|
|
files: []string{
|
|
|
|
".pulumi/stacks/a/foo.json",
|
|
|
|
},
|
|
|
|
projectName: "a",
|
|
|
|
exist: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "project exists as empty directory",
|
|
|
|
files: []string{
|
|
|
|
".pulumi/stacks/a",
|
|
|
|
},
|
|
|
|
projectName: "a",
|
|
|
|
exist: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "project does not exist",
|
|
|
|
files: []string{
|
|
|
|
".pulumi/stacks/a",
|
|
|
|
},
|
|
|
|
projectName: "b",
|
|
|
|
exist: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "subproject exist",
|
|
|
|
files: []string{
|
|
|
|
".pulumi/stacks/b/a", // Project name exist, but as a subproject
|
|
|
|
},
|
|
|
|
projectName: "a",
|
|
|
|
exist: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
tt := tt
|
|
|
|
t.Run(tt.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
bucket := memblob.OpenBucket(nil)
|
|
|
|
store := newProjectReferenceStore(bucket, func() *workspace.Project {
|
|
|
|
return &workspace.Project{Name: "test"}
|
|
|
|
})
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
for _, f := range tt.files {
|
|
|
|
require.NoError(t, bucket.WriteAll(ctx, f, []byte{}, nil))
|
|
|
|
}
|
|
|
|
|
|
|
|
exist, err := store.ProjectExists(ctx, tt.projectName)
|
2023-05-01 23:29:55 +00:00
|
|
|
assert.NoError(t, err)
|
2023-04-29 15:52:04 +00:00
|
|
|
assert.Equal(t, tt.exist, exist)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|