2014-05-16 22:20:41 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"testing"
|
2014-12-10 15:02:23 +00:00
|
|
|
"time"
|
2014-12-12 23:43:34 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/consul/structs"
|
|
|
|
"github.com/hashicorp/consul/testutil"
|
2014-05-16 22:20:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestSessionEndpoint_Apply(t *testing.T) {
|
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
client := rpcClient(t, s1)
|
|
|
|
defer client.Close()
|
|
|
|
|
|
|
|
testutil.WaitForLeader(t, client.Call, "dc1")
|
|
|
|
|
|
|
|
// Just add a node
|
|
|
|
s1.fsm.State().EnsureNode(1, structs.Node{"foo", "127.0.0.1"})
|
|
|
|
|
|
|
|
arg := structs.SessionRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.SessionCreate,
|
|
|
|
Session: structs.Session{
|
|
|
|
Node: "foo",
|
2014-06-09 18:42:28 +00:00
|
|
|
Name: "my-session",
|
2014-05-16 22:20:41 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
var out string
|
|
|
|
if err := client.Call("Session.Apply", &arg, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
id := out
|
|
|
|
|
|
|
|
// Verify
|
|
|
|
state := s1.fsm.State()
|
|
|
|
_, s, err := state.SessionGet(out)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if s == nil {
|
|
|
|
t.Fatalf("should not be nil")
|
|
|
|
}
|
2014-06-09 18:42:28 +00:00
|
|
|
if s.Node != "foo" {
|
|
|
|
t.Fatalf("bad: %v", s)
|
|
|
|
}
|
|
|
|
if s.Name != "my-session" {
|
|
|
|
t.Fatalf("bad: %v", s)
|
|
|
|
}
|
2014-05-16 22:20:41 +00:00
|
|
|
|
|
|
|
// Do a delete
|
|
|
|
arg.Op = structs.SessionDestroy
|
|
|
|
arg.Session.ID = out
|
|
|
|
if err := client.Call("Session.Apply", &arg, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify
|
|
|
|
_, s, err = state.SessionGet(id)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if s != nil {
|
|
|
|
t.Fatalf("bad: %v", s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-20 02:11:41 +00:00
|
|
|
func TestSessionEndpoint_DeleteApply(t *testing.T) {
|
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
client := rpcClient(t, s1)
|
|
|
|
defer client.Close()
|
|
|
|
|
|
|
|
testutil.WaitForLeader(t, client.Call, "dc1")
|
|
|
|
|
|
|
|
// Just add a node
|
|
|
|
s1.fsm.State().EnsureNode(1, structs.Node{"foo", "127.0.0.1"})
|
|
|
|
|
|
|
|
arg := structs.SessionRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.SessionCreate,
|
|
|
|
Session: structs.Session{
|
|
|
|
Node: "foo",
|
|
|
|
Name: "my-session",
|
|
|
|
Behavior: structs.SessionKeysDelete,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
var out string
|
|
|
|
if err := client.Call("Session.Apply", &arg, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
id := out
|
|
|
|
|
|
|
|
// Verify
|
|
|
|
state := s1.fsm.State()
|
|
|
|
_, s, err := state.SessionGet(out)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if s == nil {
|
|
|
|
t.Fatalf("should not be nil")
|
|
|
|
}
|
|
|
|
if s.Node != "foo" {
|
|
|
|
t.Fatalf("bad: %v", s)
|
|
|
|
}
|
|
|
|
if s.Name != "my-session" {
|
|
|
|
t.Fatalf("bad: %v", s)
|
|
|
|
}
|
2014-11-20 19:29:18 +00:00
|
|
|
if s.Behavior != structs.SessionKeysDelete {
|
|
|
|
t.Fatalf("bad: %v", s)
|
|
|
|
}
|
2014-11-20 02:11:41 +00:00
|
|
|
|
|
|
|
// Do a delete
|
|
|
|
arg.Op = structs.SessionDestroy
|
|
|
|
arg.Session.ID = out
|
|
|
|
if err := client.Call("Session.Apply", &arg, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify
|
|
|
|
_, s, err = state.SessionGet(id)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if s != nil {
|
|
|
|
t.Fatalf("bad: %v", s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-16 22:20:41 +00:00
|
|
|
func TestSessionEndpoint_Get(t *testing.T) {
|
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
client := rpcClient(t, s1)
|
|
|
|
defer client.Close()
|
|
|
|
|
|
|
|
testutil.WaitForLeader(t, client.Call, "dc1")
|
|
|
|
|
|
|
|
s1.fsm.State().EnsureNode(1, structs.Node{"foo", "127.0.0.1"})
|
|
|
|
arg := structs.SessionRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.SessionCreate,
|
|
|
|
Session: structs.Session{
|
2014-11-20 19:29:18 +00:00
|
|
|
Node: "foo",
|
2014-05-16 22:20:41 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
var out string
|
|
|
|
if err := client.Call("Session.Apply", &arg, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-05-16 22:49:17 +00:00
|
|
|
getR := structs.SessionSpecificRequest{
|
2014-05-16 22:20:41 +00:00
|
|
|
Datacenter: "dc1",
|
|
|
|
Session: out,
|
|
|
|
}
|
|
|
|
var sessions structs.IndexedSessions
|
|
|
|
if err := client.Call("Session.Get", &getR, &sessions); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if sessions.Index == 0 {
|
|
|
|
t.Fatalf("Bad: %v", sessions)
|
|
|
|
}
|
|
|
|
if len(sessions.Sessions) != 1 {
|
|
|
|
t.Fatalf("Bad: %v", sessions)
|
|
|
|
}
|
|
|
|
s := sessions.Sessions[0]
|
|
|
|
if s.ID != out {
|
|
|
|
t.Fatalf("bad: %v", s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSessionEndpoint_List(t *testing.T) {
|
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
client := rpcClient(t, s1)
|
|
|
|
defer client.Close()
|
|
|
|
|
|
|
|
testutil.WaitForLeader(t, client.Call, "dc1")
|
|
|
|
|
|
|
|
s1.fsm.State().EnsureNode(1, structs.Node{"foo", "127.0.0.1"})
|
|
|
|
ids := []string{}
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
arg := structs.SessionRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.SessionCreate,
|
|
|
|
Session: structs.Session{
|
|
|
|
Node: "foo",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
var out string
|
|
|
|
if err := client.Call("Session.Apply", &arg, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
ids = append(ids, out)
|
|
|
|
}
|
|
|
|
|
|
|
|
getR := structs.DCSpecificRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
}
|
|
|
|
var sessions structs.IndexedSessions
|
|
|
|
if err := client.Call("Session.List", &getR, &sessions); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if sessions.Index == 0 {
|
|
|
|
t.Fatalf("Bad: %v", sessions)
|
|
|
|
}
|
|
|
|
if len(sessions.Sessions) != 5 {
|
|
|
|
t.Fatalf("Bad: %v", sessions.Sessions)
|
|
|
|
}
|
|
|
|
for i := 0; i < len(sessions.Sessions); i++ {
|
|
|
|
s := sessions.Sessions[i]
|
|
|
|
if !strContains(ids, s.ID) {
|
|
|
|
t.Fatalf("bad: %v", s)
|
|
|
|
}
|
|
|
|
if s.Node != "foo" {
|
|
|
|
t.Fatalf("bad: %v", s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-13 05:54:29 +00:00
|
|
|
func TestSessionEndpoint_ApplyTimers(t *testing.T) {
|
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
client := rpcClient(t, s1)
|
|
|
|
defer client.Close()
|
|
|
|
|
|
|
|
testutil.WaitForLeader(t, client.Call, "dc1")
|
|
|
|
|
|
|
|
s1.fsm.State().EnsureNode(1, structs.Node{"foo", "127.0.0.1"})
|
|
|
|
arg := structs.SessionRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.SessionCreate,
|
|
|
|
Session: structs.Session{
|
|
|
|
Node: "foo",
|
|
|
|
TTL: "10s",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
var out string
|
|
|
|
if err := client.Call("Session.Apply", &arg, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the session map
|
|
|
|
if _, ok := s1.sessionTimers[out]; !ok {
|
|
|
|
t.Fatalf("missing session timer")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Destroy the session
|
|
|
|
arg.Op = structs.SessionDestroy
|
|
|
|
arg.Session.ID = out
|
|
|
|
if err := client.Call("Session.Apply", &arg, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the session map
|
|
|
|
if _, ok := s1.sessionTimers[out]; ok {
|
|
|
|
t.Fatalf("session timer exists")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-10 15:02:23 +00:00
|
|
|
func TestSessionEndpoint_Renew(t *testing.T) {
|
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
client := rpcClient(t, s1)
|
|
|
|
defer client.Close()
|
|
|
|
|
|
|
|
testutil.WaitForLeader(t, client.Call, "dc1")
|
2014-12-11 01:49:06 +00:00
|
|
|
TTL := "10s" // the minimum allowed ttl
|
|
|
|
ttl := 10 * time.Second
|
2014-12-10 15:02:23 +00:00
|
|
|
|
|
|
|
s1.fsm.State().EnsureNode(1, structs.Node{"foo", "127.0.0.1"})
|
|
|
|
ids := []string{}
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
arg := structs.SessionRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.SessionCreate,
|
|
|
|
Session: structs.Session{
|
|
|
|
Node: "foo",
|
2014-12-10 21:43:15 +00:00
|
|
|
TTL: TTL,
|
2014-12-10 15:02:23 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
var out string
|
|
|
|
if err := client.Call("Session.Apply", &arg, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
ids = append(ids, out)
|
|
|
|
}
|
|
|
|
|
2014-12-13 05:54:29 +00:00
|
|
|
// Verify the timer map is setup
|
|
|
|
if len(s1.sessionTimers) != 5 {
|
|
|
|
t.Fatalf("missing session timers")
|
|
|
|
}
|
|
|
|
|
2014-12-10 15:02:23 +00:00
|
|
|
getR := structs.DCSpecificRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
}
|
|
|
|
|
|
|
|
var sessions structs.IndexedSessions
|
|
|
|
if err := client.Call("Session.List", &getR, &sessions); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if sessions.Index == 0 {
|
|
|
|
t.Fatalf("Bad: %v", sessions)
|
|
|
|
}
|
|
|
|
if len(sessions.Sessions) != 5 {
|
|
|
|
t.Fatalf("Bad: %v", sessions.Sessions)
|
|
|
|
}
|
|
|
|
for i := 0; i < len(sessions.Sessions); i++ {
|
|
|
|
s := sessions.Sessions[i]
|
|
|
|
if !strContains(ids, s.ID) {
|
|
|
|
t.Fatalf("bad: %v", s)
|
|
|
|
}
|
|
|
|
if s.Node != "foo" {
|
|
|
|
t.Fatalf("bad: %v", s)
|
|
|
|
}
|
2014-12-10 21:43:15 +00:00
|
|
|
if s.TTL != TTL {
|
|
|
|
t.Fatalf("bad session TTL: %s %v", s.TTL, s)
|
2014-12-10 15:02:23 +00:00
|
|
|
}
|
2014-12-10 21:43:15 +00:00
|
|
|
t.Logf("Created session '%s'", s.ID)
|
2014-12-10 15:02:23 +00:00
|
|
|
}
|
|
|
|
|
2014-12-11 01:49:06 +00:00
|
|
|
// Sleep for time shorter than internal destroy ttl
|
|
|
|
time.Sleep(ttl * structs.SessionTTLMultiplier / 2)
|
2014-12-10 15:02:23 +00:00
|
|
|
|
|
|
|
// renew 3 out of 5 sessions
|
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
renewR := structs.SessionSpecificRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Session: ids[i],
|
|
|
|
}
|
|
|
|
var session structs.IndexedSessions
|
|
|
|
if err := client.Call("Session.Renew", &renewR, &session); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if session.Index == 0 {
|
|
|
|
t.Fatalf("Bad: %v", session)
|
|
|
|
}
|
|
|
|
if len(session.Sessions) != 1 {
|
|
|
|
t.Fatalf("Bad: %v", session.Sessions)
|
|
|
|
}
|
|
|
|
|
|
|
|
s := session.Sessions[0]
|
|
|
|
if !strContains(ids, s.ID) {
|
|
|
|
t.Fatalf("bad: %v", s)
|
|
|
|
}
|
|
|
|
if s.Node != "foo" {
|
|
|
|
t.Fatalf("bad: %v", s)
|
|
|
|
}
|
2014-12-10 21:43:15 +00:00
|
|
|
|
|
|
|
t.Logf("Renewed session '%s'", s.ID)
|
2014-12-10 15:02:23 +00:00
|
|
|
}
|
|
|
|
|
2014-12-11 01:49:06 +00:00
|
|
|
// now sleep for 2/3 the internal destroy TTL time for renewed sessions
|
|
|
|
// which is more than the internal destroy TTL time for the non-renewed sessions
|
|
|
|
time.Sleep((ttl * structs.SessionTTLMultiplier) * 2.0 / 3.0)
|
2014-12-10 15:02:23 +00:00
|
|
|
|
2014-12-11 01:49:06 +00:00
|
|
|
var sessionsL1 structs.IndexedSessions
|
|
|
|
if err := client.Call("Session.List", &getR, &sessionsL1); err != nil {
|
2014-12-10 15:02:23 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-12-11 01:49:06 +00:00
|
|
|
if sessionsL1.Index == 0 {
|
|
|
|
t.Fatalf("Bad: %v", sessionsL1)
|
2014-12-10 15:02:23 +00:00
|
|
|
}
|
2014-12-10 21:43:15 +00:00
|
|
|
|
|
|
|
t.Logf("Expect 2 sessions to be destroyed")
|
|
|
|
|
2014-12-11 01:49:06 +00:00
|
|
|
for i := 0; i < len(sessionsL1.Sessions); i++ {
|
|
|
|
s := sessionsL1.Sessions[i]
|
2014-12-10 15:02:23 +00:00
|
|
|
if !strContains(ids, s.ID) {
|
|
|
|
t.Fatalf("bad: %v", s)
|
|
|
|
}
|
|
|
|
if s.Node != "foo" {
|
|
|
|
t.Fatalf("bad: %v", s)
|
|
|
|
}
|
2014-12-10 21:43:15 +00:00
|
|
|
if s.TTL != TTL {
|
2014-12-10 15:02:23 +00:00
|
|
|
t.Fatalf("bad: %v", s)
|
|
|
|
}
|
2014-12-10 21:43:15 +00:00
|
|
|
if i > 2 {
|
|
|
|
t.Errorf("session '%s' should be destroyed", s.ID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-12 23:43:34 +00:00
|
|
|
if len(sessionsL1.Sessions) != 3 {
|
2014-12-11 01:49:06 +00:00
|
|
|
t.Fatalf("Bad: %v", sessionsL1.Sessions)
|
2014-12-10 15:02:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// now sleep again for ttl*2 - no sessions should still be alive
|
2014-12-11 01:49:06 +00:00
|
|
|
time.Sleep(ttl * structs.SessionTTLMultiplier)
|
2014-12-10 15:02:23 +00:00
|
|
|
|
2014-12-11 01:49:06 +00:00
|
|
|
var sessionsL2 structs.IndexedSessions
|
|
|
|
if err := client.Call("Session.List", &getR, &sessionsL2); err != nil {
|
2014-12-10 15:02:23 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-12-11 01:49:06 +00:00
|
|
|
if sessionsL2.Index == 0 {
|
|
|
|
t.Fatalf("Bad: %v", sessionsL2)
|
2014-12-10 15:02:23 +00:00
|
|
|
}
|
2014-12-11 01:49:06 +00:00
|
|
|
if len(sessionsL2.Sessions) != 0 {
|
|
|
|
for i := 0; i < len(sessionsL2.Sessions); i++ {
|
|
|
|
s := sessionsL2.Sessions[i]
|
2014-12-10 21:43:15 +00:00
|
|
|
if !strContains(ids, s.ID) {
|
|
|
|
t.Fatalf("bad: %v", s)
|
|
|
|
}
|
|
|
|
if s.Node != "foo" {
|
|
|
|
t.Fatalf("bad: %v", s)
|
|
|
|
}
|
|
|
|
if s.TTL != TTL {
|
|
|
|
t.Fatalf("bad: %v", s)
|
|
|
|
}
|
|
|
|
t.Errorf("session '%s' should be destroyed", s.ID)
|
|
|
|
}
|
2014-12-11 01:49:06 +00:00
|
|
|
|
|
|
|
t.Fatalf("Bad: %v", sessionsL2.Sessions)
|
2014-12-10 15:02:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-16 22:20:41 +00:00
|
|
|
func TestSessionEndpoint_NodeSessions(t *testing.T) {
|
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
client := rpcClient(t, s1)
|
|
|
|
defer client.Close()
|
|
|
|
|
|
|
|
testutil.WaitForLeader(t, client.Call, "dc1")
|
|
|
|
|
|
|
|
s1.fsm.State().EnsureNode(1, structs.Node{"foo", "127.0.0.1"})
|
|
|
|
s1.fsm.State().EnsureNode(1, structs.Node{"bar", "127.0.0.1"})
|
|
|
|
ids := []string{}
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
arg := structs.SessionRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.SessionCreate,
|
|
|
|
Session: structs.Session{
|
|
|
|
Node: "bar",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if i < 5 {
|
|
|
|
arg.Session.Node = "foo"
|
|
|
|
}
|
|
|
|
var out string
|
|
|
|
if err := client.Call("Session.Apply", &arg, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if i < 5 {
|
|
|
|
ids = append(ids, out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getR := structs.NodeSpecificRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
}
|
|
|
|
var sessions structs.IndexedSessions
|
|
|
|
if err := client.Call("Session.NodeSessions", &getR, &sessions); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if sessions.Index == 0 {
|
|
|
|
t.Fatalf("Bad: %v", sessions)
|
|
|
|
}
|
|
|
|
if len(sessions.Sessions) != 5 {
|
|
|
|
t.Fatalf("Bad: %v", sessions.Sessions)
|
|
|
|
}
|
|
|
|
for i := 0; i < len(sessions.Sessions); i++ {
|
|
|
|
s := sessions.Sessions[i]
|
|
|
|
if !strContains(ids, s.ID) {
|
|
|
|
t.Fatalf("bad: %v", s)
|
|
|
|
}
|
|
|
|
if s.Node != "foo" {
|
|
|
|
t.Fatalf("bad: %v", s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-03-27 05:30:04 +00:00
|
|
|
|
|
|
|
func TestSessionEndpoint_Apply_BadTTL(t *testing.T) {
|
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
client := rpcClient(t, s1)
|
|
|
|
defer client.Close()
|
|
|
|
|
|
|
|
testutil.WaitForLeader(t, client.Call, "dc1")
|
|
|
|
|
|
|
|
arg := structs.SessionRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.SessionCreate,
|
|
|
|
Session: structs.Session{
|
|
|
|
Node: "foo",
|
|
|
|
Name: "my-session",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Session with illegal TTL
|
|
|
|
arg.Session.TTL = "10z"
|
|
|
|
|
|
|
|
var out string
|
|
|
|
err := client.Call("Session.Apply", &arg, &out)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected error")
|
|
|
|
}
|
|
|
|
if err.Error() != "Session TTL '10z' invalid: time: unknown unit z in duration 10z" {
|
|
|
|
t.Fatalf("incorrect error message: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// less than SessionTTLMin
|
|
|
|
arg.Session.TTL = "5s"
|
|
|
|
|
|
|
|
err = client.Call("Session.Apply", &arg, &out)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected error")
|
|
|
|
}
|
|
|
|
if err.Error() != "Invalid Session TTL '5000000000', must be between [10s=1h0m0s]" {
|
|
|
|
t.Fatalf("incorrect error message: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// more than SessionTTLMax
|
|
|
|
arg.Session.TTL = "4000s"
|
|
|
|
|
|
|
|
err = client.Call("Session.Apply", &arg, &out)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected error")
|
|
|
|
}
|
|
|
|
if err.Error() != "Invalid Session TTL '4000000000000', must be between [10s=1h0m0s]" {
|
|
|
|
t.Fatalf("incorrect error message: %s", err.Error())
|
|
|
|
}
|
|
|
|
}
|