2018-12-07 01:24:43 +00:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
2022-03-15 12:42:43 +00:00
|
|
|
"github.com/hashicorp/nomad/ci"
|
2018-12-07 01:24:43 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/boltdd"
|
|
|
|
"github.com/hashicorp/nomad/helper/testlog"
|
|
|
|
"github.com/hashicorp/nomad/helper/uuid"
|
|
|
|
"github.com/stretchr/testify/require"
|
2022-02-23 20:04:44 +00:00
|
|
|
"go.etcd.io/bbolt"
|
2018-12-07 01:24:43 +00:00
|
|
|
)
|
|
|
|
|
2022-05-12 15:42:40 +00:00
|
|
|
func setupBoltDB(t *testing.T) *bbolt.DB {
|
|
|
|
dir := t.TempDir()
|
2018-12-18 23:36:32 +00:00
|
|
|
|
2022-02-23 20:04:44 +00:00
|
|
|
db, err := bbolt.Open(filepath.Join(dir, "state.db"), 0666, nil)
|
2022-05-12 15:42:40 +00:00
|
|
|
require.NoError(t, err)
|
2018-12-18 23:36:32 +00:00
|
|
|
|
2022-05-12 15:42:40 +00:00
|
|
|
t.Cleanup(func() {
|
2018-12-18 23:36:32 +00:00
|
|
|
require.NoError(t, db.Close())
|
2022-05-12 15:42:40 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return db
|
2018-12-18 23:36:32 +00:00
|
|
|
}
|
|
|
|
|
2018-12-07 01:24:43 +00:00
|
|
|
// TestUpgrade_NeedsUpgrade_New asserts new state dbs do not need upgrading.
|
|
|
|
func TestUpgrade_NeedsUpgrade_New(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-12-07 01:24:43 +00:00
|
|
|
|
2018-12-18 23:36:32 +00:00
|
|
|
// Setting up a new StateDB should initialize it at the latest version.
|
2022-05-12 15:42:40 +00:00
|
|
|
db := setupBoltStateDB(t)
|
2018-12-07 01:24:43 +00:00
|
|
|
|
2022-02-23 20:23:07 +00:00
|
|
|
to09, to12, err := NeedsUpgrade(db.DB().BoltDB())
|
2018-12-07 01:24:43 +00:00
|
|
|
require.NoError(t, err)
|
2022-02-23 20:23:07 +00:00
|
|
|
require.False(t, to09)
|
|
|
|
require.False(t, to12)
|
2018-12-07 01:24:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TestUpgrade_NeedsUpgrade_Old asserts state dbs with just the alloctions
|
|
|
|
// bucket *do* need upgrading.
|
|
|
|
func TestUpgrade_NeedsUpgrade_Old(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-12-07 01:24:43 +00:00
|
|
|
|
2022-05-12 15:42:40 +00:00
|
|
|
db := setupBoltDB(t)
|
2018-12-07 01:24:43 +00:00
|
|
|
|
|
|
|
// Create the allocations bucket which exists in both the old and 0.9
|
|
|
|
// schemas
|
2022-02-23 20:04:44 +00:00
|
|
|
require.NoError(t, db.Update(func(tx *bbolt.Tx) error {
|
2018-12-07 01:24:43 +00:00
|
|
|
_, err := tx.CreateBucket(allocationsBucketName)
|
|
|
|
return err
|
|
|
|
}))
|
|
|
|
|
2022-02-23 20:23:07 +00:00
|
|
|
to09, to12, err := NeedsUpgrade(db)
|
2018-12-07 01:24:43 +00:00
|
|
|
require.NoError(t, err)
|
2022-02-23 20:23:07 +00:00
|
|
|
require.True(t, to09)
|
|
|
|
require.True(t, to12)
|
2018-12-07 01:24:43 +00:00
|
|
|
|
|
|
|
// Adding meta should mark it as upgraded
|
|
|
|
require.NoError(t, db.Update(addMeta))
|
|
|
|
|
2022-02-23 20:23:07 +00:00
|
|
|
to09, to12, err = NeedsUpgrade(db)
|
2018-12-07 01:24:43 +00:00
|
|
|
require.NoError(t, err)
|
2022-02-23 20:23:07 +00:00
|
|
|
require.False(t, to09)
|
|
|
|
require.False(t, to12)
|
2018-12-07 01:24:43 +00:00
|
|
|
}
|
|
|
|
|
2018-12-18 22:49:09 +00:00
|
|
|
// TestUpgrade_NeedsUpgrade_Error asserts that an error is returned from
|
|
|
|
// NeedsUpgrade if an invalid db version is found. This is a safety measure to
|
|
|
|
// prevent invalid and unintentional upgrades when downgrading Nomad.
|
|
|
|
func TestUpgrade_NeedsUpgrade_Error(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-12-18 22:49:09 +00:00
|
|
|
|
|
|
|
cases := [][]byte{
|
|
|
|
{'"', '2', '"'}, // wrong type
|
|
|
|
{'1'}, // wrong version (never existed)
|
2022-02-23 20:23:07 +00:00
|
|
|
{'4'}, // wrong version (future)
|
2018-12-18 22:49:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
tc := tc
|
|
|
|
t.Run(fmt.Sprintf("%v", tc), func(t *testing.T) {
|
2022-05-12 15:42:40 +00:00
|
|
|
db := setupBoltDB(t)
|
2018-12-18 22:49:09 +00:00
|
|
|
|
2022-02-23 20:04:44 +00:00
|
|
|
require.NoError(t, db.Update(func(tx *bbolt.Tx) error {
|
2018-12-18 22:49:09 +00:00
|
|
|
bkt, err := tx.CreateBucketIfNotExists(metaBucketName)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
return bkt.Put(metaVersionKey, tc)
|
|
|
|
}))
|
|
|
|
|
2022-02-23 20:23:07 +00:00
|
|
|
_, _, err := NeedsUpgrade(db)
|
2018-12-18 22:49:09 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-07 01:24:43 +00:00
|
|
|
// TestUpgrade_DeleteInvalidAllocs asserts invalid allocations are deleted
|
|
|
|
// during state upgades instead of failing the entire agent.
|
|
|
|
func TestUpgrade_DeleteInvalidAllocs_NoAlloc(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-12-07 01:24:43 +00:00
|
|
|
|
2022-05-12 15:42:40 +00:00
|
|
|
bdb := setupBoltDB(t)
|
2018-12-07 01:24:43 +00:00
|
|
|
|
2018-12-18 23:36:32 +00:00
|
|
|
db := boltdd.New(bdb)
|
2018-12-07 01:24:43 +00:00
|
|
|
|
|
|
|
allocID := []byte(uuid.Generate())
|
|
|
|
|
|
|
|
// Create an allocation bucket with no `alloc` key. This is an observed
|
|
|
|
// pre-0.9 state corruption that should result in the allocation being
|
|
|
|
// dropped while allowing the upgrade to continue.
|
|
|
|
require.NoError(t, db.Update(func(tx *boltdd.Tx) error {
|
|
|
|
parentBkt, err := tx.CreateBucket(allocationsBucketName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = parentBkt.CreateBucket(allocID)
|
|
|
|
return err
|
|
|
|
}))
|
|
|
|
|
|
|
|
// Perform the Upgrade
|
|
|
|
require.NoError(t, db.Update(func(tx *boltdd.Tx) error {
|
|
|
|
return UpgradeAllocs(testlog.HCLogger(t), tx)
|
|
|
|
}))
|
|
|
|
|
|
|
|
// Assert invalid allocation bucket was removed
|
|
|
|
require.NoError(t, db.View(func(tx *boltdd.Tx) error {
|
|
|
|
parentBkt := tx.Bucket(allocationsBucketName)
|
|
|
|
if parentBkt == nil {
|
|
|
|
return fmt.Errorf("parent allocations bucket should not have been removed")
|
|
|
|
}
|
|
|
|
|
|
|
|
if parentBkt.Bucket(allocID) != nil {
|
|
|
|
return fmt.Errorf("invalid alloc bucket should have been deleted")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}))
|
|
|
|
}
|
2018-12-18 23:36:32 +00:00
|
|
|
|
|
|
|
// TestUpgrade_DeleteInvalidTaskEntries asserts invalid entries under a task
|
|
|
|
// bucket are deleted.
|
|
|
|
func TestUpgrade_upgradeTaskBucket_InvalidEntries(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-12-18 23:36:32 +00:00
|
|
|
|
2022-05-12 15:42:40 +00:00
|
|
|
db := setupBoltDB(t)
|
2018-12-18 23:36:32 +00:00
|
|
|
|
|
|
|
taskName := []byte("fake-task")
|
|
|
|
|
|
|
|
// Insert unexpected bucket, unexpected key, and missing simple-all
|
2022-02-23 20:04:44 +00:00
|
|
|
require.NoError(t, db.Update(func(tx *bbolt.Tx) error {
|
2018-12-18 23:36:32 +00:00
|
|
|
bkt, err := tx.CreateBucket(taskName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = bkt.CreateBucket([]byte("unexpectedBucket"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return bkt.Put([]byte("unexepectedKey"), []byte{'x'})
|
|
|
|
}))
|
|
|
|
|
2022-02-23 20:04:44 +00:00
|
|
|
require.NoError(t, db.Update(func(tx *bbolt.Tx) error {
|
2018-12-18 23:36:32 +00:00
|
|
|
bkt := tx.Bucket(taskName)
|
|
|
|
|
|
|
|
// upgradeTaskBucket should fail
|
|
|
|
state, err := upgradeTaskBucket(testlog.HCLogger(t), bkt)
|
|
|
|
require.Nil(t, state)
|
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
// Invalid entries should have been deleted
|
|
|
|
cur := bkt.Cursor()
|
|
|
|
for k, v := cur.First(); k != nil; k, v = cur.Next() {
|
|
|
|
t.Errorf("unexpected entry found: key=%q value=%q", k, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}))
|
|
|
|
}
|