mirror of https://github.com/hashicorp/consul
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
507 lines
12 KiB
507 lines
12 KiB
11 years ago
|
package agent
|
||
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"encoding/json"
|
||
|
"net/http"
|
||
|
"net/http/httptest"
|
||
|
"testing"
|
||
11 years ago
|
"time"
|
||
10 years ago
|
|
||
8 years ago
|
"github.com/hashicorp/consul/agent/consul"
|
||
|
"github.com/hashicorp/consul/agent/consul/structs"
|
||
8 years ago
|
"github.com/hashicorp/consul/api"
|
||
9 years ago
|
"github.com/hashicorp/consul/types"
|
||
11 years ago
|
)
|
||
|
|
||
|
func TestSessionCreate(t *testing.T) {
|
||
8 years ago
|
t.Parallel()
|
||
8 years ago
|
a := NewTestAgent(t.Name(), nil)
|
||
|
defer a.Shutdown()
|
||
|
|
||
|
// Create a health check
|
||
|
args := &structs.RegisterRequest{
|
||
|
Datacenter: "dc1",
|
||
8 years ago
|
Node: a.Config.NodeName,
|
||
8 years ago
|
Address: "127.0.0.1",
|
||
|
Check: &structs.HealthCheck{
|
||
|
CheckID: "consul",
|
||
8 years ago
|
Node: a.Config.NodeName,
|
||
8 years ago
|
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)
|
||
|
}
|
||
11 years ago
|
|
||
8 years ago
|
// Associate session with node and 2 health checks
|
||
|
body := bytes.NewBuffer(nil)
|
||
|
enc := json.NewEncoder(body)
|
||
|
raw := map[string]interface{}{
|
||
|
"Name": "my-cool-session",
|
||
8 years ago
|
"Node": a.Config.NodeName,
|
||
8 years ago
|
"Checks": []types.CheckID{consul.SerfCheckID, "consul"},
|
||
|
"LockDelay": "20s",
|
||
|
}
|
||
|
enc.Encode(raw)
|
||
11 years ago
|
|
||
8 years ago
|
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)
|
||
|
}
|
||
11 years ago
|
|
||
8 years ago
|
if _, ok := obj.(sessionCreateResponse); !ok {
|
||
|
t.Fatalf("should work")
|
||
|
}
|
||
11 years ago
|
}
|
||
|
|
||
10 years ago
|
func TestSessionCreateDelete(t *testing.T) {
|
||
8 years ago
|
t.Parallel()
|
||
8 years ago
|
a := NewTestAgent(t.Name(), nil)
|
||
|
defer a.Shutdown()
|
||
|
|
||
|
// Create a health check
|
||
|
args := &structs.RegisterRequest{
|
||
|
Datacenter: "dc1",
|
||
8 years ago
|
Node: a.Config.NodeName,
|
||
8 years ago
|
Address: "127.0.0.1",
|
||
|
Check: &structs.HealthCheck{
|
||
|
CheckID: "consul",
|
||
8 years ago
|
Node: a.Config.NodeName,
|
||
8 years ago
|
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)
|
||
|
}
|
||
10 years ago
|
|
||
8 years ago
|
// 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",
|
||
8 years ago
|
"Node": a.Config.NodeName,
|
||
8 years ago
|
"Checks": []types.CheckID{consul.SerfCheckID, "consul"},
|
||
|
"LockDelay": "20s",
|
||
|
"Behavior": structs.SessionKeysDelete,
|
||
|
}
|
||
|
enc.Encode(raw)
|
||
10 years ago
|
|
||
8 years ago
|
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)
|
||
|
}
|
||
10 years ago
|
|
||
8 years ago
|
if _, ok := obj.(sessionCreateResponse); !ok {
|
||
|
t.Fatalf("should work")
|
||
|
}
|
||
10 years ago
|
}
|
||
|
|
||
11 years ago
|
func TestFixupLockDelay(t *testing.T) {
|
||
8 years ago
|
t.Parallel()
|
||
11 years ago
|
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)
|
||
|
}
|
||
|
}
|
||
|
|
||
11 years ago
|
func makeTestSession(t *testing.T, srv *HTTPServer) string {
|
||
8 years ago
|
req, _ := http.NewRequest("PUT", "/v1/session/create", nil)
|
||
11 years ago
|
resp := httptest.NewRecorder()
|
||
|
obj, err := srv.SessionCreate(resp, req)
|
||
|
if err != nil {
|
||
|
t.Fatalf("err: %v", err)
|
||
|
}
|
||
|
sessResp := obj.(sessionCreateResponse)
|
||
|
return sessResp.ID
|
||
|
}
|
||
|
|
||
10 years ago
|
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)
|
||
|
|
||
8 years ago
|
req, _ := http.NewRequest("PUT", "/v1/session/create", body)
|
||
10 years ago
|
resp := httptest.NewRecorder()
|
||
|
obj, err := srv.SessionCreate(resp, req)
|
||
|
if err != nil {
|
||
|
t.Fatalf("err: %v", err)
|
||
|
}
|
||
|
sessResp := obj.(sessionCreateResponse)
|
||
|
return sessResp.ID
|
||
|
}
|
||
|
|
||
10 years ago
|
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)
|
||
|
|
||
8 years ago
|
req, _ := http.NewRequest("PUT", "/v1/session/create", body)
|
||
10 years ago
|
resp := httptest.NewRecorder()
|
||
|
obj, err := srv.SessionCreate(resp, req)
|
||
|
if err != nil {
|
||
|
t.Fatalf("err: %v", err)
|
||
|
}
|
||
|
sessResp := obj.(sessionCreateResponse)
|
||
|
return sessResp.ID
|
||
|
}
|
||
|
|
||
11 years ago
|
func TestSessionDestroy(t *testing.T) {
|
||
8 years ago
|
t.Parallel()
|
||
8 years ago
|
a := NewTestAgent(t.Name(), nil)
|
||
|
defer a.Shutdown()
|
||
11 years ago
|
|
||
8 years ago
|
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")
|
||
|
}
|
||
11 years ago
|
}
|
||
|
|
||
8 years ago
|
func TestSessionCustomTTL(t *testing.T) {
|
||
8 years ago
|
t.Parallel()
|
||
8 years ago
|
ttl := 250 * time.Millisecond
|
||
8 years ago
|
cfg := TestConfig()
|
||
|
cfg.SessionTTLMin = ttl
|
||
|
cfg.SessionTTLMinRaw = ttl.String()
|
||
|
a := NewTestAgent(t.Name(), cfg)
|
||
8 years ago
|
defer a.Shutdown()
|
||
10 years ago
|
|
||
8 years ago
|
id := makeTestSessionTTL(t, a.srv, ttl.String())
|
||
10 years ago
|
|
||
8 years ago
|
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)
|
||
|
}
|
||
10 years ago
|
|
||
8 years ago
|
time.Sleep(ttl*structs.SessionTTLMultiplier + ttl)
|
||
10 years ago
|
|
||
8 years ago
|
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)
|
||
|
}
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
func TestSessionTTLRenew(t *testing.T) {
|
||
8 years ago
|
t.Parallel()
|
||
8 years ago
|
ttl := 250 * time.Millisecond
|
||
8 years ago
|
cfg := TestConfig()
|
||
|
cfg.SessionTTLMin = ttl
|
||
|
cfg.SessionTTLMinRaw = ttl.String()
|
||
|
a := NewTestAgent(t.Name(), cfg)
|
||
8 years ago
|
defer a.Shutdown()
|
||
10 years ago
|
|
||
8 years ago
|
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)
|
||
|
}
|
||
10 years ago
|
|
||
8 years ago
|
// Sleep to consume some time before renew
|
||
|
time.Sleep(ttl * (structs.SessionTTLMultiplier / 2))
|
||
10 years ago
|
|
||
8 years ago
|
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)
|
||
|
}
|
||
10 years ago
|
|
||
8 years ago
|
// Sleep for ttl * TTL Multiplier
|
||
|
time.Sleep(ttl * structs.SessionTTLMultiplier)
|
||
10 years ago
|
|
||
8 years ago
|
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)
|
||
|
}
|
||
10 years ago
|
|
||
8 years ago
|
// now wait for timeout and expect session to get destroyed
|
||
|
time.Sleep(ttl * structs.SessionTTLMultiplier)
|
||
10 years ago
|
|
||
8 years ago
|
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)
|
||
|
}
|
||
10 years ago
|
}
|
||
|
|
||
11 years ago
|
func TestSessionGet(t *testing.T) {
|
||
8 years ago
|
t.Parallel()
|
||
8 years ago
|
t.Run("", func(t *testing.T) {
|
||
|
a := NewTestAgent(t.Name(), nil)
|
||
|
defer a.Shutdown()
|
||
|
|
||
8 years ago
|
req, _ := http.NewRequest("GET", "/v1/session/info/adf4238a-882b-9ddc-4a9d-5b6758e4159e", nil)
|
||
9 years ago
|
resp := httptest.NewRecorder()
|
||
8 years ago
|
obj, err := a.srv.SessionGet(resp, req)
|
||
9 years ago
|
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)
|
||
|
}
|
||
|
})
|
||
|
|
||
8 years ago
|
t.Run("", func(t *testing.T) {
|
||
|
a := NewTestAgent(t.Name(), nil)
|
||
|
defer a.Shutdown()
|
||
|
|
||
|
id := makeTestSession(t, a.srv)
|
||
11 years ago
|
|
||
8 years ago
|
req, _ := http.NewRequest("GET", "/v1/session/info/"+id, nil)
|
||
11 years ago
|
resp := httptest.NewRecorder()
|
||
8 years ago
|
obj, err := a.srv.SessionGet(resp, req)
|
||
11 years ago
|
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) {
|
||
8 years ago
|
t.Parallel()
|
||
8 years ago
|
t.Run("", func(t *testing.T) {
|
||
|
a := NewTestAgent(t.Name(), nil)
|
||
|
defer a.Shutdown()
|
||
|
|
||
8 years ago
|
req, _ := http.NewRequest("GET", "/v1/session/list", nil)
|
||
9 years ago
|
resp := httptest.NewRecorder()
|
||
8 years ago
|
obj, err := a.srv.SessionList(resp, req)
|
||
9 years ago
|
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)
|
||
|
}
|
||
|
})
|
||
|
|
||
8 years ago
|
t.Run("", func(t *testing.T) {
|
||
|
a := NewTestAgent(t.Name(), nil)
|
||
|
defer a.Shutdown()
|
||
|
|
||
11 years ago
|
var ids []string
|
||
|
for i := 0; i < 10; i++ {
|
||
8 years ago
|
ids = append(ids, makeTestSession(t, a.srv))
|
||
11 years ago
|
}
|
||
|
|
||
8 years ago
|
req, _ := http.NewRequest("GET", "/v1/session/list", nil)
|
||
11 years ago
|
resp := httptest.NewRecorder()
|
||
8 years ago
|
obj, err := a.srv.SessionList(resp, req)
|
||
11 years ago
|
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) {
|
||
8 years ago
|
t.Parallel()
|
||
8 years ago
|
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)
|
||
9 years ago
|
resp := httptest.NewRecorder()
|
||
8 years ago
|
obj, err := a.srv.SessionsForNode(resp, req)
|
||
9 years ago
|
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)
|
||
|
}
|
||
|
})
|
||
|
|
||
8 years ago
|
t.Run("", func(t *testing.T) {
|
||
|
a := NewTestAgent(t.Name(), nil)
|
||
|
defer a.Shutdown()
|
||
|
|
||
11 years ago
|
var ids []string
|
||
|
for i := 0; i < 10; i++ {
|
||
8 years ago
|
ids = append(ids, makeTestSession(t, a.srv))
|
||
11 years ago
|
}
|
||
|
|
||
8 years ago
|
req, _ := http.NewRequest("GET", "/v1/session/node/"+a.Config.NodeName, nil)
|
||
11 years ago
|
resp := httptest.NewRecorder()
|
||
8 years ago
|
obj, err := a.srv.SessionsForNode(resp, req)
|
||
11 years ago
|
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)
|
||
|
}
|
||
|
})
|
||
|
}
|
||
10 years ago
|
|
||
|
func TestSessionDeleteDestroy(t *testing.T) {
|
||
8 years ago
|
t.Parallel()
|
||
8 years ago
|
a := NewTestAgent(t.Name(), nil)
|
||
|
defer a.Shutdown()
|
||
10 years ago
|
|
||
8 years ago
|
id := makeTestSessionDelete(t, a.srv)
|
||
10 years ago
|
|
||
8 years ago
|
// 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)
|
||
|
}
|
||
10 years ago
|
|
||
8 years ago
|
if res := obj.(bool); !res {
|
||
|
t.Fatalf("should work")
|
||
|
}
|
||
10 years ago
|
|
||
8 years ago
|
// 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)
|
||
|
}
|
||
10 years ago
|
}
|