From c071932f925a51d9ce3a9219e4c55538b3bf368d Mon Sep 17 00:00:00 2001 From: Armon Dadgar Date: Mon, 19 May 2014 11:29:50 -0700 Subject: [PATCH] agent: Session endpoint tests --- command/agent/http_test.go | 10 ++ command/agent/session_endpoint.go | 10 +- command/agent/session_endpoint_test.go | 155 +++++++++++++++++++++++++ 3 files changed, 171 insertions(+), 4 deletions(-) create mode 100644 command/agent/session_endpoint_test.go diff --git a/command/agent/http_test.go b/command/agent/http_test.go index 60d28b6f46..6b57693f41 100644 --- a/command/agent/http_test.go +++ b/command/agent/http_test.go @@ -4,6 +4,7 @@ import ( "bytes" "encoding/json" "github.com/hashicorp/consul/consul/structs" + "github.com/hashicorp/consul/testutil" "io" "io/ioutil" "net/http" @@ -255,3 +256,12 @@ func getIndex(t *testing.T, resp *httptest.ResponseRecorder) uint64 { } return uint64(val) } + +func httpTest(t *testing.T, f func(srv *HTTPServer)) { + dir, srv := makeHTTPServer(t) + defer os.RemoveAll(dir) + defer srv.Shutdown() + defer srv.agent.Shutdown() + testutil.WaitForLeader(t, srv.agent.RPC, "dc1") + f(srv) +} diff --git a/command/agent/session_endpoint.go b/command/agent/session_endpoint.go index d304312e24..f8163f6f11 100644 --- a/command/agent/session_endpoint.go +++ b/command/agent/session_endpoint.go @@ -8,6 +8,11 @@ import ( "strings" ) +// sessionCreateResponse is used to wrap the session ID +type sessionCreateResponse struct { + ID string +} + // SessionCreate is used to create a new session func (s *HTTPServer) SessionCreate(resp http.ResponseWriter, req *http.Request) (interface{}, error) { // Mandate a PUT request @@ -42,10 +47,7 @@ func (s *HTTPServer) SessionCreate(resp http.ResponseWriter, req *http.Request) } // Format the response as a JSON object - type response struct { - ID string - } - return response{out}, nil + return sessionCreateResponse{out}, nil } // SessionDestroy is used to destroy an existing session diff --git a/command/agent/session_endpoint_test.go b/command/agent/session_endpoint_test.go new file mode 100644 index 0000000000..fec4828dd1 --- /dev/null +++ b/command/agent/session_endpoint_test.go @@ -0,0 +1,155 @@ +package agent + +import ( + "bytes" + "encoding/json" + "github.com/hashicorp/consul/consul" + "github.com/hashicorp/consul/consul/structs" + "net/http" + "net/http/httptest" + "testing" +) + +func TestSessionCreate(t *testing.T) { + httpTest(t, func(srv *HTTPServer) { + // Create a health check + args := &structs.RegisterRequest{ + Datacenter: "dc1", + Node: srv.agent.config.NodeName, + Address: "127.0.0.1", + Check: &structs.HealthCheck{ + CheckID: "consul", + Node: srv.agent.config.NodeName, + Name: "consul", + ServiceID: "consul", + }, + } + var out struct{} + if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil { + t.Fatalf("err: %v", err) + } + + // Associate session with node and 2 health checks + body := bytes.NewBuffer(nil) + enc := json.NewEncoder(body) + raw := map[string]interface{}{ + "Node": srv.agent.config.NodeName, + "Checks": []string{consul.SerfCheckID, "consul"}, + } + enc.Encode(raw) + + req, err := http.NewRequest("PUT", "/v1/session/create", body) + if err != nil { + t.Fatalf("err: %v", err) + } + + resp := httptest.NewRecorder() + obj, err := srv.SessionCreate(resp, req) + if err != nil { + t.Fatalf("err: %v", err) + } + + if _, ok := obj.(sessionCreateResponse); !ok { + t.Fatalf("should work") + } + + }) +} + +func makeTestSession(t *testing.T, srv *HTTPServer) string { + req, err := http.NewRequest("PUT", "/v1/session/create", nil) + if err != nil { + t.Fatalf("err: %v", err) + } + resp := httptest.NewRecorder() + obj, err := srv.SessionCreate(resp, req) + if err != nil { + t.Fatalf("err: %v", err) + } + sessResp := obj.(sessionCreateResponse) + return sessResp.ID +} + +func TestSessionDestroy(t *testing.T) { + httpTest(t, func(srv *HTTPServer) { + id := makeTestSession(t, srv) + + req, err := http.NewRequest("PUT", "/v1/session/destroy/"+id, nil) + resp := httptest.NewRecorder() + obj, err := srv.SessionDestroy(resp, req) + if err != nil { + t.Fatalf("err: %v", err) + } + if resp := obj.(bool); !resp { + t.Fatalf("should work") + } + }) +} + +func TestSessionGet(t *testing.T) { + httpTest(t, func(srv *HTTPServer) { + id := makeTestSession(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 TestSessionList(t *testing.T) { + httpTest(t, func(srv *HTTPServer) { + var ids []string + for i := 0; i < 10; i++ { + ids = append(ids, makeTestSession(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 TestSessionsForNode(t *testing.T) { + httpTest(t, func(srv *HTTPServer) { + var ids []string + for i := 0; i < 10; i++ { + ids = append(ids, makeTestSession(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) + } + }) +}