open-consul/agent/session_endpoint_test.go

507 lines
12 KiB
Go
Raw Normal View History

2014-05-19 18:29:50 +00:00
package agent
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"time"
pkg refactor command/agent/* -> agent/* command/consul/* -> agent/consul/* command/agent/command{,_test}.go -> command/agent{,_test}.go command/base/command.go -> command/base.go command/base/* -> command/* commands.go -> command/commands.go The script which did the refactor is: ( cd $GOPATH/src/github.com/hashicorp/consul git mv command/agent/command.go command/agent.go git mv command/agent/command_test.go command/agent_test.go git mv command/agent/flag_slice_value{,_test}.go command/ git mv command/agent . git mv command/base/command.go command/base.go git mv command/base/config_util{,_test}.go command/ git mv commands.go command/ git mv consul agent rmdir command/base/ gsed -i -e 's|package agent|package command|' command/agent{,_test}.go gsed -i -e 's|package agent|package command|' command/flag_slice_value{,_test}.go gsed -i -e 's|package base|package command|' command/base.go command/config_util{,_test}.go gsed -i -e 's|package main|package command|' command/commands.go gsed -i -e 's|base.Command|BaseCommand|' command/commands.go gsed -i -e 's|agent.Command|AgentCommand|' command/commands.go gsed -i -e 's|\tCommand:|\tBaseCommand:|' command/commands.go gsed -i -e 's|base\.||' command/commands.go gsed -i -e 's|command\.||' command/commands.go gsed -i -e 's|command|c|' main.go gsed -i -e 's|range Commands|range command.Commands|' main.go gsed -i -e 's|Commands: Commands|Commands: command.Commands|' main.go gsed -i -e 's|base\.BoolValue|BoolValue|' command/operator_autopilot_set.go gsed -i -e 's|base\.DurationValue|DurationValue|' command/operator_autopilot_set.go gsed -i -e 's|base\.StringValue|StringValue|' command/operator_autopilot_set.go gsed -i -e 's|base\.UintValue|UintValue|' command/operator_autopilot_set.go gsed -i -e 's|\bCommand\b|BaseCommand|' command/base.go gsed -i -e 's|BaseCommand Options|Command Options|' command/base.go gsed -i -e 's|base.Command|BaseCommand|' command/*.go gsed -i -e 's|c\.Command|c.BaseCommand|g' command/*.go gsed -i -e 's|\tCommand:|\tBaseCommand:|' command/*_test.go gsed -i -e 's|base\.||' command/*_test.go gsed -i -e 's|\bCommand\b|AgentCommand|' command/agent{,_test}.go gsed -i -e 's|cmd.AgentCommand|cmd.BaseCommand|' command/agent.go gsed -i -e 's|cli.AgentCommand = new(Command)|cli.Command = new(AgentCommand)|' command/agent_test.go gsed -i -e 's|exec.AgentCommand|exec.Command|' command/agent_test.go gsed -i -e 's|exec.BaseCommand|exec.Command|' command/agent_test.go gsed -i -e 's|NewTestAgent|agent.NewTestAgent|' command/agent_test.go gsed -i -e 's|= TestConfig|= agent.TestConfig|' command/agent_test.go gsed -i -e 's|: RetryJoin|: agent.RetryJoin|' command/agent_test.go gsed -i -e 's|\.\./\.\./|../|' command/config_util_test.go gsed -i -e 's|\bverifyUniqueListeners|VerifyUniqueListeners|' agent/config{,_test}.go command/agent.go gsed -i -e 's|\bserfLANKeyring\b|SerfLANKeyring|g' agent/{agent,keyring,testagent}.go command/agent.go gsed -i -e 's|\bserfWANKeyring\b|SerfWANKeyring|g' agent/{agent,keyring,testagent}.go command/agent.go gsed -i -e 's|\bNewAgent\b|agent.New|g' command/agent{,_test}.go gsed -i -e 's|\bNewAgent|New|' agent/{acl_test,agent,testagent}.go gsed -i -e 's|\bAgent\b|agent.&|g' command/agent{,_test}.go gsed -i -e 's|\bBool\b|agent.&|g' command/agent{,_test}.go gsed -i -e 's|\bConfig\b|agent.&|g' command/agent{,_test}.go gsed -i -e 's|\bDefaultConfig\b|agent.&|g' command/agent{,_test}.go gsed -i -e 's|\bDevConfig\b|agent.&|g' command/agent{,_test}.go gsed -i -e 's|\bMergeConfig\b|agent.&|g' command/agent{,_test}.go gsed -i -e 's|\bReadConfigPaths\b|agent.&|g' command/agent{,_test}.go gsed -i -e 's|\bParseMetaPair\b|agent.&|g' command/agent{,_test}.go gsed -i -e 's|\bSerfLANKeyring\b|agent.&|g' command/agent{,_test}.go gsed -i -e 's|\bSerfWANKeyring\b|agent.&|g' command/agent{,_test}.go gsed -i -e 's|circonus\.agent|circonus|g' command/agent{,_test}.go gsed -i -e 's|logger\.agent|logger|g' command/agent{,_test}.go gsed -i -e 's|metrics\.agent|metrics|g' command/agent{,_test}.go gsed -i -e 's|// agent.Agent|// agent|' command/agent{,_test}.go gsed -i -e 's|a\.agent\.Config|a.Config|' command/agent{,_test}.go gsed -i -e 's|agent\.AppendSliceValue|AppendSliceValue|' command/{configtest,validate}.go gsed -i -e 's|consul/consul|agent/consul|' GNUmakefile gsed -i -e 's|\.\./test|../../test|' agent/consul/server_test.go # fix imports f=$(grep -rl 'github.com/hashicorp/consul/command/agent' * | grep '\.go') gsed -i -e 's|github.com/hashicorp/consul/command/agent|github.com/hashicorp/consul/agent|' $f goimports -w $f f=$(grep -rl 'github.com/hashicorp/consul/consul' * | grep '\.go') gsed -i -e 's|github.com/hashicorp/consul/consul|github.com/hashicorp/consul/agent/consul|' $f goimports -w $f goimports -w command/*.go main.go )
2017-06-09 22:28:28 +00:00
"github.com/hashicorp/consul/agent/consul"
"github.com/hashicorp/consul/agent/consul/structs"
"github.com/hashicorp/consul/api"
"github.com/hashicorp/consul/types"
2014-05-19 18:29:50 +00:00
)
func TestSessionCreate(t *testing.T) {
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-05-21 18:31:20 +00:00
"Checks": []types.CheckID{consul.SerfCheckID, "consul"},
"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
}
func TestSessionCreateDelete(t *testing.T) {
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)
}
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-05-21 18:31:20 +00:00
"Checks": []types.CheckID{consul.SerfCheckID, "consul"},
"LockDelay": "20s",
"Behavior": structs.SessionKeysDelete,
}
enc.Encode(raw)
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)
}
2017-05-21 18:31:20 +00:00
if _, ok := obj.(sessionCreateResponse); !ok {
t.Fatalf("should work")
}
}
func TestFixupLockDelay(t *testing.T) {
t.Parallel()
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 {
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
}
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)
req, _ := http.NewRequest("PUT", "/v1/session/create", body)
resp := httptest.NewRecorder()
obj, err := srv.SessionCreate(resp, req)
if err != nil {
t.Fatalf("err: %v", err)
}
sessResp := obj.(sessionCreateResponse)
return sessResp.ID
}
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)
req, _ := http.NewRequest("PUT", "/v1/session/create", body)
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) {
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) {
t.Parallel()
2017-04-27 09:46:38 +00:00
ttl := 250 * time.Millisecond
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()
2017-05-21 18:31:20 +00:00
id := makeTestSessionTTL(t, a.srv, ttl.String())
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)
}
2017-05-21 18:31:20 +00:00
time.Sleep(ttl*structs.SessionTTLMultiplier + ttl)
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)
}
}
func TestSessionTTLRenew(t *testing.T) {
t.Parallel()
2017-04-27 09:46:38 +00:00
ttl := 250 * time.Millisecond
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()
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)
}
2017-05-21 18:31:20 +00:00
// Sleep to consume some time before renew
time.Sleep(ttl * (structs.SessionTTLMultiplier / 2))
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)
}
2017-05-21 18:31:20 +00:00
// Sleep for ttl * TTL Multiplier
time.Sleep(ttl * structs.SessionTTLMultiplier)
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)
}
2017-05-21 18:31:20 +00:00
// now wait for timeout and expect session to get destroyed
time.Sleep(ttl * structs.SessionTTLMultiplier)
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-05-19 18:29:50 +00:00
func TestSessionGet(t *testing.T) {
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/info/adf4238a-882b-9ddc-4a9d-5b6758e4159e", nil)
resp := httptest.NewRecorder()
2017-05-21 18:31:20 +00:00
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 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
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) {
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/list", nil)
resp := httptest.NewRecorder()
2017-05-21 18:31:20 +00:00
obj, err := a.srv.SessionList(resp, req)
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
}
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) {
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)
resp := httptest.NewRecorder()
2017-05-21 18:31:20 +00:00
obj, err := a.srv.SessionsForNode(resp, req)
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)
}
})
}
func TestSessionDeleteDestroy(t *testing.T) {
t.Parallel()
2017-05-21 18:31:20 +00:00
a := NewTestAgent(t.Name(), nil)
defer a.Shutdown()
2017-05-21 18:31:20 +00:00
id := makeTestSessionDelete(t, a.srv)
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)
}
2017-05-21 18:31:20 +00:00
if res := obj.(bool); !res {
t.Fatalf("should work")
}
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)
}
}