2020-04-30 13:13:00 +00:00
|
|
|
package volumewatcher
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
memdb "github.com/hashicorp/go-memdb"
|
|
|
|
"github.com/hashicorp/nomad/helper/testlog"
|
|
|
|
"github.com/hashicorp/nomad/nomad/mock"
|
|
|
|
"github.com/hashicorp/nomad/nomad/state"
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TestVolumeWatch_EnableDisable tests the watcher registration logic that needs
|
|
|
|
// to happen during leader step-up/step-down
|
|
|
|
func TestVolumeWatch_EnableDisable(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
srv := &MockRPCServer{}
|
|
|
|
srv.state = state.TestStateStore(t)
|
|
|
|
index := uint64(100)
|
|
|
|
|
2020-08-07 19:37:27 +00:00
|
|
|
watcher := NewVolumesWatcher(testlog.HCLogger(t), srv, "")
|
2020-04-30 13:13:00 +00:00
|
|
|
watcher.SetEnabled(true, srv.State())
|
|
|
|
|
|
|
|
plugin := mock.CSIPlugin()
|
2020-08-06 18:31:18 +00:00
|
|
|
node := testNode(plugin, srv.State())
|
2020-04-30 13:13:00 +00:00
|
|
|
alloc := mock.Alloc()
|
|
|
|
alloc.ClientStatus = structs.AllocClientStatusComplete
|
2020-08-06 18:31:18 +00:00
|
|
|
vol := testVolume(plugin, alloc, node.ID)
|
2020-04-30 13:13:00 +00:00
|
|
|
|
|
|
|
index++
|
|
|
|
err := srv.State().CSIVolumeRegister(index, []*structs.CSIVolume{vol})
|
|
|
|
require.NoError(err)
|
|
|
|
|
2020-11-11 18:06:30 +00:00
|
|
|
claim := &structs.CSIVolumeClaim{
|
|
|
|
Mode: structs.CSIVolumeClaimGC,
|
|
|
|
State: structs.CSIVolumeClaimStateNodeDetached,
|
|
|
|
}
|
2020-04-30 13:13:00 +00:00
|
|
|
index++
|
|
|
|
err = srv.State().CSIVolumeClaim(index, vol.Namespace, vol.ID, claim)
|
|
|
|
require.NoError(err)
|
|
|
|
require.Eventually(func() bool {
|
|
|
|
return 1 == len(watcher.watchers)
|
|
|
|
}, time.Second, 10*time.Millisecond)
|
|
|
|
|
|
|
|
watcher.SetEnabled(false, srv.State())
|
|
|
|
require.Equal(0, len(watcher.watchers))
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestVolumeWatch_Checkpoint tests the checkpointing of progress across
|
|
|
|
// leader leader step-up/step-down
|
|
|
|
func TestVolumeWatch_Checkpoint(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
srv := &MockRPCServer{}
|
|
|
|
srv.state = state.TestStateStore(t)
|
|
|
|
index := uint64(100)
|
|
|
|
|
2020-08-07 19:37:27 +00:00
|
|
|
watcher := NewVolumesWatcher(testlog.HCLogger(t), srv, "")
|
2020-04-30 13:13:00 +00:00
|
|
|
|
|
|
|
plugin := mock.CSIPlugin()
|
2020-08-06 18:31:18 +00:00
|
|
|
node := testNode(plugin, srv.State())
|
2020-04-30 13:13:00 +00:00
|
|
|
alloc := mock.Alloc()
|
|
|
|
alloc.ClientStatus = structs.AllocClientStatusComplete
|
2020-08-06 18:31:18 +00:00
|
|
|
vol := testVolume(plugin, alloc, node.ID)
|
2020-04-30 13:13:00 +00:00
|
|
|
|
|
|
|
watcher.SetEnabled(true, srv.State())
|
|
|
|
|
|
|
|
index++
|
|
|
|
err := srv.State().CSIVolumeRegister(index, []*structs.CSIVolume{vol})
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
// we should get or start up a watcher when we get an update for
|
|
|
|
// the volume from the state store
|
|
|
|
require.Eventually(func() bool {
|
|
|
|
return 1 == len(watcher.watchers)
|
|
|
|
}, time.Second, 10*time.Millisecond)
|
|
|
|
|
|
|
|
// step-down (this is sync, but step-up is async)
|
|
|
|
watcher.SetEnabled(false, srv.State())
|
|
|
|
require.Equal(0, len(watcher.watchers))
|
|
|
|
|
|
|
|
// step-up again
|
|
|
|
watcher.SetEnabled(true, srv.State())
|
|
|
|
require.Eventually(func() bool {
|
2020-05-11 13:32:05 +00:00
|
|
|
return 1 == len(watcher.watchers) &&
|
|
|
|
!watcher.watchers[vol.ID+vol.Namespace].isRunning()
|
2020-04-30 13:13:00 +00:00
|
|
|
}, time.Second, 10*time.Millisecond)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestVolumeWatch_StartStop tests the start and stop of the watcher when
|
|
|
|
// it receives notifcations and has completed its work
|
|
|
|
func TestVolumeWatch_StartStop(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
srv := &MockStatefulRPCServer{}
|
|
|
|
srv.state = state.TestStateStore(t)
|
|
|
|
index := uint64(100)
|
2020-08-07 19:37:27 +00:00
|
|
|
watcher := NewVolumesWatcher(testlog.HCLogger(t), srv, "")
|
2020-04-30 13:13:00 +00:00
|
|
|
|
|
|
|
watcher.SetEnabled(true, srv.State())
|
|
|
|
require.Equal(0, len(watcher.watchers))
|
|
|
|
|
|
|
|
plugin := mock.CSIPlugin()
|
2020-08-06 18:31:18 +00:00
|
|
|
node := testNode(plugin, srv.State())
|
2020-05-11 13:32:05 +00:00
|
|
|
alloc1 := mock.Alloc()
|
|
|
|
alloc1.ClientStatus = structs.AllocClientStatusRunning
|
2020-04-30 13:13:00 +00:00
|
|
|
alloc2 := mock.Alloc()
|
2020-05-11 13:32:05 +00:00
|
|
|
alloc2.Job = alloc1.Job
|
2020-04-30 13:13:00 +00:00
|
|
|
alloc2.ClientStatus = structs.AllocClientStatusRunning
|
|
|
|
index++
|
2020-10-19 13:30:15 +00:00
|
|
|
err := srv.State().UpsertJob(structs.MsgTypeTestSetup, index, alloc1.Job)
|
2020-04-30 13:13:00 +00:00
|
|
|
require.NoError(err)
|
|
|
|
index++
|
2020-10-19 13:30:15 +00:00
|
|
|
err = srv.State().UpsertAllocs(structs.MsgTypeTestSetup, index, []*structs.Allocation{alloc1, alloc2})
|
2020-04-30 13:13:00 +00:00
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
// register a volume
|
2020-08-06 18:31:18 +00:00
|
|
|
vol := testVolume(plugin, alloc1, node.ID)
|
2020-04-30 13:13:00 +00:00
|
|
|
index++
|
|
|
|
err = srv.State().CSIVolumeRegister(index, []*structs.CSIVolume{vol})
|
|
|
|
require.NoError(err)
|
|
|
|
|
2020-05-11 13:32:05 +00:00
|
|
|
// assert we get a watcher; there are no claims so it should immediately stop
|
2020-04-30 13:13:00 +00:00
|
|
|
require.Eventually(func() bool {
|
2020-05-11 13:32:05 +00:00
|
|
|
return 1 == len(watcher.watchers) &&
|
|
|
|
!watcher.watchers[vol.ID+vol.Namespace].isRunning()
|
|
|
|
}, time.Second*2, 10*time.Millisecond)
|
2020-04-30 13:13:00 +00:00
|
|
|
|
|
|
|
// claim the volume for both allocs
|
|
|
|
claim := &structs.CSIVolumeClaim{
|
2020-05-11 13:32:05 +00:00
|
|
|
AllocationID: alloc1.ID,
|
2020-04-30 13:13:00 +00:00
|
|
|
NodeID: node.ID,
|
|
|
|
Mode: structs.CSIVolumeClaimRead,
|
|
|
|
}
|
|
|
|
index++
|
|
|
|
err = srv.State().CSIVolumeClaim(index, vol.Namespace, vol.ID, claim)
|
|
|
|
require.NoError(err)
|
|
|
|
claim.AllocationID = alloc2.ID
|
|
|
|
index++
|
|
|
|
err = srv.State().CSIVolumeClaim(index, vol.Namespace, vol.ID, claim)
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
// reap the volume and assert nothing has happened
|
|
|
|
claim = &structs.CSIVolumeClaim{
|
2020-05-11 13:32:05 +00:00
|
|
|
AllocationID: alloc1.ID,
|
2020-04-30 13:13:00 +00:00
|
|
|
NodeID: node.ID,
|
|
|
|
}
|
|
|
|
index++
|
|
|
|
err = srv.State().CSIVolumeClaim(index, vol.Namespace, vol.ID, claim)
|
|
|
|
require.NoError(err)
|
2020-05-11 13:32:05 +00:00
|
|
|
|
|
|
|
ws := memdb.NewWatchSet()
|
|
|
|
vol, _ = srv.State().CSIVolumeByID(ws, vol.Namespace, vol.ID)
|
|
|
|
require.Equal(2, len(vol.ReadAllocs))
|
2020-04-30 13:13:00 +00:00
|
|
|
|
|
|
|
// alloc becomes terminal
|
2020-05-11 13:32:05 +00:00
|
|
|
alloc1.ClientStatus = structs.AllocClientStatusComplete
|
2020-04-30 13:13:00 +00:00
|
|
|
index++
|
2020-10-19 13:30:15 +00:00
|
|
|
err = srv.State().UpsertAllocs(structs.MsgTypeTestSetup, index, []*structs.Allocation{alloc1})
|
2020-04-30 13:13:00 +00:00
|
|
|
require.NoError(err)
|
|
|
|
index++
|
2020-04-30 21:11:31 +00:00
|
|
|
claim.State = structs.CSIVolumeClaimStateReadyToFree
|
2020-04-30 13:13:00 +00:00
|
|
|
err = srv.State().CSIVolumeClaim(index, vol.Namespace, vol.ID, claim)
|
|
|
|
require.NoError(err)
|
|
|
|
|
2020-05-11 13:32:05 +00:00
|
|
|
// 1 claim has been released and watcher stops
|
2020-04-30 13:13:00 +00:00
|
|
|
require.Eventually(func() bool {
|
|
|
|
ws := memdb.NewWatchSet()
|
|
|
|
vol, _ := srv.State().CSIVolumeByID(ws, vol.Namespace, vol.ID)
|
|
|
|
return len(vol.ReadAllocs) == 1 && len(vol.PastClaims) == 0
|
|
|
|
}, time.Second*2, 10*time.Millisecond)
|
|
|
|
|
2020-05-11 13:32:05 +00:00
|
|
|
require.Eventually(func() bool {
|
|
|
|
return !watcher.watchers[vol.ID+vol.Namespace].isRunning()
|
|
|
|
}, time.Second*5, 10*time.Millisecond)
|
2020-04-30 13:13:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TestVolumeWatch_RegisterDeregister tests the start and stop of
|
|
|
|
// watchers around registration
|
|
|
|
func TestVolumeWatch_RegisterDeregister(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
srv := &MockStatefulRPCServer{}
|
|
|
|
srv.state = state.TestStateStore(t)
|
|
|
|
|
|
|
|
index := uint64(100)
|
|
|
|
|
2020-08-07 19:37:27 +00:00
|
|
|
watcher := NewVolumesWatcher(testlog.HCLogger(t), srv, "")
|
2020-04-30 13:13:00 +00:00
|
|
|
|
|
|
|
watcher.SetEnabled(true, srv.State())
|
|
|
|
require.Equal(0, len(watcher.watchers))
|
|
|
|
|
|
|
|
plugin := mock.CSIPlugin()
|
|
|
|
alloc := mock.Alloc()
|
|
|
|
alloc.ClientStatus = structs.AllocClientStatusComplete
|
|
|
|
|
2020-08-06 18:31:18 +00:00
|
|
|
// register a volume without claims
|
|
|
|
vol := mock.CSIVolume(plugin)
|
2020-04-30 13:13:00 +00:00
|
|
|
index++
|
|
|
|
err := srv.State().CSIVolumeRegister(index, []*structs.CSIVolume{vol})
|
|
|
|
require.NoError(err)
|
|
|
|
|
2020-08-06 18:31:18 +00:00
|
|
|
// watcher should be started but immediately stopped
|
2020-04-30 13:13:00 +00:00
|
|
|
require.Eventually(func() bool {
|
|
|
|
return 1 == len(watcher.watchers)
|
|
|
|
}, time.Second, 10*time.Millisecond)
|
|
|
|
|
|
|
|
require.False(watcher.watchers[vol.ID+vol.Namespace].isRunning())
|
|
|
|
}
|