6f243a2a76
The main problem is to distinguish the case of initial initialization and update from version 0. We can't do this at `Open`, because of `resync_metabase` flag. Thus, the following approach was taken: 1. During `Open` check whether the metabase was initialized. 2. Check for the version in `Init` or write the new one if the metabase is new. 3. Update version in `Reset`. Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
81 lines
1.9 KiB
Go
81 lines
1.9 KiB
Go
package meta
|
|
|
|
import (
|
|
"encoding/binary"
|
|
"errors"
|
|
"fmt"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
"go.etcd.io/bbolt"
|
|
)
|
|
|
|
func TestVersion(t *testing.T) {
|
|
dir := t.TempDir()
|
|
|
|
newDB := func(t *testing.T) *DB {
|
|
return New(WithPath(filepath.Join(dir, t.Name())),
|
|
WithPermissions(0600))
|
|
}
|
|
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)
|
|
require.NoError(t, db.Open())
|
|
require.NoError(t, db.Init())
|
|
check(t, db)
|
|
require.NoError(t, db.Close())
|
|
|
|
t.Run("reopen", func(t *testing.T) {
|
|
require.NoError(t, db.Open())
|
|
require.NoError(t, db.Init())
|
|
check(t, db)
|
|
require.NoError(t, db.Close())
|
|
})
|
|
})
|
|
t.Run("old data", func(t *testing.T) {
|
|
db := newDB(t)
|
|
require.NoError(t, db.Open())
|
|
require.NoError(t, db.WriteShardID([]byte{1, 2, 3, 4}))
|
|
require.NoError(t, db.Close())
|
|
|
|
require.NoError(t, db.Open())
|
|
require.NoError(t, db.Init())
|
|
check(t, db)
|
|
require.NoError(t, db.Close())
|
|
})
|
|
t.Run("invalid version", func(t *testing.T) {
|
|
db := newDB(t)
|
|
require.NoError(t, db.Open())
|
|
require.NoError(t, db.boltDB.Update(func(tx *bbolt.Tx) error {
|
|
return updateVersion(tx, version+1)
|
|
}))
|
|
require.NoError(t, db.Close())
|
|
|
|
require.NoError(t, db.Open())
|
|
require.Error(t, db.Init())
|
|
require.NoError(t, db.Close())
|
|
|
|
t.Run("reset", func(t *testing.T) {
|
|
require.NoError(t, db.Open())
|
|
require.NoError(t, db.Reset())
|
|
check(t, db)
|
|
require.NoError(t, db.Close())
|
|
})
|
|
})
|
|
}
|