2018-09-24 18:37:45 +00:00
|
|
|
package taskrunner
|
|
|
|
|
2019-02-23 23:36:48 +00:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
plugin "github.com/hashicorp/go-plugin"
|
|
|
|
"github.com/hashicorp/nomad/client/allocrunner/interfaces"
|
2019-02-21 23:37:22 +00:00
|
|
|
"github.com/hashicorp/nomad/helper"
|
2019-02-23 23:36:48 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/testlog"
|
|
|
|
"github.com/hashicorp/nomad/nomad/mock"
|
|
|
|
pstructs "github.com/hashicorp/nomad/plugins/shared/structs"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
2018-09-24 18:37:45 +00:00
|
|
|
|
|
|
|
// Statically assert the logmon hook implements the expected interfaces
|
|
|
|
var _ interfaces.TaskPrestartHook = (*logmonHook)(nil)
|
|
|
|
var _ interfaces.TaskStopHook = (*logmonHook)(nil)
|
2019-02-23 23:36:48 +00:00
|
|
|
|
|
|
|
// TestTaskRunner_LogmonHook_LoadReattach unit tests loading logmon reattach
|
|
|
|
// config from persisted hook state.
|
|
|
|
func TestTaskRunner_LogmonHook_LoadReattach(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
// No hook data should return nothing
|
|
|
|
cfg, err := reattachConfigFromHookData(nil)
|
|
|
|
require.Nil(t, cfg)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Hook data without the appropriate key should return nothing
|
|
|
|
cfg, err = reattachConfigFromHookData(map[string]string{"foo": "bar"})
|
|
|
|
require.Nil(t, cfg)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Create a realistic reattach config and roundtrip it
|
|
|
|
addr, err := net.ResolveTCPAddr("tcp", "127.0.0.1:0")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
orig := &plugin.ReattachConfig{
|
|
|
|
Protocol: plugin.ProtocolGRPC,
|
|
|
|
Addr: addr,
|
|
|
|
Pid: 4,
|
|
|
|
}
|
|
|
|
origJSON, err := json.Marshal(pstructs.ReattachConfigFromGoPlugin(orig))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
cfg, err = reattachConfigFromHookData(map[string]string{
|
|
|
|
logmonReattachKey: string(origJSON),
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, orig, cfg)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestTaskRunner_LogmonHook_StartStop asserts that a new logmon is created the
|
|
|
|
// first time Prestart is called, reattached to on subsequent restarts, and
|
|
|
|
// killed on Stop.
|
|
|
|
func TestTaskRunner_LogmonHook_StartStop(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
alloc := mock.BatchAlloc()
|
|
|
|
task := alloc.Job.TaskGroups[0].Tasks[0]
|
|
|
|
|
|
|
|
dir, err := ioutil.TempDir("", "nomadtest")
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer func() {
|
|
|
|
require.NoError(t, os.RemoveAll(dir))
|
|
|
|
}()
|
|
|
|
|
|
|
|
hookConf := newLogMonHookConfig(task.Name, dir)
|
2019-12-07 03:11:41 +00:00
|
|
|
runner := &TaskRunner{logmonHookConfig: hookConf}
|
|
|
|
hook := newLogMonHook(runner, testlog.HCLogger(t))
|
2019-02-23 23:36:48 +00:00
|
|
|
|
|
|
|
req := interfaces.TaskPrestartRequest{
|
|
|
|
Task: task,
|
|
|
|
}
|
|
|
|
resp := interfaces.TaskPrestartResponse{}
|
|
|
|
|
|
|
|
// First prestart should set reattach key but never be Done as it needs
|
|
|
|
// to rerun on agent restarts to reattach.
|
|
|
|
require.NoError(t, hook.Prestart(context.Background(), &req, &resp))
|
|
|
|
defer hook.Stop(context.Background(), nil, nil)
|
|
|
|
|
|
|
|
require.False(t, resp.Done)
|
2019-02-21 23:37:22 +00:00
|
|
|
origHookData := resp.State[logmonReattachKey]
|
2019-02-23 23:36:48 +00:00
|
|
|
require.NotEmpty(t, origHookData)
|
|
|
|
|
|
|
|
// Running prestart again should effectively noop as it reattaches to
|
|
|
|
// the running logmon.
|
2019-02-21 23:37:22 +00:00
|
|
|
req.PreviousState = map[string]string{
|
2019-02-23 23:36:48 +00:00
|
|
|
logmonReattachKey: origHookData,
|
|
|
|
}
|
|
|
|
require.NoError(t, hook.Prestart(context.Background(), &req, &resp))
|
|
|
|
require.False(t, resp.Done)
|
2019-02-21 23:37:22 +00:00
|
|
|
origHookData = resp.State[logmonReattachKey]
|
|
|
|
require.Equal(t, origHookData, req.PreviousState[logmonReattachKey])
|
2019-02-23 23:36:48 +00:00
|
|
|
|
|
|
|
// Running stop should shutdown logmon
|
2019-02-21 23:37:22 +00:00
|
|
|
stopReq := interfaces.TaskStopRequest{
|
|
|
|
ExistingState: helper.CopyMapStringString(resp.State),
|
|
|
|
}
|
|
|
|
require.NoError(t, hook.Stop(context.Background(), &stopReq, nil))
|
2019-02-23 23:36:48 +00:00
|
|
|
}
|