2018-12-07 01:24:43 +00:00
|
|
|
package state_test
|
|
|
|
|
|
|
|
import (
|
2022-02-23 20:23:07 +00:00
|
|
|
"bytes"
|
2018-12-07 01:24:43 +00:00
|
|
|
"compress/gzip"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/client/allocrunner"
|
|
|
|
"github.com/hashicorp/nomad/client/allocwatcher"
|
|
|
|
clientconfig "github.com/hashicorp/nomad/client/config"
|
|
|
|
"github.com/hashicorp/nomad/client/consul"
|
|
|
|
"github.com/hashicorp/nomad/client/devicemanager"
|
|
|
|
dmstate "github.com/hashicorp/nomad/client/devicemanager/state"
|
|
|
|
"github.com/hashicorp/nomad/client/pluginmanager/drivermanager"
|
|
|
|
. "github.com/hashicorp/nomad/client/state"
|
|
|
|
"github.com/hashicorp/nomad/client/vaultclient"
|
|
|
|
"github.com/hashicorp/nomad/helper/boltdd"
|
|
|
|
"github.com/hashicorp/nomad/helper/testlog"
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2019-01-15 01:02:44 +00:00
|
|
|
pstructs "github.com/hashicorp/nomad/plugins/shared/structs"
|
2018-12-07 01:24:43 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2022-02-23 20:29:37 +00:00
|
|
|
"go.etcd.io/bbolt"
|
2018-12-07 01:24:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// TestBoltStateDB_Upgrade_Ok asserts upgading an old state db does not error
|
|
|
|
// during upgrade and restore.
|
|
|
|
func TestBoltStateDB_UpgradeOld_Ok(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2022-02-23 20:23:07 +00:00
|
|
|
dbFromTestFile := func(t *testing.T, dir, fn string) *BoltStateDB {
|
2018-12-07 01:24:43 +00:00
|
|
|
|
2022-02-23 20:23:07 +00:00
|
|
|
var src io.ReadCloser
|
|
|
|
src, err := os.Open(fn)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer src.Close()
|
2018-12-07 01:24:43 +00:00
|
|
|
|
2022-02-23 20:23:07 +00:00
|
|
|
// testdata may be gzip'd; decode on copy
|
|
|
|
if strings.HasSuffix(fn, ".gz") {
|
|
|
|
src, err = gzip.NewReader(src)
|
2018-12-07 01:24:43 +00:00
|
|
|
require.NoError(t, err)
|
2022-02-23 20:23:07 +00:00
|
|
|
}
|
2018-12-07 01:24:43 +00:00
|
|
|
|
2022-02-23 20:23:07 +00:00
|
|
|
dst, err := os.Create(filepath.Join(dir, "state.db"))
|
|
|
|
require.NoError(t, err)
|
2018-12-07 01:24:43 +00:00
|
|
|
|
2022-02-23 20:23:07 +00:00
|
|
|
// Copy test files before testing them for safety
|
|
|
|
_, err = io.Copy(dst, src)
|
|
|
|
require.NoError(t, err)
|
2018-12-07 01:24:43 +00:00
|
|
|
|
2022-02-23 20:23:07 +00:00
|
|
|
require.NoError(t, src.Close())
|
2018-12-07 01:24:43 +00:00
|
|
|
|
2022-02-23 20:23:07 +00:00
|
|
|
dbI, err := NewBoltStateDB(testlog.HCLogger(t), dir)
|
|
|
|
require.NoError(t, err)
|
2018-12-07 01:24:43 +00:00
|
|
|
|
2022-02-23 20:23:07 +00:00
|
|
|
db := dbI.(*BoltStateDB)
|
|
|
|
return db
|
|
|
|
}
|
|
|
|
|
|
|
|
pre09files := []string{
|
|
|
|
"testdata/state-0.7.1.db.gz",
|
|
|
|
"testdata/state-0.8.6-empty.db.gz",
|
|
|
|
"testdata/state-0.8.6-no-deploy.db.gz"}
|
|
|
|
|
|
|
|
for _, fn := range pre09files {
|
|
|
|
t.Run(fn, func(t *testing.T) {
|
|
|
|
|
|
|
|
dir, err := ioutil.TempDir("", "nomadtest")
|
2018-12-07 01:24:43 +00:00
|
|
|
require.NoError(t, err)
|
2022-02-23 20:23:07 +00:00
|
|
|
defer os.RemoveAll(dir)
|
2018-12-07 01:24:43 +00:00
|
|
|
|
2022-02-23 20:23:07 +00:00
|
|
|
db := dbFromTestFile(t, dir, fn)
|
|
|
|
defer db.Close()
|
2018-12-07 01:24:43 +00:00
|
|
|
|
|
|
|
// Simply opening old files should *not* alter them
|
|
|
|
require.NoError(t, db.DB().View(func(tx *boltdd.Tx) error {
|
|
|
|
b := tx.Bucket([]byte("meta"))
|
|
|
|
if b != nil {
|
|
|
|
return fmt.Errorf("meta bucket found but should not exist yet!")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}))
|
|
|
|
|
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.True(t, to09)
|
|
|
|
require.True(t, to12)
|
2018-12-07 01:24:43 +00:00
|
|
|
|
2022-02-23 20:23:07 +00:00
|
|
|
// Attempt the upgrade
|
2018-12-07 01:24:43 +00:00
|
|
|
require.NoError(t, db.Upgrade())
|
|
|
|
|
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
|
|
|
|
|
|
|
// Ensure Allocations can be restored and
|
|
|
|
// NewAR/AR.Restore do not error.
|
|
|
|
allocs, errs, err := db.GetAllAllocations()
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Len(t, errs, 0)
|
|
|
|
|
|
|
|
for _, alloc := range allocs {
|
|
|
|
checkUpgradedAlloc(t, dir, db, alloc)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should be nil for all upgrades
|
|
|
|
ps, err := db.GetDevicePluginState()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Nil(t, ps)
|
|
|
|
|
|
|
|
ps = &dmstate.PluginState{
|
2019-01-15 01:02:44 +00:00
|
|
|
ReattachConfigs: map[string]*pstructs.ReattachConfig{
|
2018-12-19 00:49:21 +00:00
|
|
|
"test": {Pid: 1},
|
2018-12-07 01:24:43 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
require.NoError(t, db.PutDevicePluginState(ps))
|
|
|
|
|
2022-02-23 20:23:07 +00:00
|
|
|
registry, err := db.GetDynamicPluginRegistryState()
|
|
|
|
require.Nil(t, registry)
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
2018-12-07 01:24:43 +00:00
|
|
|
require.NoError(t, db.Close())
|
|
|
|
})
|
|
|
|
}
|
2022-02-23 20:23:07 +00:00
|
|
|
|
|
|
|
t.Run("testdata/state-1.2.6.db.gz", func(t *testing.T) {
|
|
|
|
fn := "testdata/state-1.2.6.db.gz"
|
|
|
|
dir, err := ioutil.TempDir("", "nomadtest")
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
|
|
|
db := dbFromTestFile(t, dir, fn)
|
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
// Simply opening old files should *not* alter them
|
2022-02-23 20:29:37 +00:00
|
|
|
db.DB().BoltDB().View(func(tx *bbolt.Tx) error {
|
2022-02-23 20:23:07 +00:00
|
|
|
b := tx.Bucket([]byte("meta"))
|
|
|
|
if b == nil {
|
|
|
|
return fmt.Errorf("meta bucket should exist")
|
|
|
|
}
|
|
|
|
v := b.Get([]byte("version"))
|
|
|
|
if len(v) == 0 {
|
|
|
|
return fmt.Errorf("meta version is missing")
|
|
|
|
}
|
|
|
|
if !bytes.Equal(v, []byte{'2'}) {
|
|
|
|
return fmt.Errorf("meta version should not have changed")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
to09, to12, err := NeedsUpgrade(db.DB().BoltDB())
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.False(t, to09)
|
|
|
|
require.True(t, to12)
|
|
|
|
|
|
|
|
// Attempt the upgrade
|
|
|
|
require.NoError(t, db.Upgrade())
|
|
|
|
|
|
|
|
to09, to12, err = NeedsUpgrade(db.DB().BoltDB())
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.False(t, to09)
|
|
|
|
require.False(t, to12)
|
|
|
|
|
|
|
|
registry, err := db.GetDynamicPluginRegistryState()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, registry)
|
|
|
|
require.Len(t, registry.Plugins["csi-node"], 2)
|
|
|
|
|
|
|
|
require.NoError(t, db.Close())
|
|
|
|
})
|
2018-12-07 01:24:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// checkUpgradedAlloc creates and restores an AllocRunner from an upgraded
|
|
|
|
// database.
|
|
|
|
//
|
|
|
|
// It does not call AR.Run as its intended to be used against a wide test
|
|
|
|
// corpus in testdata that may be expensive to run and require unavailable
|
|
|
|
// dependencies.
|
|
|
|
func checkUpgradedAlloc(t *testing.T, path string, db StateDB, alloc *structs.Allocation) {
|
|
|
|
_, err := db.GetDeploymentStatus(alloc.ID)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
tg := alloc.Job.LookupTaskGroup(alloc.TaskGroup)
|
|
|
|
for _, task := range tg.Tasks {
|
|
|
|
_, _, err := db.GetTaskRunnerState(alloc.ID, task.Name)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
clientConf, cleanup := clientconfig.TestClientConfig(t)
|
|
|
|
|
|
|
|
// Does *not* cleanup overridden StateDir below. That's left alone for
|
|
|
|
// the caller to cleanup.
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
clientConf.StateDir = path
|
|
|
|
|
|
|
|
conf := &allocrunner.Config{
|
|
|
|
Alloc: alloc,
|
|
|
|
Logger: clientConf.Logger,
|
|
|
|
ClientConfig: clientConf,
|
|
|
|
StateDB: db,
|
|
|
|
Consul: consul.NewMockConsulServiceClient(t, clientConf.Logger),
|
|
|
|
Vault: vaultclient.NewMockVaultClient(),
|
|
|
|
StateUpdater: &allocrunner.MockStateUpdater{},
|
|
|
|
PrevAllocWatcher: allocwatcher.NoopPrevAlloc{},
|
|
|
|
PrevAllocMigrator: allocwatcher.NoopPrevAlloc{},
|
|
|
|
DeviceManager: devicemanager.NoopMockManager(),
|
|
|
|
DriverManager: drivermanager.TestDriverManager(t),
|
|
|
|
}
|
|
|
|
ar, err := allocrunner.NewAllocRunner(conf)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// AllocRunner.Restore should not error
|
|
|
|
require.NoError(t, ar.Restore())
|
|
|
|
}
|