2014-05-19 18:29:50 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2022-12-14 15:24:22 +00:00
|
|
|
"context"
|
2014-05-19 18:29:50 +00:00
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2019-11-25 17:07:04 +00:00
|
|
|
"reflect"
|
2014-05-19 18:29:50 +00:00
|
|
|
"testing"
|
2014-05-19 20:12:15 +00:00
|
|
|
"time"
|
2015-03-27 05:30:04 +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"
|
2018-08-06 23:46:09 +00:00
|
|
|
"github.com/hashicorp/consul/testrpc"
|
2016-06-07 20:24:51 +00:00
|
|
|
"github.com/hashicorp/consul/types"
|
2020-05-14 18:41:20 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2014-05-19 18:29:50 +00:00
|
|
|
)
|
|
|
|
|
2019-03-26 21:04:58 +00:00
|
|
|
func verifySession(t *testing.T, r *retry.R, a *TestAgent, want structs.Session) {
|
2019-11-25 17:07:04 +00:00
|
|
|
t.Helper()
|
|
|
|
|
2017-12-14 01:25:29 +00:00
|
|
|
args := &structs.SessionSpecificRequest{
|
|
|
|
Datacenter: "dc1",
|
2019-11-25 17:07:04 +00:00
|
|
|
SessionID: want.ID,
|
2017-12-14 01:25:29 +00:00
|
|
|
}
|
|
|
|
var out structs.IndexedSessions
|
2022-12-14 15:24:22 +00:00
|
|
|
if err := a.RPC(context.Background(), "Session.Get", args, &out); err != nil {
|
2018-08-14 16:08:33 +00:00
|
|
|
r.Fatalf("err: %v", err)
|
2017-12-14 01:25:29 +00:00
|
|
|
}
|
|
|
|
if len(out.Sessions) != 1 {
|
2018-08-14 16:08:33 +00:00
|
|
|
r.Fatalf("bad: %#v", out.Sessions)
|
2017-12-14 01:25:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make a copy so we don't modify the state store copy for an in-mem
|
|
|
|
// RPC and zero out the Raft info for the compare.
|
|
|
|
got := *(out.Sessions[0])
|
|
|
|
got.CreateIndex = 0
|
|
|
|
got.ModifyIndex = 0
|
2019-11-25 17:07:04 +00:00
|
|
|
|
|
|
|
if got.ID != want.ID {
|
|
|
|
t.Fatalf("bad session ID: expected %s, got %s", want.ID, got.ID)
|
|
|
|
}
|
|
|
|
if got.Node != want.Node {
|
|
|
|
t.Fatalf("bad session Node: expected %s, got %s", want.Node, got.Node)
|
|
|
|
}
|
|
|
|
if got.Behavior != want.Behavior {
|
|
|
|
t.Fatalf("bad session Behavior: expected %s, got %s", want.Behavior, got.Behavior)
|
|
|
|
}
|
|
|
|
if got.LockDelay != want.LockDelay {
|
|
|
|
t.Fatalf("bad session LockDelay: expected %s, got %s", want.LockDelay, got.LockDelay)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(got.Checks, want.Checks) {
|
|
|
|
t.Fatalf("bad session Checks: expected %+v, got %+v", want.Checks, got.Checks)
|
|
|
|
}
|
2019-12-10 02:26:41 +00:00
|
|
|
if !reflect.DeepEqual(got.NodeChecks, want.NodeChecks) {
|
|
|
|
t.Fatalf("bad session NodeChecks: expected %+v, got %+v", want.NodeChecks, got.NodeChecks)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(got.ServiceChecks, want.ServiceChecks) {
|
|
|
|
t.Fatalf("bad session ServiceChecks: expected %+v, got %+v", want.ServiceChecks, got.ServiceChecks)
|
|
|
|
}
|
2017-12-14 01:25:29 +00:00
|
|
|
}
|
|
|
|
|
2014-05-19 18:29:50 +00:00
|
|
|
func TestSessionCreate(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()
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, "")
|
2017-05-21 18:31:20 +00:00
|
|
|
defer a.Shutdown()
|
2018-08-14 16:08:33 +00:00
|
|
|
|
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
2017-05-21 18:31:20 +00:00
|
|
|
|
|
|
|
// Create a health check
|
|
|
|
args := &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
2017-05-21 18:32:48 +00:00
|
|
|
Node: a.Config.NodeName,
|
2017-05-21 18:31:20 +00:00
|
|
|
Address: "127.0.0.1",
|
|
|
|
Check: &structs.HealthCheck{
|
|
|
|
CheckID: "consul",
|
2017-05-21 18:32:48 +00:00
|
|
|
Node: a.Config.NodeName,
|
2017-05-21 18:31:20 +00:00
|
|
|
Name: "consul",
|
|
|
|
ServiceID: "consul",
|
|
|
|
Status: api.HealthPassing,
|
|
|
|
},
|
|
|
|
}
|
2014-05-19 18:29:50 +00:00
|
|
|
|
2018-08-14 16:08:33 +00:00
|
|
|
retry.Run(t, func(r *retry.R) {
|
|
|
|
var out struct{}
|
2022-12-14 15:24:22 +00:00
|
|
|
if err := a.RPC(context.Background(), "Catalog.Register", args, &out); err != nil {
|
2018-08-14 16:08:33 +00:00
|
|
|
r.Fatalf("err: %v", err)
|
|
|
|
}
|
2014-05-19 18:29:50 +00:00
|
|
|
|
2018-08-14 16:08:33 +00:00
|
|
|
// Associate session with node and 2 health checks
|
|
|
|
body := bytes.NewBuffer(nil)
|
|
|
|
enc := json.NewEncoder(body)
|
|
|
|
raw := map[string]interface{}{
|
|
|
|
"Name": "my-cool-session",
|
|
|
|
"Node": a.Config.NodeName,
|
2019-12-10 02:26:41 +00:00
|
|
|
"Checks": []types.CheckID{"consul"},
|
2018-08-14 16:08:33 +00:00
|
|
|
"LockDelay": "20s",
|
|
|
|
}
|
2020-08-13 22:39:58 +00:00
|
|
|
require.NoError(r, enc.Encode(raw))
|
2014-05-19 18:29:50 +00:00
|
|
|
|
2018-08-14 16:08:33 +00:00
|
|
|
req, _ := http.NewRequest("PUT", "/v1/session/create", body)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := a.srv.SessionCreate(resp, req)
|
|
|
|
if err != nil {
|
|
|
|
r.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
want := structs.Session{
|
2019-12-10 02:26:41 +00:00
|
|
|
ID: obj.(sessionCreateResponse).ID,
|
|
|
|
Name: "my-cool-session",
|
|
|
|
Node: a.Config.NodeName,
|
|
|
|
Checks: []types.CheckID{"consul"},
|
|
|
|
NodeChecks: []string{string(structs.SerfCheckID)},
|
|
|
|
LockDelay: 20 * time.Second,
|
|
|
|
Behavior: structs.SessionKeysRelease,
|
2018-08-14 16:08:33 +00:00
|
|
|
}
|
2019-03-26 21:04:58 +00:00
|
|
|
verifySession(t, r, a, want)
|
2018-08-14 16:08:33 +00:00
|
|
|
})
|
2014-05-19 18:29:50 +00:00
|
|
|
}
|
|
|
|
|
2019-12-10 02:26:41 +00:00
|
|
|
func TestSessionCreate_NodeChecks(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()
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, "")
|
2017-05-21 18:31:20 +00:00
|
|
|
defer a.Shutdown()
|
2019-12-10 02:26:41 +00:00
|
|
|
|
2018-08-14 16:08:33 +00:00
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
2017-05-21 18:31:20 +00:00
|
|
|
|
|
|
|
// Create a health check
|
|
|
|
args := &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
2017-05-21 18:32:48 +00:00
|
|
|
Node: a.Config.NodeName,
|
2017-05-21 18:31:20 +00:00
|
|
|
Address: "127.0.0.1",
|
|
|
|
Check: &structs.HealthCheck{
|
|
|
|
CheckID: "consul",
|
2017-05-21 18:32:48 +00:00
|
|
|
Node: a.Config.NodeName,
|
2017-05-21 18:31:20 +00:00
|
|
|
Name: "consul",
|
|
|
|
ServiceID: "consul",
|
|
|
|
Status: api.HealthPassing,
|
|
|
|
},
|
|
|
|
}
|
2019-12-10 02:26:41 +00:00
|
|
|
|
2018-08-06 23:46:09 +00:00
|
|
|
retry.Run(t, func(r *retry.R) {
|
|
|
|
var out struct{}
|
2022-12-14 15:24:22 +00:00
|
|
|
if err := a.RPC(context.Background(), "Catalog.Register", args, &out); err != nil {
|
2018-08-14 16:08:33 +00:00
|
|
|
r.Fatalf("err: %v", err)
|
2018-08-06 23:46:09 +00:00
|
|
|
}
|
2014-11-20 02:11:41 +00:00
|
|
|
|
2019-12-10 02:26:41 +00:00
|
|
|
// Associate session with node and 2 health checks
|
2018-08-06 23:46:09 +00:00
|
|
|
body := bytes.NewBuffer(nil)
|
|
|
|
enc := json.NewEncoder(body)
|
|
|
|
raw := map[string]interface{}{
|
2019-12-10 02:26:41 +00:00
|
|
|
"Name": "my-cool-session",
|
|
|
|
"Node": a.Config.NodeName,
|
|
|
|
"ServiceChecks": []structs.ServiceCheck{
|
|
|
|
{ID: "consul", Namespace: ""},
|
|
|
|
},
|
|
|
|
"NodeChecks": []types.CheckID{structs.SerfCheckID},
|
|
|
|
"LockDelay": "20s",
|
2018-08-06 23:46:09 +00:00
|
|
|
}
|
2020-08-13 22:39:58 +00:00
|
|
|
require.NoError(r, enc.Encode(raw))
|
2014-11-20 02:11:41 +00:00
|
|
|
|
2018-08-06 23:46:09 +00:00
|
|
|
req, _ := http.NewRequest("PUT", "/v1/session/create", body)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := a.srv.SessionCreate(resp, req)
|
|
|
|
if err != nil {
|
2018-08-14 16:08:33 +00:00
|
|
|
r.Fatalf("err: %v", err)
|
2018-08-06 23:46:09 +00:00
|
|
|
}
|
2014-11-20 02:11:41 +00:00
|
|
|
|
2018-08-06 23:46:09 +00:00
|
|
|
want := structs.Session{
|
2019-12-10 02:26:41 +00:00
|
|
|
ID: obj.(sessionCreateResponse).ID,
|
|
|
|
Name: "my-cool-session",
|
|
|
|
Node: a.Config.NodeName,
|
|
|
|
NodeChecks: []string{string(structs.SerfCheckID)},
|
|
|
|
ServiceChecks: []structs.ServiceCheck{{ID: "consul", Namespace: ""}},
|
|
|
|
LockDelay: 20 * time.Second,
|
|
|
|
Behavior: structs.SessionKeysRelease,
|
2018-08-06 23:46:09 +00:00
|
|
|
}
|
2019-03-26 21:04:58 +00:00
|
|
|
verifySession(t, r, a, want)
|
2018-08-06 23:46:09 +00:00
|
|
|
})
|
2017-12-14 01:25:29 +00:00
|
|
|
}
|
|
|
|
|
2019-12-10 02:26:41 +00:00
|
|
|
func TestSessionCreate_Delete(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2017-12-14 01:25:29 +00:00
|
|
|
t.Parallel()
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, "")
|
2017-12-14 01:25:29 +00:00
|
|
|
defer a.Shutdown()
|
2018-08-14 16:08:33 +00:00
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
2017-12-14 01:25:29 +00:00
|
|
|
|
2019-12-10 02:26:41 +00:00
|
|
|
// Create a health check
|
|
|
|
args := &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: a.Config.NodeName,
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
Check: &structs.HealthCheck{
|
|
|
|
CheckID: "consul",
|
|
|
|
Node: a.Config.NodeName,
|
|
|
|
Name: "consul",
|
|
|
|
ServiceID: "consul",
|
|
|
|
Status: api.HealthPassing,
|
|
|
|
},
|
2017-12-14 01:25:29 +00:00
|
|
|
}
|
2018-08-06 23:46:09 +00:00
|
|
|
retry.Run(t, func(r *retry.R) {
|
2019-12-10 02:26:41 +00:00
|
|
|
var out struct{}
|
2022-12-14 15:24:22 +00:00
|
|
|
if err := a.RPC(context.Background(), "Catalog.Register", args, &out); err != nil {
|
2019-12-10 02:26:41 +00:00
|
|
|
r.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Associate session with node and 2 health checks, and make it delete on session destroy
|
|
|
|
body := bytes.NewBuffer(nil)
|
|
|
|
enc := json.NewEncoder(body)
|
|
|
|
raw := map[string]interface{}{
|
|
|
|
"Name": "my-cool-session",
|
|
|
|
"Node": a.Config.NodeName,
|
|
|
|
"Checks": []types.CheckID{"consul"},
|
|
|
|
"NodeChecks": []string{string(structs.SerfCheckID)},
|
|
|
|
"LockDelay": "20s",
|
|
|
|
"Behavior": structs.SessionKeysDelete,
|
|
|
|
}
|
2020-08-13 22:39:58 +00:00
|
|
|
require.NoError(r, enc.Encode(raw))
|
2019-12-10 02:26:41 +00:00
|
|
|
|
|
|
|
req, _ := http.NewRequest("PUT", "/v1/session/create", body)
|
|
|
|
resp := httptest.NewRecorder()
|
2018-08-06 23:46:09 +00:00
|
|
|
obj, err := a.srv.SessionCreate(resp, req)
|
|
|
|
if err != nil {
|
2018-08-14 16:08:33 +00:00
|
|
|
r.Fatalf("err: %v", err)
|
2018-08-06 23:46:09 +00:00
|
|
|
}
|
2017-12-14 01:25:29 +00:00
|
|
|
|
2018-08-06 23:46:09 +00:00
|
|
|
want := structs.Session{
|
2019-12-10 02:26:41 +00:00
|
|
|
ID: obj.(sessionCreateResponse).ID,
|
|
|
|
Name: "my-cool-session",
|
|
|
|
Node: a.Config.NodeName,
|
|
|
|
Checks: []types.CheckID{"consul"},
|
|
|
|
NodeChecks: []string{string(structs.SerfCheckID)},
|
|
|
|
LockDelay: 20 * time.Second,
|
|
|
|
Behavior: structs.SessionKeysDelete,
|
2018-08-06 23:46:09 +00:00
|
|
|
}
|
2019-03-26 21:04:58 +00:00
|
|
|
verifySession(t, r, a, want)
|
2018-08-06 23:46:09 +00:00
|
|
|
})
|
2017-12-14 01:25:29 +00:00
|
|
|
}
|
|
|
|
|
2019-12-10 02:26:41 +00:00
|
|
|
func TestSessionCreate_DefaultCheck(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2017-12-14 01:25:29 +00:00
|
|
|
t.Parallel()
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, "")
|
2017-12-14 01:25:29 +00:00
|
|
|
defer a.Shutdown()
|
2019-12-10 02:26:41 +00:00
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
2017-12-14 01:25:29 +00:00
|
|
|
|
|
|
|
raw := map[string]interface{}{
|
|
|
|
"Name": "my-cool-session",
|
|
|
|
"Node": a.Config.NodeName,
|
|
|
|
"LockDelay": "20s",
|
|
|
|
}
|
|
|
|
|
2018-08-06 23:46:09 +00:00
|
|
|
retry.Run(t, func(r *retry.R) {
|
2020-08-13 22:39:58 +00:00
|
|
|
body := bytes.NewBuffer(nil)
|
|
|
|
enc := json.NewEncoder(body)
|
|
|
|
require.NoError(r, enc.Encode(raw))
|
|
|
|
req, _ := http.NewRequest("PUT", "/v1/session/create", body)
|
|
|
|
resp := httptest.NewRecorder()
|
2018-08-06 23:46:09 +00:00
|
|
|
obj, err := a.srv.SessionCreate(resp, req)
|
2020-08-13 22:39:58 +00:00
|
|
|
require.NoError(r, err)
|
|
|
|
require.Equal(r, resp.Code, http.StatusOK)
|
2017-12-14 01:25:29 +00:00
|
|
|
|
2018-08-06 23:46:09 +00:00
|
|
|
want := structs.Session{
|
2019-12-10 02:26:41 +00:00
|
|
|
ID: obj.(sessionCreateResponse).ID,
|
|
|
|
Name: "my-cool-session",
|
|
|
|
Node: a.Config.NodeName,
|
|
|
|
NodeChecks: []string{string(structs.SerfCheckID)},
|
|
|
|
LockDelay: 20 * time.Second,
|
|
|
|
Behavior: structs.SessionKeysRelease,
|
2018-08-06 23:46:09 +00:00
|
|
|
}
|
2019-03-26 21:04:58 +00:00
|
|
|
verifySession(t, r, a, want)
|
2018-08-06 23:46:09 +00:00
|
|
|
})
|
2014-11-20 02:11:41 +00:00
|
|
|
}
|
|
|
|
|
2019-12-10 02:26:41 +00:00
|
|
|
func TestSessionCreate_NoCheck(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2019-12-10 02:26:41 +00:00
|
|
|
t.Parallel()
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, "")
|
2019-12-10 02:26:41 +00:00
|
|
|
defer a.Shutdown()
|
|
|
|
|
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
|
|
|
|
2020-08-13 22:39:58 +00:00
|
|
|
raw := map[string]interface{}{
|
|
|
|
"Name": "my-cool-session",
|
|
|
|
"Node": a.Config.NodeName,
|
|
|
|
"LockDelay": "20s",
|
|
|
|
}
|
2019-12-10 02:26:41 +00:00
|
|
|
|
2020-08-13 22:39:58 +00:00
|
|
|
t.Run("no check fields should yield default serfHealth", func(t *testing.T) {
|
2019-12-10 02:26:41 +00:00
|
|
|
retry.Run(t, func(r *retry.R) {
|
2020-08-13 22:39:58 +00:00
|
|
|
body := bytes.NewBuffer(nil)
|
|
|
|
enc := json.NewEncoder(body)
|
|
|
|
require.NoError(r, enc.Encode(raw))
|
|
|
|
|
|
|
|
req, _ := http.NewRequest("PUT", "/v1/session/create", body)
|
|
|
|
resp := httptest.NewRecorder()
|
2019-12-10 02:26:41 +00:00
|
|
|
obj, err := a.srv.SessionCreate(resp, req)
|
2020-08-13 22:39:58 +00:00
|
|
|
require.NoError(r, err)
|
|
|
|
require.Equal(r, resp.Code, http.StatusOK, resp.Body.String())
|
2019-12-10 02:26:41 +00:00
|
|
|
|
|
|
|
want := structs.Session{
|
|
|
|
ID: obj.(sessionCreateResponse).ID,
|
|
|
|
Name: "my-cool-session",
|
|
|
|
Node: a.Config.NodeName,
|
|
|
|
NodeChecks: []string{string(structs.SerfCheckID)},
|
|
|
|
LockDelay: 20 * time.Second,
|
|
|
|
Behavior: structs.SessionKeysRelease,
|
|
|
|
}
|
|
|
|
verifySession(t, r, a, want)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("overwrite nodechecks to associate with no checks", func(t *testing.T) {
|
|
|
|
raw := map[string]interface{}{
|
|
|
|
"Name": "my-cool-session",
|
|
|
|
"Node": a.Config.NodeName,
|
|
|
|
"NodeChecks": []string{},
|
|
|
|
"LockDelay": "20s",
|
|
|
|
}
|
|
|
|
|
|
|
|
retry.Run(t, func(r *retry.R) {
|
2020-08-13 22:39:58 +00:00
|
|
|
body := bytes.NewBuffer(nil)
|
|
|
|
enc := json.NewEncoder(body)
|
|
|
|
require.NoError(r, enc.Encode(raw))
|
|
|
|
req, _ := http.NewRequest("PUT", "/v1/session/create", body)
|
|
|
|
resp := httptest.NewRecorder()
|
2019-12-10 02:26:41 +00:00
|
|
|
obj, err := a.srv.SessionCreate(resp, req)
|
2020-08-13 22:39:58 +00:00
|
|
|
require.NoError(r, err)
|
|
|
|
require.Equal(r, resp.Code, http.StatusOK)
|
2019-12-10 02:26:41 +00:00
|
|
|
|
|
|
|
want := structs.Session{
|
|
|
|
ID: obj.(sessionCreateResponse).ID,
|
|
|
|
Name: "my-cool-session",
|
|
|
|
Node: a.Config.NodeName,
|
|
|
|
NodeChecks: []string{},
|
|
|
|
LockDelay: 20 * time.Second,
|
|
|
|
Behavior: structs.SessionKeysRelease,
|
|
|
|
}
|
|
|
|
verifySession(t, r, a, want)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("overwrite checks to associate with no checks", func(t *testing.T) {
|
|
|
|
raw := map[string]interface{}{
|
|
|
|
"Name": "my-cool-session",
|
|
|
|
"Node": a.Config.NodeName,
|
|
|
|
"Checks": []string{},
|
|
|
|
"LockDelay": "20s",
|
|
|
|
}
|
|
|
|
|
|
|
|
retry.Run(t, func(r *retry.R) {
|
2020-08-13 22:39:58 +00:00
|
|
|
body := bytes.NewBuffer(nil)
|
|
|
|
enc := json.NewEncoder(body)
|
|
|
|
require.NoError(r, enc.Encode(raw))
|
|
|
|
|
|
|
|
req, _ := http.NewRequest("PUT", "/v1/session/create", body)
|
|
|
|
resp := httptest.NewRecorder()
|
2019-12-10 02:26:41 +00:00
|
|
|
obj, err := a.srv.SessionCreate(resp, req)
|
2020-08-13 22:39:58 +00:00
|
|
|
require.NoError(r, err)
|
|
|
|
require.Equal(r, resp.Code, http.StatusOK)
|
2019-12-10 02:26:41 +00:00
|
|
|
|
|
|
|
want := structs.Session{
|
|
|
|
ID: obj.(sessionCreateResponse).ID,
|
|
|
|
Name: "my-cool-session",
|
|
|
|
Node: a.Config.NodeName,
|
|
|
|
NodeChecks: []string{},
|
|
|
|
Checks: []types.CheckID{},
|
|
|
|
LockDelay: 20 * time.Second,
|
|
|
|
Behavior: structs.SessionKeysRelease,
|
|
|
|
}
|
|
|
|
verifySession(t, r, a, want)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-09-04 18:42:15 +00:00
|
|
|
func makeTestSession(t *testing.T, srv *HTTPHandlers) string {
|
2020-08-13 22:39:58 +00:00
|
|
|
t.Helper()
|
2019-11-25 17:07:04 +00:00
|
|
|
url := "/v1/session/create"
|
|
|
|
req, _ := http.NewRequest("PUT", url, nil)
|
2014-05-19 18:29:50 +00:00
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := srv.SessionCreate(resp, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
sessResp := obj.(sessionCreateResponse)
|
|
|
|
return sessResp.ID
|
|
|
|
}
|
|
|
|
|
2020-09-04 18:42:15 +00:00
|
|
|
func makeTestSessionDelete(t *testing.T, srv *HTTPHandlers) string {
|
2020-08-13 22:39:58 +00:00
|
|
|
t.Helper()
|
2014-11-20 02:11:41 +00:00
|
|
|
// Create Session with delete behavior
|
|
|
|
body := bytes.NewBuffer(nil)
|
|
|
|
enc := json.NewEncoder(body)
|
|
|
|
raw := map[string]interface{}{
|
|
|
|
"Behavior": "delete",
|
|
|
|
}
|
2020-08-13 22:39:58 +00:00
|
|
|
require.NoError(t, enc.Encode(raw))
|
2014-11-20 02:11:41 +00:00
|
|
|
|
2019-11-25 17:07:04 +00:00
|
|
|
url := "/v1/session/create"
|
|
|
|
req, _ := http.NewRequest("PUT", url, body)
|
2014-11-20 02:11:41 +00:00
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := srv.SessionCreate(resp, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
sessResp := obj.(sessionCreateResponse)
|
|
|
|
return sessResp.ID
|
|
|
|
}
|
|
|
|
|
2020-09-04 18:42:15 +00:00
|
|
|
func makeTestSessionTTL(t *testing.T, srv *HTTPHandlers, ttl string) string {
|
2020-08-13 22:39:58 +00:00
|
|
|
t.Helper()
|
2014-11-25 16:06:14 +00:00
|
|
|
// Create Session with TTL
|
|
|
|
body := bytes.NewBuffer(nil)
|
|
|
|
enc := json.NewEncoder(body)
|
|
|
|
raw := map[string]interface{}{
|
|
|
|
"TTL": ttl,
|
|
|
|
}
|
2020-08-13 22:39:58 +00:00
|
|
|
require.NoError(t, enc.Encode(raw))
|
2014-11-25 16:06:14 +00:00
|
|
|
|
2019-11-25 17:07:04 +00:00
|
|
|
url := "/v1/session/create"
|
|
|
|
req, _ := http.NewRequest("PUT", url, body)
|
2014-11-25 16:06:14 +00:00
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := srv.SessionCreate(resp, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
sessResp := obj.(sessionCreateResponse)
|
|
|
|
return sessResp.ID
|
|
|
|
}
|
|
|
|
|
2014-05-19 18:29:50 +00:00
|
|
|
func TestSessionDestroy(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()
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, "")
|
2017-05-21 18:31:20 +00:00
|
|
|
defer a.Shutdown()
|
2018-08-14 16:08:33 +00:00
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
2014-05-19 18:29:50 +00:00
|
|
|
|
2017-05-21 18:31:20 +00:00
|
|
|
id := makeTestSession(t, a.srv)
|
|
|
|
|
|
|
|
req, _ := http.NewRequest("PUT", "/v1/session/destroy/"+id, nil)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := a.srv.SessionDestroy(resp, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp := obj.(bool); !resp {
|
|
|
|
t.Fatalf("should work")
|
|
|
|
}
|
2014-05-19 18:29:50 +00:00
|
|
|
}
|
|
|
|
|
2017-04-27 09:46:38 +00:00
|
|
|
func TestSessionCustomTTL(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()
|
2017-04-27 09:46:38 +00:00
|
|
|
ttl := 250 * time.Millisecond
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, `
|
2017-09-25 18:40:42 +00:00
|
|
|
session_ttl_min = "250ms"
|
|
|
|
`)
|
2017-05-21 18:31:20 +00:00
|
|
|
defer a.Shutdown()
|
2018-08-14 16:08:33 +00:00
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
|
|
|
|
2018-08-06 23:46:09 +00:00
|
|
|
retry.Run(t, func(r *retry.R) {
|
|
|
|
id := makeTestSessionTTL(t, a.srv, ttl.String())
|
2015-03-27 05:30:04 +00:00
|
|
|
|
2018-08-06 23:46:09 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/session/info/"+id, nil)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := a.srv.SessionGet(resp, req)
|
|
|
|
if err != nil {
|
2018-08-14 16:08:33 +00:00
|
|
|
r.Fatalf("err: %v", err)
|
2018-08-06 23:46:09 +00:00
|
|
|
}
|
|
|
|
respObj, ok := obj.(structs.Sessions)
|
|
|
|
if !ok {
|
2018-08-14 16:08:33 +00:00
|
|
|
r.Fatalf("should work")
|
2018-08-06 23:46:09 +00:00
|
|
|
}
|
|
|
|
if len(respObj) != 1 {
|
2018-08-14 16:08:33 +00:00
|
|
|
r.Fatalf("bad: %v", respObj)
|
2018-08-06 23:46:09 +00:00
|
|
|
}
|
|
|
|
if respObj[0].TTL != ttl.String() {
|
2018-08-14 16:08:33 +00:00
|
|
|
r.Fatalf("Incorrect TTL: %s", respObj[0].TTL)
|
2018-08-06 23:46:09 +00:00
|
|
|
}
|
2014-11-25 16:06:14 +00:00
|
|
|
|
2018-08-06 23:46:09 +00:00
|
|
|
time.Sleep(ttl*structs.SessionTTLMultiplier + ttl)
|
2014-11-25 16:06:14 +00:00
|
|
|
|
2018-08-06 23:46:09 +00:00
|
|
|
req, _ = http.NewRequest("GET", "/v1/session/info/"+id, nil)
|
|
|
|
resp = httptest.NewRecorder()
|
|
|
|
obj, err = a.srv.SessionGet(resp, req)
|
|
|
|
if err != nil {
|
2018-08-14 16:08:33 +00:00
|
|
|
r.Fatalf("err: %v", err)
|
2018-08-06 23:46:09 +00:00
|
|
|
}
|
|
|
|
respObj, ok = obj.(structs.Sessions)
|
2020-06-05 19:28:03 +00:00
|
|
|
require.True(r, ok, "unexpected type: %T", obj)
|
|
|
|
require.Len(r, respObj, 0)
|
2018-08-06 23:46:09 +00:00
|
|
|
})
|
2014-12-11 10:34:31 +00:00
|
|
|
}
|
|
|
|
|
2014-11-25 16:06:14 +00:00
|
|
|
func TestSessionTTLRenew(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
|
2017-04-27 09:46:38 +00:00
|
|
|
ttl := 250 * time.Millisecond
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, `
|
2017-09-25 18:40:42 +00:00
|
|
|
session_ttl_min = "250ms"
|
|
|
|
`)
|
2017-05-21 18:31:20 +00:00
|
|
|
defer a.Shutdown()
|
2018-08-14 16:08:33 +00:00
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
2014-11-25 16:06:14 +00:00
|
|
|
|
2017-05-21 18:31:20 +00:00
|
|
|
id := makeTestSessionTTL(t, a.srv, ttl.String())
|
|
|
|
|
|
|
|
req, _ := http.NewRequest("GET", "/v1/session/info/"+id, nil)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := a.srv.SessionGet(resp, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
respObj, ok := obj.(structs.Sessions)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("should work")
|
|
|
|
}
|
|
|
|
if len(respObj) != 1 {
|
|
|
|
t.Fatalf("bad: %v", respObj)
|
|
|
|
}
|
|
|
|
if respObj[0].TTL != ttl.String() {
|
|
|
|
t.Fatalf("Incorrect TTL: %s", respObj[0].TTL)
|
|
|
|
}
|
2014-11-25 16:06:14 +00:00
|
|
|
|
2017-05-21 18:31:20 +00:00
|
|
|
// Sleep to consume some time before renew
|
2019-04-17 20:52:23 +00:00
|
|
|
sleepFor := ttl * structs.SessionTTLMultiplier / 3
|
|
|
|
if sleepFor <= 0 {
|
|
|
|
t.Fatalf("timing tests need to sleep")
|
|
|
|
}
|
|
|
|
time.Sleep(sleepFor)
|
2014-11-25 16:06:14 +00:00
|
|
|
|
2017-05-21 18:31:20 +00:00
|
|
|
req, _ = http.NewRequest("PUT", "/v1/session/renew/"+id, nil)
|
|
|
|
resp = httptest.NewRecorder()
|
|
|
|
obj, err = a.srv.SessionRenew(resp, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2019-04-17 20:52:23 +00:00
|
|
|
if obj == nil {
|
|
|
|
t.Fatalf("session '%s' expired before renewal", id)
|
|
|
|
}
|
2017-05-21 18:31:20 +00:00
|
|
|
respObj, ok = obj.(structs.Sessions)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("should work")
|
|
|
|
}
|
|
|
|
if len(respObj) != 1 {
|
|
|
|
t.Fatalf("bad: %v", respObj)
|
|
|
|
}
|
2014-11-25 16:06:14 +00:00
|
|
|
|
2017-05-21 18:31:20 +00:00
|
|
|
// Sleep for ttl * TTL Multiplier
|
|
|
|
time.Sleep(ttl * structs.SessionTTLMultiplier)
|
2014-11-25 16:06:14 +00:00
|
|
|
|
2017-05-21 18:31:20 +00:00
|
|
|
req, _ = http.NewRequest("GET", "/v1/session/info/"+id, nil)
|
|
|
|
resp = httptest.NewRecorder()
|
|
|
|
obj, err = a.srv.SessionGet(resp, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
respObj, ok = obj.(structs.Sessions)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("session '%s' should have renewed", id)
|
|
|
|
}
|
|
|
|
if len(respObj) != 1 {
|
|
|
|
t.Fatalf("session '%s' should have renewed", id)
|
|
|
|
}
|
2014-11-25 16:06:14 +00:00
|
|
|
|
2017-05-21 18:31:20 +00:00
|
|
|
// now wait for timeout and expect session to get destroyed
|
|
|
|
time.Sleep(ttl * structs.SessionTTLMultiplier)
|
2014-11-25 16:06:14 +00:00
|
|
|
|
2017-05-21 18:31:20 +00:00
|
|
|
req, _ = http.NewRequest("GET", "/v1/session/info/"+id, nil)
|
|
|
|
resp = httptest.NewRecorder()
|
|
|
|
obj, err = a.srv.SessionGet(resp, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
respObj, ok = obj.(structs.Sessions)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("session '%s' should have destroyed", id)
|
|
|
|
}
|
|
|
|
if len(respObj) != 0 {
|
|
|
|
t.Fatalf("session '%s' should have destroyed", id)
|
|
|
|
}
|
2014-11-25 16:06:14 +00:00
|
|
|
}
|
|
|
|
|
2014-05-19 18:29:50 +00:00
|
|
|
func TestSessionGet(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()
|
2017-05-21 18:31:20 +00:00
|
|
|
t.Run("", func(t *testing.T) {
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, "")
|
2017-05-21 18:31:20 +00:00
|
|
|
defer a.Shutdown()
|
2018-08-14 16:08:33 +00:00
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
2017-05-21 18:31:20 +00:00
|
|
|
|
2018-08-06 23:46:09 +00:00
|
|
|
retry.Run(t, func(r *retry.R) {
|
2020-08-13 22:39:58 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/session/info/adf4238a-882b-9ddc-4a9d-5b6758e4159e", nil)
|
|
|
|
resp := httptest.NewRecorder()
|
2018-08-06 23:46:09 +00:00
|
|
|
obj, err := a.srv.SessionGet(resp, req)
|
|
|
|
if err != nil {
|
2018-08-14 16:08:33 +00:00
|
|
|
r.Fatalf("err: %v", err)
|
2018-08-06 23:46:09 +00:00
|
|
|
}
|
|
|
|
respObj, ok := obj.(structs.Sessions)
|
|
|
|
if !ok {
|
2018-08-14 16:08:33 +00:00
|
|
|
r.Fatalf("should work")
|
2018-08-06 23:46:09 +00:00
|
|
|
}
|
|
|
|
if respObj == nil || len(respObj) != 0 {
|
2018-08-14 16:08:33 +00:00
|
|
|
r.Fatalf("bad: %v", respObj)
|
2018-08-06 23:46:09 +00:00
|
|
|
}
|
|
|
|
})
|
2015-11-15 05:05:37 +00:00
|
|
|
})
|
|
|
|
|
2017-05-21 18:31:20 +00:00
|
|
|
t.Run("", func(t *testing.T) {
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, "")
|
2017-05-21 18:31:20 +00:00
|
|
|
defer a.Shutdown()
|
2018-08-14 16:08:33 +00:00
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
2017-05-21 18:31:20 +00:00
|
|
|
|
|
|
|
id := makeTestSession(t, a.srv)
|
2014-05-19 18:29:50 +00:00
|
|
|
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/session/info/"+id, nil)
|
2014-05-19 18:29:50 +00:00
|
|
|
resp := httptest.NewRecorder()
|
2017-05-21 18:31:20 +00:00
|
|
|
obj, err := a.srv.SessionGet(resp, req)
|
2014-05-19 18:29:50 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
respObj, ok := obj.(structs.Sessions)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("should work")
|
|
|
|
}
|
|
|
|
if len(respObj) != 1 {
|
|
|
|
t.Fatalf("bad: %v", respObj)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSessionList(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2017-05-21 18:31:20 +00:00
|
|
|
t.Run("", func(t *testing.T) {
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, "")
|
2017-05-21 18:31:20 +00:00
|
|
|
defer a.Shutdown()
|
2018-08-14 16:08:33 +00:00
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
2017-05-21 18:31:20 +00:00
|
|
|
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/session/list", nil)
|
2015-11-15 05:05:37 +00:00
|
|
|
resp := httptest.NewRecorder()
|
2017-05-21 18:31:20 +00:00
|
|
|
obj, err := a.srv.SessionList(resp, req)
|
2015-11-15 05:05:37 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
respObj, ok := obj.(structs.Sessions)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("should work")
|
|
|
|
}
|
|
|
|
if respObj == nil || len(respObj) != 0 {
|
|
|
|
t.Fatalf("bad: %v", respObj)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2017-05-21 18:31:20 +00:00
|
|
|
t.Run("", func(t *testing.T) {
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, "")
|
2017-05-21 18:31:20 +00:00
|
|
|
defer a.Shutdown()
|
2018-08-14 16:08:33 +00:00
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
2017-05-21 18:31:20 +00:00
|
|
|
|
2014-05-19 18:29:50 +00:00
|
|
|
var ids []string
|
|
|
|
for i := 0; i < 10; i++ {
|
2017-05-21 18:31:20 +00:00
|
|
|
ids = append(ids, makeTestSession(t, a.srv))
|
2014-05-19 18:29:50 +00:00
|
|
|
}
|
|
|
|
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/session/list", nil)
|
2014-05-19 18:29:50 +00:00
|
|
|
resp := httptest.NewRecorder()
|
2017-05-21 18:31:20 +00:00
|
|
|
obj, err := a.srv.SessionList(resp, req)
|
2014-05-19 18:29:50 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
respObj, ok := obj.(structs.Sessions)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("should work")
|
|
|
|
}
|
2020-05-14 21:02:52 +00:00
|
|
|
respIDs := make([]string, 0, len(respObj))
|
|
|
|
for _, obj := range respObj {
|
|
|
|
respIDs = append(respIDs, obj.ID)
|
2014-05-19 18:29:50 +00:00
|
|
|
}
|
2020-05-14 21:02:52 +00:00
|
|
|
require.ElementsMatch(t, respIDs, ids)
|
2014-05-19 18:29:50 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSessionsForNode(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()
|
2017-05-21 18:31:20 +00:00
|
|
|
t.Run("", func(t *testing.T) {
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, "")
|
2017-05-21 18:31:20 +00:00
|
|
|
defer a.Shutdown()
|
2018-08-14 16:08:33 +00:00
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
2017-05-21 18:31:20 +00:00
|
|
|
|
|
|
|
req, _ := http.NewRequest("GET", "/v1/session/node/"+a.Config.NodeName, nil)
|
2015-11-15 05:05:37 +00:00
|
|
|
resp := httptest.NewRecorder()
|
2017-05-21 18:31:20 +00:00
|
|
|
obj, err := a.srv.SessionsForNode(resp, req)
|
2015-11-15 05:05:37 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
respObj, ok := obj.(structs.Sessions)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("should work")
|
|
|
|
}
|
|
|
|
if respObj == nil || len(respObj) != 0 {
|
|
|
|
t.Fatalf("bad: %v", respObj)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2017-05-21 18:31:20 +00:00
|
|
|
t.Run("", func(t *testing.T) {
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, "")
|
2017-05-21 18:31:20 +00:00
|
|
|
defer a.Shutdown()
|
2018-08-14 16:08:33 +00:00
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
2017-05-21 18:31:20 +00:00
|
|
|
|
2014-05-19 18:29:50 +00:00
|
|
|
var ids []string
|
|
|
|
for i := 0; i < 10; i++ {
|
2017-05-21 18:31:20 +00:00
|
|
|
ids = append(ids, makeTestSession(t, a.srv))
|
2014-05-19 18:29:50 +00:00
|
|
|
}
|
|
|
|
|
2017-05-21 18:31:20 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/session/node/"+a.Config.NodeName, nil)
|
2014-05-19 18:29:50 +00:00
|
|
|
resp := httptest.NewRecorder()
|
2017-05-21 18:31:20 +00:00
|
|
|
obj, err := a.srv.SessionsForNode(resp, req)
|
2014-05-19 18:29:50 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
respObj, ok := obj.(structs.Sessions)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("should work")
|
|
|
|
}
|
2020-05-14 18:41:20 +00:00
|
|
|
respIDs := make([]string, 0, len(ids))
|
|
|
|
for _, session := range respObj {
|
|
|
|
respIDs = append(respIDs, session.ID)
|
2014-05-19 18:29:50 +00:00
|
|
|
}
|
2020-05-14 18:41:20 +00:00
|
|
|
require.ElementsMatch(t, ids, respIDs)
|
2014-05-19 18:29:50 +00:00
|
|
|
})
|
|
|
|
}
|
2014-11-20 02:11:41 +00:00
|
|
|
|
|
|
|
func TestSessionDeleteDestroy(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()
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, "")
|
2017-05-21 18:31:20 +00:00
|
|
|
defer a.Shutdown()
|
2018-08-14 16:08:33 +00:00
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
2014-11-20 02:11:41 +00:00
|
|
|
|
2017-05-21 18:31:20 +00:00
|
|
|
id := makeTestSessionDelete(t, a.srv)
|
2014-11-20 02:11:41 +00:00
|
|
|
|
2017-05-21 18:31:20 +00:00
|
|
|
// now create a new key for the session and acquire it
|
|
|
|
buf := bytes.NewBuffer([]byte("test"))
|
|
|
|
req, _ := http.NewRequest("PUT", "/v1/kv/ephemeral?acquire="+id, buf)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := a.srv.KVSEndpoint(resp, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2014-11-20 02:11:41 +00:00
|
|
|
|
2017-05-21 18:31:20 +00:00
|
|
|
if res := obj.(bool); !res {
|
|
|
|
t.Fatalf("should work")
|
|
|
|
}
|
2014-11-20 02:11:41 +00:00
|
|
|
|
2017-05-21 18:31:20 +00:00
|
|
|
// now destroy the session, this should delete the key created above
|
|
|
|
req, _ = http.NewRequest("PUT", "/v1/session/destroy/"+id, nil)
|
|
|
|
resp = httptest.NewRecorder()
|
|
|
|
obj, err = a.srv.SessionDestroy(resp, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp := obj.(bool); !resp {
|
|
|
|
t.Fatalf("should work")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that the key is gone
|
|
|
|
req, _ = http.NewRequest("GET", "/v1/kv/ephemeral", nil)
|
|
|
|
resp = httptest.NewRecorder()
|
|
|
|
obj, _ = a.srv.KVSEndpoint(resp, req)
|
|
|
|
res, found := obj.(structs.DirEntries)
|
|
|
|
if found || len(res) != 0 {
|
|
|
|
t.Fatalf("bad: %v found, should be nothing", res)
|
|
|
|
}
|
2014-11-20 02:11:41 +00:00
|
|
|
}
|