pulumi/pkg/backend/diy/bucket_test.go

98 lines
3.3 KiB
Go
Raw Permalink Normal View History

package diy
import (
"context"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"gocloud.dev/blob"
)
func mustNotHaveError(t *testing.T, context string, err error) {
t.Helper()
if err != nil {
t.Fatalf("Error in testcase %q, aborting: %v", context, err)
}
}
// The wrappedBucket type exists so that when we use the blob.Bucket type we can present a consistent
// view of file paths. Since it will assume that backslashes (file separators on Windows) are part of
// file names, and this causes "problems".
func TestWrappedBucket(t *testing.T) {
t.Parallel()
// wrappedBucket will only massage file paths IFF it is needed, as filepath.ToSlash is a noop.
if filepath.Separator == '/' {
assert.Equal(t, `foo\bar\baz`, filepath.ToSlash(`foo\bar\baz`))
t.Skip("Skipping wrappedBucket tests because file paths won't be modified.")
}
// Initialize a diy backend, using the default Pulumi directory.
cloudURL := FilePathPrefix + "~"
ctx := context.Background()
b, err := New(ctx, nil, cloudURL, nil)
if err != nil {
t.Fatalf("Initializing new diy backend: %v", err)
}
diyBackend, ok := b.(*diyBackend)
if !ok {
t.Fatalf("backend wasn't of type diyBackend?")
}
wrappedBucket, ok := diyBackend.bucket.(*wrappedBucket)
if !ok {
t.Fatalf("diyBackend.bucket wasn't of type wrappedBucket?")
}
// Perform basic file operations using wrappedBucket and verify that it will
// successfully handle both "/" and "\" as file separators. (And probably fail in
// exciting ways if you try to give it a file on a system that supports "\" or "/" as
// a valid character in a filename.)
//nolint:paralleltest // uses shared state with parent
t.Run("SanityCheck", func(t *testing.T) {
randomData := []byte("Just some random data")
err := wrappedBucket.WriteAll(ctx, ".pulumi/bucket-test/foo", randomData, &blob.WriterOptions{})
mustNotHaveError(t, "WriteAll", err)
readData, err := wrappedBucket.ReadAll(ctx, `.pulumi\bucket-test\foo`)
mustNotHaveError(t, "ReadAll", err)
assert.EqualValues(t, randomData, readData, "data read from bucket doesn't match what was written")
// Verify the leading slash isn't necessary.
err = wrappedBucket.Delete(ctx, ".pulumi/bucket-test/foo")
mustNotHaveError(t, "Delete", err)
exists, err := wrappedBucket.Exists(ctx, ".pulumi/bucket-test/foo")
mustNotHaveError(t, "Exists", err)
assert.False(t, exists, "Deleted file still found?")
})
// Verify ListObjects / listBucket works with regard to differeing file separators too.
//nolint:paralleltest // uses shared state with parent
t.Run("ListObjects", func(t *testing.T) {
randomData := []byte("Just some random data")
filenames := []string{"a.json", "b.json", "c.json"}
// Write some data.
for _, filename := range filenames {
Enable perfsprint linter (#14813) <!--- 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. --> Prompted by a comment in another review: https://github.com/pulumi/pulumi/pull/14654#discussion_r1419995945 This lints that we don't use `fmt.Errorf` when `errors.New` will suffice, it also covers a load of other cases where `Sprintf` is sub-optimal. Most of these edits were made by running `perfsprint --fix`. ## 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. --> - [ ] 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-12 12:19:42 +00:00
key := ".pulumi\\bucket-test\\" + filename
err := wrappedBucket.WriteAll(ctx, key, randomData, &blob.WriterOptions{})
mustNotHaveError(t, "WriteAll", err)
}
// Verify it is found. NOTE: This requires that any files created
// during other tests have successfully been cleaned up too.
objects, err := listBucket(ctx, wrappedBucket, `.pulumi\bucket-test`)
mustNotHaveError(t, "listBucket", err)
if len(objects) != len(filenames) {
assert.Equal(t, 3, len(objects), "listBucket returned unexpected number of objects.")
for _, object := range objects {
t.Logf("Got object: %+v", object)
}
}
})
}