mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-06 02:57:37 +01:00
iam: test legacy identity.json write racing the migration rename
Teach the fake filer server AtomicRenameEntry with a pre-rename hook, and cover both the clean migration path and a legacy write that lands between the load-time read and the rename. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
1 parent
6ec6cce3fe
commit
49d9d0c1cd
2 files changed
+86
No files matched your search
@@ -2,6 +2,7 @@ package filer_etc
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/seaweedfs/seaweedfs/weed/filer"
|
||||
@@ -142,3 +143,64 @@ func TestLoadConfigurationSucceedsOnValidFiles(t *testing.T) {
|
||||
}
|
||||
assert.ElementsMatch(t, []string{"alice", "bob"}, names)
|
||||
}
|
||||
|
||||
// A legacy client that rewrites identity.json between the migration's
|
||||
// load-time read and its rename strands the newer content in
|
||||
// identity.json.old. The migration must pick up those entries instead of
|
||||
// losing them.
|
||||
func TestLoadConfigurationMigratesLegacyWriteRacingWithRename(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
store, server := newPolicyTestStoreWithServer(t)
|
||||
|
||||
v1 := []byte(`{"identities":[{"name":"alice","credentials":[{"accessKey":"AK1","secretKey":"SK1"}]}]}`)
|
||||
v2 := []byte(`{"identities":[{"name":"alice","credentials":[{"accessKey":"AK1","secretKey":"SK1"}]},{"name":"bob","credentials":[{"accessKey":"AK2","secretKey":"SK2"}]}]}`)
|
||||
putEntry(t, server, filer.IamConfigDirectory, IamLegacyIdentityFile, v1)
|
||||
|
||||
var once sync.Once
|
||||
server.beforeAtomicRename = func(dir, name string) {
|
||||
once.Do(func() {
|
||||
server.mu.Lock()
|
||||
defer server.mu.Unlock()
|
||||
key := filerEntryKey(dir, name)
|
||||
entry := cloneEntry(server.entries[key])
|
||||
entry.Content = v2
|
||||
server.entries[key] = entry
|
||||
})
|
||||
}
|
||||
|
||||
_, err := store.LoadConfiguration(ctx)
|
||||
require.NoError(t, err)
|
||||
|
||||
bob, err := store.GetUser(ctx, "bob")
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "bob", bob.Name)
|
||||
|
||||
alice, err := store.GetUser(ctx, "alice")
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "alice", alice.Name)
|
||||
|
||||
_, found, err := store.readInsideFiler(ctx, filer.IamConfigDirectory, IamLegacyIdentityFile)
|
||||
require.NoError(t, err)
|
||||
assert.False(t, found)
|
||||
}
|
||||
|
||||
// Without a concurrent write, the migration still renames the legacy file and
|
||||
// writes out every identity.
|
||||
func TestLoadConfigurationMigratesLegacyFile(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
store, server := newPolicyTestStoreWithServer(t)
|
||||
|
||||
legacy := []byte(`{"identities":[{"name":"alice","credentials":[{"accessKey":"AK1","secretKey":"SK1"}]}]}`)
|
||||
putEntry(t, server, filer.IamConfigDirectory, IamLegacyIdentityFile, legacy)
|
||||
|
||||
_, err := store.LoadConfiguration(ctx)
|
||||
require.NoError(t, err)
|
||||
|
||||
alice, err := store.GetUser(ctx, "alice")
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "alice", alice.Name)
|
||||
|
||||
_, found, err := store.readInsideFiler(ctx, filer.IamConfigDirectory, IamLegacyIdentityFile)
|
||||
require.NoError(t, err)
|
||||
assert.False(t, found)
|
||||
}
|
||||
@@ -32,6 +32,7 @@ type policyTestFilerServer struct {
|
||||
afterListEntry func(string, string)
|
||||
beforeDelete func(string, string) error
|
||||
beforeUpdate func(string, string) error
|
||||
beforeAtomicRename func(string, string)
|
||||
}
|
||||
|
||||
func newPolicyTestFilerServer() *policyTestFilerServer {
|
||||
@@ -139,6 +140,29 @@ func (s *policyTestFilerServer) DeleteEntry(_ context.Context, req *filer_pb.Del
|
||||
return &filer_pb.DeleteEntryResponse{}, nil
|
||||
}
|
||||
|
||||
func (s *policyTestFilerServer) AtomicRenameEntry(_ context.Context, req *filer_pb.AtomicRenameEntryRequest) (*filer_pb.AtomicRenameEntryResponse, error) {
|
||||
s.mu.RLock()
|
||||
beforeAtomicRename := s.beforeAtomicRename
|
||||
s.mu.RUnlock()
|
||||
if beforeAtomicRename != nil {
|
||||
beforeAtomicRename(req.OldDirectory, req.OldName)
|
||||
}
|
||||
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
oldKey := filerEntryKey(req.OldDirectory, req.OldName)
|
||||
entry, found := s.entries[oldKey]
|
||||
if !found {
|
||||
return nil, status.Error(codes.NotFound, filer_pb.ErrNotFound.Error())
|
||||
}
|
||||
delete(s.entries, oldKey)
|
||||
renamed := cloneEntry(entry)
|
||||
renamed.Name = req.NewName
|
||||
s.entries[filerEntryKey(req.NewDirectory, req.NewName)] = renamed
|
||||
return &filer_pb.AtomicRenameEntryResponse{}, nil
|
||||
}
|
||||
|
||||
func newPolicyTestStore(t *testing.T) *FilerEtcStore {
|
||||
store, _ := newPolicyTestStoreWithServer(t)
|
||||
return store
|
||||
|
||||
Reference in new issue
Block a user