github.com/iqoqo/nomad@v0.11.3-0.20200911112621-d7021c74d101/nomad/state/testing.go (about) 1 package state 2 3 import ( 4 testing "github.com/mitchellh/go-testing-interface" 5 6 "github.com/hashicorp/nomad/helper/testlog" 7 "github.com/hashicorp/nomad/helper/uuid" 8 "github.com/hashicorp/nomad/nomad/mock" 9 "github.com/hashicorp/nomad/nomad/structs" 10 ) 11 12 func TestStateStore(t testing.T) *StateStore { 13 config := &StateStoreConfig{ 14 Logger: testlog.HCLogger(t), 15 Region: "global", 16 } 17 state, err := NewStateStore(config) 18 if err != nil { 19 t.Fatalf("err: %v", err) 20 } 21 if state == nil { 22 t.Fatalf("missing state") 23 } 24 TestInitState(t, state) 25 return state 26 } 27 28 // CreateTestPlugin is a helper that generates the node + fingerprint results necessary to 29 // create a CSIPlugin by directly inserting into the state store. It's exported for use in 30 // other test packages 31 func CreateTestCSIPlugin(s *StateStore, id string) func() { 32 // Create some nodes 33 ns := make([]*structs.Node, 3) 34 for i := range ns { 35 n := mock.Node() 36 ns[i] = n 37 } 38 39 // Install healthy plugin fingerprinting results 40 ns[0].CSIControllerPlugins = map[string]*structs.CSIInfo{ 41 id: { 42 PluginID: id, 43 AllocID: uuid.Generate(), 44 Healthy: true, 45 HealthDescription: "healthy", 46 RequiresControllerPlugin: true, 47 RequiresTopologies: false, 48 ControllerInfo: &structs.CSIControllerInfo{ 49 SupportsReadOnlyAttach: true, 50 SupportsAttachDetach: true, 51 SupportsListVolumes: true, 52 SupportsListVolumesAttachedNodes: false, 53 }, 54 }, 55 } 56 57 // Install healthy plugin fingerprinting results 58 for _, n := range ns[1:] { 59 n.CSINodePlugins = map[string]*structs.CSIInfo{ 60 id: { 61 PluginID: id, 62 AllocID: uuid.Generate(), 63 Healthy: true, 64 HealthDescription: "healthy", 65 RequiresControllerPlugin: true, 66 RequiresTopologies: false, 67 NodeInfo: &structs.CSINodeInfo{ 68 ID: n.ID, 69 MaxVolumes: 64, 70 RequiresNodeStageVolume: true, 71 }, 72 }, 73 } 74 } 75 76 // Insert them into the state store 77 index := uint64(999) 78 for _, n := range ns { 79 index++ 80 s.UpsertNode(index, n) 81 } 82 83 ids := make([]string, len(ns)) 84 for i, n := range ns { 85 ids[i] = n.ID 86 } 87 88 // Return cleanup function that deletes the nodes 89 return func() { 90 index++ 91 s.DeleteNode(index, ids) 92 } 93 }