2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2017-10-13 21:36:02 +00:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
2022-01-27 14:30:03 +00:00
|
|
|
"math"
|
2021-06-07 19:01:01 +00:00
|
|
|
"testing"
|
2022-01-27 14:30:03 +00:00
|
|
|
"time"
|
2019-01-15 19:46:12 +00:00
|
|
|
|
2018-03-06 18:12:17 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/testlog"
|
2020-03-26 21:07:18 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/uuid"
|
|
|
|
"github.com/hashicorp/nomad/nomad/mock"
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2017-10-13 21:36:02 +00:00
|
|
|
)
|
|
|
|
|
2021-06-07 19:01:01 +00:00
|
|
|
func TestStateStore(t testing.TB) *StateStore {
|
2017-10-13 21:36:02 +00:00
|
|
|
config := &StateStoreConfig{
|
2018-09-15 23:23:13 +00:00
|
|
|
Logger: testlog.HCLogger(t),
|
|
|
|
Region: "global",
|
2017-10-13 21:36:02 +00:00
|
|
|
}
|
|
|
|
state, err := NewStateStore(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if state == nil {
|
|
|
|
t.Fatalf("missing state")
|
|
|
|
}
|
|
|
|
return state
|
|
|
|
}
|
2020-03-26 21:07:18 +00:00
|
|
|
|
2021-06-07 19:01:01 +00:00
|
|
|
func TestStateStorePublisher(t testing.TB) *StateStoreConfig {
|
2020-09-23 14:52:04 +00:00
|
|
|
return &StateStoreConfig{
|
2020-10-06 20:21:58 +00:00
|
|
|
Logger: testlog.HCLogger(t),
|
|
|
|
Region: "global",
|
|
|
|
EnablePublisher: true,
|
2020-09-23 14:52:04 +00:00
|
|
|
}
|
|
|
|
}
|
2021-06-07 19:01:01 +00:00
|
|
|
func TestStateStoreCfg(t testing.TB, cfg *StateStoreConfig) *StateStore {
|
2020-09-23 14:52:04 +00:00
|
|
|
state, err := NewStateStore(cfg)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if state == nil {
|
|
|
|
t.Fatalf("missing state")
|
|
|
|
}
|
|
|
|
return state
|
|
|
|
}
|
|
|
|
|
2020-08-11 16:24:41 +00:00
|
|
|
// CreateTestCSIPlugin is a helper that generates the node + fingerprint results necessary
|
|
|
|
// to create a CSIPlugin by directly inserting into the state store. The plugin requires a
|
|
|
|
// controller.
|
2020-03-26 21:07:18 +00:00
|
|
|
func CreateTestCSIPlugin(s *StateStore, id string) func() {
|
2020-08-11 16:24:41 +00:00
|
|
|
return createTestCSIPlugin(s, id, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateTestCSIPluginNodeOnly is a helper that generates the node + fingerprint results
|
|
|
|
// necessary to create a CSIPlugin by directly inserting into the state store. The plugin
|
|
|
|
// does not require a controller. In tests that exercise volume registration, this prevents
|
|
|
|
// an error attempting to RPC the node.
|
|
|
|
func CreateTestCSIPluginNodeOnly(s *StateStore, id string) func() {
|
|
|
|
return createTestCSIPlugin(s, id, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func createTestCSIPlugin(s *StateStore, id string, requiresController bool) func() {
|
2020-03-26 21:07:18 +00:00
|
|
|
// Create some nodes
|
|
|
|
ns := make([]*structs.Node, 3)
|
|
|
|
for i := range ns {
|
|
|
|
n := mock.Node()
|
2020-08-11 16:24:41 +00:00
|
|
|
n.Attributes["nomad.version"] = "0.11.0"
|
2020-03-26 21:07:18 +00:00
|
|
|
ns[i] = n
|
|
|
|
}
|
|
|
|
|
|
|
|
// Install healthy plugin fingerprinting results
|
|
|
|
ns[0].CSIControllerPlugins = map[string]*structs.CSIInfo{
|
|
|
|
id: {
|
|
|
|
PluginID: id,
|
|
|
|
AllocID: uuid.Generate(),
|
|
|
|
Healthy: true,
|
|
|
|
HealthDescription: "healthy",
|
2020-08-11 16:24:41 +00:00
|
|
|
RequiresControllerPlugin: requiresController,
|
2020-03-26 21:07:18 +00:00
|
|
|
RequiresTopologies: false,
|
|
|
|
ControllerInfo: &structs.CSIControllerInfo{
|
|
|
|
SupportsReadOnlyAttach: true,
|
|
|
|
SupportsAttachDetach: true,
|
|
|
|
SupportsListVolumes: true,
|
|
|
|
SupportsListVolumesAttachedNodes: false,
|
2021-03-29 20:04:53 +00:00
|
|
|
SupportsCreateDeleteSnapshot: true,
|
|
|
|
SupportsListSnapshots: true,
|
2020-03-26 21:07:18 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Install healthy plugin fingerprinting results
|
|
|
|
for _, n := range ns[1:] {
|
|
|
|
n.CSINodePlugins = map[string]*structs.CSIInfo{
|
|
|
|
id: {
|
|
|
|
PluginID: id,
|
|
|
|
AllocID: uuid.Generate(),
|
|
|
|
Healthy: true,
|
|
|
|
HealthDescription: "healthy",
|
2020-08-11 16:24:41 +00:00
|
|
|
RequiresControllerPlugin: requiresController,
|
2020-03-26 21:07:18 +00:00
|
|
|
RequiresTopologies: false,
|
|
|
|
NodeInfo: &structs.CSINodeInfo{
|
|
|
|
ID: n.ID,
|
|
|
|
MaxVolumes: 64,
|
|
|
|
RequiresNodeStageVolume: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insert them into the state store
|
|
|
|
index := uint64(999)
|
|
|
|
for _, n := range ns {
|
|
|
|
index++
|
2020-10-19 13:30:15 +00:00
|
|
|
s.UpsertNode(structs.MsgTypeTestSetup, index, n)
|
2020-03-26 21:07:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ids := make([]string, len(ns))
|
|
|
|
for i, n := range ns {
|
|
|
|
ids[i] = n.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return cleanup function that deletes the nodes
|
|
|
|
return func() {
|
|
|
|
index++
|
2020-10-19 13:30:15 +00:00
|
|
|
s.DeleteNode(structs.MsgTypeTestSetup, index, ids)
|
2020-03-26 21:07:18 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-27 14:30:03 +00:00
|
|
|
|
|
|
|
func TestBadCSIState(t testing.TB, store *StateStore) error {
|
|
|
|
|
|
|
|
pluginID := "org.democratic-csi.nfs"
|
|
|
|
|
|
|
|
controllerInfo := func(isHealthy bool) map[string]*structs.CSIInfo {
|
|
|
|
desc := "healthy"
|
|
|
|
if !isHealthy {
|
|
|
|
desc = "failed fingerprinting with error"
|
|
|
|
}
|
|
|
|
return map[string]*structs.CSIInfo{
|
|
|
|
pluginID: {
|
|
|
|
PluginID: pluginID,
|
|
|
|
AllocID: uuid.Generate(),
|
|
|
|
Healthy: isHealthy,
|
|
|
|
HealthDescription: desc,
|
|
|
|
RequiresControllerPlugin: true,
|
|
|
|
ControllerInfo: &structs.CSIControllerInfo{
|
|
|
|
SupportsReadOnlyAttach: true,
|
|
|
|
SupportsAttachDetach: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nodeInfo := func(nodeName string, isHealthy bool) map[string]*structs.CSIInfo {
|
|
|
|
desc := "healthy"
|
|
|
|
if !isHealthy {
|
|
|
|
desc = "failed fingerprinting with error"
|
|
|
|
}
|
|
|
|
return map[string]*structs.CSIInfo{
|
|
|
|
pluginID: {
|
|
|
|
PluginID: pluginID,
|
|
|
|
AllocID: uuid.Generate(),
|
|
|
|
Healthy: isHealthy,
|
|
|
|
HealthDescription: desc,
|
|
|
|
RequiresControllerPlugin: true,
|
|
|
|
NodeInfo: &structs.CSINodeInfo{
|
|
|
|
ID: nodeName,
|
|
|
|
MaxVolumes: math.MaxInt64,
|
|
|
|
RequiresNodeStageVolume: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nodes := make([]*structs.Node, 3)
|
|
|
|
for i := range nodes {
|
|
|
|
n := mock.Node()
|
|
|
|
n.Attributes["nomad.version"] = "1.2.4"
|
|
|
|
nodes[i] = n
|
|
|
|
}
|
|
|
|
|
|
|
|
nodes[0].CSIControllerPlugins = controllerInfo(true)
|
|
|
|
nodes[0].CSINodePlugins = nodeInfo("nomad-client0", true)
|
|
|
|
|
|
|
|
drainID := uuid.Generate()
|
|
|
|
|
|
|
|
// drained node
|
|
|
|
nodes[1].CSIControllerPlugins = controllerInfo(false)
|
|
|
|
nodes[1].CSINodePlugins = nodeInfo("nomad-client1", false)
|
|
|
|
|
|
|
|
nodes[1].LastDrain = &structs.DrainMetadata{
|
|
|
|
StartedAt: time.Now().Add(-10 * time.Minute),
|
|
|
|
UpdatedAt: time.Now().Add(-30 * time.Second),
|
|
|
|
Status: structs.DrainStatusComplete,
|
|
|
|
AccessorID: drainID,
|
|
|
|
}
|
|
|
|
nodes[1].SchedulingEligibility = structs.NodeSchedulingIneligible
|
|
|
|
|
|
|
|
// previously drained but now eligible
|
|
|
|
nodes[2].CSIControllerPlugins = controllerInfo(true)
|
|
|
|
nodes[2].CSINodePlugins = nodeInfo("nomad-client2", true)
|
|
|
|
nodes[2].LastDrain = &structs.DrainMetadata{
|
|
|
|
StartedAt: time.Now().Add(-15 * time.Minute),
|
|
|
|
UpdatedAt: time.Now().Add(-5 * time.Minute),
|
|
|
|
Status: structs.DrainStatusComplete,
|
|
|
|
AccessorID: drainID,
|
|
|
|
}
|
|
|
|
nodes[2].SchedulingEligibility = structs.NodeSchedulingEligible
|
|
|
|
|
|
|
|
// Insert nodes into the state store
|
|
|
|
index := uint64(999)
|
|
|
|
for _, n := range nodes {
|
|
|
|
index++
|
|
|
|
err := store.UpsertNode(structs.MsgTypeTestSetup, index, n)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
allocID0 := uuid.Generate() // nil alloc
|
|
|
|
allocID2 := uuid.Generate() // nil alloc
|
|
|
|
|
|
|
|
alloc1 := mock.Alloc()
|
2022-01-27 15:05:41 +00:00
|
|
|
alloc1.ClientStatus = structs.AllocClientStatusRunning
|
|
|
|
alloc1.DesiredStatus = structs.AllocDesiredStatusRun
|
2022-01-27 14:30:03 +00:00
|
|
|
|
|
|
|
// Insert allocs into the state store
|
|
|
|
err := store.UpsertAllocs(structs.MsgTypeTestSetup, index, []*structs.Allocation{alloc1})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
vol := &structs.CSIVolume{
|
|
|
|
ID: "csi-volume-nfs0",
|
|
|
|
Name: "csi-volume-nfs0",
|
|
|
|
ExternalID: "csi-volume-nfs0",
|
|
|
|
Namespace: "default",
|
|
|
|
AccessMode: structs.CSIVolumeAccessModeSingleNodeWriter,
|
|
|
|
AttachmentMode: structs.CSIVolumeAttachmentModeFilesystem,
|
|
|
|
MountOptions: &structs.CSIMountOptions{
|
|
|
|
MountFlags: []string{"noatime"},
|
|
|
|
},
|
|
|
|
Context: map[string]string{
|
|
|
|
"node_attach_driver": "nfs",
|
|
|
|
"provisioner_driver": "nfs-client",
|
|
|
|
"server": "192.168.56.69",
|
|
|
|
},
|
|
|
|
Capacity: 0,
|
|
|
|
RequestedCapacityMin: 107374182,
|
|
|
|
RequestedCapacityMax: 107374182,
|
|
|
|
RequestedCapabilities: []*structs.CSIVolumeCapability{
|
|
|
|
{
|
|
|
|
AttachmentMode: structs.CSIVolumeAttachmentModeFilesystem,
|
|
|
|
AccessMode: structs.CSIVolumeAccessModeMultiNodeMultiWriter,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
AttachmentMode: structs.CSIVolumeAttachmentModeFilesystem,
|
|
|
|
AccessMode: structs.CSIVolumeAccessModeSingleNodeWriter,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
AttachmentMode: structs.CSIVolumeAttachmentModeFilesystem,
|
|
|
|
AccessMode: structs.CSIVolumeAccessModeSingleNodeReader,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
WriteAllocs: map[string]*structs.Allocation{
|
|
|
|
allocID0: nil,
|
|
|
|
alloc1.ID: nil,
|
|
|
|
allocID2: nil,
|
|
|
|
},
|
|
|
|
WriteClaims: map[string]*structs.CSIVolumeClaim{
|
|
|
|
allocID0: {
|
|
|
|
AllocationID: allocID0,
|
|
|
|
NodeID: nodes[0].ID,
|
|
|
|
Mode: structs.CSIVolumeClaimWrite,
|
|
|
|
AccessMode: structs.CSIVolumeAccessModeSingleNodeWriter,
|
|
|
|
AttachmentMode: structs.CSIVolumeAttachmentModeFilesystem,
|
|
|
|
State: structs.CSIVolumeClaimStateTaken,
|
|
|
|
},
|
|
|
|
alloc1.ID: {
|
|
|
|
AllocationID: alloc1.ID,
|
|
|
|
NodeID: nodes[1].ID,
|
|
|
|
Mode: structs.CSIVolumeClaimWrite,
|
|
|
|
AccessMode: structs.CSIVolumeAccessModeMultiNodeMultiWriter,
|
|
|
|
AttachmentMode: structs.CSIVolumeAttachmentModeFilesystem,
|
|
|
|
State: structs.CSIVolumeClaimStateTaken,
|
|
|
|
},
|
|
|
|
allocID2: {
|
|
|
|
AllocationID: allocID2,
|
|
|
|
NodeID: nodes[2].ID,
|
|
|
|
Mode: structs.CSIVolumeClaimWrite,
|
|
|
|
AccessMode: structs.CSIVolumeAccessModeMultiNodeMultiWriter,
|
|
|
|
AttachmentMode: structs.CSIVolumeAttachmentModeFilesystem,
|
|
|
|
State: structs.CSIVolumeClaimStateTaken,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Schedulable: true,
|
|
|
|
PluginID: pluginID,
|
|
|
|
Provider: pluginID,
|
|
|
|
ProviderVersion: "1.4.3",
|
|
|
|
ControllerRequired: true,
|
|
|
|
ControllersHealthy: 2,
|
|
|
|
ControllersExpected: 2,
|
|
|
|
NodesHealthy: 2,
|
|
|
|
NodesExpected: 0,
|
|
|
|
}
|
2022-01-27 15:05:41 +00:00
|
|
|
vol = vol.Copy() // canonicalize
|
2022-01-27 14:30:03 +00:00
|
|
|
|
2022-03-07 16:06:59 +00:00
|
|
|
err = store.UpsertCSIVolume(index, []*structs.CSIVolume{vol})
|
2022-01-27 14:30:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|