2021-10-01 13:59:55 +00:00
|
|
|
//go:build !release
|
2018-12-20 01:22:07 +00:00
|
|
|
// +build !release
|
|
|
|
|
2018-12-04 17:16:39 +00:00
|
|
|
package allocrunner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
|
2021-04-14 14:17:28 +00:00
|
|
|
"github.com/hashicorp/nomad/client/lib/cgutil"
|
|
|
|
|
2018-12-04 17:16:39 +00:00
|
|
|
"github.com/hashicorp/nomad/client/allocwatcher"
|
|
|
|
clientconfig "github.com/hashicorp/nomad/client/config"
|
|
|
|
"github.com/hashicorp/nomad/client/consul"
|
|
|
|
"github.com/hashicorp/nomad/client/devicemanager"
|
2018-11-28 03:42:22 +00:00
|
|
|
"github.com/hashicorp/nomad/client/pluginmanager/drivermanager"
|
2018-12-04 17:16:39 +00:00
|
|
|
"github.com/hashicorp/nomad/client/state"
|
|
|
|
"github.com/hashicorp/nomad/client/vaultclient"
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
// MockStateUpdater implements the AllocStateHandler interface and records
|
|
|
|
// alloc updates.
|
|
|
|
type MockStateUpdater struct {
|
|
|
|
Updates []*structs.Allocation
|
|
|
|
mu sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
// AllocStateUpdated implements the AllocStateHandler interface and records an
|
|
|
|
// alloc update.
|
|
|
|
func (m *MockStateUpdater) AllocStateUpdated(alloc *structs.Allocation) {
|
|
|
|
m.mu.Lock()
|
|
|
|
m.Updates = append(m.Updates, alloc)
|
|
|
|
m.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Last returns a copy of the last alloc (or nil) update. Safe for concurrent
|
|
|
|
// access with updates.
|
|
|
|
func (m *MockStateUpdater) Last() *structs.Allocation {
|
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
n := len(m.Updates)
|
|
|
|
if n == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return m.Updates[n-1].Copy()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset resets the recorded alloc updates.
|
|
|
|
func (m *MockStateUpdater) Reset() {
|
|
|
|
m.mu.Lock()
|
|
|
|
m.Updates = nil
|
|
|
|
m.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAllocRunnerConfig(t *testing.T, alloc *structs.Allocation) (*Config, func()) {
|
|
|
|
clientConf, cleanup := clientconfig.TestClientConfig(t)
|
|
|
|
conf := &Config{
|
|
|
|
// Copy the alloc in case the caller edits and reuses it
|
2019-05-10 15:51:06 +00:00
|
|
|
Alloc: alloc.Copy(),
|
|
|
|
Logger: clientConf.Logger,
|
|
|
|
ClientConfig: clientConf,
|
|
|
|
StateDB: state.NoopDB{},
|
|
|
|
Consul: consul.NewMockConsulServiceClient(t, clientConf.Logger),
|
2019-11-27 21:41:45 +00:00
|
|
|
ConsulSI: consul.NewMockServiceIdentitiesClient(),
|
2019-05-10 15:51:06 +00:00
|
|
|
Vault: vaultclient.NewMockVaultClient(),
|
|
|
|
StateUpdater: &MockStateUpdater{},
|
|
|
|
PrevAllocWatcher: allocwatcher.NoopPrevAlloc{},
|
|
|
|
PrevAllocMigrator: allocwatcher.NoopPrevAlloc{},
|
|
|
|
DeviceManager: devicemanager.NoopMockManager(),
|
|
|
|
DriverManager: drivermanager.TestDriverManager(t),
|
2021-04-14 14:17:28 +00:00
|
|
|
CpusetManager: cgutil.NoopCpusetManager(),
|
2019-05-10 15:51:06 +00:00
|
|
|
ServersContactedCh: make(chan struct{}),
|
2018-12-04 17:16:39 +00:00
|
|
|
}
|
|
|
|
return conf, cleanup
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAllocRunnerFromAlloc(t *testing.T, alloc *structs.Allocation) (*allocRunner, func()) {
|
|
|
|
t.Helper()
|
|
|
|
cfg, cleanup := testAllocRunnerConfig(t, alloc)
|
|
|
|
ar, err := NewAllocRunner(cfg)
|
|
|
|
if err != nil {
|
|
|
|
require.NoError(t, err, "Failed to setup AllocRunner")
|
|
|
|
}
|
|
|
|
|
|
|
|
return ar, cleanup
|
|
|
|
}
|