2014-05-19 18:29:50 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"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"
|
2018-08-06 23:46:09 +00:00
|
|
|
"github.com/hashicorp/consul/testrpc"
|
|
|
|
"github.com/hashicorp/consul/testutil/retry"
|
2016-06-07 20:24:51 +00:00
|
|
|
"github.com/hashicorp/consul/types"
|
2017-12-14 01:25:29 +00:00
|
|
|
"github.com/pascaldekloe/goe/verify"
|
2014-05-19 18:29:50 +00:00
|
|
|
)
|
|
|
|
|
2018-08-14 16:08:33 +00:00
|
|
|
func verifySession(r *retry.R, a *TestAgent, want structs.Session) {
|
2017-12-14 01:25:29 +00:00
|
|
|
args := &structs.SessionSpecificRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Session: want.ID,
|
|
|
|
}
|
|
|
|
var out structs.IndexedSessions
|
|
|
|
if err := a.RPC("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
|
2018-08-14 16:08:33 +00:00
|
|
|
verify.Values(r, "", got, want)
|
2017-12-14 01:25:29 +00:00
|
|
|
}
|
|
|
|
|
2014-05-19 18:29:50 +00:00
|
|
|
func TestSessionCreate(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-09-25 18:40:42 +00:00
|
|
|
a := NewTestAgent(t.Name(), "")
|
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{}
|
|
|
|
if err := a.RPC("Catalog.Register", args, &out); err != nil {
|
|
|
|
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,
|
|
|
|
"Checks": []types.CheckID{structs.SerfCheckID, "consul"},
|
|
|
|
"LockDelay": "20s",
|
|
|
|
}
|
|
|
|
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{
|
|
|
|
ID: obj.(sessionCreateResponse).ID,
|
|
|
|
Name: "my-cool-session",
|
|
|
|
Node: a.Config.NodeName,
|
|
|
|
Checks: []types.CheckID{structs.SerfCheckID, "consul"},
|
|
|
|
LockDelay: 20 * time.Second,
|
|
|
|
Behavior: structs.SessionKeysRelease,
|
|
|
|
}
|
|
|
|
verifySession(r, a, want)
|
|
|
|
})
|
2014-05-19 18:29:50 +00:00
|
|
|
}
|
|
|
|
|
2017-12-14 01:25:29 +00:00
|
|
|
func TestSessionCreate_Delete(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-09-25 18:40:42 +00:00
|
|
|
a := NewTestAgent(t.Name(), "")
|
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,
|
|
|
|
},
|
|
|
|
}
|
2018-08-06 23:46:09 +00:00
|
|
|
retry.Run(t, func(r *retry.R) {
|
|
|
|
var out struct{}
|
|
|
|
if err := a.RPC("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
|
|
|
|
2018-08-06 23:46:09 +00:00
|
|
|
// 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{structs.SerfCheckID, "consul"},
|
|
|
|
"LockDelay": "20s",
|
|
|
|
"Behavior": structs.SessionKeysDelete,
|
|
|
|
}
|
|
|
|
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{
|
|
|
|
ID: obj.(sessionCreateResponse).ID,
|
|
|
|
Name: "my-cool-session",
|
|
|
|
Node: a.Config.NodeName,
|
|
|
|
Checks: []types.CheckID{structs.SerfCheckID, "consul"},
|
|
|
|
LockDelay: 20 * time.Second,
|
|
|
|
Behavior: structs.SessionKeysDelete,
|
|
|
|
}
|
2018-08-14 16:08:33 +00:00
|
|
|
verifySession(r, a, want)
|
2018-08-06 23:46:09 +00:00
|
|
|
})
|
2017-12-14 01:25:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSessionCreate_DefaultCheck(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
a := NewTestAgent(t.Name(), "")
|
|
|
|
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
|
|
|
|
|
|
|
// 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,
|
|
|
|
"LockDelay": "20s",
|
|
|
|
}
|
|
|
|
enc.Encode(raw)
|
|
|
|
|
|
|
|
req, _ := http.NewRequest("PUT", "/v1/session/create", body)
|
|
|
|
resp := httptest.NewRecorder()
|
2018-08-06 23:46:09 +00:00
|
|
|
retry.Run(t, func(r *retry.R) {
|
|
|
|
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{
|
|
|
|
ID: obj.(sessionCreateResponse).ID,
|
|
|
|
Name: "my-cool-session",
|
|
|
|
Node: a.Config.NodeName,
|
|
|
|
Checks: []types.CheckID{structs.SerfCheckID},
|
|
|
|
LockDelay: 20 * time.Second,
|
|
|
|
Behavior: structs.SessionKeysRelease,
|
|
|
|
}
|
2018-08-14 16:08:33 +00:00
|
|
|
verifySession(r, a, want)
|
2018-08-06 23:46:09 +00:00
|
|
|
})
|
2017-12-14 01:25:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSessionCreate_NoCheck(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
a := NewTestAgent(t.Name(), "")
|
|
|
|
defer a.Shutdown()
|
2018-08-06 23:46:09 +00:00
|
|
|
testrpc.WaitForLeader(t, a.RPC, "dc1")
|
2017-12-14 01:25:29 +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,
|
|
|
|
"Checks": []types.CheckID{},
|
|
|
|
"LockDelay": "20s",
|
|
|
|
}
|
|
|
|
enc.Encode(raw)
|
|
|
|
|
|
|
|
req, _ := http.NewRequest("PUT", "/v1/session/create", body)
|
|
|
|
resp := httptest.NewRecorder()
|
2018-08-06 23:46:09 +00:00
|
|
|
retry.Run(t, func(r *retry.R) {
|
|
|
|
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{
|
|
|
|
ID: obj.(sessionCreateResponse).ID,
|
|
|
|
Name: "my-cool-session",
|
|
|
|
Node: a.Config.NodeName,
|
|
|
|
Checks: []types.CheckID{},
|
|
|
|
LockDelay: 20 * time.Second,
|
|
|
|
Behavior: structs.SessionKeysRelease,
|
|
|
|
}
|
2018-08-14 16:08:33 +00:00
|
|
|
verifySession(r, a, want)
|
2018-08-06 23:46:09 +00:00
|
|
|
})
|
2014-11-20 02:11:41 +00:00
|
|
|
}
|
|
|
|
|
2014-05-19 20:12:15 +00:00
|
|
|
func TestFixupLockDelay(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2014-05-19 20:12:15 +00:00
|
|
|
inp := map[string]interface{}{
|
|
|
|
"lockdelay": float64(15),
|
|
|
|
}
|
|
|
|
if err := FixupLockDelay(inp); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if inp["lockdelay"] != 15*time.Second {
|
|
|
|
t.Fatalf("bad: %v", inp)
|
|
|
|
}
|
|
|
|
|
|
|
|
inp = map[string]interface{}{
|
|
|
|
"lockDelay": float64(15 * time.Second),
|
|
|
|
}
|
|
|
|
if err := FixupLockDelay(inp); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if inp["lockDelay"] != 15*time.Second {
|
|
|
|
t.Fatalf("bad: %v", inp)
|
|
|
|
}
|
|
|
|
|
|
|
|
inp = map[string]interface{}{
|
|
|
|
"LockDelay": "15s",
|
|
|
|
}
|
|
|
|
if err := FixupLockDelay(inp); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if inp["LockDelay"] != 15*time.Second {
|
|
|
|
t.Fatalf("bad: %v", inp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-19 18:29:50 +00:00
|
|
|
func makeTestSession(t *testing.T, srv *HTTPServer) string {
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("PUT", "/v1/session/create", 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
|
|
|
|
}
|
|
|
|
|
2014-11-20 02:11:41 +00:00
|
|
|
func makeTestSessionDelete(t *testing.T, srv *HTTPServer) string {
|
|
|
|
// Create Session with delete behavior
|
|
|
|
body := bytes.NewBuffer(nil)
|
|
|
|
enc := json.NewEncoder(body)
|
|
|
|
raw := map[string]interface{}{
|
|
|
|
"Behavior": "delete",
|
|
|
|
}
|
|
|
|
enc.Encode(raw)
|
|
|
|
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("PUT", "/v1/session/create", 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
|
|
|
|
}
|
|
|
|
|
2014-11-25 16:06:14 +00:00
|
|
|
func makeTestSessionTTL(t *testing.T, srv *HTTPServer, ttl string) string {
|
|
|
|
// Create Session with TTL
|
|
|
|
body := bytes.NewBuffer(nil)
|
|
|
|
enc := json.NewEncoder(body)
|
|
|
|
raw := map[string]interface{}{
|
|
|
|
"TTL": ttl,
|
|
|
|
}
|
|
|
|
enc.Encode(raw)
|
|
|
|
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("PUT", "/v1/session/create", 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) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-09-25 18:40:42 +00:00
|
|
|
a := NewTestAgent(t.Name(), "")
|
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) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-04-27 09:46:38 +00:00
|
|
|
ttl := 250 * time.Millisecond
|
2017-09-25 18:40:42 +00:00
|
|
|
a := NewTestAgent(t.Name(), `
|
|
|
|
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)
|
|
|
|
if len(respObj) != 0 {
|
2018-08-14 16:08:33 +00:00
|
|
|
r.Fatalf("session '%s' should have been destroyed", id)
|
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) {
|
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
|
2017-09-25 18:40:42 +00:00
|
|
|
a := NewTestAgent(t.Name(), `
|
|
|
|
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-01-07 21:53:54 +00:00
|
|
|
time.Sleep(ttl * (structs.SessionTTLMultiplier / 3))
|
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)
|
|
|
|
}
|
|
|
|
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) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-05-21 18:31:20 +00:00
|
|
|
t.Run("", func(t *testing.T) {
|
2017-09-25 18:40:42 +00:00
|
|
|
a := NewTestAgent(t.Name(), "")
|
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/info/adf4238a-882b-9ddc-4a9d-5b6758e4159e", nil)
|
2015-11-15 05:05:37 +00:00
|
|
|
resp := httptest.NewRecorder()
|
2018-08-06 23:46:09 +00:00
|
|
|
retry.Run(t, func(r *retry.R) {
|
|
|
|
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) {
|
2017-09-25 18:40:42 +00:00
|
|
|
a := NewTestAgent(t.Name(), "")
|
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) {
|
2017-05-21 18:31:20 +00:00
|
|
|
t.Run("", func(t *testing.T) {
|
2017-09-25 18:40:42 +00:00
|
|
|
a := NewTestAgent(t.Name(), "")
|
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) {
|
2017-09-25 18:40:42 +00:00
|
|
|
a := NewTestAgent(t.Name(), "")
|
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")
|
|
|
|
}
|
|
|
|
if len(respObj) != 10 {
|
|
|
|
t.Fatalf("bad: %v", respObj)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSessionsForNode(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-05-21 18:31:20 +00:00
|
|
|
t.Run("", func(t *testing.T) {
|
2017-09-25 18:40:42 +00:00
|
|
|
a := NewTestAgent(t.Name(), "")
|
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) {
|
2017-09-25 18:40:42 +00:00
|
|
|
a := NewTestAgent(t.Name(), "")
|
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")
|
|
|
|
}
|
|
|
|
if len(respObj) != 10 {
|
|
|
|
t.Fatalf("bad: %v", respObj)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2014-11-20 02:11:41 +00:00
|
|
|
|
|
|
|
func TestSessionDeleteDestroy(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-09-25 18:40:42 +00:00
|
|
|
a := NewTestAgent(t.Name(), "")
|
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
|
|
|
}
|