2016-10-03 19:42:18 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-11-02 20:03:54 +00:00
|
|
|
"io"
|
2016-10-03 19:42:18 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
ctestutil "github.com/hashicorp/consul/testutil"
|
|
|
|
"github.com/hashicorp/nomad/client/config"
|
|
|
|
"github.com/hashicorp/nomad/client/driver/env"
|
2017-07-06 23:50:52 +00:00
|
|
|
"github.com/hashicorp/nomad/helper"
|
2016-10-03 19:42:18 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/mock"
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
sconfig "github.com/hashicorp/nomad/nomad/structs/config"
|
|
|
|
"github.com/hashicorp/nomad/testutil"
|
|
|
|
)
|
|
|
|
|
2017-01-14 23:13:27 +00:00
|
|
|
const (
|
|
|
|
// TestTaskName is the name of the injected task. It should appear in the
|
|
|
|
// environment variable $NOMAD_TASK_NAME
|
|
|
|
TestTaskName = "test-task"
|
|
|
|
)
|
|
|
|
|
2016-10-03 19:42:18 +00:00
|
|
|
// MockTaskHooks is a mock of the TaskHooks interface useful for testing
|
|
|
|
type MockTaskHooks struct {
|
|
|
|
Restarts int
|
2016-10-06 21:51:54 +00:00
|
|
|
RestartCh chan struct{}
|
|
|
|
|
|
|
|
Signals []os.Signal
|
|
|
|
SignalCh chan struct{}
|
|
|
|
|
2016-10-10 21:49:37 +00:00
|
|
|
// SignalError is returned when Signal is called on the mock hook
|
|
|
|
SignalError error
|
|
|
|
|
2016-10-06 21:51:54 +00:00
|
|
|
UnblockCh chan struct{}
|
2016-10-03 19:42:18 +00:00
|
|
|
Unblocked bool
|
2016-10-06 21:51:54 +00:00
|
|
|
|
2016-10-06 22:17:34 +00:00
|
|
|
KillReason string
|
2016-10-10 21:49:37 +00:00
|
|
|
KillCh chan struct{}
|
2016-10-03 19:42:18 +00:00
|
|
|
}
|
|
|
|
|
2016-10-06 21:51:54 +00:00
|
|
|
func NewMockTaskHooks() *MockTaskHooks {
|
|
|
|
return &MockTaskHooks{
|
|
|
|
UnblockCh: make(chan struct{}, 1),
|
|
|
|
RestartCh: make(chan struct{}, 1),
|
|
|
|
SignalCh: make(chan struct{}, 1),
|
2016-10-10 21:49:37 +00:00
|
|
|
KillCh: make(chan struct{}, 1),
|
2016-10-06 21:51:54 +00:00
|
|
|
}
|
|
|
|
}
|
2016-10-06 22:17:34 +00:00
|
|
|
func (m *MockTaskHooks) Restart(source, reason string) {
|
2016-10-06 21:51:54 +00:00
|
|
|
m.Restarts++
|
|
|
|
select {
|
|
|
|
case m.RestartCh <- struct{}{}:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-10 21:49:37 +00:00
|
|
|
func (m *MockTaskHooks) Signal(source, reason string, s os.Signal) error {
|
2016-10-06 21:51:54 +00:00
|
|
|
m.Signals = append(m.Signals, s)
|
|
|
|
select {
|
|
|
|
case m.SignalCh <- struct{}{}:
|
|
|
|
default:
|
|
|
|
}
|
2016-10-10 21:49:37 +00:00
|
|
|
|
|
|
|
return m.SignalError
|
|
|
|
}
|
|
|
|
|
2016-10-21 00:27:16 +00:00
|
|
|
func (m *MockTaskHooks) Kill(source, reason string, fail bool) {
|
2016-10-10 21:49:37 +00:00
|
|
|
m.KillReason = reason
|
|
|
|
select {
|
|
|
|
case m.KillCh <- struct{}{}:
|
|
|
|
default:
|
|
|
|
}
|
2016-10-06 21:51:54 +00:00
|
|
|
}
|
|
|
|
|
2016-10-06 22:17:34 +00:00
|
|
|
func (m *MockTaskHooks) UnblockStart(source string) {
|
2016-10-06 21:51:54 +00:00
|
|
|
if !m.Unblocked {
|
|
|
|
close(m.UnblockCh)
|
|
|
|
}
|
|
|
|
|
|
|
|
m.Unblocked = true
|
|
|
|
}
|
2016-10-03 19:42:18 +00:00
|
|
|
|
|
|
|
// testHarness is used to test the TaskTemplateManager by spinning up
|
|
|
|
// Consul/Vault as needed
|
|
|
|
type testHarness struct {
|
|
|
|
manager *TaskTemplateManager
|
|
|
|
mockHooks *MockTaskHooks
|
|
|
|
templates []*structs.Template
|
2017-05-23 20:29:01 +00:00
|
|
|
envBuilder *env.Builder
|
2016-10-03 19:42:18 +00:00
|
|
|
node *structs.Node
|
|
|
|
config *config.Config
|
|
|
|
vaultToken string
|
|
|
|
taskDir string
|
|
|
|
vault *testutil.TestVault
|
|
|
|
consul *ctestutil.TestServer
|
|
|
|
}
|
|
|
|
|
|
|
|
// newTestHarness returns a harness starting a dev consul and vault server,
|
|
|
|
// building the appropriate config and creating a TaskTemplateManager
|
2016-10-28 22:50:35 +00:00
|
|
|
func newTestHarness(t *testing.T, templates []*structs.Template, consul, vault bool) *testHarness {
|
2017-05-23 20:29:01 +00:00
|
|
|
region := "global"
|
2016-10-03 19:42:18 +00:00
|
|
|
harness := &testHarness{
|
|
|
|
mockHooks: NewMockTaskHooks(),
|
|
|
|
templates: templates,
|
|
|
|
node: mock.Node(),
|
2017-05-23 20:29:01 +00:00
|
|
|
config: &config.Config{Region: region},
|
2016-10-03 19:42:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Build the task environment
|
2017-05-23 20:29:01 +00:00
|
|
|
a := mock.Alloc()
|
2017-05-23 20:39:54 +00:00
|
|
|
task := a.Job.TaskGroups[0].Tasks[0]
|
|
|
|
task.Name = TestTaskName
|
|
|
|
harness.envBuilder = env.NewBuilder(harness.node, a, task, region)
|
2016-10-03 19:42:18 +00:00
|
|
|
|
|
|
|
// Make a tempdir
|
2017-05-23 20:29:01 +00:00
|
|
|
d, err := ioutil.TempDir("", "ct_test")
|
2016-10-03 19:42:18 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to make tmpdir: %v", err)
|
|
|
|
}
|
|
|
|
harness.taskDir = d
|
|
|
|
|
|
|
|
if consul {
|
2017-05-06 00:08:51 +00:00
|
|
|
harness.consul, err = ctestutil.NewTestServer()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error starting test Consul server: %v", err)
|
|
|
|
}
|
2016-10-03 19:42:18 +00:00
|
|
|
harness.config.ConsulConfig = &sconfig.ConsulConfig{
|
|
|
|
Addr: harness.consul.HTTPAddr,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if vault {
|
|
|
|
harness.vault = testutil.NewTestVault(t).Start()
|
|
|
|
harness.config.VaultConfig = harness.vault.Config
|
|
|
|
harness.vaultToken = harness.vault.RootToken
|
|
|
|
}
|
|
|
|
|
2016-10-28 22:50:35 +00:00
|
|
|
return harness
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *testHarness) start(t *testing.T) {
|
|
|
|
manager, err := NewTaskTemplateManager(h.mockHooks, h.templates,
|
2017-05-23 20:29:01 +00:00
|
|
|
h.config, h.vaultToken, h.taskDir, h.envBuilder)
|
2016-10-03 19:42:18 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to build task template manager: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-10-28 22:50:35 +00:00
|
|
|
h.manager = manager
|
2016-10-03 19:42:18 +00:00
|
|
|
}
|
|
|
|
|
2016-11-02 20:03:54 +00:00
|
|
|
func (h *testHarness) startWithErr() error {
|
|
|
|
manager, err := NewTaskTemplateManager(h.mockHooks, h.templates,
|
2017-05-23 20:29:01 +00:00
|
|
|
h.config, h.vaultToken, h.taskDir, h.envBuilder)
|
2016-11-02 20:03:54 +00:00
|
|
|
h.manager = manager
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-10-03 19:42:18 +00:00
|
|
|
// stop is used to stop any running Vault or Consul server plus the task manager
|
|
|
|
func (h *testHarness) stop() {
|
|
|
|
if h.vault != nil {
|
|
|
|
h.vault.Stop()
|
|
|
|
}
|
|
|
|
if h.consul != nil {
|
|
|
|
h.consul.Stop()
|
|
|
|
}
|
|
|
|
if h.manager != nil {
|
|
|
|
h.manager.Stop()
|
|
|
|
}
|
|
|
|
if h.taskDir != "" {
|
|
|
|
os.RemoveAll(h.taskDir)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTaskTemplateManager_Invalid(t *testing.T) {
|
|
|
|
hooks := NewMockTaskHooks()
|
|
|
|
var tmpls []*structs.Template
|
2017-05-23 20:29:01 +00:00
|
|
|
region := "global"
|
|
|
|
config := &config.Config{Region: region}
|
2016-10-03 19:42:18 +00:00
|
|
|
taskDir := "foo"
|
|
|
|
vaultToken := ""
|
2017-05-23 20:29:01 +00:00
|
|
|
a := mock.Alloc()
|
|
|
|
envBuilder := env.NewBuilder(mock.Node(), a, a.Job.TaskGroups[0].Tasks[0], config.Region)
|
2016-10-03 19:42:18 +00:00
|
|
|
|
2016-10-28 22:50:35 +00:00
|
|
|
_, err := NewTaskTemplateManager(nil, nil, nil, "", "", nil)
|
2016-10-03 19:42:18 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Expected error")
|
|
|
|
}
|
|
|
|
|
2017-05-23 20:29:01 +00:00
|
|
|
_, err = NewTaskTemplateManager(nil, tmpls, config, vaultToken, taskDir, envBuilder)
|
2016-10-03 19:42:18 +00:00
|
|
|
if err == nil || !strings.Contains(err.Error(), "task hook") {
|
|
|
|
t.Fatalf("Expected invalid task hook error: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-05-23 20:29:01 +00:00
|
|
|
_, err = NewTaskTemplateManager(hooks, tmpls, nil, vaultToken, taskDir, envBuilder)
|
2016-10-03 19:42:18 +00:00
|
|
|
if err == nil || !strings.Contains(err.Error(), "config") {
|
|
|
|
t.Fatalf("Expected invalid config error: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-05-23 20:29:01 +00:00
|
|
|
_, err = NewTaskTemplateManager(hooks, tmpls, config, vaultToken, "", envBuilder)
|
2016-10-03 19:42:18 +00:00
|
|
|
if err == nil || !strings.Contains(err.Error(), "task directory") {
|
|
|
|
t.Fatalf("Expected invalid task dir error: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-10-28 22:50:35 +00:00
|
|
|
_, err = NewTaskTemplateManager(hooks, tmpls, config, vaultToken, taskDir, nil)
|
2016-10-03 19:42:18 +00:00
|
|
|
if err == nil || !strings.Contains(err.Error(), "task environment") {
|
|
|
|
t.Fatalf("Expected invalid task environment error: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-05-23 20:29:01 +00:00
|
|
|
tm, err := NewTaskTemplateManager(hooks, tmpls, config, vaultToken, taskDir, envBuilder)
|
2016-10-03 19:42:18 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected error: %v", err)
|
|
|
|
} else if tm == nil {
|
|
|
|
t.Fatalf("Bad %v", tm)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build a template with a bad signal
|
|
|
|
tmpl := &structs.Template{
|
|
|
|
DestPath: "foo",
|
|
|
|
EmbeddedTmpl: "hello, world",
|
|
|
|
ChangeMode: structs.TemplateChangeModeSignal,
|
|
|
|
ChangeSignal: "foobarbaz",
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpls = append(tmpls, tmpl)
|
2017-05-23 20:29:01 +00:00
|
|
|
tm, err = NewTaskTemplateManager(hooks, tmpls, config, vaultToken, taskDir, envBuilder)
|
2016-10-03 19:42:18 +00:00
|
|
|
if err == nil || !strings.Contains(err.Error(), "Failed to parse signal") {
|
|
|
|
t.Fatalf("Expected signal parsing error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-02 20:03:54 +00:00
|
|
|
func TestTaskTemplateManager_HostPath(t *testing.T) {
|
|
|
|
// Make a template that will render immediately and write it to a tmp file
|
|
|
|
f, err := ioutil.TempFile("", "")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Bad: %v", err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
defer os.Remove(f.Name())
|
|
|
|
|
|
|
|
content := "hello, world!"
|
|
|
|
if _, err := io.WriteString(f, content); err != nil {
|
|
|
|
t.Fatalf("Bad: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
file := "my.tmpl"
|
|
|
|
template := &structs.Template{
|
|
|
|
SourcePath: f.Name(),
|
|
|
|
DestPath: file,
|
|
|
|
ChangeMode: structs.TemplateChangeModeNoop,
|
|
|
|
}
|
|
|
|
|
|
|
|
harness := newTestHarness(t, []*structs.Template{template}, false, false)
|
|
|
|
harness.start(t)
|
|
|
|
defer harness.stop()
|
|
|
|
|
|
|
|
// Wait for the unblock
|
|
|
|
select {
|
|
|
|
case <-harness.mockHooks.UnblockCh:
|
|
|
|
case <-time.After(time.Duration(5*testutil.TestMultiplier()) * time.Second):
|
|
|
|
t.Fatalf("Task unblock should have been called")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the file is there
|
|
|
|
path := filepath.Join(harness.taskDir, file)
|
|
|
|
raw, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to read rendered template from %q: %v", path, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if s := string(raw); s != content {
|
|
|
|
t.Fatalf("Unexpected template data; got %q, want %q", s, content)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Change the config to disallow host sources
|
|
|
|
harness = newTestHarness(t, []*structs.Template{template}, false, false)
|
|
|
|
harness.config.Options = map[string]string{
|
|
|
|
hostSrcOption: "false",
|
|
|
|
}
|
|
|
|
if err := harness.startWithErr(); err == nil || !strings.Contains(err.Error(), "absolute") {
|
|
|
|
t.Fatalf("Expected absolute template path disallowed: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-03 19:42:18 +00:00
|
|
|
func TestTaskTemplateManager_Unblock_Static(t *testing.T) {
|
|
|
|
// Make a template that will render immediately
|
|
|
|
content := "hello, world!"
|
|
|
|
file := "my.tmpl"
|
|
|
|
template := &structs.Template{
|
|
|
|
EmbeddedTmpl: content,
|
|
|
|
DestPath: file,
|
|
|
|
ChangeMode: structs.TemplateChangeModeNoop,
|
|
|
|
}
|
|
|
|
|
2016-10-28 22:50:35 +00:00
|
|
|
harness := newTestHarness(t, []*structs.Template{template}, false, false)
|
|
|
|
harness.start(t)
|
2016-10-03 19:42:18 +00:00
|
|
|
defer harness.stop()
|
|
|
|
|
2016-10-06 21:51:54 +00:00
|
|
|
// Wait for the unblock
|
|
|
|
select {
|
|
|
|
case <-harness.mockHooks.UnblockCh:
|
|
|
|
case <-time.After(time.Duration(5*testutil.TestMultiplier()) * time.Second):
|
2016-10-03 19:42:18 +00:00
|
|
|
t.Fatalf("Task unblock should have been called")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the file is there
|
|
|
|
path := filepath.Join(harness.taskDir, file)
|
|
|
|
raw, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to read rendered template from %q: %v", path, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if s := string(raw); s != content {
|
|
|
|
t.Fatalf("Unexpected template data; got %q, want %q", s, content)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-01 04:00:33 +00:00
|
|
|
func TestTaskTemplateManager_Permissions(t *testing.T) {
|
|
|
|
// Make a template that will render immediately
|
|
|
|
content := "hello, world!"
|
|
|
|
file := "my.tmpl"
|
|
|
|
template := &structs.Template{
|
|
|
|
EmbeddedTmpl: content,
|
|
|
|
DestPath: file,
|
|
|
|
ChangeMode: structs.TemplateChangeModeNoop,
|
|
|
|
Perms: "777",
|
|
|
|
}
|
|
|
|
|
|
|
|
harness := newTestHarness(t, []*structs.Template{template}, false, false)
|
|
|
|
harness.start(t)
|
|
|
|
defer harness.stop()
|
|
|
|
|
|
|
|
// Wait for the unblock
|
|
|
|
select {
|
|
|
|
case <-harness.mockHooks.UnblockCh:
|
|
|
|
case <-time.After(time.Duration(5*testutil.TestMultiplier()) * time.Second):
|
|
|
|
t.Fatalf("Task unblock should have been called")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the file is there
|
|
|
|
path := filepath.Join(harness.taskDir, file)
|
|
|
|
fi, err := os.Stat(path)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to stat file: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if m := fi.Mode(); m != os.ModePerm {
|
|
|
|
t.Fatalf("Got mode %v; want %v", m, os.ModePerm)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-14 23:13:27 +00:00
|
|
|
func TestTaskTemplateManager_Unblock_Static_NomadEnv(t *testing.T) {
|
|
|
|
// Make a template that will render immediately
|
|
|
|
content := `Hello Nomad Task: {{env "NOMAD_TASK_NAME"}}`
|
|
|
|
expected := fmt.Sprintf("Hello Nomad Task: %s", TestTaskName)
|
|
|
|
file := "my.tmpl"
|
|
|
|
template := &structs.Template{
|
|
|
|
EmbeddedTmpl: content,
|
|
|
|
DestPath: file,
|
|
|
|
ChangeMode: structs.TemplateChangeModeNoop,
|
|
|
|
}
|
|
|
|
|
|
|
|
harness := newTestHarness(t, []*structs.Template{template}, false, false)
|
|
|
|
harness.start(t)
|
|
|
|
defer harness.stop()
|
|
|
|
|
|
|
|
// Wait for the unblock
|
|
|
|
select {
|
|
|
|
case <-harness.mockHooks.UnblockCh:
|
|
|
|
case <-time.After(time.Duration(5*testutil.TestMultiplier()) * time.Second):
|
|
|
|
t.Fatalf("Task unblock should have been called")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the file is there
|
|
|
|
path := filepath.Join(harness.taskDir, file)
|
|
|
|
raw, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to read rendered template from %q: %v", path, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if s := string(raw); s != expected {
|
|
|
|
t.Fatalf("Unexpected template data; got %q, want %q", s, expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-28 22:50:35 +00:00
|
|
|
func TestTaskTemplateManager_Unblock_Static_AlreadyRendered(t *testing.T) {
|
|
|
|
// Make a template that will render immediately
|
|
|
|
content := "hello, world!"
|
|
|
|
file := "my.tmpl"
|
|
|
|
template := &structs.Template{
|
|
|
|
EmbeddedTmpl: content,
|
|
|
|
DestPath: file,
|
|
|
|
ChangeMode: structs.TemplateChangeModeNoop,
|
|
|
|
}
|
|
|
|
|
|
|
|
harness := newTestHarness(t, []*structs.Template{template}, false, false)
|
|
|
|
|
|
|
|
// Write the contents
|
|
|
|
path := filepath.Join(harness.taskDir, file)
|
|
|
|
if err := ioutil.WriteFile(path, []byte(content), 0777); err != nil {
|
|
|
|
t.Fatalf("Failed to write data: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
harness.start(t)
|
|
|
|
defer harness.stop()
|
|
|
|
|
|
|
|
// Wait for the unblock
|
|
|
|
select {
|
|
|
|
case <-harness.mockHooks.UnblockCh:
|
|
|
|
case <-time.After(time.Duration(5*testutil.TestMultiplier()) * time.Second):
|
|
|
|
t.Fatalf("Task unblock should have been called")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the file is there
|
|
|
|
path = filepath.Join(harness.taskDir, file)
|
|
|
|
raw, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to read rendered template from %q: %v", path, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if s := string(raw); s != content {
|
|
|
|
t.Fatalf("Unexpected template data; got %q, want %q", s, content)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-03 19:42:18 +00:00
|
|
|
func TestTaskTemplateManager_Unblock_Consul(t *testing.T) {
|
|
|
|
// Make a template that will render based on a key in Consul
|
|
|
|
key := "foo"
|
|
|
|
content := "barbaz"
|
|
|
|
embedded := fmt.Sprintf(`{{key "%s"}}`, key)
|
|
|
|
file := "my.tmpl"
|
|
|
|
template := &structs.Template{
|
|
|
|
EmbeddedTmpl: embedded,
|
|
|
|
DestPath: file,
|
|
|
|
ChangeMode: structs.TemplateChangeModeNoop,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Drop the retry rate
|
|
|
|
testRetryRate = 10 * time.Millisecond
|
|
|
|
|
2016-10-28 22:50:35 +00:00
|
|
|
harness := newTestHarness(t, []*structs.Template{template}, true, false)
|
|
|
|
harness.start(t)
|
2016-10-03 19:42:18 +00:00
|
|
|
defer harness.stop()
|
|
|
|
|
2016-10-06 21:51:54 +00:00
|
|
|
// Ensure no unblock
|
|
|
|
select {
|
|
|
|
case <-harness.mockHooks.UnblockCh:
|
|
|
|
t.Fatalf("Task unblock should have not have been called")
|
|
|
|
case <-time.After(time.Duration(1*testutil.TestMultiplier()) * time.Second):
|
2016-10-03 19:42:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write the key to Consul
|
2017-05-06 00:08:51 +00:00
|
|
|
harness.consul.SetKV(t, key, []byte(content))
|
2016-10-03 19:42:18 +00:00
|
|
|
|
2016-10-06 21:51:54 +00:00
|
|
|
// Wait for the unblock
|
|
|
|
select {
|
|
|
|
case <-harness.mockHooks.UnblockCh:
|
|
|
|
case <-time.After(time.Duration(5*testutil.TestMultiplier()) * time.Second):
|
2016-10-03 19:42:18 +00:00
|
|
|
t.Fatalf("Task unblock should have been called")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the file is there
|
|
|
|
path := filepath.Join(harness.taskDir, file)
|
|
|
|
raw, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to read rendered template from %q: %v", path, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if s := string(raw); s != content {
|
|
|
|
t.Fatalf("Unexpected template data; got %q, want %q", s, content)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTaskTemplateManager_Unblock_Vault(t *testing.T) {
|
|
|
|
// Make a template that will render based on a key in Vault
|
|
|
|
vaultPath := "secret/password"
|
|
|
|
key := "password"
|
|
|
|
content := "barbaz"
|
|
|
|
embedded := fmt.Sprintf(`{{with secret "%s"}}{{.Data.%s}}{{end}}`, vaultPath, key)
|
|
|
|
file := "my.tmpl"
|
|
|
|
template := &structs.Template{
|
|
|
|
EmbeddedTmpl: embedded,
|
|
|
|
DestPath: file,
|
|
|
|
ChangeMode: structs.TemplateChangeModeNoop,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Drop the retry rate
|
|
|
|
testRetryRate = 10 * time.Millisecond
|
|
|
|
|
2016-10-28 22:50:35 +00:00
|
|
|
harness := newTestHarness(t, []*structs.Template{template}, false, true)
|
|
|
|
harness.start(t)
|
2016-10-03 19:42:18 +00:00
|
|
|
defer harness.stop()
|
|
|
|
|
2016-10-06 21:51:54 +00:00
|
|
|
// Ensure no unblock
|
|
|
|
select {
|
|
|
|
case <-harness.mockHooks.UnblockCh:
|
2016-10-03 19:42:18 +00:00
|
|
|
t.Fatalf("Task unblock should not have been called")
|
2016-10-06 21:51:54 +00:00
|
|
|
case <-time.After(time.Duration(1*testutil.TestMultiplier()) * time.Second):
|
2016-10-03 19:42:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write the secret to Vault
|
|
|
|
logical := harness.vault.Client.Logical()
|
|
|
|
logical.Write(vaultPath, map[string]interface{}{key: content})
|
|
|
|
|
2016-10-06 21:51:54 +00:00
|
|
|
// Wait for the unblock
|
|
|
|
select {
|
|
|
|
case <-harness.mockHooks.UnblockCh:
|
|
|
|
case <-time.After(time.Duration(5*testutil.TestMultiplier()) * time.Second):
|
2016-10-03 19:42:18 +00:00
|
|
|
t.Fatalf("Task unblock should have been called")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the file is there
|
|
|
|
path := filepath.Join(harness.taskDir, file)
|
|
|
|
raw, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to read rendered template from %q: %v", path, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if s := string(raw); s != content {
|
|
|
|
t.Fatalf("Unexpected template data; got %q, want %q", s, content)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTaskTemplateManager_Unblock_Multi_Template(t *testing.T) {
|
|
|
|
// Make a template that will render immediately
|
|
|
|
staticContent := "hello, world!"
|
|
|
|
staticFile := "my.tmpl"
|
|
|
|
template := &structs.Template{
|
|
|
|
EmbeddedTmpl: staticContent,
|
|
|
|
DestPath: staticFile,
|
|
|
|
ChangeMode: structs.TemplateChangeModeNoop,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make a template that will render based on a key in Consul
|
|
|
|
consulKey := "foo"
|
|
|
|
consulContent := "barbaz"
|
|
|
|
consulEmbedded := fmt.Sprintf(`{{key "%s"}}`, consulKey)
|
|
|
|
consulFile := "consul.tmpl"
|
|
|
|
template2 := &structs.Template{
|
|
|
|
EmbeddedTmpl: consulEmbedded,
|
|
|
|
DestPath: consulFile,
|
|
|
|
ChangeMode: structs.TemplateChangeModeNoop,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Drop the retry rate
|
|
|
|
testRetryRate = 10 * time.Millisecond
|
|
|
|
|
2016-10-28 22:50:35 +00:00
|
|
|
harness := newTestHarness(t, []*structs.Template{template, template2}, true, false)
|
|
|
|
harness.start(t)
|
2016-10-03 19:42:18 +00:00
|
|
|
defer harness.stop()
|
|
|
|
|
2016-10-06 21:51:54 +00:00
|
|
|
// Ensure no unblock
|
|
|
|
select {
|
|
|
|
case <-harness.mockHooks.UnblockCh:
|
|
|
|
t.Fatalf("Task unblock should have not have been called")
|
|
|
|
case <-time.After(time.Duration(1*testutil.TestMultiplier()) * time.Second):
|
2016-10-03 19:42:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the static file has been rendered
|
|
|
|
path := filepath.Join(harness.taskDir, staticFile)
|
|
|
|
raw, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to read rendered template from %q: %v", path, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if s := string(raw); s != staticContent {
|
|
|
|
t.Fatalf("Unexpected template data; got %q, want %q", s, staticContent)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write the key to Consul
|
2017-05-06 00:08:51 +00:00
|
|
|
harness.consul.SetKV(t, consulKey, []byte(consulContent))
|
2016-10-03 19:42:18 +00:00
|
|
|
|
2016-10-06 21:51:54 +00:00
|
|
|
// Wait for the unblock
|
|
|
|
select {
|
|
|
|
case <-harness.mockHooks.UnblockCh:
|
|
|
|
case <-time.After(time.Duration(5*testutil.TestMultiplier()) * time.Second):
|
2016-10-03 19:42:18 +00:00
|
|
|
t.Fatalf("Task unblock should have been called")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the consul file is there
|
|
|
|
path = filepath.Join(harness.taskDir, consulFile)
|
|
|
|
raw, err = ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to read rendered template from %q: %v", path, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if s := string(raw); s != consulContent {
|
|
|
|
t.Fatalf("Unexpected template data; got %q, want %q", s, consulContent)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTaskTemplateManager_Rerender_Noop(t *testing.T) {
|
|
|
|
// Make a template that will render based on a key in Consul
|
|
|
|
key := "foo"
|
|
|
|
content1 := "bar"
|
|
|
|
content2 := "baz"
|
|
|
|
embedded := fmt.Sprintf(`{{key "%s"}}`, key)
|
|
|
|
file := "my.tmpl"
|
|
|
|
template := &structs.Template{
|
|
|
|
EmbeddedTmpl: embedded,
|
|
|
|
DestPath: file,
|
|
|
|
ChangeMode: structs.TemplateChangeModeNoop,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Drop the retry rate
|
|
|
|
testRetryRate = 10 * time.Millisecond
|
|
|
|
|
2016-10-28 22:50:35 +00:00
|
|
|
harness := newTestHarness(t, []*structs.Template{template}, true, false)
|
|
|
|
harness.start(t)
|
2016-10-03 19:42:18 +00:00
|
|
|
defer harness.stop()
|
|
|
|
|
2016-10-06 21:51:54 +00:00
|
|
|
// Ensure no unblock
|
|
|
|
select {
|
|
|
|
case <-harness.mockHooks.UnblockCh:
|
|
|
|
t.Fatalf("Task unblock should have not have been called")
|
|
|
|
case <-time.After(time.Duration(1*testutil.TestMultiplier()) * time.Second):
|
2016-10-03 19:42:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write the key to Consul
|
2017-05-06 00:08:51 +00:00
|
|
|
harness.consul.SetKV(t, key, []byte(content1))
|
2016-10-03 19:42:18 +00:00
|
|
|
|
2016-10-06 21:51:54 +00:00
|
|
|
// Wait for the unblock
|
|
|
|
select {
|
|
|
|
case <-harness.mockHooks.UnblockCh:
|
|
|
|
case <-time.After(time.Duration(5*testutil.TestMultiplier()) * time.Second):
|
2016-10-03 19:42:18 +00:00
|
|
|
t.Fatalf("Task unblock should have been called")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the file is there
|
|
|
|
path := filepath.Join(harness.taskDir, file)
|
|
|
|
raw, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to read rendered template from %q: %v", path, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if s := string(raw); s != content1 {
|
|
|
|
t.Fatalf("Unexpected template data; got %q, want %q", s, content1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the key in Consul
|
2017-05-06 00:08:51 +00:00
|
|
|
harness.consul.SetKV(t, key, []byte(content2))
|
2016-10-03 19:42:18 +00:00
|
|
|
|
2016-10-06 21:51:54 +00:00
|
|
|
select {
|
|
|
|
case <-harness.mockHooks.RestartCh:
|
|
|
|
t.Fatalf("Noop ignored: %+v", harness.mockHooks)
|
|
|
|
case <-harness.mockHooks.SignalCh:
|
2016-10-03 19:42:18 +00:00
|
|
|
t.Fatalf("Noop ignored: %+v", harness.mockHooks)
|
2016-10-06 21:51:54 +00:00
|
|
|
case <-time.After(time.Duration(1*testutil.TestMultiplier()) * time.Second):
|
2016-10-03 19:42:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check the file has been updated
|
|
|
|
path = filepath.Join(harness.taskDir, file)
|
|
|
|
raw, err = ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to read rendered template from %q: %v", path, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if s := string(raw); s != content2 {
|
|
|
|
t.Fatalf("Unexpected template data; got %q, want %q", s, content2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTaskTemplateManager_Rerender_Signal(t *testing.T) {
|
|
|
|
// Make a template that renders based on a key in Consul and sends SIGALRM
|
|
|
|
key1 := "foo"
|
|
|
|
content1_1 := "bar"
|
|
|
|
content1_2 := "baz"
|
|
|
|
embedded1 := fmt.Sprintf(`{{key "%s"}}`, key1)
|
|
|
|
file1 := "my.tmpl"
|
|
|
|
template := &structs.Template{
|
|
|
|
EmbeddedTmpl: embedded1,
|
|
|
|
DestPath: file1,
|
|
|
|
ChangeMode: structs.TemplateChangeModeSignal,
|
|
|
|
ChangeSignal: "SIGALRM",
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make a template that renders based on a key in Consul and sends SIGBUS
|
|
|
|
key2 := "bam"
|
|
|
|
content2_1 := "cat"
|
|
|
|
content2_2 := "dog"
|
|
|
|
embedded2 := fmt.Sprintf(`{{key "%s"}}`, key2)
|
|
|
|
file2 := "my-second.tmpl"
|
|
|
|
template2 := &structs.Template{
|
|
|
|
EmbeddedTmpl: embedded2,
|
|
|
|
DestPath: file2,
|
|
|
|
ChangeMode: structs.TemplateChangeModeSignal,
|
|
|
|
ChangeSignal: "SIGBUS",
|
|
|
|
}
|
|
|
|
|
|
|
|
// Drop the retry rate
|
|
|
|
testRetryRate = 10 * time.Millisecond
|
|
|
|
|
2016-10-28 22:50:35 +00:00
|
|
|
harness := newTestHarness(t, []*structs.Template{template, template2}, true, false)
|
|
|
|
harness.start(t)
|
2016-10-03 19:42:18 +00:00
|
|
|
defer harness.stop()
|
|
|
|
|
2016-10-06 21:51:54 +00:00
|
|
|
// Ensure no unblock
|
|
|
|
select {
|
|
|
|
case <-harness.mockHooks.UnblockCh:
|
|
|
|
t.Fatalf("Task unblock should have not have been called")
|
|
|
|
case <-time.After(time.Duration(1*testutil.TestMultiplier()) * time.Second):
|
2016-10-03 19:42:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write the key to Consul
|
2017-05-06 00:08:51 +00:00
|
|
|
harness.consul.SetKV(t, key1, []byte(content1_1))
|
|
|
|
harness.consul.SetKV(t, key2, []byte(content2_1))
|
2016-10-03 19:42:18 +00:00
|
|
|
|
2016-10-06 21:51:54 +00:00
|
|
|
// Wait for the unblock
|
|
|
|
select {
|
|
|
|
case <-harness.mockHooks.UnblockCh:
|
|
|
|
case <-time.After(time.Duration(5*testutil.TestMultiplier()) * time.Second):
|
2016-10-03 19:42:18 +00:00
|
|
|
t.Fatalf("Task unblock should have been called")
|
|
|
|
}
|
|
|
|
|
2016-10-28 22:50:35 +00:00
|
|
|
if len(harness.mockHooks.Signals) != 0 {
|
|
|
|
t.Fatalf("Should not have received any signals: %+v", harness.mockHooks)
|
|
|
|
}
|
|
|
|
|
2016-10-03 19:42:18 +00:00
|
|
|
// Update the keys in Consul
|
2017-05-06 00:08:51 +00:00
|
|
|
harness.consul.SetKV(t, key1, []byte(content1_2))
|
|
|
|
harness.consul.SetKV(t, key2, []byte(content2_2))
|
2016-10-03 19:42:18 +00:00
|
|
|
|
2016-10-06 21:51:54 +00:00
|
|
|
// Wait for signals
|
|
|
|
timeout := time.After(time.Duration(1*testutil.TestMultiplier()) * time.Second)
|
|
|
|
OUTER:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-harness.mockHooks.RestartCh:
|
|
|
|
t.Fatalf("Restart with signal policy: %+v", harness.mockHooks)
|
|
|
|
case <-harness.mockHooks.SignalCh:
|
|
|
|
if len(harness.mockHooks.Signals) != 2 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
break OUTER
|
|
|
|
case <-timeout:
|
|
|
|
t.Fatalf("Should have received two signals: %+v", harness.mockHooks)
|
|
|
|
}
|
2016-10-03 19:42:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check the files have been updated
|
|
|
|
path := filepath.Join(harness.taskDir, file1)
|
|
|
|
raw, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to read rendered template from %q: %v", path, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if s := string(raw); s != content1_2 {
|
|
|
|
t.Fatalf("Unexpected template data; got %q, want %q", s, content1_2)
|
|
|
|
}
|
|
|
|
|
|
|
|
path = filepath.Join(harness.taskDir, file2)
|
|
|
|
raw, err = ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to read rendered template from %q: %v", path, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if s := string(raw); s != content2_2 {
|
|
|
|
t.Fatalf("Unexpected template data; got %q, want %q", s, content2_2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTaskTemplateManager_Rerender_Restart(t *testing.T) {
|
|
|
|
// Make a template that renders based on a key in Consul and sends restart
|
|
|
|
key1 := "bam"
|
|
|
|
content1_1 := "cat"
|
|
|
|
content1_2 := "dog"
|
|
|
|
embedded1 := fmt.Sprintf(`{{key "%s"}}`, key1)
|
|
|
|
file1 := "my.tmpl"
|
|
|
|
template := &structs.Template{
|
|
|
|
EmbeddedTmpl: embedded1,
|
|
|
|
DestPath: file1,
|
|
|
|
ChangeMode: structs.TemplateChangeModeRestart,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Drop the retry rate
|
|
|
|
testRetryRate = 10 * time.Millisecond
|
|
|
|
|
2016-10-28 22:50:35 +00:00
|
|
|
harness := newTestHarness(t, []*structs.Template{template}, true, false)
|
|
|
|
harness.start(t)
|
2016-10-03 19:42:18 +00:00
|
|
|
defer harness.stop()
|
|
|
|
|
2016-10-06 21:51:54 +00:00
|
|
|
// Ensure no unblock
|
|
|
|
select {
|
|
|
|
case <-harness.mockHooks.UnblockCh:
|
|
|
|
t.Fatalf("Task unblock should have not have been called")
|
|
|
|
case <-time.After(time.Duration(1*testutil.TestMultiplier()) * time.Second):
|
2016-10-03 19:42:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write the key to Consul
|
2017-05-06 00:08:51 +00:00
|
|
|
harness.consul.SetKV(t, key1, []byte(content1_1))
|
2016-10-03 19:42:18 +00:00
|
|
|
|
2016-10-06 21:51:54 +00:00
|
|
|
// Wait for the unblock
|
|
|
|
select {
|
|
|
|
case <-harness.mockHooks.UnblockCh:
|
|
|
|
case <-time.After(time.Duration(5*testutil.TestMultiplier()) * time.Second):
|
2016-10-03 19:42:18 +00:00
|
|
|
t.Fatalf("Task unblock should have been called")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the keys in Consul
|
2017-05-06 00:08:51 +00:00
|
|
|
harness.consul.SetKV(t, key1, []byte(content1_2))
|
2016-10-03 19:42:18 +00:00
|
|
|
|
2016-10-06 21:51:54 +00:00
|
|
|
// Wait for restart
|
|
|
|
timeout := time.After(time.Duration(1*testutil.TestMultiplier()) * time.Second)
|
|
|
|
OUTER:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-harness.mockHooks.RestartCh:
|
|
|
|
break OUTER
|
|
|
|
case <-harness.mockHooks.SignalCh:
|
|
|
|
t.Fatalf("Signal with restart policy: %+v", harness.mockHooks)
|
|
|
|
case <-timeout:
|
|
|
|
t.Fatalf("Should have received a restart: %+v", harness.mockHooks)
|
|
|
|
}
|
2016-10-03 19:42:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check the files have been updated
|
|
|
|
path := filepath.Join(harness.taskDir, file1)
|
|
|
|
raw, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to read rendered template from %q: %v", path, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if s := string(raw); s != content1_2 {
|
|
|
|
t.Fatalf("Unexpected template data; got %q, want %q", s, content1_2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTaskTemplateManager_Interpolate_Destination(t *testing.T) {
|
|
|
|
// Make a template that will have its destination interpolated
|
|
|
|
content := "hello, world!"
|
|
|
|
file := "${node.unique.id}.tmpl"
|
|
|
|
template := &structs.Template{
|
|
|
|
EmbeddedTmpl: content,
|
|
|
|
DestPath: file,
|
|
|
|
ChangeMode: structs.TemplateChangeModeNoop,
|
|
|
|
}
|
|
|
|
|
2016-10-28 22:50:35 +00:00
|
|
|
harness := newTestHarness(t, []*structs.Template{template}, false, false)
|
|
|
|
harness.start(t)
|
2016-10-03 19:42:18 +00:00
|
|
|
defer harness.stop()
|
|
|
|
|
2016-10-06 21:51:54 +00:00
|
|
|
// Ensure unblock
|
|
|
|
select {
|
|
|
|
case <-harness.mockHooks.UnblockCh:
|
|
|
|
case <-time.After(time.Duration(5*testutil.TestMultiplier()) * time.Second):
|
2016-10-03 19:42:18 +00:00
|
|
|
t.Fatalf("Task unblock should have been called")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the file is there
|
|
|
|
actual := fmt.Sprintf("%s.tmpl", harness.node.ID)
|
|
|
|
path := filepath.Join(harness.taskDir, actual)
|
|
|
|
raw, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to read rendered template from %q: %v", path, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if s := string(raw); s != content {
|
|
|
|
t.Fatalf("Unexpected template data; got %q, want %q", s, content)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-28 22:50:35 +00:00
|
|
|
func TestTaskTemplateManager_Signal_Error(t *testing.T) {
|
2016-10-03 19:42:18 +00:00
|
|
|
// Make a template that renders based on a key in Consul and sends SIGALRM
|
|
|
|
key1 := "foo"
|
2016-10-28 22:50:35 +00:00
|
|
|
content1 := "bar"
|
|
|
|
content2 := "baz"
|
2016-10-03 19:42:18 +00:00
|
|
|
embedded1 := fmt.Sprintf(`{{key "%s"}}`, key1)
|
|
|
|
file1 := "my.tmpl"
|
|
|
|
template := &structs.Template{
|
|
|
|
EmbeddedTmpl: embedded1,
|
|
|
|
DestPath: file1,
|
|
|
|
ChangeMode: structs.TemplateChangeModeSignal,
|
|
|
|
ChangeSignal: "SIGALRM",
|
|
|
|
}
|
|
|
|
|
|
|
|
// Drop the retry rate
|
|
|
|
testRetryRate = 10 * time.Millisecond
|
|
|
|
|
2016-10-28 22:50:35 +00:00
|
|
|
harness := newTestHarness(t, []*structs.Template{template}, true, false)
|
|
|
|
harness.start(t)
|
2016-10-03 19:42:18 +00:00
|
|
|
defer harness.stop()
|
|
|
|
|
2016-10-28 22:50:35 +00:00
|
|
|
harness.mockHooks.SignalError = fmt.Errorf("test error")
|
|
|
|
|
2016-10-03 19:42:18 +00:00
|
|
|
// Write the key to Consul
|
2017-05-06 00:08:51 +00:00
|
|
|
harness.consul.SetKV(t, key1, []byte(content1))
|
2016-10-03 19:42:18 +00:00
|
|
|
|
2016-10-28 22:50:35 +00:00
|
|
|
// Wait a little
|
2016-10-10 21:49:37 +00:00
|
|
|
select {
|
2016-10-28 22:50:35 +00:00
|
|
|
case <-harness.mockHooks.UnblockCh:
|
|
|
|
case <-time.After(time.Duration(2*testutil.TestMultiplier()) * time.Second):
|
|
|
|
t.Fatalf("Should have received unblock: %+v", harness.mockHooks)
|
2016-10-10 21:49:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write the key to Consul
|
2017-05-06 00:08:51 +00:00
|
|
|
harness.consul.SetKV(t, key1, []byte(content2))
|
2016-10-10 21:49:37 +00:00
|
|
|
|
|
|
|
// Wait for kill channel
|
|
|
|
select {
|
|
|
|
case <-harness.mockHooks.KillCh:
|
|
|
|
break
|
|
|
|
case <-time.After(time.Duration(1*testutil.TestMultiplier()) * time.Second):
|
|
|
|
t.Fatalf("Should have received a signals: %+v", harness.mockHooks)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(harness.mockHooks.KillReason, "Sending signals") {
|
2017-02-28 00:00:19 +00:00
|
|
|
t.Fatalf("Unexpected error: %v", harness.mockHooks.KillReason)
|
2016-10-10 21:49:37 +00:00
|
|
|
}
|
|
|
|
}
|
2017-05-24 00:06:19 +00:00
|
|
|
|
|
|
|
// TestTaskTemplateManager_Env asserts templates with the env flag set are read
|
|
|
|
// into the task's environment.
|
|
|
|
func TestTaskTemplateManager_Env(t *testing.T) {
|
|
|
|
template := &structs.Template{
|
|
|
|
EmbeddedTmpl: `
|
|
|
|
# Comment lines are ok
|
|
|
|
|
|
|
|
FOO=bar
|
|
|
|
foo=123
|
2017-06-02 22:03:27 +00:00
|
|
|
ANYTHING_goes=Spaces are=ok!
|
2017-05-24 00:06:19 +00:00
|
|
|
`,
|
|
|
|
DestPath: "test.env",
|
|
|
|
ChangeMode: structs.TemplateChangeModeNoop,
|
|
|
|
Envvars: true,
|
|
|
|
}
|
|
|
|
harness := newTestHarness(t, []*structs.Template{template}, true, false)
|
|
|
|
harness.start(t)
|
|
|
|
defer harness.stop()
|
|
|
|
|
|
|
|
// Wait a little
|
|
|
|
select {
|
|
|
|
case <-harness.mockHooks.UnblockCh:
|
|
|
|
case <-time.After(time.Duration(2*testutil.TestMultiplier()) * time.Second):
|
|
|
|
t.Fatalf("Should have received unblock: %+v", harness.mockHooks)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate environment
|
|
|
|
env := harness.envBuilder.Build().Map()
|
|
|
|
if len(env) < 3 {
|
|
|
|
t.Fatalf("expected at least 3 env vars but found %d:\n%#v\n", len(env), env)
|
|
|
|
}
|
|
|
|
if env["FOO"] != "bar" {
|
|
|
|
t.Errorf("expected FOO=bar but found %q", env["FOO"])
|
|
|
|
}
|
|
|
|
if env["foo"] != "123" {
|
|
|
|
t.Errorf("expected foo=123 but found %q", env["foo"])
|
|
|
|
}
|
|
|
|
if env["ANYTHING_goes"] != "Spaces are=ok!" {
|
|
|
|
t.Errorf("expected ANYTHING_GOES='Spaces are ok!' but found %q", env["ANYTHING_goes"])
|
|
|
|
}
|
|
|
|
}
|
2017-05-26 00:13:33 +00:00
|
|
|
|
|
|
|
// TestTaskTemplateManager_Env_Missing asserts the core env
|
|
|
|
// template processing function returns errors when files don't exist
|
|
|
|
func TestTaskTemplateManager_Env_Missing(t *testing.T) {
|
|
|
|
d, err := ioutil.TempDir("", "ct_env_missing")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(d)
|
|
|
|
|
|
|
|
// Fake writing the file so we don't have to run the whole template manager
|
|
|
|
err = ioutil.WriteFile(filepath.Join(d, "exists.env"), []byte("FOO=bar\n"), 0644)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error writing template file: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
templates := []*structs.Template{
|
|
|
|
{
|
|
|
|
EmbeddedTmpl: "FOO=bar\n",
|
|
|
|
DestPath: "exists.env",
|
|
|
|
Envvars: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
EmbeddedTmpl: "WHAT=ever\n",
|
|
|
|
DestPath: "missing.env",
|
|
|
|
Envvars: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if vars, err := loadTemplateEnv(templates, d); err == nil {
|
|
|
|
t.Fatalf("expected an error but instead got env vars: %#v", vars)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestTaskTemplateManager_Env_Multi asserts the core env
|
|
|
|
// template processing function returns combined env vars from multiple
|
|
|
|
// templates correctly.
|
|
|
|
func TestTaskTemplateManager_Env_Multi(t *testing.T) {
|
|
|
|
d, err := ioutil.TempDir("", "ct_env_missing")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(d)
|
|
|
|
|
|
|
|
// Fake writing the files so we don't have to run the whole template manager
|
|
|
|
err = ioutil.WriteFile(filepath.Join(d, "zzz.env"), []byte("FOO=bar\nSHARED=nope\n"), 0644)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error writing template file 1: %v", err)
|
|
|
|
}
|
|
|
|
err = ioutil.WriteFile(filepath.Join(d, "aaa.env"), []byte("BAR=foo\nSHARED=yup\n"), 0644)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error writing template file 2: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Templates will get loaded in order (not alpha sorted)
|
|
|
|
templates := []*structs.Template{
|
|
|
|
{
|
|
|
|
DestPath: "zzz.env",
|
|
|
|
Envvars: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
DestPath: "aaa.env",
|
|
|
|
Envvars: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
vars, err := loadTemplateEnv(templates, d)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("expected an error but instead got env vars: %#v", vars)
|
|
|
|
}
|
|
|
|
if vars["FOO"] != "bar" {
|
2017-05-30 19:08:59 +00:00
|
|
|
t.Errorf("expected FOO=bar but found %q", vars["FOO"])
|
2017-05-26 00:13:33 +00:00
|
|
|
}
|
|
|
|
if vars["BAR"] != "foo" {
|
2017-05-30 19:08:59 +00:00
|
|
|
t.Errorf("expected BAR=foo but found %q", vars["BAR"])
|
2017-05-26 00:13:33 +00:00
|
|
|
}
|
|
|
|
if vars["SHARED"] != "yup" {
|
2017-05-30 19:08:59 +00:00
|
|
|
t.Errorf("expected FOO=bar but found %q", vars["yup"])
|
2017-05-26 00:13:33 +00:00
|
|
|
}
|
|
|
|
}
|
2017-07-06 23:50:52 +00:00
|
|
|
|
|
|
|
// TestTaskTemplateManager_Config_ServerName asserts the tls_server_name
|
|
|
|
// setting is propogated to consul-template's configuration. See #2776
|
|
|
|
func TestTaskTemplateManager_Config_ServerName(t *testing.T) {
|
|
|
|
c := config.DefaultConfig()
|
|
|
|
c.VaultConfig = &sconfig.VaultConfig{
|
|
|
|
Enabled: helper.BoolToPtr(true),
|
|
|
|
Addr: "https://localhost/",
|
|
|
|
TLSServerName: "notlocalhost",
|
|
|
|
}
|
|
|
|
ctconf, err := runnerConfig(c, "token")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if *ctconf.Vault.SSL.ServerName != c.VaultConfig.TLSServerName {
|
|
|
|
t.Fatalf("expected %q but found %q", c.VaultConfig.TLSServerName, *ctconf.Vault.SSL.ServerName)
|
|
|
|
}
|
|
|
|
}
|