2022-06-15 09:50:59 +00:00
|
|
|
package meta
|
|
|
|
|
|
|
|
import (
|
2023-08-31 16:26:47 +00:00
|
|
|
"context"
|
2022-06-15 09:50:59 +00:00
|
|
|
"encoding/binary"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
2024-02-09 06:17:17 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/shard/mode"
|
2022-06-15 09:50:59 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"go.etcd.io/bbolt"
|
|
|
|
)
|
|
|
|
|
2022-07-27 18:34:25 +00:00
|
|
|
type epochStateImpl struct{}
|
|
|
|
|
|
|
|
func (s epochStateImpl) CurrentEpoch() uint64 {
|
2022-09-09 10:27:15 +00:00
|
|
|
return 0
|
2022-07-27 18:34:25 +00:00
|
|
|
}
|
|
|
|
|
2022-06-15 09:50:59 +00:00
|
|
|
func TestVersion(t *testing.T) {
|
|
|
|
dir := t.TempDir()
|
|
|
|
|
|
|
|
newDB := func(t *testing.T) *DB {
|
|
|
|
return New(WithPath(filepath.Join(dir, t.Name())),
|
2023-10-31 11:56:55 +00:00
|
|
|
WithPermissions(0o600), WithEpochState(epochStateImpl{}))
|
2022-06-15 09:50:59 +00:00
|
|
|
}
|
|
|
|
check := func(t *testing.T, db *DB) {
|
|
|
|
require.NoError(t, db.boltDB.View(func(tx *bbolt.Tx) error {
|
|
|
|
b := tx.Bucket(shardInfoBucket)
|
|
|
|
if b == nil {
|
|
|
|
return errors.New("shard info bucket not found")
|
|
|
|
}
|
|
|
|
data := b.Get(versionKey)
|
|
|
|
if len(data) != 8 {
|
|
|
|
return errors.New("invalid version data")
|
|
|
|
}
|
|
|
|
if stored := binary.LittleEndian.Uint64(data); stored != version {
|
|
|
|
return fmt.Errorf("invalid version: %d != %d", stored, version)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
t.Run("simple", func(t *testing.T) {
|
|
|
|
db := newDB(t)
|
2024-02-09 06:17:17 +00:00
|
|
|
require.NoError(t, db.Open(context.Background(), mode.ReadWrite))
|
2022-06-15 09:50:59 +00:00
|
|
|
require.NoError(t, db.Init())
|
|
|
|
check(t, db)
|
|
|
|
require.NoError(t, db.Close())
|
|
|
|
|
|
|
|
t.Run("reopen", func(t *testing.T) {
|
2024-02-09 06:17:17 +00:00
|
|
|
require.NoError(t, db.Open(context.Background(), mode.ReadWrite))
|
2022-06-15 09:50:59 +00:00
|
|
|
require.NoError(t, db.Init())
|
|
|
|
check(t, db)
|
|
|
|
require.NoError(t, db.Close())
|
|
|
|
})
|
|
|
|
})
|
|
|
|
t.Run("old data", func(t *testing.T) {
|
|
|
|
db := newDB(t)
|
2024-10-21 08:56:38 +00:00
|
|
|
require.NoError(t, db.SetShardID(context.Background(), []byte{1, 2, 3, 4}, mode.ReadWrite))
|
2022-06-15 09:50:59 +00:00
|
|
|
|
2024-02-09 06:17:17 +00:00
|
|
|
require.NoError(t, db.Open(context.Background(), mode.ReadWrite))
|
2022-06-15 09:50:59 +00:00
|
|
|
require.NoError(t, db.Init())
|
|
|
|
check(t, db)
|
|
|
|
require.NoError(t, db.Close())
|
|
|
|
})
|
|
|
|
t.Run("invalid version", func(t *testing.T) {
|
|
|
|
db := newDB(t)
|
2024-02-09 06:17:17 +00:00
|
|
|
require.NoError(t, db.Open(context.Background(), mode.ReadWrite))
|
2022-06-15 09:50:59 +00:00
|
|
|
require.NoError(t, db.boltDB.Update(func(tx *bbolt.Tx) error {
|
|
|
|
return updateVersion(tx, version+1)
|
|
|
|
}))
|
|
|
|
require.NoError(t, db.Close())
|
|
|
|
|
2024-02-09 06:17:17 +00:00
|
|
|
require.NoError(t, db.Open(context.Background(), mode.ReadWrite))
|
2022-06-15 09:50:59 +00:00
|
|
|
require.Error(t, db.Init())
|
|
|
|
require.NoError(t, db.Close())
|
|
|
|
|
|
|
|
t.Run("reset", func(t *testing.T) {
|
2024-02-09 06:17:17 +00:00
|
|
|
require.NoError(t, db.Open(context.Background(), mode.ReadWrite))
|
2022-06-15 09:50:59 +00:00
|
|
|
require.NoError(t, db.Reset())
|
|
|
|
check(t, db)
|
|
|
|
require.NoError(t, db.Close())
|
|
|
|
})
|
|
|
|
})
|
2024-08-29 13:33:30 +00:00
|
|
|
t.Run("incompleted upgrade", func(t *testing.T) {
|
|
|
|
db := newDB(t)
|
|
|
|
require.NoError(t, db.Open(context.Background(), mode.ReadWrite))
|
|
|
|
require.NoError(t, db.Init())
|
|
|
|
require.NoError(t, db.Close())
|
|
|
|
|
|
|
|
require.NoError(t, db.Open(context.Background(), mode.ReadWrite))
|
|
|
|
require.NoError(t, db.boltDB.Update(func(tx *bbolt.Tx) error {
|
|
|
|
return tx.Bucket(shardInfoBucket).Put(upgradeKey, zeroValue)
|
|
|
|
}))
|
|
|
|
require.ErrorIs(t, db.Init(), ErrIncompletedUpgrade)
|
|
|
|
require.NoError(t, db.Close())
|
|
|
|
|
|
|
|
require.NoError(t, db.Open(context.Background(), mode.ReadWrite))
|
|
|
|
require.NoError(t, db.boltDB.Update(func(tx *bbolt.Tx) error {
|
|
|
|
return tx.Bucket(shardInfoBucket).Delete(upgradeKey)
|
|
|
|
}))
|
|
|
|
require.NoError(t, db.Init())
|
|
|
|
require.NoError(t, db.Close())
|
|
|
|
})
|
2022-06-15 09:50:59 +00:00
|
|
|
}
|