2014-09-01 17:42:35 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
2014-09-01 18:21:57 +00:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2016-02-02 00:42:04 +00:00
|
|
|
"fmt"
|
2014-09-01 18:21:57 +00:00
|
|
|
"reflect"
|
2014-09-01 17:42:35 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
2014-09-01 18:21:57 +00:00
|
|
|
|
pkg refactor
command/agent/* -> agent/*
command/consul/* -> agent/consul/*
command/agent/command{,_test}.go -> command/agent{,_test}.go
command/base/command.go -> command/base.go
command/base/* -> command/*
commands.go -> command/commands.go
The script which did the refactor is:
(
cd $GOPATH/src/github.com/hashicorp/consul
git mv command/agent/command.go command/agent.go
git mv command/agent/command_test.go command/agent_test.go
git mv command/agent/flag_slice_value{,_test}.go command/
git mv command/agent .
git mv command/base/command.go command/base.go
git mv command/base/config_util{,_test}.go command/
git mv commands.go command/
git mv consul agent
rmdir command/base/
gsed -i -e 's|package agent|package command|' command/agent{,_test}.go
gsed -i -e 's|package agent|package command|' command/flag_slice_value{,_test}.go
gsed -i -e 's|package base|package command|' command/base.go command/config_util{,_test}.go
gsed -i -e 's|package main|package command|' command/commands.go
gsed -i -e 's|base.Command|BaseCommand|' command/commands.go
gsed -i -e 's|agent.Command|AgentCommand|' command/commands.go
gsed -i -e 's|\tCommand:|\tBaseCommand:|' command/commands.go
gsed -i -e 's|base\.||' command/commands.go
gsed -i -e 's|command\.||' command/commands.go
gsed -i -e 's|command|c|' main.go
gsed -i -e 's|range Commands|range command.Commands|' main.go
gsed -i -e 's|Commands: Commands|Commands: command.Commands|' main.go
gsed -i -e 's|base\.BoolValue|BoolValue|' command/operator_autopilot_set.go
gsed -i -e 's|base\.DurationValue|DurationValue|' command/operator_autopilot_set.go
gsed -i -e 's|base\.StringValue|StringValue|' command/operator_autopilot_set.go
gsed -i -e 's|base\.UintValue|UintValue|' command/operator_autopilot_set.go
gsed -i -e 's|\bCommand\b|BaseCommand|' command/base.go
gsed -i -e 's|BaseCommand Options|Command Options|' command/base.go
gsed -i -e 's|base.Command|BaseCommand|' command/*.go
gsed -i -e 's|c\.Command|c.BaseCommand|g' command/*.go
gsed -i -e 's|\tCommand:|\tBaseCommand:|' command/*_test.go
gsed -i -e 's|base\.||' command/*_test.go
gsed -i -e 's|\bCommand\b|AgentCommand|' command/agent{,_test}.go
gsed -i -e 's|cmd.AgentCommand|cmd.BaseCommand|' command/agent.go
gsed -i -e 's|cli.AgentCommand = new(Command)|cli.Command = new(AgentCommand)|' command/agent_test.go
gsed -i -e 's|exec.AgentCommand|exec.Command|' command/agent_test.go
gsed -i -e 's|exec.BaseCommand|exec.Command|' command/agent_test.go
gsed -i -e 's|NewTestAgent|agent.NewTestAgent|' command/agent_test.go
gsed -i -e 's|= TestConfig|= agent.TestConfig|' command/agent_test.go
gsed -i -e 's|: RetryJoin|: agent.RetryJoin|' command/agent_test.go
gsed -i -e 's|\.\./\.\./|../|' command/config_util_test.go
gsed -i -e 's|\bverifyUniqueListeners|VerifyUniqueListeners|' agent/config{,_test}.go command/agent.go
gsed -i -e 's|\bserfLANKeyring\b|SerfLANKeyring|g' agent/{agent,keyring,testagent}.go command/agent.go
gsed -i -e 's|\bserfWANKeyring\b|SerfWANKeyring|g' agent/{agent,keyring,testagent}.go command/agent.go
gsed -i -e 's|\bNewAgent\b|agent.New|g' command/agent{,_test}.go
gsed -i -e 's|\bNewAgent|New|' agent/{acl_test,agent,testagent}.go
gsed -i -e 's|\bAgent\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bBool\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bConfig\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bDefaultConfig\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bDevConfig\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bMergeConfig\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bReadConfigPaths\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bParseMetaPair\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bSerfLANKeyring\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bSerfWANKeyring\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|circonus\.agent|circonus|g' command/agent{,_test}.go
gsed -i -e 's|logger\.agent|logger|g' command/agent{,_test}.go
gsed -i -e 's|metrics\.agent|metrics|g' command/agent{,_test}.go
gsed -i -e 's|// agent.Agent|// agent|' command/agent{,_test}.go
gsed -i -e 's|a\.agent\.Config|a.Config|' command/agent{,_test}.go
gsed -i -e 's|agent\.AppendSliceValue|AppendSliceValue|' command/{configtest,validate}.go
gsed -i -e 's|consul/consul|agent/consul|' GNUmakefile
gsed -i -e 's|\.\./test|../../test|' agent/consul/server_test.go
# fix imports
f=$(grep -rl 'github.com/hashicorp/consul/command/agent' * | grep '\.go')
gsed -i -e 's|github.com/hashicorp/consul/command/agent|github.com/hashicorp/consul/agent|' $f
goimports -w $f
f=$(grep -rl 'github.com/hashicorp/consul/consul' * | grep '\.go')
gsed -i -e 's|github.com/hashicorp/consul/consul|github.com/hashicorp/consul/agent/consul|' $f
goimports -w $f
goimports -w command/*.go main.go
)
2017-06-09 22:28:28 +00:00
|
|
|
"github.com/hashicorp/consul/agent/consul/structs"
|
2017-04-19 23:00:11 +00:00
|
|
|
"github.com/hashicorp/consul/api"
|
2016-01-29 19:42:34 +00:00
|
|
|
"github.com/hashicorp/go-uuid"
|
2014-09-01 17:42:35 +00:00
|
|
|
)
|
|
|
|
|
2016-01-29 19:42:34 +00:00
|
|
|
func generateUUID() (ret string) {
|
|
|
|
var err error
|
|
|
|
if ret, err = uuid.GenerateUUID(); err != nil {
|
2016-02-02 00:42:04 +00:00
|
|
|
panic(fmt.Sprintf("Unable to generate a UUID, %v", err))
|
2016-01-29 19:42:34 +00:00
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2014-09-01 17:42:35 +00:00
|
|
|
func TestRexecWriter(t *testing.T) {
|
2017-07-04 10:44:24 +00:00
|
|
|
// t.Parallel() // timing test. no parallel
|
2014-09-01 17:42:35 +00:00
|
|
|
writer := &rexecWriter{
|
|
|
|
BufCh: make(chan []byte, 16),
|
|
|
|
BufSize: 16,
|
2017-07-04 10:44:24 +00:00
|
|
|
BufIdle: 100 * time.Millisecond,
|
2014-09-01 17:42:35 +00:00
|
|
|
CancelCh: make(chan struct{}),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write short, wait for idle
|
|
|
|
start := time.Now()
|
|
|
|
n, err := writer.Write([]byte("test"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if n != 4 {
|
|
|
|
t.Fatalf("bad: %v", n)
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case b := <-writer.BufCh:
|
|
|
|
if len(b) != 4 {
|
|
|
|
t.Fatalf("Bad: %v", b)
|
|
|
|
}
|
|
|
|
if time.Now().Sub(start) < writer.BufIdle {
|
|
|
|
t.Fatalf("too early")
|
|
|
|
}
|
|
|
|
case <-time.After(2 * writer.BufIdle):
|
|
|
|
t.Fatalf("timeout")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write in succession to prevent the timeout
|
|
|
|
writer.Write([]byte("test"))
|
|
|
|
time.Sleep(writer.BufIdle / 2)
|
|
|
|
writer.Write([]byte("test"))
|
|
|
|
time.Sleep(writer.BufIdle / 2)
|
|
|
|
start = time.Now()
|
|
|
|
writer.Write([]byte("test"))
|
|
|
|
|
|
|
|
select {
|
|
|
|
case b := <-writer.BufCh:
|
|
|
|
if len(b) != 12 {
|
|
|
|
t.Fatalf("Bad: %v", b)
|
|
|
|
}
|
|
|
|
if time.Now().Sub(start) < writer.BufIdle {
|
|
|
|
t.Fatalf("too early")
|
|
|
|
}
|
|
|
|
case <-time.After(2 * writer.BufIdle):
|
|
|
|
t.Fatalf("timeout")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write large values, multiple flushes required
|
|
|
|
writer.Write([]byte("01234567890123456789012345678901"))
|
|
|
|
|
|
|
|
select {
|
|
|
|
case b := <-writer.BufCh:
|
|
|
|
if string(b) != "0123456789012345" {
|
|
|
|
t.Fatalf("bad: %s", b)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
t.Fatalf("should have buf")
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case b := <-writer.BufCh:
|
|
|
|
if string(b) != "6789012345678901" {
|
|
|
|
t.Fatalf("bad: %s", b)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
t.Fatalf("should have buf")
|
|
|
|
}
|
|
|
|
}
|
2014-09-01 18:21:57 +00:00
|
|
|
|
|
|
|
func TestRemoteExecGetSpec(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-07-17 04:12:16 +00:00
|
|
|
testRemoteExecGetSpec(t, nil, "", true)
|
2015-01-29 18:30:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRemoteExecGetSpec_ACLToken(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-05-22 11:03:59 +00:00
|
|
|
cfg := TestConfig()
|
|
|
|
cfg.ACLDatacenter = "dc1"
|
2017-07-17 04:12:16 +00:00
|
|
|
cfg.ACLMasterToken = "root"
|
2017-05-22 11:03:59 +00:00
|
|
|
cfg.ACLToken = "root"
|
|
|
|
cfg.ACLDefaultPolicy = "deny"
|
2017-07-17 04:12:16 +00:00
|
|
|
testRemoteExecGetSpec(t, cfg, "root", true)
|
2015-01-29 18:30:58 +00:00
|
|
|
}
|
|
|
|
|
2017-07-17 04:12:16 +00:00
|
|
|
func TestRemoteExecGetSpec_ACLAgentToken(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
cfg := TestConfig()
|
|
|
|
cfg.ACLDatacenter = "dc1"
|
|
|
|
cfg.ACLMasterToken = "root"
|
|
|
|
cfg.ACLAgentToken = "root"
|
|
|
|
cfg.ACLDefaultPolicy = "deny"
|
|
|
|
testRemoteExecGetSpec(t, cfg, "root", true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRemoteExecGetSpec_ACLDeny(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
cfg := TestConfig()
|
|
|
|
cfg.ACLDatacenter = "dc1"
|
|
|
|
cfg.ACLMasterToken = "root"
|
|
|
|
cfg.ACLDefaultPolicy = "deny"
|
|
|
|
testRemoteExecGetSpec(t, cfg, "root", false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testRemoteExecGetSpec(t *testing.T, c *Config, token string, shouldSucceed bool) {
|
|
|
|
a := NewTestAgent(t.Name(), c)
|
2017-05-21 07:11:09 +00:00
|
|
|
defer a.Shutdown()
|
2014-09-01 18:21:57 +00:00
|
|
|
|
|
|
|
event := &remoteExecEvent{
|
|
|
|
Prefix: "_rexec",
|
2017-07-17 04:12:16 +00:00
|
|
|
Session: makeRexecSession(t, a.Agent, token),
|
2014-09-01 18:21:57 +00:00
|
|
|
}
|
2017-07-17 04:12:16 +00:00
|
|
|
defer destroySession(t, a.Agent, event.Session, token)
|
2014-09-01 18:21:57 +00:00
|
|
|
|
|
|
|
spec := &remoteExecSpec{
|
|
|
|
Command: "uptime",
|
|
|
|
Script: []byte("#!/bin/bash"),
|
|
|
|
Wait: time.Second,
|
|
|
|
}
|
|
|
|
buf, err := json.Marshal(spec)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
key := "_rexec/" + event.Session + "/job"
|
2017-07-17 04:12:16 +00:00
|
|
|
setKV(t, a.Agent, key, buf, token)
|
2014-09-01 18:21:57 +00:00
|
|
|
|
|
|
|
var out remoteExecSpec
|
2017-07-17 04:12:16 +00:00
|
|
|
if shouldSucceed != a.remoteExecGetSpec(event, &out) {
|
2014-09-01 18:21:57 +00:00
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
2017-07-17 04:12:16 +00:00
|
|
|
if shouldSucceed && !reflect.DeepEqual(spec, &out) {
|
2014-09-01 18:21:57 +00:00
|
|
|
t.Fatalf("bad spec")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRemoteExecWrites(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-07-17 04:12:16 +00:00
|
|
|
testRemoteExecWrites(t, nil, "", true)
|
2015-01-29 18:30:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRemoteExecWrites_ACLToken(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-05-22 11:03:59 +00:00
|
|
|
cfg := TestConfig()
|
|
|
|
cfg.ACLDatacenter = "dc1"
|
2017-07-17 04:12:16 +00:00
|
|
|
cfg.ACLMasterToken = "root"
|
2017-05-22 11:03:59 +00:00
|
|
|
cfg.ACLToken = "root"
|
|
|
|
cfg.ACLDefaultPolicy = "deny"
|
2017-07-17 04:12:16 +00:00
|
|
|
testRemoteExecWrites(t, cfg, "root", true)
|
2015-01-29 18:30:58 +00:00
|
|
|
}
|
|
|
|
|
2017-07-17 04:12:16 +00:00
|
|
|
func TestRemoteExecWrites_ACLAgentToken(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
cfg := TestConfig()
|
|
|
|
cfg.ACLDatacenter = "dc1"
|
|
|
|
cfg.ACLMasterToken = "root"
|
|
|
|
cfg.ACLAgentToken = "root"
|
|
|
|
cfg.ACLDefaultPolicy = "deny"
|
|
|
|
testRemoteExecWrites(t, cfg, "root", true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRemoteExecWrites_ACLDeny(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
cfg := TestConfig()
|
|
|
|
cfg.ACLDatacenter = "dc1"
|
|
|
|
cfg.ACLMasterToken = "root"
|
|
|
|
cfg.ACLDefaultPolicy = "deny"
|
|
|
|
testRemoteExecWrites(t, cfg, "root", false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testRemoteExecWrites(t *testing.T, c *Config, token string, shouldSucceed bool) {
|
|
|
|
a := NewTestAgent(t.Name(), c)
|
2017-05-21 07:11:09 +00:00
|
|
|
defer a.Shutdown()
|
2014-09-01 18:21:57 +00:00
|
|
|
|
|
|
|
event := &remoteExecEvent{
|
|
|
|
Prefix: "_rexec",
|
2017-07-17 04:12:16 +00:00
|
|
|
Session: makeRexecSession(t, a.Agent, token),
|
2014-09-01 18:21:57 +00:00
|
|
|
}
|
2017-07-17 04:12:16 +00:00
|
|
|
defer destroySession(t, a.Agent, event.Session, token)
|
2014-09-01 18:21:57 +00:00
|
|
|
|
2017-07-17 04:12:16 +00:00
|
|
|
if shouldSucceed != a.remoteExecWriteAck(event) {
|
2014-09-01 18:21:57 +00:00
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
|
|
|
|
|
|
|
output := []byte("testing")
|
2017-07-17 04:12:16 +00:00
|
|
|
if shouldSucceed != a.remoteExecWriteOutput(event, 0, output) {
|
2014-09-01 18:21:57 +00:00
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
2017-07-17 04:12:16 +00:00
|
|
|
if shouldSucceed != a.remoteExecWriteOutput(event, 10, output) {
|
2014-09-01 18:21:57 +00:00
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
|
|
|
|
2017-07-17 04:12:16 +00:00
|
|
|
// Bypass the remaining checks if the write was expected to fail.
|
|
|
|
if !shouldSucceed {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-09-17 19:17:51 +00:00
|
|
|
exitCode := 1
|
2017-05-21 07:11:09 +00:00
|
|
|
if !a.remoteExecWriteExitCode(event, &exitCode) {
|
2014-09-01 18:21:57 +00:00
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
|
|
|
|
2017-05-21 07:11:09 +00:00
|
|
|
key := "_rexec/" + event.Session + "/" + a.Config.NodeName + "/ack"
|
2017-07-17 04:12:16 +00:00
|
|
|
d := getKV(t, a.Agent, key, token)
|
2014-09-01 18:21:57 +00:00
|
|
|
if d == nil || d.Session != event.Session {
|
|
|
|
t.Fatalf("bad ack: %#v", d)
|
|
|
|
}
|
|
|
|
|
2017-05-21 07:11:09 +00:00
|
|
|
key = "_rexec/" + event.Session + "/" + a.Config.NodeName + "/out/00000"
|
2017-07-17 04:12:16 +00:00
|
|
|
d = getKV(t, a.Agent, key, token)
|
2014-09-01 18:21:57 +00:00
|
|
|
if d == nil || d.Session != event.Session || !bytes.Equal(d.Value, output) {
|
|
|
|
t.Fatalf("bad output: %#v", d)
|
|
|
|
}
|
|
|
|
|
2017-05-21 07:11:09 +00:00
|
|
|
key = "_rexec/" + event.Session + "/" + a.Config.NodeName + "/out/0000a"
|
2017-07-17 04:12:16 +00:00
|
|
|
d = getKV(t, a.Agent, key, token)
|
2014-09-01 18:21:57 +00:00
|
|
|
if d == nil || d.Session != event.Session || !bytes.Equal(d.Value, output) {
|
|
|
|
t.Fatalf("bad output: %#v", d)
|
|
|
|
}
|
|
|
|
|
2017-05-21 07:11:09 +00:00
|
|
|
key = "_rexec/" + event.Session + "/" + a.Config.NodeName + "/exit"
|
2017-07-17 04:12:16 +00:00
|
|
|
d = getKV(t, a.Agent, key, token)
|
2014-09-01 18:21:57 +00:00
|
|
|
if d == nil || d.Session != event.Session || string(d.Value) != "1" {
|
|
|
|
t.Fatalf("bad output: %#v", d)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-18 09:04:20 +00:00
|
|
|
func testHandleRemoteExec(t *testing.T, command string, expectedSubstring string, expectedReturnCode string) {
|
2017-05-21 07:11:09 +00:00
|
|
|
a := NewTestAgent(t.Name(), nil)
|
|
|
|
defer a.Shutdown()
|
2014-09-01 18:21:57 +00:00
|
|
|
|
|
|
|
event := &remoteExecEvent{
|
|
|
|
Prefix: "_rexec",
|
2017-07-17 04:12:16 +00:00
|
|
|
Session: makeRexecSession(t, a.Agent, ""),
|
2014-09-01 18:21:57 +00:00
|
|
|
}
|
2017-07-17 04:12:16 +00:00
|
|
|
defer destroySession(t, a.Agent, event.Session, "")
|
2014-09-01 18:21:57 +00:00
|
|
|
|
|
|
|
spec := &remoteExecSpec{
|
2014-09-17 19:33:03 +00:00
|
|
|
Command: command,
|
2014-09-01 18:21:57 +00:00
|
|
|
Wait: time.Second,
|
|
|
|
}
|
|
|
|
buf, err := json.Marshal(spec)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
key := "_rexec/" + event.Session + "/job"
|
2017-07-17 04:12:16 +00:00
|
|
|
setKV(t, a.Agent, key, buf, "")
|
2014-09-01 18:21:57 +00:00
|
|
|
|
|
|
|
buf, err = json.Marshal(event)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
msg := &UserEvent{
|
|
|
|
ID: generateUUID(),
|
|
|
|
Payload: buf,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle the event...
|
2017-05-21 07:11:09 +00:00
|
|
|
a.handleRemoteExec(msg)
|
2014-09-01 18:21:57 +00:00
|
|
|
|
|
|
|
// Verify we have an ack
|
2017-05-21 07:11:09 +00:00
|
|
|
key = "_rexec/" + event.Session + "/" + a.Config.NodeName + "/ack"
|
2017-07-17 04:12:16 +00:00
|
|
|
d := getKV(t, a.Agent, key, "")
|
2014-09-01 18:21:57 +00:00
|
|
|
if d == nil || d.Session != event.Session {
|
|
|
|
t.Fatalf("bad ack: %#v", d)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify we have output
|
2017-05-21 07:11:09 +00:00
|
|
|
key = "_rexec/" + event.Session + "/" + a.Config.NodeName + "/out/00000"
|
2017-07-17 04:12:16 +00:00
|
|
|
d = getKV(t, a.Agent, key, "")
|
2014-09-01 18:21:57 +00:00
|
|
|
if d == nil || d.Session != event.Session ||
|
2014-09-17 19:33:03 +00:00
|
|
|
!bytes.Contains(d.Value, []byte(expectedSubstring)) {
|
2014-09-01 18:21:57 +00:00
|
|
|
t.Fatalf("bad output: %#v", d)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify we have an exit code
|
2017-05-21 07:11:09 +00:00
|
|
|
key = "_rexec/" + event.Session + "/" + a.Config.NodeName + "/exit"
|
2017-07-17 04:12:16 +00:00
|
|
|
d = getKV(t, a.Agent, key, "")
|
2014-09-17 19:33:03 +00:00
|
|
|
if d == nil || d.Session != event.Session || string(d.Value) != expectedReturnCode {
|
2014-09-01 18:21:57 +00:00
|
|
|
t.Fatalf("bad output: %#v", d)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-17 19:33:03 +00:00
|
|
|
func TestHandleRemoteExec(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2014-09-18 10:55:09 +00:00
|
|
|
testHandleRemoteExec(t, "uptime", "load", "0")
|
2014-09-17 19:33:03 +00:00
|
|
|
}
|
2014-09-17 19:17:51 +00:00
|
|
|
|
2014-09-17 19:33:03 +00:00
|
|
|
func TestHandleRemoteExecFailed(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2014-09-18 10:55:09 +00:00
|
|
|
testHandleRemoteExec(t, "echo failing;exit 2", "failing", "2")
|
2014-09-17 19:17:51 +00:00
|
|
|
}
|
|
|
|
|
2017-07-17 04:12:16 +00:00
|
|
|
func makeRexecSession(t *testing.T, a *Agent, token string) string {
|
2014-09-01 18:21:57 +00:00
|
|
|
args := structs.SessionRequest{
|
2017-05-21 07:11:09 +00:00
|
|
|
Datacenter: a.config.Datacenter,
|
2014-09-01 18:21:57 +00:00
|
|
|
Op: structs.SessionCreate,
|
|
|
|
Session: structs.Session{
|
2017-05-21 07:11:09 +00:00
|
|
|
Node: a.config.NodeName,
|
2014-09-01 18:21:57 +00:00
|
|
|
LockDelay: 15 * time.Second,
|
|
|
|
},
|
2017-07-17 04:12:16 +00:00
|
|
|
WriteRequest: structs.WriteRequest{
|
|
|
|
Token: token,
|
|
|
|
},
|
2014-09-01 18:21:57 +00:00
|
|
|
}
|
|
|
|
var out string
|
2017-05-21 07:11:09 +00:00
|
|
|
if err := a.RPC("Session.Apply", &args, &out); err != nil {
|
2014-09-01 18:21:57 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2017-07-17 04:12:16 +00:00
|
|
|
func destroySession(t *testing.T, a *Agent, session string, token string) {
|
2014-09-01 18:21:57 +00:00
|
|
|
args := structs.SessionRequest{
|
2017-05-21 07:11:09 +00:00
|
|
|
Datacenter: a.config.Datacenter,
|
2014-09-01 18:21:57 +00:00
|
|
|
Op: structs.SessionDestroy,
|
|
|
|
Session: structs.Session{
|
|
|
|
ID: session,
|
|
|
|
},
|
2017-07-17 04:12:16 +00:00
|
|
|
WriteRequest: structs.WriteRequest{
|
|
|
|
Token: token,
|
|
|
|
},
|
2014-09-01 18:21:57 +00:00
|
|
|
}
|
|
|
|
var out string
|
2017-05-21 07:11:09 +00:00
|
|
|
if err := a.RPC("Session.Apply", &args, &out); err != nil {
|
2014-09-01 18:21:57 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-17 04:12:16 +00:00
|
|
|
func setKV(t *testing.T, a *Agent, key string, val []byte, token string) {
|
2014-09-01 18:21:57 +00:00
|
|
|
write := structs.KVSRequest{
|
2017-05-21 07:11:09 +00:00
|
|
|
Datacenter: a.config.Datacenter,
|
2017-04-19 23:00:11 +00:00
|
|
|
Op: api.KVSet,
|
2014-09-01 18:21:57 +00:00
|
|
|
DirEnt: structs.DirEntry{
|
|
|
|
Key: key,
|
|
|
|
Value: val,
|
|
|
|
},
|
2017-07-17 04:12:16 +00:00
|
|
|
WriteRequest: structs.WriteRequest{
|
|
|
|
Token: token,
|
|
|
|
},
|
2014-09-01 18:21:57 +00:00
|
|
|
}
|
|
|
|
var success bool
|
2017-05-21 07:11:09 +00:00
|
|
|
if err := a.RPC("KVS.Apply", &write, &success); err != nil {
|
2014-09-01 18:21:57 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-17 04:12:16 +00:00
|
|
|
func getKV(t *testing.T, a *Agent, key string, token string) *structs.DirEntry {
|
2014-09-01 18:21:57 +00:00
|
|
|
req := structs.KeyRequest{
|
2017-05-21 07:11:09 +00:00
|
|
|
Datacenter: a.config.Datacenter,
|
2014-09-01 18:21:57 +00:00
|
|
|
Key: key,
|
2017-07-17 04:12:16 +00:00
|
|
|
QueryOptions: structs.QueryOptions{
|
|
|
|
Token: token,
|
|
|
|
},
|
2014-09-01 18:21:57 +00:00
|
|
|
}
|
|
|
|
var out structs.IndexedDirEntries
|
2017-05-21 07:11:09 +00:00
|
|
|
if err := a.RPC("KVS.Get", &req, &out); err != nil {
|
2014-09-01 18:21:57 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if len(out.Entries) > 0 {
|
|
|
|
return out.Entries[0]
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|