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-06-13 21:02:11 +00:00
|
|
|
cstructs "github.com/hashicorp/nomad/client/structs"
|
2017-08-07 22:54:05 +00:00
|
|
|
"github.com/hashicorp/nomad/command/agent/consul"
|
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-06-13 21:02:11 +00:00
|
|
|
net *cstructs.DriverNetwork
|
2017-02-01 00:43:57 +00:00
|
|
|
}
|
|
|
|
|
2017-06-13 21:02:11 +00:00
|
|
|
func newMockConsulOp(op, allocID string, task *structs.Task, exec driver.ScriptExecutor, net *cstructs.DriverNetwork) mockConsulOp {
|
2017-08-07 22:54:05 +00:00
|
|
|
if op != "add" && op != "remove" && op != "update" && op != "alloc_registrations" {
|
2017-04-19 00:18:23 +00:00
|
|
|
panic(fmt.Errorf("invalid consul op: %s", op))
|
|
|
|
}
|
|
|
|
return mockConsulOp{
|
|
|
|
op: op,
|
|
|
|
allocID: allocID,
|
|
|
|
task: task,
|
|
|
|
exec: exec,
|
2017-06-13 21:02:11 +00:00
|
|
|
net: net,
|
2017-04-19 00:18:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
2017-07-04 19:24:27 +00:00
|
|
|
|
2017-08-07 22:54:05 +00:00
|
|
|
// allocRegistrationsFn allows injecting return values for the
|
|
|
|
// AllocRegistrations function.
|
|
|
|
allocRegistrationsFn func(allocID string) (*consul.AllocRegistration, error)
|
2017-02-01 00:43:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-06-16 23:35:16 +00:00
|
|
|
func (m *mockConsulServiceClient) UpdateTask(allocID string, old, new *structs.Task, exec driver.ScriptExecutor, net *cstructs.DriverNetwork) error {
|
2017-02-01 00:43:57 +00:00
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
2017-06-16 23:35:16 +00:00
|
|
|
m.logger.Printf("[TEST] mock_consul: UpdateTask(%q, %v, %v, %T, %x)", allocID, old, new, exec, net.Hash())
|
|
|
|
m.ops = append(m.ops, newMockConsulOp("update", allocID, new, exec, net))
|
2017-04-14 20:58:38 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-06-13 21:02:11 +00:00
|
|
|
func (m *mockConsulServiceClient) RegisterTask(allocID string, task *structs.Task, exec driver.ScriptExecutor, net *cstructs.DriverNetwork) error {
|
2017-04-14 20:58:38 +00:00
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
2017-06-16 23:35:16 +00:00
|
|
|
m.logger.Printf("[TEST] mock_consul: RegisterTask(%q, %q, %T, %x)", allocID, task.Name, exec, net.Hash())
|
2017-06-13 21:02:11 +00:00
|
|
|
m.ops = append(m.ops, newMockConsulOp("add", allocID, task, exec, net))
|
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-06-13 21:02:11 +00:00
|
|
|
m.ops = append(m.ops, newMockConsulOp("remove", allocID, task, nil, nil))
|
2017-02-01 00:43:57 +00:00
|
|
|
}
|
2017-07-04 19:24:27 +00:00
|
|
|
|
2017-08-07 22:54:05 +00:00
|
|
|
func (m *mockConsulServiceClient) AllocRegistrations(allocID string) (*consul.AllocRegistration, error) {
|
2017-07-04 19:24:27 +00:00
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
2017-08-07 22:54:05 +00:00
|
|
|
m.logger.Printf("[TEST] mock_consul: AllocRegistrations(%q)", allocID)
|
|
|
|
m.ops = append(m.ops, newMockConsulOp("alloc_registrations", allocID, nil, nil, nil))
|
2017-07-04 19:24:27 +00:00
|
|
|
|
2017-08-07 22:54:05 +00:00
|
|
|
if m.allocRegistrationsFn != nil {
|
|
|
|
return m.allocRegistrationsFn(allocID)
|
2017-07-04 19:24:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|