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"
|
2016-06-07 20:24:51 +00:00
|
|
|
"github.com/hashicorp/consul/types"
|
2014-05-19 18:29:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestSessionCreate(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-05-21 18:31:20 +00:00
|
|
|
a := NewTestAgent(t.Name(), nil)
|
|
|
|
defer a.Shutdown()
|
|
|
|
|
|
|
|
// 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,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
var out struct{}
|
|
|
|
if err := a.RPC("Catalog.Register", args, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2014-05-19 18:29:50 +00:00
|
|
|
|
2017-05-21 18:31:20 +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",
|
2017-05-21 18:32:48 +00:00
|
|
|
"Node": a.Config.NodeName,
|
2017-07-14 05:33:47 +00:00
|
|
|
"Checks": []types.CheckID{structs.SerfCheckID, "consul"},
|
2017-05-21 18:31:20 +00:00
|
|
|
"LockDelay": "20s",
|
|
|
|
}
|
|
|
|
enc.Encode(raw)
|
2014-05-19 18:29:50 +00:00
|
|
|
|
2017-05-21 18:31:20 +00:00
|
|
|
req, _ := http.NewRequest("PUT", "/v1/session/create", body)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := a.srv.SessionCreate(resp, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2014-05-19 18:29:50 +00:00
|
|
|
|
2017-05-21 18:31:20 +00:00
|
|
|
if _, ok := obj.(sessionCreateResponse); !ok {
|
|
|
|
t.Fatalf("should work")
|
|
|
|
}
|
2014-05-19 18:29:50 +00:00
|
|
|
}
|
|
|
|
|
2014-11-20 02:11:41 +00:00
|
|
|
func TestSessionCreateDelete(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-05-21 18:31:20 +00:00
|
|
|
a := NewTestAgent(t.Name(), nil)
|
|
|
|
defer a.Shutdown()
|
|
|
|
|
|
|
|
// 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,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
var out struct{}
|
|
|
|
if err := a.RPC("Catalog.Register", args, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2014-11-20 02:11:41 +00:00
|
|
|
|
2017-05-21 18:31:20 +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",
|
2017-05-21 18:32:48 +00:00
|
|
|
"Node": a.Config.NodeName,
|
2017-07-14 05:33:47 +00:00
|
|
|
"Checks": []types.CheckID{structs.SerfCheckID, "consul"},
|
2017-05-21 18:31:20 +00:00
|
|
|
"LockDelay": "20s",
|
|
|
|
"Behavior": structs.SessionKeysDelete,
|
|
|
|
}
|
|
|
|
enc.Encode(raw)
|
2014-11-20 02:11:41 +00:00
|
|
|
|
2017-05-21 18:31:20 +00:00
|
|
|
req, _ := http.NewRequest("PUT", "/v1/session/create", body)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := a.srv.SessionCreate(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 _, ok := obj.(sessionCreateResponse); !ok {
|
|
|
|
t.Fatalf("should work")
|
|
|
|
}
|
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-05-21 18:31:20 +00:00
|
|
|
a := NewTestAgent(t.Name(), nil)
|
|
|
|
defer a.Shutdown()
|
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-05-22 11:03:59 +00:00
|
|
|
cfg := TestConfig()
|
|
|
|
cfg.SessionTTLMin = ttl
|
|
|
|
cfg.SessionTTLMinRaw = ttl.String()
|
|
|
|
a := NewTestAgent(t.Name(), cfg)
|
2017-05-21 18:31:20 +00:00
|
|
|
defer a.Shutdown()
|
2015-03-27 05:30:04 +00:00
|
|
|
|
2017-05-21 18:31:20 +00:00
|
|
|
id := makeTestSessionTTL(t, a.srv, ttl.String())
|
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("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
|
|
|
time.Sleep(ttl*structs.SessionTTLMultiplier + ttl)
|
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 len(respObj) != 0 {
|
|
|
|
t.Fatalf("session '%s' should have been destroyed", id)
|
|
|
|
}
|
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-05-22 11:03:59 +00:00
|
|
|
cfg := TestConfig()
|
|
|
|
cfg.SessionTTLMin = ttl
|
|
|
|
cfg.SessionTTLMinRaw = ttl.String()
|
|
|
|
a := NewTestAgent(t.Name(), cfg)
|
2017-05-21 18:31:20 +00:00
|
|
|
defer a.Shutdown()
|
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
|
|
|
|
time.Sleep(ttl * (structs.SessionTTLMultiplier / 2))
|
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) {
|
|
|
|
a := NewTestAgent(t.Name(), nil)
|
|
|
|
defer a.Shutdown()
|
|
|
|
|
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()
|
2017-05-21 18:31:20 +00:00
|
|
|
obj, err := a.srv.SessionGet(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) {
|
|
|
|
a := NewTestAgent(t.Name(), nil)
|
|
|
|
defer a.Shutdown()
|
|
|
|
|
|
|
|
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 07:54:40 +00:00
|
|
|
t.Parallel()
|
2017-05-21 18:31:20 +00:00
|
|
|
t.Run("", func(t *testing.T) {
|
|
|
|
a := NewTestAgent(t.Name(), nil)
|
|
|
|
defer a.Shutdown()
|
|
|
|
|
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) {
|
|
|
|
a := NewTestAgent(t.Name(), nil)
|
|
|
|
defer a.Shutdown()
|
|
|
|
|
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) {
|
|
|
|
a := NewTestAgent(t.Name(), nil)
|
|
|
|
defer a.Shutdown()
|
|
|
|
|
|
|
|
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) {
|
|
|
|
a := NewTestAgent(t.Name(), nil)
|
|
|
|
defer a.Shutdown()
|
|
|
|
|
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-05-21 18:31:20 +00:00
|
|
|
a := NewTestAgent(t.Name(), nil)
|
|
|
|
defer a.Shutdown()
|
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
|
|
|
}
|