2017-02-01 00:43:57 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2017-04-19 00:18:23 +00:00
|
|
|
"fmt"
|
2017-02-01 00:43:57 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
|
2017-04-14 20:58:38 +00:00
|
|
|
"github.com/hashicorp/nomad/client/driver"
|
2017-02-01 00:43:57 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
)
|
|
|
|
|
|
|
|
// mockConsulOp represents the register/deregister operations.
|
|
|
|
type mockConsulOp struct {
|
2017-04-19 00:18:23 +00:00
|
|
|
op string // add, remove, or update
|
2017-02-01 00:43:57 +00:00
|
|
|
allocID string
|
|
|
|
task *structs.Task
|
2017-04-14 20:58:38 +00:00
|
|
|
exec driver.ScriptExecutor
|
2017-02-01 00:43:57 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 00:18:23 +00:00
|
|
|
func newMockConsulOp(op, allocID string, task *structs.Task, exec driver.ScriptExecutor) mockConsulOp {
|
|
|
|
if op != "add" && op != "remove" && op != "update" {
|
|
|
|
panic(fmt.Errorf("invalid consul op: %s", op))
|
|
|
|
}
|
|
|
|
return mockConsulOp{
|
|
|
|
op: op,
|
|
|
|
allocID: allocID,
|
|
|
|
task: task,
|
|
|
|
exec: exec,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-01 00:43:57 +00:00
|
|
|
// mockConsulServiceClient implements the ConsulServiceAPI interface to record
|
|
|
|
// and log task registration/deregistration.
|
|
|
|
type mockConsulServiceClient struct {
|
2017-04-19 00:18:23 +00:00
|
|
|
ops []mockConsulOp
|
|
|
|
mu sync.Mutex
|
2017-02-01 00:43:57 +00:00
|
|
|
|
|
|
|
logger *log.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMockConsulServiceClient() *mockConsulServiceClient {
|
|
|
|
m := mockConsulServiceClient{
|
2017-04-19 00:18:23 +00:00
|
|
|
ops: make([]mockConsulOp, 0, 20),
|
|
|
|
logger: log.New(ioutil.Discard, "", 0),
|
2017-02-01 00:43:57 +00:00
|
|
|
}
|
|
|
|
if testing.Verbose() {
|
|
|
|
m.logger = log.New(os.Stderr, "", log.LstdFlags)
|
|
|
|
}
|
|
|
|
return &m
|
|
|
|
}
|
|
|
|
|
2017-04-14 20:58:38 +00:00
|
|
|
func (m *mockConsulServiceClient) UpdateTask(allocID string, old, new *structs.Task, exec driver.ScriptExecutor) error {
|
2017-02-01 00:43:57 +00:00
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
2017-04-19 20:05:41 +00:00
|
|
|
m.logger.Printf("[TEST] mock_consul: UpdateTask(%q, %v, %v, %T)", allocID, old, new, exec)
|
2017-04-19 00:18:23 +00:00
|
|
|
m.ops = append(m.ops, newMockConsulOp("update", allocID, old, exec))
|
2017-04-14 20:58:38 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockConsulServiceClient) RegisterTask(allocID string, task *structs.Task, exec driver.ScriptExecutor) error {
|
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
m.logger.Printf("[TEST] mock_consul: RegisterTask(%q, %q, %T)", allocID, task.Name, exec)
|
2017-04-19 00:18:23 +00:00
|
|
|
m.ops = append(m.ops, newMockConsulOp("add", allocID, task, exec))
|
2017-02-01 00:43:57 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockConsulServiceClient) RemoveTask(allocID string, task *structs.Task) {
|
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
2017-04-14 20:58:38 +00:00
|
|
|
m.logger.Printf("[TEST] mock_consul: RemoveTask(%q, %q)", allocID, task.Name)
|
2017-04-19 00:18:23 +00:00
|
|
|
m.ops = append(m.ops, newMockConsulOp("remove", allocID, task, nil))
|
2017-02-01 00:43:57 +00:00
|
|
|
}
|