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
|
|
|
"log"
|
|
|
|
"sync"
|
|
|
|
|
2017-08-07 22:54:05 +00:00
|
|
|
"github.com/hashicorp/nomad/command/agent/consul"
|
2018-01-15 22:56:38 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/testlog"
|
|
|
|
"github.com/mitchellh/go-testing-interface"
|
2017-02-01 00:43:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
2018-04-23 23:34:53 +00:00
|
|
|
task string
|
2017-02-01 00:43:57 +00:00
|
|
|
}
|
|
|
|
|
2018-04-23 23:34:53 +00:00
|
|
|
func newMockConsulOp(op, allocID, task string) 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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-01-15 22:56:38 +00:00
|
|
|
func newMockConsulServiceClient(t testing.T) *mockConsulServiceClient {
|
2017-02-01 00:43:57 +00:00
|
|
|
m := mockConsulServiceClient{
|
2017-04-19 00:18:23 +00:00
|
|
|
ops: make([]mockConsulOp, 0, 20),
|
2018-01-15 22:56:38 +00:00
|
|
|
logger: testlog.Logger(t),
|
2017-02-01 00:43:57 +00:00
|
|
|
}
|
|
|
|
return &m
|
|
|
|
}
|
|
|
|
|
2018-04-23 23:34:53 +00:00
|
|
|
func (m *mockConsulServiceClient) UpdateTask(old, new *consul.TaskServices) error {
|
2017-02-01 00:43:57 +00:00
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
2018-04-23 23:34:53 +00:00
|
|
|
m.logger.Printf("[TEST] mock_consul: UpdateTask(alloc: %s, task: %s)", new.AllocID[:6], new.Name)
|
|
|
|
m.ops = append(m.ops, newMockConsulOp("update", new.AllocID, new.Name))
|
2017-04-14 20:58:38 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-04-23 23:34:53 +00:00
|
|
|
func (m *mockConsulServiceClient) RegisterTask(task *consul.TaskServices) error {
|
2017-04-14 20:58:38 +00:00
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
2018-04-23 23:34:53 +00:00
|
|
|
m.logger.Printf("[TEST] mock_consul: RegisterTask(alloc: %s, task: %s)", task.AllocID, task.Name)
|
|
|
|
m.ops = append(m.ops, newMockConsulOp("add", task.AllocID, task.Name))
|
2017-02-01 00:43:57 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-04-23 23:34:53 +00:00
|
|
|
func (m *mockConsulServiceClient) RemoveTask(task *consul.TaskServices) {
|
2017-02-01 00:43:57 +00:00
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
2018-04-23 23:34:53 +00:00
|
|
|
m.logger.Printf("[TEST] mock_consul: RemoveTask(%q, %q)", task.AllocID, task.Name)
|
|
|
|
m.ops = append(m.ops, newMockConsulOp("remove", task.AllocID, task.Name))
|
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)
|
2018-04-23 23:34:53 +00:00
|
|
|
m.ops = append(m.ops, newMockConsulOp("alloc_registrations", allocID, ""))
|
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
|
|
|
|
}
|