2018-10-16 22:17:36 +00:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2018-10-29 22:25:22 +00:00
|
|
|
"reflect"
|
2018-10-16 22:17:36 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
trstate "github.com/hashicorp/nomad/client/allocrunner/taskrunner/state"
|
2018-11-01 01:00:30 +00:00
|
|
|
dmstate "github.com/hashicorp/nomad/client/devicemanager/state"
|
2020-03-12 20:24:58 +00:00
|
|
|
"github.com/hashicorp/nomad/client/dynamicplugins"
|
2018-11-28 03:42:22 +00:00
|
|
|
driverstate "github.com/hashicorp/nomad/client/pluginmanager/drivermanager/state"
|
2018-12-07 01:24:43 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/testlog"
|
2018-10-16 22:17:36 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/mock"
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2018-10-29 22:25:22 +00:00
|
|
|
"github.com/kr/pretty"
|
2018-10-16 22:17:36 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2018-12-18 23:36:32 +00:00
|
|
|
func setupBoltStateDB(t *testing.T) (*BoltStateDB, func()) {
|
2018-10-16 22:17:36 +00:00
|
|
|
dir, err := ioutil.TempDir("", "nomadtest")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2018-12-07 01:24:43 +00:00
|
|
|
db, err := NewBoltStateDB(testlog.HCLogger(t), dir)
|
2018-10-16 22:17:36 +00:00
|
|
|
if err != nil {
|
|
|
|
if err := os.RemoveAll(dir); err != nil {
|
|
|
|
t.Logf("error removing boltdb dir: %v", err)
|
|
|
|
}
|
|
|
|
t.Fatalf("error creating boltdb: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup := func() {
|
|
|
|
if err := db.Close(); err != nil {
|
|
|
|
t.Errorf("error closing boltdb: %v", err)
|
|
|
|
}
|
|
|
|
if err := os.RemoveAll(dir); err != nil {
|
|
|
|
t.Logf("error removing boltdb dir: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return db.(*BoltStateDB), cleanup
|
|
|
|
}
|
|
|
|
|
|
|
|
func testDB(t *testing.T, f func(*testing.T, StateDB)) {
|
2018-12-18 23:36:32 +00:00
|
|
|
boltdb, cleanup := setupBoltStateDB(t)
|
2018-10-16 22:17:36 +00:00
|
|
|
defer cleanup()
|
|
|
|
|
2019-03-04 21:37:20 +00:00
|
|
|
memdb := NewMemDB(testlog.HCLogger(t))
|
2018-10-16 22:17:36 +00:00
|
|
|
|
|
|
|
impls := []StateDB{boltdb, memdb}
|
|
|
|
|
|
|
|
for _, db := range impls {
|
|
|
|
db := db
|
|
|
|
t.Run(db.Name(), func(t *testing.T) {
|
|
|
|
f(t, db)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestStateDB asserts the behavior of GetAllAllocations, PutAllocation, and
|
|
|
|
// DeleteAllocationBucket for all operational StateDB implementations.
|
|
|
|
func TestStateDB_Allocations(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
testDB(t, func(t *testing.T, db StateDB) {
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
// Empty database should return empty non-nil results
|
|
|
|
allocs, errs, err := db.GetAllAllocations()
|
|
|
|
require.NoError(err)
|
|
|
|
require.NotNil(allocs)
|
|
|
|
require.Empty(allocs)
|
|
|
|
require.NotNil(errs)
|
|
|
|
require.Empty(errs)
|
|
|
|
|
|
|
|
// Put allocations
|
|
|
|
alloc1 := mock.Alloc()
|
|
|
|
alloc2 := mock.BatchAlloc()
|
2018-10-29 22:25:22 +00:00
|
|
|
|
2018-10-16 22:17:36 +00:00
|
|
|
require.NoError(db.PutAllocation(alloc1))
|
|
|
|
require.NoError(db.PutAllocation(alloc2))
|
|
|
|
|
|
|
|
// Retrieve them
|
|
|
|
allocs, errs, err = db.GetAllAllocations()
|
|
|
|
require.NoError(err)
|
|
|
|
require.NotNil(allocs)
|
|
|
|
require.Len(allocs, 2)
|
2018-10-29 22:25:22 +00:00
|
|
|
for _, a := range allocs {
|
|
|
|
switch a.ID {
|
|
|
|
case alloc1.ID:
|
|
|
|
if !reflect.DeepEqual(a, alloc1) {
|
|
|
|
pretty.Ldiff(t, a, alloc1)
|
|
|
|
t.Fatalf("alloc %q unequal", a.ID)
|
|
|
|
}
|
|
|
|
case alloc2.ID:
|
|
|
|
if !reflect.DeepEqual(a, alloc2) {
|
|
|
|
pretty.Ldiff(t, a, alloc2)
|
|
|
|
t.Fatalf("alloc %q unequal", a.ID)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
t.Fatalf("unexpected alloc id %q", a.ID)
|
|
|
|
}
|
|
|
|
}
|
2018-10-16 22:17:36 +00:00
|
|
|
require.NotNil(errs)
|
|
|
|
require.Empty(errs)
|
|
|
|
|
|
|
|
// Add another
|
|
|
|
alloc3 := mock.SystemAlloc()
|
|
|
|
require.NoError(db.PutAllocation(alloc3))
|
|
|
|
allocs, errs, err = db.GetAllAllocations()
|
|
|
|
require.NoError(err)
|
|
|
|
require.NotNil(allocs)
|
|
|
|
require.Len(allocs, 3)
|
|
|
|
require.Contains(allocs, alloc1)
|
|
|
|
require.Contains(allocs, alloc2)
|
|
|
|
require.Contains(allocs, alloc3)
|
|
|
|
require.NotNil(errs)
|
|
|
|
require.Empty(errs)
|
|
|
|
|
|
|
|
// Deleting a nonexistent alloc is a noop
|
|
|
|
require.NoError(db.DeleteAllocationBucket("asdf"))
|
2018-10-29 22:25:22 +00:00
|
|
|
allocs, _, err = db.GetAllAllocations()
|
2018-10-16 22:17:36 +00:00
|
|
|
require.NoError(err)
|
|
|
|
require.NotNil(allocs)
|
|
|
|
require.Len(allocs, 3)
|
|
|
|
|
|
|
|
// Delete alloc1
|
|
|
|
require.NoError(db.DeleteAllocationBucket(alloc1.ID))
|
|
|
|
allocs, errs, err = db.GetAllAllocations()
|
|
|
|
require.NoError(err)
|
|
|
|
require.NotNil(allocs)
|
|
|
|
require.Len(allocs, 2)
|
|
|
|
require.Contains(allocs, alloc2)
|
|
|
|
require.Contains(allocs, alloc3)
|
|
|
|
require.NotNil(errs)
|
|
|
|
require.Empty(errs)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestStateDB_TaskState asserts the behavior of task state related StateDB
|
|
|
|
// methods.
|
|
|
|
func TestStateDB_TaskState(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
testDB(t, func(t *testing.T, db StateDB) {
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
// Getting nonexistent state should return nils
|
|
|
|
ls, ts, err := db.GetTaskRunnerState("allocid", "taskname")
|
|
|
|
require.NoError(err)
|
|
|
|
require.Nil(ls)
|
|
|
|
require.Nil(ts)
|
|
|
|
|
|
|
|
// Putting TaskState without first putting the allocation should work
|
|
|
|
state := structs.NewTaskState()
|
|
|
|
state.Failed = true // set a non-default value
|
|
|
|
require.NoError(db.PutTaskState("allocid", "taskname", state))
|
|
|
|
|
|
|
|
// Getting should return the available state
|
|
|
|
ls, ts, err = db.GetTaskRunnerState("allocid", "taskname")
|
|
|
|
require.NoError(err)
|
|
|
|
require.Nil(ls)
|
|
|
|
require.Equal(state, ts)
|
|
|
|
|
|
|
|
// Deleting a nonexistent task should not error
|
|
|
|
require.NoError(db.DeleteTaskBucket("adsf", "asdf"))
|
|
|
|
require.NoError(db.DeleteTaskBucket("asllocid", "asdf"))
|
|
|
|
|
|
|
|
// Data should be untouched
|
|
|
|
ls, ts, err = db.GetTaskRunnerState("allocid", "taskname")
|
|
|
|
require.NoError(err)
|
|
|
|
require.Nil(ls)
|
|
|
|
require.Equal(state, ts)
|
|
|
|
|
|
|
|
// Deleting the task should remove the state
|
|
|
|
require.NoError(db.DeleteTaskBucket("allocid", "taskname"))
|
|
|
|
ls, ts, err = db.GetTaskRunnerState("allocid", "taskname")
|
|
|
|
require.NoError(err)
|
|
|
|
require.Nil(ls)
|
|
|
|
require.Nil(ts)
|
|
|
|
|
|
|
|
// Putting LocalState should work just like TaskState
|
|
|
|
origLocalState := trstate.NewLocalState()
|
|
|
|
require.NoError(db.PutTaskRunnerLocalState("allocid", "taskname", origLocalState))
|
|
|
|
ls, ts, err = db.GetTaskRunnerState("allocid", "taskname")
|
|
|
|
require.NoError(err)
|
|
|
|
require.Equal(origLocalState, ls)
|
|
|
|
require.Nil(ts)
|
|
|
|
})
|
|
|
|
}
|
2018-09-28 17:09:01 +00:00
|
|
|
|
|
|
|
// TestStateDB_DeviceManager asserts the behavior of device manager state related StateDB
|
|
|
|
// methods.
|
|
|
|
func TestStateDB_DeviceManager(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
testDB(t, func(t *testing.T, db StateDB) {
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
// Getting nonexistent state should return nils
|
|
|
|
ps, err := db.GetDevicePluginState()
|
|
|
|
require.NoError(err)
|
|
|
|
require.Nil(ps)
|
|
|
|
|
|
|
|
// Putting PluginState should work
|
2018-11-01 01:00:30 +00:00
|
|
|
state := &dmstate.PluginState{}
|
2018-09-28 17:09:01 +00:00
|
|
|
require.NoError(db.PutDevicePluginState(state))
|
|
|
|
|
|
|
|
// Getting should return the available state
|
|
|
|
ps, err = db.GetDevicePluginState()
|
|
|
|
require.NoError(err)
|
|
|
|
require.NotNil(ps)
|
|
|
|
require.Equal(state, ps)
|
|
|
|
})
|
|
|
|
}
|
2018-11-28 03:42:22 +00:00
|
|
|
|
|
|
|
// TestStateDB_DriverManager asserts the behavior of device manager state related StateDB
|
|
|
|
// methods.
|
|
|
|
func TestStateDB_DriverManager(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
testDB(t, func(t *testing.T, db StateDB) {
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
// Getting nonexistent state should return nils
|
|
|
|
ps, err := db.GetDriverPluginState()
|
|
|
|
require.NoError(err)
|
|
|
|
require.Nil(ps)
|
|
|
|
|
|
|
|
// Putting PluginState should work
|
|
|
|
state := &driverstate.PluginState{}
|
|
|
|
require.NoError(db.PutDriverPluginState(state))
|
|
|
|
|
|
|
|
// Getting should return the available state
|
|
|
|
ps, err = db.GetDriverPluginState()
|
|
|
|
require.NoError(err)
|
|
|
|
require.NotNil(ps)
|
|
|
|
require.Equal(state, ps)
|
|
|
|
})
|
|
|
|
}
|
2018-12-07 01:24:43 +00:00
|
|
|
|
2020-03-12 20:24:58 +00:00
|
|
|
// TestStateDB_DynamicRegistry asserts the behavior of dynamic registry state related StateDB
|
|
|
|
// methods.
|
|
|
|
func TestStateDB_DynamicRegistry(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
testDB(t, func(t *testing.T, db StateDB) {
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
// Getting nonexistent state should return nils
|
|
|
|
ps, err := db.GetDynamicPluginRegistryState()
|
|
|
|
require.NoError(err)
|
|
|
|
require.Nil(ps)
|
|
|
|
|
|
|
|
// Putting PluginState should work
|
|
|
|
state := &dynamicplugins.RegistryState{}
|
|
|
|
require.NoError(db.PutDynamicPluginRegistryState(state))
|
|
|
|
|
|
|
|
// Getting should return the available state
|
|
|
|
ps, err = db.GetDynamicPluginRegistryState()
|
|
|
|
require.NoError(err)
|
|
|
|
require.NotNil(ps)
|
|
|
|
require.Equal(state, ps)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-12-07 01:24:43 +00:00
|
|
|
// TestStateDB_Upgrade asserts calling Upgrade on new databases always
|
|
|
|
// succeeds.
|
|
|
|
func TestStateDB_Upgrade(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
testDB(t, func(t *testing.T, db StateDB) {
|
|
|
|
require.NoError(t, db.Upgrade())
|
|
|
|
})
|
|
|
|
}
|