mirror of
https://github.com/kemko/nomad.git
synced 2026-01-06 18:35:44 +03:00
test: Refactor mock CSI manager (#18554)
and MockCSIManager to support the call counting that csi_hook_test expects instead of implementing csimanager interfaces in two separate places: * client/allocrunner/csi_hook_test * client/csi_endpoint_test they can both use the same mocks defined in client/pluginmanager/csimanager/ alongside the actual implementations of them. also refactor TestCSINode_DetachVolume to use use it like Node_ExpandVolume so we can also test the happy path there
This commit is contained in:
42
testutil/mock_calls.go
Normal file
42
testutil/mock_calls.go
Normal file
@@ -0,0 +1,42 @@
|
||||
// Copyright (c) HashiCorp, Inc.
|
||||
// SPDX-License-Identifier: BUSL-1.1
|
||||
|
||||
package testutil
|
||||
|
||||
import (
|
||||
"maps"
|
||||
"sync"
|
||||
|
||||
"github.com/mitchellh/go-testing-interface"
|
||||
)
|
||||
|
||||
func NewCallCounter() *CallCounter {
|
||||
return &CallCounter{
|
||||
counts: make(map[string]int),
|
||||
}
|
||||
}
|
||||
|
||||
type CallCounter struct {
|
||||
lock sync.Mutex
|
||||
counts map[string]int
|
||||
}
|
||||
|
||||
func (c *CallCounter) Inc(name string) {
|
||||
c.lock.Lock()
|
||||
defer c.lock.Unlock()
|
||||
c.counts[name]++
|
||||
}
|
||||
|
||||
func (c *CallCounter) Get() map[string]int {
|
||||
c.lock.Lock()
|
||||
defer c.lock.Unlock()
|
||||
return maps.Clone(c.counts)
|
||||
}
|
||||
|
||||
func (c *CallCounter) AssertCalled(t testing.T, name string) {
|
||||
t.Helper()
|
||||
counts := c.Get()
|
||||
if _, ok := counts[name]; !ok {
|
||||
t.Errorf("'%s' not called; all counts: %v", counts)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user