mirror of
https://github.com/kemko/nomad.git
synced 2026-01-06 10:25:42 +03:00
client: refactor AllocRunner for testing
This commit is contained in:
@@ -27,11 +27,14 @@ type taskStatus struct {
|
||||
Description string
|
||||
}
|
||||
|
||||
// AllocStateUpdater is used to update the status of an allocation
|
||||
type AllocStateUpdater func(alloc *structs.Allocation) error
|
||||
|
||||
// AllocRunner is used to wrap an allocation and provide the execution context.
|
||||
type AllocRunner struct {
|
||||
config *config.Config
|
||||
client *Client
|
||||
logger *log.Logger
|
||||
config *config.Config
|
||||
updater AllocStateUpdater
|
||||
logger *log.Logger
|
||||
|
||||
alloc *structs.Allocation
|
||||
|
||||
@@ -59,11 +62,11 @@ type allocRunnerState struct {
|
||||
}
|
||||
|
||||
// NewAllocRunner is used to create a new allocation context
|
||||
func NewAllocRunner(config *config.Config, client *Client, alloc *structs.Allocation) *AllocRunner {
|
||||
func NewAllocRunner(logger *log.Logger, config *config.Config, updater AllocStateUpdater, alloc *structs.Allocation) *AllocRunner {
|
||||
ar := &AllocRunner{
|
||||
config: config,
|
||||
client: client,
|
||||
logger: client.logger,
|
||||
updater: updater,
|
||||
logger: logger,
|
||||
alloc: alloc,
|
||||
dirtyCh: make(chan struct{}, 1),
|
||||
tasks: make(map[string]*TaskRunner),
|
||||
@@ -197,7 +200,7 @@ func (r *AllocRunner) syncStatus() {
|
||||
}
|
||||
|
||||
// Attempt to update the status
|
||||
if err := r.client.updateAllocStatus(r.alloc); err != nil {
|
||||
if err := r.updater(r.alloc); err != nil {
|
||||
r.logger.Printf("[ERR] client: failed to update alloc '%s' status to %s: %s",
|
||||
r.alloc.ID, r.alloc.ClientStatus, err)
|
||||
retryCh = time.After(allocSyncRetryIntv + randomStagger(allocSyncRetryIntv))
|
||||
|
||||
42
client/alloc_runner_test.go
Normal file
42
client/alloc_runner_test.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/nomad/nomad/mock"
|
||||
"github.com/hashicorp/nomad/nomad/structs"
|
||||
)
|
||||
|
||||
type MockAllocStateUpdater struct {
|
||||
Count int
|
||||
Allocs []*structs.Allocation
|
||||
Err error
|
||||
}
|
||||
|
||||
func (m *MockAllocStateUpdater) Update(alloc *structs.Allocation) error {
|
||||
m.Count += 1
|
||||
m.Allocs = append(m.Allocs, alloc)
|
||||
return m.Err
|
||||
}
|
||||
|
||||
func testAllocRunner() (*MockAllocStateUpdater, *AllocRunner) {
|
||||
logger := testLogger()
|
||||
conf := DefaultConfig()
|
||||
conf.StateDir = "/tmp"
|
||||
upd := &MockAllocStateUpdater{}
|
||||
alloc := mock.Alloc()
|
||||
ar := NewAllocRunner(logger, conf, upd.Update, alloc)
|
||||
return upd, ar
|
||||
}
|
||||
|
||||
func TestAllocRunner_SimpleRun(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestAllocRunner_Update(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestAllocRunner_Destroy(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestAllocRunner_SaveRestoreState(t *testing.T) {
|
||||
}
|
||||
@@ -236,7 +236,7 @@ func (c *Client) restoreState() error {
|
||||
for _, entry := range list {
|
||||
id := entry.Name()
|
||||
alloc := &structs.Allocation{ID: id}
|
||||
ar := NewAllocRunner(c.config, c, alloc)
|
||||
ar := NewAllocRunner(c.logger, c.config, c.updateAllocStatus, alloc)
|
||||
c.allocs[id] = ar
|
||||
if err := ar.RestoreState(); err != nil {
|
||||
c.logger.Printf("[ERR] client: failed to restore state for alloc %s: %v",
|
||||
@@ -576,7 +576,7 @@ func (c *Client) updateAlloc(exist, update *structs.Allocation) error {
|
||||
func (c *Client) addAlloc(alloc *structs.Allocation) error {
|
||||
c.allocLock.Lock()
|
||||
defer c.allocLock.Unlock()
|
||||
ar := NewAllocRunner(c.config, c, alloc)
|
||||
ar := NewAllocRunner(c.logger, c.config, c.updateAllocStatus, alloc)
|
||||
c.allocs[alloc.ID] = ar
|
||||
go ar.Run()
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user