Dmitrii Stepanov
882c068410
All checks were successful
DCO action / DCO (pull_request) Successful in 1m35s
Tests and linters / Run gofumpt (pull_request) Successful in 1m52s
Build / Build Components (1.22) (pull_request) Successful in 2m40s
Build / Build Components (1.23) (pull_request) Successful in 2m39s
Vulncheck / Vulncheck (pull_request) Successful in 2m20s
Pre-commit hooks / Pre-commit (pull_request) Successful in 2m53s
Tests and linters / Tests (1.22) (pull_request) Successful in 3m11s
Tests and linters / Tests (1.23) (pull_request) Successful in 3m13s
Tests and linters / Staticcheck (pull_request) Successful in 3m46s
Tests and linters / Tests with -race (pull_request) Successful in 4m16s
Tests and linters / Lint (pull_request) Successful in 4m23s
Tests and linters / gopls check (pull_request) Successful in 4m14s
This allows to check if metabase upgrade was not completed. Signed-off-by: Dmitrii Stepanov <d.stepanov@yadro.com>
107 lines
3 KiB
Go
107 lines
3 KiB
Go
package meta
|
|
|
|
import (
|
|
"context"
|
|
"encoding/binary"
|
|
"errors"
|
|
"fmt"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/shard/mode"
|
|
"github.com/stretchr/testify/require"
|
|
"go.etcd.io/bbolt"
|
|
)
|
|
|
|
type epochStateImpl struct{}
|
|
|
|
func (s epochStateImpl) CurrentEpoch() uint64 {
|
|
return 0
|
|
}
|
|
|
|
func TestVersion(t *testing.T) {
|
|
dir := t.TempDir()
|
|
|
|
newDB := func(t *testing.T) *DB {
|
|
return New(WithPath(filepath.Join(dir, t.Name())),
|
|
WithPermissions(0o600), WithEpochState(epochStateImpl{}))
|
|
}
|
|
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(context.Background(), mode.ReadWrite))
|
|
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(context.Background(), mode.ReadWrite))
|
|
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.SetShardID([]byte{1, 2, 3, 4}, mode.ReadWrite))
|
|
|
|
require.NoError(t, db.Open(context.Background(), mode.ReadWrite))
|
|
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(context.Background(), mode.ReadWrite))
|
|
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(context.Background(), mode.ReadWrite))
|
|
require.Error(t, db.Init())
|
|
require.NoError(t, db.Close())
|
|
|
|
t.Run("reset", func(t *testing.T) {
|
|
require.NoError(t, db.Open(context.Background(), mode.ReadWrite))
|
|
require.NoError(t, db.Reset())
|
|
check(t, db)
|
|
require.NoError(t, db.Close())
|
|
})
|
|
})
|
|
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())
|
|
})
|
|
}
|