Files
upstream-seaweedfs/weed/filer/leveldb2/empty_folder_race_test.go
upstream-mirror 89f5be005d
Some checks failed
go: build dev binaries / cleanup (push) Successful in 4s
Codespell / Check for spelling errors (push) Failing after 3s
docker: build dev containers / build-rust-binaries (amd64, x86_64-unknown-linux-musl) (push) Failing after 3s
docker: build dev containers / build-dev-containers (push) Has been skipped
docker: build dev containers / build-rust-binaries (arm64, true, aarch64-unknown-linux-musl) (push) Failing after 3s
End to End / FUSE Mount (push) Failing after 2s
EC Integration Tests / EC Integration Tests (push) Failing after 30s
FUSE DLM Integration Tests / FUSE DLM Integration Tests (push) Failing after 3s
FUSE Volume Server Failover Tests / FUSE Volume Server Failover (push) Failing after 3s
FUSE Integration Tests / FUSE Integration Testing (push) Failing after 2s
FUSE P2P Peer Chunk Sharing Integration Tests / FUSE P2P Peer Chunk Sharing (push) Failing after 3s
go: build binary / Go Vet (push) Failing after 2s
go: build binary / Go Vet 32-bit (push) Failing after 3s
go: build binary / Build (push) Failing after 4s
go: build binary / Build cross-platform (freebsd/amd64) (push) Failing after 3s
go: build binary / Build cross-platform (windows/amd64) (push) Failing after 3s
go: build binary / Build cross-platform (darwin/arm64) (push) Failing after 3s
go: build binary / Build cross-platform (windows/arm64) (push) Failing after 3s
go: build binary / Test (push) Failing after 2s
go: build binary / Test 32-bit (push) Failing after 3s
helm: lint and test charts / lint-test (push) Failing after 3s
Java Client Integration Tests / Java Integration Tests (11) (push) Failing after 3s
Java Client Integration Tests / Java Integration Tests (17) (push) Failing after 3s
go: build dev binaries / build_dev_linux_windows (amd64, linux) (push) Failing after 2s
go: build dev binaries / build_dev_linux_windows (amd64, windows) (push) Failing after 3s
go: build dev binaries / build_dev_darwin (amd64, darwin) (push) Has been skipped
go: build dev binaries / build_dev_darwin (arm64, darwin) (push) Has been skipped
Java Client Unit Tests / Java Unit Tests (11) (push) Failing after 3s
Java Client Unit Tests / Java Unit Tests (17) (push) Failing after 2s
Java Client Unit Tests / Java Unit Tests (21) (push) Failing after 3s
Java Client Unit Tests / Java Unit Tests (8) (push) Failing after 3s
Kafka Quick Test (Load Test with Schema Registry) / Kafka Client Load Test (Quick) (push) Failing after 3s
Kafka Gateway Tests / Kafka Unit Tests (unit-tests-1) (push) Failing after 11s
Kafka Gateway Tests / Kafka Integration Tests (Critical) (integration-1) (push) Failing after 3s
Kafka Gateway Tests / Kafka End-to-End Tests (with SMQ) (e2e-1) (push) Failing after 3s
Kafka Gateway Tests / Kafka Consumer Group Tests (Highly Isolated) (consumer-group-1) (push) Failing after 2s
Kafka Gateway Tests / Kafka Client Compatibility (with SMQ) (client-compat-1) (push) Failing after 3s
Kafka Gateway Tests / Kafka SMQ Integration Tests (Full Stack) (smq-integration-1) (push) Failing after 3s
Kafka Gateway Tests / Kafka Protocol Tests (Isolated) (protocol-1) (push) Failing after 3s
KMS Tests / KMS Provider Integration Tests (push) Failing after 2s
KMS Tests / S3 KMS End-to-End Tests (push) Failing after 3s
Master Cold Start Tests / Master Cold Start Tests (push) Failing after 4s
Metadata Subscribe Integration Tests / Metadata Subscribe Integration Tests (push) Failing after 3s
Multi-Master Tests / Multi-Master Failover Tests (push) Failing after 3s
Performance / CPU and Heap Profile (push) Failing after 2s
Performance / S3 Read/Write Benchmark (rust volume) (push) Failing after 3s
pjdfstest POSIX Compliance / pjdfstest (push) Failing after 2s
Plugin Worker Integration Tests / Plugin Worker: erasure_coding (push) Failing after 3s
Plugin Worker Integration Tests / Plugin Worker: vacuum (push) Failing after 3s
Rust Volume Server Tests / Rust Integration Tests (push) Failing after 3s
Rust Volume Server Tests / Go Tests with Rust Volume (grpc - Shard 1) (push) Failing after 2s
Rust Volume Server Tests / Go Tests with Rust Volume (http - Shard 1) (push) Failing after 3s
Rust Volume Server Tests / Go Tests with Rust Volume (grpc - Shard 2) (push) Failing after 3s
Rust Volume Server Tests / Go Tests with Rust Volume (http - Shard 2) (push) Failing after 3s
Rust Volume Server Tests / Go Tests with Rust Volume (grpc - Shard 3) (push) Failing after 3s
S3 IAM Integration Tests / S3 IAM Distributed Tests (push) Failing after 2s
S3 IAM Integration Tests / S3 IAM Performance Tests (push) Failing after 3s
Performance / Throughput Benchmark (go volume) (push) Failing after 3s
Performance / Throughput Benchmark (rust volume) (push) Failing after 2s
Performance / S3 Read/Write Benchmark (go volume) (push) Failing after 3s
Plugin Worker Integration Tests / Plugin Worker: volume_balance (push) Failing after 3s
PostgreSQL Gateway Tests / PostgreSQL Basic Tests (push) Failing after 3s
Rust Volume Server Tests / Rust Unit Tests (push) Failing after 2s
Rust Volume Server Tests / Go Tests with Rust Volume (http - Shard 3) (push) Failing after 3s
Rust Plugin Worker Tests / Rust Plugin Worker Build and Unit Tests (push) Failing after 3s
rust: build dev volume server binaries / cleanup (push) Successful in 3s
S3 ETag and ACL Tests / S3 ETag + ACL Integration Tests (push) Failing after 3s
S3 IAM Integration Tests / IAM Unit Tests (push) Failing after 3s
S3 IAM Integration Tests / S3 IAM Integration Tests (advanced) (push) Failing after 3s
S3 IAM Integration Tests / S3 IAM Integration Tests (basic) (push) Failing after 2s
S3 IAM Integration Tests / S3 IAM Integration Tests (group) (push) Failing after 3s
S3 IAM Integration Tests / S3 IAM Integration Tests (policy-enforcement) (push) Failing after 2s
S3 IAM Integration Tests / S3 IAM Integration Tests (sts) (push) Failing after 4s
S3 Keycloak Integration Tests / S3 Keycloak Integration Tests (push) Failing after 3s
S3 PyArrow Parquet Tests / PyArrow Parquet Tests (Python 3.11) (push) Failing after 3s
S3 PyArrow Parquet Tests / Go Unit Tests (Implicit Directory) (push) Has been cancelled
S3 PyArrow Parquet Tests / PyArrow Parquet Tests (Python 3.12) (push) Has been cancelled
rust: build dev volume server binaries / build-rust-volume-dev-darwin (darwin-amd64, x86_64-apple-darwin) (push) Has been cancelled
rust: build dev volume server binaries / build-rust-volume-dev-darwin (darwin-arm64, aarch64-apple-darwin) (push) Has been cancelled
rust: build dev volume server binaries / build-rust-volume-dev-linux (linux-amd64, x86_64-unknown-linux-gnu) (push) Has been cancelled
S3 PyArrow Parquet Tests / PyArrow Parquet Tests (Python 3.9) (push) Has been cancelled
S3 Proxy Signature Tests / S3 Proxy Signature Verification Tests (push) Has been cancelled
S3 SDK V2 Route Disambiguation Tests / S3 SDK V2 Routing Tests (push) Has been cancelled
S3 SSE Tests / S3 SSE Integration Tests (quick) (push) Has been cancelled
S3 SSE Tests / S3 SSE Compatibility Test (push) Has been cancelled
S3 SSE Tests / S3 SSE Metadata Persistence Test (push) Has been cancelled
S3 SSE Tests / S3 SSE Copy Operations Test (push) Has been cancelled
S3 SSE Tests / S3 SSE Multipart Upload Test (push) Has been cancelled
S3 SSE Tests / S3 SSE Performance Test (push) Has been cancelled
S3 SSE Tests / S3 Volume Encryption Test (push) Has been cancelled
S3 SSE Tests / S3 SSE Integration Tests (comprehensive) (push) Has been cancelled
Spark Integration Tests / Spark Integration Tests (push) Failing after 2s
Telemetry Integration Tests / telemetry-integration-test (push) Failing after 3s
Volume Server Integration Tests / Volume Server Integration Tests (grpc - Shard 1) (push) Failing after 3s
Volume Server Integration Tests / Volume Server Integration Tests (http - Shard 1) (push) Failing after 3s
Ceph S3 tests / Basic S3 tests (KV store) (push) Has been cancelled
Ceph S3 tests / S3 Versioning & Object Lock tests (push) Has been cancelled
S3 Policy Integration Tests / S3 Policy Unit Tests (push) Failing after 3s
Ceph S3 tests / S3 CORS tests (push) Has been cancelled
Ceph S3 tests / SeaweedFS Custom S3 Copy tests (push) Has been cancelled
Ceph S3 tests / Basic S3 tests (SQL store) (push) Has been cancelled
test s3 over https using aws-cli / awscli-tests (push) Has been cancelled
TLS Rotation Integration Tests / TLS Rotation Integration Tests (push) Has been cancelled
Vacuum Integration Tests / Vacuum Integration Tests (push) Has been cancelled
S3 Policy Integration Tests / S3 Policy Variables Integration Tests (push) Failing after 3s
S3 Policy Integration Tests / S3 Policy Enforcement Integration Tests (advanced-policy) (push) Failing after 3s
S3 Policy Integration Tests / S3 Policy Enforcement Integration Tests (basic-policy) (push) Failing after 2s
S3 Policy Integration Tests / S3 Policy Enforcement Integration Tests (contextual-policy) (push) Failing after 4s
S3 Policy Integration Tests / Trusted Proxy Detection Tests (push) Failing after 2s
S3 Policy Integration Tests / S3 Policy Shell Integration Tests (push) Failing after 3s
Samba on FUSE Integration / samba-integration (push) Failing after 2s
SFTP Integration Tests / SFTP Integration Testing (push) Failing after 3s
terraform: validate and test modules / fmt, validate, plan-level tests (push) Failing after 3s
terraform: validate and test modules / local cluster smoke test (real weed) (push) Failing after 2s
Volume Server Integration Tests / Volume Server Integration Tests (grpc - Shard 3) (push) Failing after 3s
Volume Server Integration Tests / Volume Server Integration Tests (grpc - Shard 2) (push) Failing after 3s
Volume Server Integration Tests / Volume Server Integration Tests (http - Shard 2) (push) Failing after 2s
Volume Server Integration Tests / Volume Server Integration Tests (http - Shard 3) (push) Failing after 5s
mount: windows conformance / WinFsp conformance (push) Has been cancelled
mount: windows / Mount on Windows (push) Has been cancelled
snapshot of https://github.com/seaweedfs/seaweedfs.git (master, 2026-09-16; blobs > 131072B omitted)
2026-09-16 03:23:42 +05:00

425 lines
16 KiB
Go

package leveldb
import (
"context"
"errors"
"os"
"testing"
"github.com/seaweedfs/seaweedfs/weed/filer"
"github.com/seaweedfs/seaweedfs/weed/filer/empty_folder_cleanup"
"github.com/seaweedfs/seaweedfs/weed/pb"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
"github.com/seaweedfs/seaweedfs/weed/util"
)
// hookStore runs a hook once, right after the store call it is attached to returns,
// so a test can act inside a window that is otherwise not reachable.
type hookStore struct {
filer.FilerStore
afterList func()
listFired bool
afterInsert func(entry *filer.Entry)
insertFired bool
}
func (s *hookStore) ListDirectoryPrefixedEntries(ctx context.Context, dirPath util.FullPath, startFileName string, includeStartFile bool, limit int64, prefix string, eachEntryFunc filer.ListEachEntryFunc) (string, error) {
lastFileName, err := s.FilerStore.ListDirectoryPrefixedEntries(ctx, dirPath, startFileName, includeStartFile, limit, prefix, eachEntryFunc)
if s.afterList != nil && !s.listFired {
s.listFired = true
s.afterList()
}
return lastFileName, err
}
func (s *hookStore) InsertEntry(ctx context.Context, entry *filer.Entry) error {
err := s.FilerStore.InsertEntry(ctx, entry)
if s.afterInsert != nil && !s.insertFired {
s.insertFired = true
s.afterInsert(entry)
}
return err
}
// TestNonRecursiveFolderDeleteKeepsRacingChild covers the S3 empty-folder
// cleanup path: the delete lists a folder, finds it empty, and must not then
// bulk-delete its children, because an object written in between would be
// destroyed after the write was already acknowledged.
func TestNonRecursiveFolderDeleteKeepsRacingChild(t *testing.T) {
testFiler := filer.NewFiler(pb.ServerDiscovery{}, nil, "", "", "", "", "", 255, nil)
store := &LevelDB2Store{}
if err := store.initialize(t.TempDir(), 2); err != nil {
t.Fatal(err)
}
hooked := &hookStore{FilerStore: store}
testFiler.SetStore(hooked)
// the test has no metadata log consumer
ctx := filer.WithSuppressedMetadataEvents(context.Background())
dir := util.FullPath("/buckets/testbucket/data/abc")
child := dir.Child("obj")
dirEntry := &filer.Entry{FullPath: dir, Attr: filer.Attr{Mode: os.ModeDir | 0755}}
if err := testFiler.CreateEntry(ctx, dirEntry, nil, false, false, nil, false, testFiler.MaxFilenameLength); err != nil {
t.Fatalf("create folder: %v", err)
}
hooked.afterList = func() {
entry := &filer.Entry{FullPath: child, Attr: filer.Attr{Mode: 0640}}
if err := testFiler.CreateEntry(ctx, entry, nil, false, false, nil, false, testFiler.MaxFilenameLength); err != nil {
t.Errorf("create entry racing the folder delete: %v", err)
}
}
if err := testFiler.DeleteEntryMetaAndData(ctx, dir, false, false, false, false, nil, 0); err != nil {
t.Fatalf("delete empty folder: %v", err)
}
if _, err := testFiler.FindEntry(ctx, child); err != nil {
t.Errorf("entry created during the folder delete was removed: %v", err)
}
// The folder entry itself still goes, so the surviving entry is reachable by
// path but absent from listings until the folder comes back. Pinning that here
// keeps the remaining exposure visible; tighten it if the two steps ever become
// atomic.
if _, err := testFiler.FindEntry(ctx, dir); !errors.Is(err, filer_pb.ErrNotFound) {
t.Errorf("folder entry should still be removed, got %v", err)
}
}
// TestEnsureDirectoryEntryRestoresRacingParent covers the leftover of the same
// race: the entry survives the folder delete but its directory does not, so the
// entry drops out of listings until the directory is put back.
func TestEnsureDirectoryEntryRestoresRacingParent(t *testing.T) {
testFiler := filer.NewFiler(pb.ServerDiscovery{}, nil, "", "", "", "", "", 255, nil)
store := &LevelDB2Store{}
if err := store.initialize(t.TempDir(), 2); err != nil {
t.Fatal(err)
}
hooked := &hookStore{FilerStore: store}
testFiler.SetStore(hooked)
ctx := filer.WithSuppressedMetadataEvents(context.Background())
parent := util.FullPath("/buckets/testbucket/data")
dir := parent.Child("abc")
child := dir.Child("obj")
dirEntry := &filer.Entry{FullPath: dir, Attr: filer.Attr{Mode: os.ModeDir | 0755}}
if err := testFiler.CreateEntry(ctx, dirEntry, nil, false, false, nil, false, testFiler.MaxFilenameLength); err != nil {
t.Fatalf("create folder: %v", err)
}
// the cleaner reads these before deleting, so a restore does not have to guess
attrs, err := testFiler.DirectoryAttributes(ctx, dir)
if err != nil {
t.Fatalf("read folder attributes: %v", err)
}
hooked.afterList = func() {
entry := &filer.Entry{FullPath: child, Attr: filer.Attr{Mode: 0640}}
if err := testFiler.CreateEntry(ctx, entry, nil, false, false, nil, false, testFiler.MaxFilenameLength); err != nil {
t.Errorf("create entry racing the folder delete: %v", err)
}
}
if err := testFiler.DeleteEntryMetaAndData(ctx, dir, false, false, false, false, nil, 0); err != nil {
t.Fatalf("delete empty folder: %v", err)
}
if names := listNames(ctx, t, testFiler, parent); len(names) != 0 {
t.Fatalf("folder should be gone from its parent before the restore, got %v", names)
}
if err := testFiler.EnsureDirectoryEntry(ctx, dir, attrs); err != nil {
t.Fatalf("restore folder: %v", err)
}
restored, err := testFiler.FindEntry(ctx, dir)
if err != nil {
t.Fatalf("find restored folder: %v", err)
}
if !restored.IsDirectory() {
t.Errorf("restored %s is not a directory", dir)
}
if names := listNames(ctx, t, testFiler, parent); len(names) != 1 || names[0] != "abc" {
t.Errorf("restored folder should be listed under its parent, got %v", names)
}
}
// TestEnsureDirectoryEntryRestoresExactAttributes checks that a restored directory
// comes back exactly as it was. Guessing from an ancestor, or forcing traversal bits,
// would hand back a directory that grants access the original one denied.
func TestEnsureDirectoryEntryRestoresExactAttributes(t *testing.T) {
testFiler := filer.NewFiler(pb.ServerDiscovery{}, nil, "", "", "", "", "", 255, nil)
store := &LevelDB2Store{}
if err := store.initialize(t.TempDir(), 2); err != nil {
t.Fatal(err)
}
testFiler.SetStore(store)
ctx := filer.WithSuppressedMetadataEvents(context.Background())
// a private directory under a world-traversable parent
dir := util.FullPath("/buckets/testbucket/data").Child("private")
dirEntry := &filer.Entry{FullPath: dir, Attr: filer.Attr{Mode: os.ModeDir | 0700, Uid: 4242, Gid: 4343}}
if err := testFiler.CreateEntry(ctx, dirEntry, nil, false, false, nil, false, testFiler.MaxFilenameLength); err != nil {
t.Fatalf("create folder: %v", err)
}
attrs, err := testFiler.DirectoryAttributes(ctx, dir)
if err != nil {
t.Fatalf("read folder attributes: %v", err)
}
if err := testFiler.DeleteEntryMetaAndData(ctx, dir, false, false, false, false, nil, 0); err != nil {
t.Fatalf("delete folder: %v", err)
}
if err := testFiler.EnsureDirectoryEntry(ctx, dir, attrs); err != nil {
t.Fatalf("restore folder: %v", err)
}
restored, err := testFiler.FindEntry(ctx, dir)
if err != nil {
t.Fatalf("find restored folder: %v", err)
}
if !restored.IsDirectory() {
t.Errorf("restored %s is not a directory", dir)
}
if restored.Mode.Perm() != 0700 {
t.Errorf("restored folder should keep mode 0700, got %o", restored.Mode.Perm())
}
if restored.Uid != 4242 || restored.Gid != 4343 {
t.Errorf("restored folder should keep its owner, got uid=%d gid=%d", restored.Uid, restored.Gid)
}
}
// TestCreateEntryCreatesParentAfterTheEntry covers the writer's half of the pair: the
// folder is taken while the entry is landing, which used to orphan it.
func TestCreateEntryCreatesParentAfterTheEntry(t *testing.T) {
testFiler := filer.NewFiler(pb.ServerDiscovery{}, nil, "", "", "", "", "", 255, nil)
store := &LevelDB2Store{}
if err := store.initialize(t.TempDir(), 2); err != nil {
t.Fatal(err)
}
hooked := &hookStore{FilerStore: store}
testFiler.SetStore(hooked)
ctx := filer.WithSuppressedMetadataEvents(context.Background())
parent := util.FullPath("/buckets/testbucket/data")
dir := parent.Child("abc")
child := dir.Child("obj")
dirEntry := &filer.Entry{FullPath: dir, Attr: filer.Attr{Mode: os.ModeDir | 0755}}
if err := testFiler.CreateEntry(ctx, dirEntry, nil, false, false, nil, false, testFiler.MaxFilenameLength); err != nil {
t.Fatalf("create folder: %v", err)
}
// the cleaner found the folder empty a moment ago and removes it now
hooked.afterInsert = func(entry *filer.Entry) {
if entry.FullPath != child {
return
}
if err := store.DeleteEntry(ctx, dir); err != nil {
t.Errorf("delete folder racing the insert: %v", err)
}
}
entry := &filer.Entry{FullPath: child, Attr: filer.Attr{Mode: 0640}}
if err := testFiler.CreateEntry(ctx, entry, nil, false, false, nil, false, testFiler.MaxFilenameLength); err != nil {
t.Fatalf("create entry racing the folder delete: %v", err)
}
if _, err := testFiler.FindEntry(ctx, child); err != nil {
t.Fatalf("entry created during the folder delete is gone: %v", err)
}
restored, err := testFiler.FindEntry(ctx, dir)
if err != nil {
t.Fatalf("parent taken during the insert should be created after it: %v", err)
}
if !restored.IsDirectory() {
t.Errorf("recreated %s is not a directory", dir)
}
if names := listNames(ctx, t, testFiler, parent); len(names) != 1 || names[0] != "abc" {
t.Errorf("entry should be reachable by listing again, got %v", names)
}
}
// TestCreateEntryKeepsTheEntryWhenTheParentCannotBeCreated pins the cost of going
// second: the entry is already stored when the parent fails, and it is left there,
// since nothing can tell it apart from a concurrent create that has succeeded.
func TestCreateEntryKeepsTheEntryWhenTheParentCannotBeCreated(t *testing.T) {
testFiler := filer.NewFiler(pb.ServerDiscovery{}, nil, "", "", "", "", "", 255, nil)
store := &LevelDB2Store{}
if err := store.initialize(t.TempDir(), 2); err != nil {
t.Fatal(err)
}
testFiler.SetStore(store)
ctx := filer.WithSuppressedMetadataEvents(context.Background())
// an invalid bucket name is rejected while the parents are being created
child := util.FullPath("/buckets/Not_A_Valid_Bucket/obj")
entry := &filer.Entry{FullPath: child, Attr: filer.Attr{Mode: 0640}}
if err := testFiler.CreateEntry(ctx, entry, nil, false, false, nil, false, testFiler.MaxFilenameLength); err == nil {
t.Fatal("an invalid bucket name should still fail the create")
}
if _, err := testFiler.FindEntry(ctx, child); err != nil {
t.Errorf("the entry is kept rather than risking a concurrent create: %v", err)
}
}
// TestCreateEntryDoesNotAnnounceAnEntryWhoseParentFailed pins the other half of keeping
// the entry: it stays off the log. The aggregator replicates creates into peer stores,
// so announcing a create that failed would put the parentless row on every filer.
func TestCreateEntryDoesNotAnnounceAnEntryWhoseParentFailed(t *testing.T) {
testFiler := filer.NewFiler(pb.ServerDiscovery{}, nil, "", "", "", "", "", 255, nil)
store := &LevelDB2Store{}
if err := store.initialize(t.TempDir(), 2); err != nil {
t.Fatal(err)
}
testFiler.SetStore(store)
ctx, sink := filer.WithMetadataEventSink(context.Background())
child := util.FullPath("/buckets/Not_A_Valid_Bucket/obj")
entry := &filer.Entry{FullPath: child, Attr: filer.Attr{Mode: 0640}}
if err := testFiler.CreateEntry(ctx, entry, nil, false, false, nil, false, testFiler.MaxFilenameLength); err == nil {
t.Fatal("an invalid bucket name should still fail the create")
}
if event := sink.Last(); event != nil {
t.Errorf("a create that failed must not be announced, got %+v", event.EventNotification)
}
if _, err := testFiler.FindEntry(ctx, child); err != nil {
t.Errorf("the entry itself is still kept: %v", err)
}
}
// TestEnsureDirectoryEntryNarrowsAWiderRestore covers a writer getting to the missing
// parent first and minting it wider than the one that was deleted.
func TestEnsureDirectoryEntryNarrowsAWiderRestore(t *testing.T) {
testFiler := filer.NewFiler(pb.ServerDiscovery{}, nil, "", "", "", "", "", 255, nil)
store := &LevelDB2Store{}
if err := store.initialize(t.TempDir(), 2); err != nil {
t.Fatal(err)
}
testFiler.SetStore(store)
ctx := filer.WithSuppressedMetadataEvents(context.Background())
dir := util.FullPath("/buckets/testbucket/data").Child("private")
dirEntry := &filer.Entry{FullPath: dir, Attr: filer.Attr{Mode: os.ModeDir | 0700, Uid: 4242, Gid: 4343}}
if err := testFiler.CreateEntry(ctx, dirEntry, nil, false, false, nil, false, testFiler.MaxFilenameLength); err != nil {
t.Fatalf("create folder: %v", err)
}
attrs, err := testFiler.DirectoryAttributes(ctx, dir)
if err != nil {
t.Fatalf("read folder attributes: %v", err)
}
if err := testFiler.DeleteEntryMetaAndData(ctx, dir, false, false, false, false, nil, 0); err != nil {
t.Fatalf("delete folder: %v", err)
}
// the writer's own entry is 0640, so the parent it mints comes back 0751
entry := &filer.Entry{FullPath: dir.Child("obj"), Attr: filer.Attr{Mode: 0640}}
if err := testFiler.CreateEntry(ctx, entry, nil, false, false, nil, false, testFiler.MaxFilenameLength); err != nil {
t.Fatalf("create entry: %v", err)
}
widened, err := testFiler.FindEntry(ctx, dir)
if err != nil {
t.Fatalf("find minted folder: %v", err)
}
if widened.Mode.Perm() == 0700 {
t.Skip("the writer no longer widens the parent, nothing left to narrow")
}
if err := testFiler.EnsureDirectoryEntry(ctx, dir, attrs); err != nil {
t.Fatalf("restore folder: %v", err)
}
restored, err := testFiler.FindEntry(ctx, dir)
if err != nil {
t.Fatalf("find restored folder: %v", err)
}
if !restored.IsDirectory() {
t.Errorf("restored %s is not a directory", dir)
}
if restored.Mode.Perm() != 0700 {
t.Errorf("restore should hand back mode 0700, got %o", restored.Mode.Perm())
}
if _, err := testFiler.FindEntry(ctx, entry.FullPath); err != nil {
t.Errorf("narrowing the folder should not disturb what it holds: %v", err)
}
}
// TestEnsureDirectoryEntryKeepsATightenedDirectory checks the other direction: a
// restore only ever narrows, so a deliberate tightening is left alone.
func TestEnsureDirectoryEntryKeepsATightenedDirectory(t *testing.T) {
testFiler := filer.NewFiler(pb.ServerDiscovery{}, nil, "", "", "", "", "", 255, nil)
store := &LevelDB2Store{}
if err := store.initialize(t.TempDir(), 2); err != nil {
t.Fatal(err)
}
testFiler.SetStore(store)
ctx := filer.WithSuppressedMetadataEvents(context.Background())
dir := util.FullPath("/buckets/testbucket/data").Child("tightened")
dirEntry := &filer.Entry{FullPath: dir, Attr: filer.Attr{Mode: os.ModeDir | 0700}}
if err := testFiler.CreateEntry(ctx, dirEntry, nil, false, false, nil, false, testFiler.MaxFilenameLength); err != nil {
t.Fatalf("create folder: %v", err)
}
if err := testFiler.EnsureDirectoryEntry(ctx, dir, empty_folder_cleanup.DirectoryAttributes{Mode: 0755}); err != nil {
t.Fatalf("restore folder: %v", err)
}
kept, err := testFiler.FindEntry(ctx, dir)
if err != nil {
t.Fatalf("find folder: %v", err)
}
if kept.Mode.Perm() != 0700 {
t.Errorf("a directory tightened since should keep mode 0700, got %o", kept.Mode.Perm())
}
}
// TestEnsureDirectoryEntryIntersectsMixedModes covers modes that are not nested, where
// replacing rather than intersecting would grant a bit the directory denied.
func TestEnsureDirectoryEntryIntersectsMixedModes(t *testing.T) {
testFiler := filer.NewFiler(pb.ServerDiscovery{}, nil, "", "", "", "", "", 255, nil)
store := &LevelDB2Store{}
if err := store.initialize(t.TempDir(), 2); err != nil {
t.Fatal(err)
}
testFiler.SetStore(store)
ctx := filer.WithSuppressedMetadataEvents(context.Background())
dir := util.FullPath("/buckets/testbucket/data").Child("mixed")
dirEntry := &filer.Entry{FullPath: dir, Attr: filer.Attr{Mode: os.ModeDir | 0705}}
if err := testFiler.CreateEntry(ctx, dirEntry, nil, false, false, nil, false, testFiler.MaxFilenameLength); err != nil {
t.Fatalf("create folder: %v", err)
}
// 0750 grants group access that 0705 denies, and denies the other access it grants
if err := testFiler.EnsureDirectoryEntry(ctx, dir, empty_folder_cleanup.DirectoryAttributes{Mode: 0750}); err != nil {
t.Fatalf("restore folder: %v", err)
}
restored, err := testFiler.FindEntry(ctx, dir)
if err != nil {
t.Fatalf("find folder: %v", err)
}
if restored.Mode.Perm() != 0700 {
t.Errorf("restore should grant only what both modes allow, got %o", restored.Mode.Perm())
}
}
func listNames(ctx context.Context, t *testing.T, f *filer.Filer, dir util.FullPath) []string {
t.Helper()
entries, _, err := f.ListDirectoryEntries(ctx, dir, "", false, 100, "", "", "")
if err != nil {
t.Fatalf("list %s: %v", dir, err)
}
var names []string
for _, entry := range entries {
names = append(names, entry.Name())
}
return names
}