2014-09-01 17:42:35 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
2014-09-01 18:21:57 +00:00
|
|
|
"bytes"
|
2022-12-14 15:24:22 +00:00
|
|
|
"context"
|
2014-09-01 18:21:57 +00:00
|
|
|
"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"
|
2017-09-25 18:40:42 +00:00
|
|
|
|
2014-09-01 17:42:35 +00:00
|
|
|
"time"
|
2014-09-01 18:21:57 +00:00
|
|
|
|
2017-07-06 10:34:00 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2017-04-19 23:00:11 +00:00
|
|
|
"github.com/hashicorp/consul/api"
|
2019-03-27 12:54:56 +00:00
|
|
|
"github.com/hashicorp/consul/sdk/testutil/retry"
|
2019-04-25 16:26:33 +00:00
|
|
|
"github.com/hashicorp/consul/testrpc"
|
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) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
2017-10-17 18:38:24 +00:00
|
|
|
if time.Since(start) < writer.BufIdle {
|
2014-09-01 17:42:35 +00:00
|
|
|
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)
|
|
|
|
}
|
2017-10-17 18:38:24 +00:00
|
|
|
if time.Since(start) < writer.BufIdle {
|
2014-09-01 17:42:35 +00:00
|
|
|
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) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2018-08-06 23:46:09 +00:00
|
|
|
testRemoteExecGetSpec(t, "", "", true, "")
|
2015-01-29 18:30:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRemoteExecGetSpec_ACLToken(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2018-08-06 23:46:09 +00:00
|
|
|
dc := "dc1"
|
2017-09-25 18:40:42 +00:00
|
|
|
testRemoteExecGetSpec(t, `
|
2021-12-07 12:48:50 +00:00
|
|
|
primary_datacenter = "`+dc+`"
|
|
|
|
|
|
|
|
acl {
|
|
|
|
enabled = true
|
|
|
|
default_policy = "deny"
|
|
|
|
|
|
|
|
tokens {
|
|
|
|
initial_management = "root"
|
|
|
|
default = "root"
|
|
|
|
}
|
|
|
|
}
|
2018-08-06 23:46:09 +00:00
|
|
|
`, "root", true, dc)
|
2015-01-29 18:30:58 +00:00
|
|
|
}
|
|
|
|
|
2017-07-17 04:12:16 +00:00
|
|
|
func TestRemoteExecGetSpec_ACLAgentToken(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2017-07-17 04:12:16 +00:00
|
|
|
t.Parallel()
|
2018-08-06 23:46:09 +00:00
|
|
|
dc := "dc1"
|
2017-09-25 18:40:42 +00:00
|
|
|
testRemoteExecGetSpec(t, `
|
2021-12-07 12:48:50 +00:00
|
|
|
primary_datacenter = "`+dc+`"
|
|
|
|
|
|
|
|
acl {
|
|
|
|
enabled = true
|
|
|
|
default_policy = "deny"
|
|
|
|
|
|
|
|
tokens {
|
|
|
|
initial_management = "root"
|
|
|
|
agent = "root"
|
|
|
|
}
|
|
|
|
}
|
2018-08-06 23:46:09 +00:00
|
|
|
`, "root", true, dc)
|
2017-07-17 04:12:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRemoteExecGetSpec_ACLDeny(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2017-07-17 04:12:16 +00:00
|
|
|
t.Parallel()
|
2018-08-06 23:46:09 +00:00
|
|
|
dc := "dc1"
|
2017-09-25 18:40:42 +00:00
|
|
|
testRemoteExecGetSpec(t, `
|
2021-12-07 12:48:50 +00:00
|
|
|
primary_datacenter = "`+dc+`"
|
|
|
|
|
|
|
|
acl {
|
|
|
|
enabled = true
|
|
|
|
default_policy = "deny"
|
|
|
|
|
|
|
|
tokens {
|
|
|
|
initial_management = "root"
|
|
|
|
}
|
|
|
|
}
|
2018-08-06 23:46:09 +00:00
|
|
|
`, "root", false, dc)
|
2017-07-17 04:12:16 +00:00
|
|
|
}
|
|
|
|
|
2018-08-06 23:46:09 +00:00
|
|
|
func testRemoteExecGetSpec(t *testing.T, hcl string, token string, shouldSucceed bool, dc string) {
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, hcl)
|
2017-05-21 07:11:09 +00:00
|
|
|
defer a.Shutdown()
|
2018-08-06 23:46:09 +00:00
|
|
|
if dc != "" {
|
|
|
|
testrpc.WaitForLeader(t, a.RPC, dc)
|
2018-09-10 15:58:53 +00:00
|
|
|
} else {
|
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
2018-08-06 23:46:09 +00:00
|
|
|
}
|
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"
|
2019-07-16 20:47:45 +00:00
|
|
|
if err := setKV(a.Agent, key, buf, token); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
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) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2018-08-06 23:46:09 +00:00
|
|
|
testRemoteExecWrites(t, "", "", true, "")
|
2015-01-29 18:30:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRemoteExecWrites_ACLToken(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2018-08-06 23:46:09 +00:00
|
|
|
dc := "dc1"
|
2017-09-25 18:40:42 +00:00
|
|
|
testRemoteExecWrites(t, `
|
2021-12-07 12:48:50 +00:00
|
|
|
primary_datacenter = "`+dc+`"
|
|
|
|
|
|
|
|
acl {
|
|
|
|
enabled = true
|
|
|
|
default_policy = "deny"
|
|
|
|
|
|
|
|
tokens {
|
|
|
|
initial_management = "root"
|
|
|
|
default = "root"
|
|
|
|
}
|
|
|
|
}
|
2018-08-06 23:46:09 +00:00
|
|
|
`, "root", true, dc)
|
2015-01-29 18:30:58 +00:00
|
|
|
}
|
|
|
|
|
2017-07-17 04:12:16 +00:00
|
|
|
func TestRemoteExecWrites_ACLAgentToken(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2017-07-17 04:12:16 +00:00
|
|
|
t.Parallel()
|
2018-08-06 23:46:09 +00:00
|
|
|
dc := "dc1"
|
2017-09-25 18:40:42 +00:00
|
|
|
testRemoteExecWrites(t, `
|
2021-12-07 12:48:50 +00:00
|
|
|
primary_datacenter = "`+dc+`"
|
|
|
|
|
|
|
|
acl {
|
|
|
|
enabled = true
|
|
|
|
default_policy = "deny"
|
|
|
|
|
|
|
|
tokens {
|
|
|
|
initial_management = "root"
|
|
|
|
agent = "root"
|
|
|
|
}
|
|
|
|
}
|
2018-08-06 23:46:09 +00:00
|
|
|
`, "root", true, dc)
|
2017-07-17 04:12:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRemoteExecWrites_ACLDeny(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2017-07-17 04:12:16 +00:00
|
|
|
t.Parallel()
|
2018-08-06 23:46:09 +00:00
|
|
|
dc := "dc1"
|
2017-09-25 18:40:42 +00:00
|
|
|
testRemoteExecWrites(t, `
|
2021-12-07 12:48:50 +00:00
|
|
|
primary_datacenter = "`+dc+`"
|
|
|
|
|
|
|
|
acl {
|
|
|
|
enabled = true
|
|
|
|
default_policy = "deny"
|
|
|
|
|
|
|
|
tokens {
|
|
|
|
initial_management = "root"
|
|
|
|
}
|
|
|
|
}
|
2018-08-06 23:46:09 +00:00
|
|
|
`, "root", false, dc)
|
2017-07-17 04:12:16 +00:00
|
|
|
}
|
|
|
|
|
2018-08-06 23:46:09 +00:00
|
|
|
func testRemoteExecWrites(t *testing.T, hcl string, token string, shouldSucceed bool, dc string) {
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, hcl)
|
2017-05-21 07:11:09 +00:00
|
|
|
defer a.Shutdown()
|
2018-08-06 23:46:09 +00:00
|
|
|
if dc != "" {
|
|
|
|
testrpc.WaitForLeader(t, a.RPC, dc)
|
|
|
|
} else {
|
|
|
|
// For slow machines, ensure we wait a bit
|
2018-09-10 15:58:53 +00:00
|
|
|
testrpc.WaitForLeader(t, a.RPC, "dc1")
|
2018-08-06 23:46:09 +00:00
|
|
|
}
|
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"
|
2019-07-16 20:47:45 +00:00
|
|
|
d, err := getKV(a.Agent, key, token)
|
|
|
|
if d == nil || d.Session != event.Session || err != nil {
|
2014-09-01 18:21:57 +00:00
|
|
|
t.Fatalf("bad ack: %#v", d)
|
|
|
|
}
|
|
|
|
|
2017-05-21 07:11:09 +00:00
|
|
|
key = "_rexec/" + event.Session + "/" + a.Config.NodeName + "/out/00000"
|
2019-07-16 20:47:45 +00:00
|
|
|
d, err = getKV(a.Agent, key, token)
|
|
|
|
if d == nil || d.Session != event.Session || !bytes.Equal(d.Value, output) || err != nil {
|
2014-09-01 18:21:57 +00:00
|
|
|
t.Fatalf("bad output: %#v", d)
|
|
|
|
}
|
|
|
|
|
2017-05-21 07:11:09 +00:00
|
|
|
key = "_rexec/" + event.Session + "/" + a.Config.NodeName + "/out/0000a"
|
2019-07-16 20:47:45 +00:00
|
|
|
d, err = getKV(a.Agent, key, token)
|
|
|
|
if d == nil || d.Session != event.Session || !bytes.Equal(d.Value, output) || err != nil {
|
2014-09-01 18:21:57 +00:00
|
|
|
t.Fatalf("bad output: %#v", d)
|
|
|
|
}
|
|
|
|
|
2017-05-21 07:11:09 +00:00
|
|
|
key = "_rexec/" + event.Session + "/" + a.Config.NodeName + "/exit"
|
2019-07-16 20:47:45 +00:00
|
|
|
d, err = getKV(a.Agent, key, token)
|
|
|
|
if d == nil || d.Session != event.Session || string(d.Value) != "1" || err != nil {
|
2014-09-01 18:21:57 +00:00
|
|
|
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) {
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, "")
|
2017-05-21 07:11:09 +00:00
|
|
|
defer a.Shutdown()
|
2018-09-10 15:58:53 +00:00
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
2018-08-06 23:46:09 +00:00
|
|
|
retry.Run(t, func(r *retry.R) {
|
|
|
|
event := &remoteExecEvent{
|
|
|
|
Prefix: "_rexec",
|
|
|
|
Session: makeRexecSession(t, a.Agent, ""),
|
|
|
|
}
|
|
|
|
defer destroySession(t, a.Agent, event.Session, "")
|
2014-09-01 18:21:57 +00:00
|
|
|
|
2018-08-06 23:46:09 +00:00
|
|
|
spec := &remoteExecSpec{
|
|
|
|
Command: command,
|
|
|
|
Wait: time.Second,
|
|
|
|
}
|
|
|
|
buf, err := json.Marshal(spec)
|
|
|
|
if err != nil {
|
2019-07-16 20:47:45 +00:00
|
|
|
r.Fatalf("err: %v", err)
|
2018-08-06 23:46:09 +00:00
|
|
|
}
|
|
|
|
key := "_rexec/" + event.Session + "/job"
|
2019-07-16 20:47:45 +00:00
|
|
|
if err := setKV(a.Agent, key, buf, ""); err != nil {
|
|
|
|
r.Fatalf("err: %v", err)
|
|
|
|
}
|
2014-09-01 18:21:57 +00:00
|
|
|
|
2018-08-06 23:46:09 +00:00
|
|
|
buf, err = json.Marshal(event)
|
|
|
|
if err != nil {
|
2019-07-16 20:47:45 +00:00
|
|
|
r.Fatalf("err: %v", err)
|
2018-08-06 23:46:09 +00:00
|
|
|
}
|
|
|
|
msg := &UserEvent{
|
|
|
|
ID: generateUUID(),
|
|
|
|
Payload: buf,
|
|
|
|
}
|
2014-09-01 18:21:57 +00:00
|
|
|
|
2018-08-06 23:46:09 +00:00
|
|
|
// Handle the event...
|
|
|
|
a.handleRemoteExec(msg)
|
2014-09-01 18:21:57 +00:00
|
|
|
|
2018-08-06 23:46:09 +00:00
|
|
|
// Verify we have an ack
|
|
|
|
key = "_rexec/" + event.Session + "/" + a.Config.NodeName + "/ack"
|
2019-07-16 20:47:45 +00:00
|
|
|
d, err := getKV(a.Agent, key, "")
|
|
|
|
if d == nil || d.Session != event.Session || err != nil {
|
|
|
|
r.Fatalf("bad ack: %#v", d)
|
2018-08-06 23:46:09 +00:00
|
|
|
}
|
2014-09-01 18:21:57 +00:00
|
|
|
|
2018-08-06 23:46:09 +00:00
|
|
|
// Verify we have output
|
|
|
|
key = "_rexec/" + event.Session + "/" + a.Config.NodeName + "/out/00000"
|
2019-07-16 20:47:45 +00:00
|
|
|
d, err = getKV(a.Agent, key, "")
|
2018-08-06 23:46:09 +00:00
|
|
|
if d == nil || d.Session != event.Session ||
|
2019-07-16 20:47:45 +00:00
|
|
|
!bytes.Contains(d.Value, []byte(expectedSubstring)) || err != nil {
|
|
|
|
r.Fatalf("bad output: %#v", d)
|
2018-08-06 23:46:09 +00:00
|
|
|
}
|
2014-09-01 18:21:57 +00:00
|
|
|
|
2018-08-06 23:46:09 +00:00
|
|
|
// Verify we have an exit code
|
|
|
|
key = "_rexec/" + event.Session + "/" + a.Config.NodeName + "/exit"
|
2019-07-16 20:47:45 +00:00
|
|
|
d, err = getKV(a.Agent, key, "")
|
|
|
|
if d == nil || d.Session != event.Session || string(d.Value) != expectedReturnCode || err != nil {
|
|
|
|
r.Fatalf("bad output: %#v", d)
|
2018-08-06 23:46:09 +00:00
|
|
|
}
|
|
|
|
})
|
2014-09-01 18:21:57 +00:00
|
|
|
}
|
|
|
|
|
2014-09-17 19:33:03 +00:00
|
|
|
func TestHandleRemoteExec(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
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) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
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
|
2022-12-14 15:24:22 +00:00
|
|
|
if err := a.RPC(context.Background(), "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
|
2022-12-14 15:24:22 +00:00
|
|
|
if err := a.RPC(context.Background(), "Session.Apply", &args, &out); err != nil {
|
2014-09-01 18:21:57 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-16 20:47:45 +00:00
|
|
|
func setKV(a *Agent, key string, val []byte, token string) error {
|
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
|
2022-12-14 15:24:22 +00:00
|
|
|
if err := a.RPC(context.Background(), "KVS.Apply", &write, &success); err != nil {
|
2019-07-16 20:47:45 +00:00
|
|
|
return err
|
2014-09-01 18:21:57 +00:00
|
|
|
}
|
2019-07-16 20:47:45 +00:00
|
|
|
return nil
|
2014-09-01 18:21:57 +00:00
|
|
|
}
|
|
|
|
|
2019-07-16 20:47:45 +00:00
|
|
|
func getKV(a *Agent, key string, token string) (*structs.DirEntry, error) {
|
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
|
2022-12-14 15:24:22 +00:00
|
|
|
if err := a.RPC(context.Background(), "KVS.Get", &req, &out); err != nil {
|
2019-07-16 20:47:45 +00:00
|
|
|
return nil, err
|
2014-09-01 18:21:57 +00:00
|
|
|
}
|
|
|
|
if len(out.Entries) > 0 {
|
2019-07-16 20:47:45 +00:00
|
|
|
return out.Entries[0], nil
|
2014-09-01 18:21:57 +00:00
|
|
|
}
|
2019-07-16 20:47:45 +00:00
|
|
|
return nil, nil
|
2014-09-01 18:21:57 +00:00
|
|
|
}
|