github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/client/pluginmanager/csimanager/instance_test.go (about) 1 package csimanager 2 3 import ( 4 "context" 5 "sync" 6 "testing" 7 "time" 8 9 "github.com/hashicorp/nomad/client/dynamicplugins" 10 "github.com/hashicorp/nomad/helper/testlog" 11 "github.com/hashicorp/nomad/nomad/structs" 12 "github.com/hashicorp/nomad/plugins/csi" 13 "github.com/hashicorp/nomad/plugins/csi/fake" 14 "github.com/stretchr/testify/require" 15 ) 16 17 func setupTestNodeInstanceManager(t *testing.T) (*fake.Client, *instanceManager) { 18 tp := &fake.Client{} 19 20 logger := testlog.HCLogger(t) 21 pinfo := &dynamicplugins.PluginInfo{ 22 Name: "test-plugin", 23 } 24 25 return tp, &instanceManager{ 26 logger: logger, 27 info: pinfo, 28 client: tp, 29 fp: &pluginFingerprinter{ 30 logger: logger.Named("fingerprinter"), 31 info: pinfo, 32 client: tp, 33 fingerprintNode: true, 34 hadFirstSuccessfulFingerprintCh: make(chan struct{}), 35 }, 36 } 37 } 38 39 func TestInstanceManager_Shutdown(t *testing.T) { 40 41 var pluginHealth bool 42 var lock sync.Mutex 43 ctx, cancelFn := context.WithCancel(context.Background()) 44 client, im := setupTestNodeInstanceManager(t) 45 im.shutdownCtx = ctx 46 im.shutdownCtxCancelFn = cancelFn 47 im.shutdownCh = make(chan struct{}) 48 im.updater = func(_ string, info *structs.CSIInfo) { 49 lock.Lock() 50 defer lock.Unlock() 51 pluginHealth = info.Healthy 52 } 53 54 // set up a mock successful fingerprint so that we can get 55 // a healthy plugin before shutting down 56 client.NextPluginGetCapabilitiesResponse = &csi.PluginCapabilitySet{} 57 client.NextPluginGetCapabilitiesErr = nil 58 client.NextNodeGetInfoResponse = &csi.NodeGetInfoResponse{NodeID: "foo"} 59 client.NextNodeGetInfoErr = nil 60 client.NextNodeGetCapabilitiesResponse = &csi.NodeCapabilitySet{} 61 client.NextNodeGetCapabilitiesErr = nil 62 client.NextPluginProbeResponse = true 63 64 go im.runLoop() 65 66 require.Eventually(t, func() bool { 67 lock.Lock() 68 defer lock.Unlock() 69 return pluginHealth 70 }, 1*time.Second, 10*time.Millisecond) 71 72 cancelFn() // fires im.shutdown() 73 74 require.Eventually(t, func() bool { 75 lock.Lock() 76 defer lock.Unlock() 77 return !pluginHealth 78 }, 1*time.Second, 10*time.Millisecond) 79 80 }