From 6ef03a806c162a538231b20faad94ad2802c4379 Mon Sep 17 00:00:00 2001 From: Atin Malaviya Date: Thu, 20 Nov 2014 14:29:18 -0500 Subject: [PATCH] Clean up tests, use switch to default session.Behavior value if unspecified, unrecognized --- command/agent/session_endpoint_test.go | 72 +---------- consul/session_endpoint.go | 6 +- consul/session_endpoint_test.go | 160 +------------------------ consul/state_store.go | 7 +- 4 files changed, 16 insertions(+), 229 deletions(-) diff --git a/command/agent/session_endpoint_test.go b/command/agent/session_endpoint_test.go index 5f32bc1737..74ec20ebf2 100644 --- a/command/agent/session_endpoint_test.go +++ b/command/agent/session_endpoint_test.go @@ -87,7 +87,7 @@ func TestSessionCreateDelete(t *testing.T) { "Node": srv.agent.config.NodeName, "Checks": []string{consul.SerfCheckID, "consul"}, "LockDelay": "20s", - "Behavior": "delete", + "Behavior": structs.SessionKeysDelete, } enc.Encode(raw) @@ -280,7 +280,7 @@ func TestSessionDeleteDestroy(t *testing.T) { t.Fatalf("should work") } - // now destroy the session, this should delete the key create above + // now destroy the session, this should delete the key created above req, err = http.NewRequest("PUT", "/v1/session/destroy/"+id, nil) resp = httptest.NewRecorder() obj, err = srv.SessionDestroy(resp, req) @@ -301,71 +301,3 @@ func TestSessionDeleteDestroy(t *testing.T) { } }) } - -func TestSessionDeleteGet(t *testing.T) { - httpTest(t, func(srv *HTTPServer) { - id := makeTestSessionDelete(t, srv) - - req, err := http.NewRequest("GET", - "/v1/session/info/"+id, nil) - resp := httptest.NewRecorder() - obj, err := 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) - } - }) -} - -func TestSessionDeleteList(t *testing.T) { - httpTest(t, func(srv *HTTPServer) { - var ids []string - for i := 0; i < 10; i++ { - ids = append(ids, makeTestSessionDelete(t, srv)) - } - - req, err := http.NewRequest("GET", "/v1/session/list", nil) - resp := httptest.NewRecorder() - obj, err := srv.SessionList(resp, req) - 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 TestSessionsDeleteForNode(t *testing.T) { - httpTest(t, func(srv *HTTPServer) { - var ids []string - for i := 0; i < 10; i++ { - ids = append(ids, makeTestSessionDelete(t, srv)) - } - - req, err := http.NewRequest("GET", - "/v1/session/node/"+srv.agent.config.NodeName, nil) - resp := httptest.NewRecorder() - obj, err := srv.SessionsForNode(resp, req) - 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) - } - }) -} diff --git a/consul/session_endpoint.go b/consul/session_endpoint.go index 297e32f328..f7e46636f4 100644 --- a/consul/session_endpoint.go +++ b/consul/session_endpoint.go @@ -28,7 +28,11 @@ func (s *Session) Apply(args *structs.SessionRequest, reply *string) error { if args.Session.Node == "" && args.Op == structs.SessionCreate { return fmt.Errorf("Must provide Node") } - if args.Session.Behavior == "" { + switch args.Session.Behavior { + case structs.SessionKeysRelease, structs.SessionKeysDelete: + // we like it, use it + + default: args.Session.Behavior = structs.SessionKeysRelease // force default behavior } diff --git a/consul/session_endpoint_test.go b/consul/session_endpoint_test.go index 5e2b03caa9..7949752947 100644 --- a/consul/session_endpoint_test.go +++ b/consul/session_endpoint_test.go @@ -108,6 +108,9 @@ func TestSessionEndpoint_DeleteApply(t *testing.T) { if s.Name != "my-session" { t.Fatalf("bad: %v", s) } + if s.Behavior != structs.SessionKeysDelete { + t.Fatalf("bad: %v", s) + } // Do a delete arg.Op = structs.SessionDestroy @@ -140,51 +143,7 @@ func TestSessionEndpoint_Get(t *testing.T) { 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) - } - - getR := structs.SessionSpecificRequest{ - 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_DeleteGet(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", - Behavior: structs.SessionKeysDelete, + Node: "foo", }, } var out string @@ -264,58 +223,6 @@ func TestSessionEndpoint_List(t *testing.T) { } } -func TestSessionEndpoint_DeleteList(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", - Behavior: structs.SessionKeysDelete, - }, - } - 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) - } - } -} - func TestSessionEndpoint_NodeSessions(t *testing.T) { dir1, s1 := testServer(t) defer os.RemoveAll(dir1) @@ -373,62 +280,3 @@ func TestSessionEndpoint_NodeSessions(t *testing.T) { } } } - -func TestSessionEndpoint_DeleteNodeSessions(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", - Behavior: structs.SessionKeysDelete, - }, - } - 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) - } - } -} diff --git a/consul/state_store.go b/consul/state_store.go index bc0da3639f..2bd0396ef4 100644 --- a/consul/state_store.go +++ b/consul/state_store.go @@ -1327,8 +1327,11 @@ func (s *StateStore) SessionCreate(index uint64, session *structs.Session) error return fmt.Errorf("Missing Session ID") } - // make sure we have a default set for session.Behavior - if session.Behavior == "" { + switch session.Behavior { + case structs.SessionKeysRelease, structs.SessionKeysDelete: + // we like + default: + // force SessionKeysRelease session.Behavior = structs.SessionKeysRelease }