github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/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  	return state
    25  }
    26  
    27  func TestStateStorePublisher(t testing.T) *StateStoreConfig {
    28  	return &StateStoreConfig{
    29  		Logger:          testlog.HCLogger(t),
    30  		Region:          "global",
    31  		EnablePublisher: true,
    32  	}
    33  }
    34  func TestStateStoreCfg(t testing.T, cfg *StateStoreConfig) *StateStore {
    35  	state, err := NewStateStore(cfg)
    36  	if err != nil {
    37  		t.Fatalf("err: %v", err)
    38  	}
    39  
    40  	if state == nil {
    41  		t.Fatalf("missing state")
    42  	}
    43  	return state
    44  }
    45  
    46  // CreateTestCSIPlugin is a helper that generates the node + fingerprint results necessary
    47  // to create a CSIPlugin by directly inserting into the state store. The plugin requires a
    48  // controller.
    49  func CreateTestCSIPlugin(s *StateStore, id string) func() {
    50  	return createTestCSIPlugin(s, id, true)
    51  }
    52  
    53  // CreateTestCSIPluginNodeOnly is a helper that generates the node + fingerprint results
    54  // necessary to create a CSIPlugin by directly inserting into the state store. The plugin
    55  // does not require a controller. In tests that exercise volume registration, this prevents
    56  // an error attempting to RPC the node.
    57  func CreateTestCSIPluginNodeOnly(s *StateStore, id string) func() {
    58  	return createTestCSIPlugin(s, id, false)
    59  }
    60  
    61  func createTestCSIPlugin(s *StateStore, id string, requiresController bool) func() {
    62  	// Create some nodes
    63  	ns := make([]*structs.Node, 3)
    64  	for i := range ns {
    65  		n := mock.Node()
    66  		n.Attributes["nomad.version"] = "0.11.0"
    67  		ns[i] = n
    68  	}
    69  
    70  	// Install healthy plugin fingerprinting results
    71  	ns[0].CSIControllerPlugins = map[string]*structs.CSIInfo{
    72  		id: {
    73  			PluginID:                 id,
    74  			AllocID:                  uuid.Generate(),
    75  			Healthy:                  true,
    76  			HealthDescription:        "healthy",
    77  			RequiresControllerPlugin: requiresController,
    78  			RequiresTopologies:       false,
    79  			ControllerInfo: &structs.CSIControllerInfo{
    80  				SupportsReadOnlyAttach:           true,
    81  				SupportsAttachDetach:             true,
    82  				SupportsListVolumes:              true,
    83  				SupportsListVolumesAttachedNodes: false,
    84  			},
    85  		},
    86  	}
    87  
    88  	// Install healthy plugin fingerprinting results
    89  	for _, n := range ns[1:] {
    90  		n.CSINodePlugins = map[string]*structs.CSIInfo{
    91  			id: {
    92  				PluginID:                 id,
    93  				AllocID:                  uuid.Generate(),
    94  				Healthy:                  true,
    95  				HealthDescription:        "healthy",
    96  				RequiresControllerPlugin: requiresController,
    97  				RequiresTopologies:       false,
    98  				NodeInfo: &structs.CSINodeInfo{
    99  					ID:                      n.ID,
   100  					MaxVolumes:              64,
   101  					RequiresNodeStageVolume: true,
   102  				},
   103  			},
   104  		}
   105  	}
   106  
   107  	// Insert them into the state store
   108  	index := uint64(999)
   109  	for _, n := range ns {
   110  		index++
   111  		s.UpsertNode(structs.MsgTypeTestSetup, index, n)
   112  	}
   113  
   114  	ids := make([]string, len(ns))
   115  	for i, n := range ns {
   116  		ids[i] = n.ID
   117  	}
   118  
   119  	// Return cleanup function that deletes the nodes
   120  	return func() {
   121  		index++
   122  		s.DeleteNode(structs.MsgTypeTestSetup, index, ids)
   123  	}
   124  }