Skip to content

Commit

Permalink
Merge 31d8249 into 07e02db
Browse files Browse the repository at this point in the history
  • Loading branch information
Cholerae Hu committed Jun 30, 2017
2 parents 07e02db + 31d8249 commit 39c1555
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 0 deletions.
1 change: 1 addition & 0 deletions server/api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func createRouter(prefix string, svr *server.Server) *mux.Router {

router.Handle("/api/v1/regions", newRegionsHandler(svr, rd)).Methods("GET")
router.Handle("/api/v1/version", newVersionHandler(rd)).Methods("GET")
router.Handle("/api/v1/status", newStatusHandler(rd)).Methods("GET")

router.Handle("/api/v1/members", newMemberListHandler(svr, rd)).Methods("GET")
router.Handle("/api/v1/members/{name}", newMemberDeleteHandler(svr, rd)).Methods("DELETE")
Expand Down
45 changes: 45 additions & 0 deletions server/api/status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2017 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

package api

import (
"net/http"

"github.com/pingcap/pd/server"
"github.com/unrolled/render"
)

type statusHandler struct {
rd *render.Render
}

type status struct {
BuildTS string `json:"build_ts"`
GitHash string `json:"git_hash"`
}

func newStatusHandler(rd *render.Render) *statusHandler {
return &statusHandler{
rd: rd,
}
}

func (h *statusHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
version := status{
BuildTS: server.PDBuildTS,
GitHash: server.PDGitHash,
}

h.rd.JSON(w, http.StatusOK, version)
}
65 changes: 65 additions & 0 deletions server/api/status_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright 2017 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

package api

import (
"encoding/json"
"io/ioutil"
"math/rand"
"net/http"
"strings"

. "github.com/pingcap/check"
"github.com/pingcap/pd/server"
)

var _ = Suite(&testStatusAPISuite{})

type testStatusAPISuite struct {
hc *http.Client
}

func (s *testStatusAPISuite) SetUpSuite(c *C) {
s.hc = newUnixSocketClient()
}

func checkStatusResponse(c *C, body []byte, cfgs []*server.Config) {
got := status{}
json.Unmarshal(body, &got)

c.Assert(got.BuildTS, Equals, server.PDBuildTS)
c.Assert(got.GitHash, Equals, server.PDGitHash)
}

func (s *testStatusAPISuite) testStatusInternal(c *C, num int) {
cfgs, _, clean := mustNewCluster(c, num)
defer clean()

parts := []string{cfgs[rand.Intn(len(cfgs))].ClientUrls, apiPrefix, "/api/v1/status"}
addr := mustUnixAddrToHTTPAddr(c, strings.Join(parts, ""))
resp, err := s.hc.Get(addr)
c.Assert(err, IsNil)
defer resp.Body.Close()
buf, err := ioutil.ReadAll(resp.Body)
c.Assert(err, IsNil)
checkStatusResponse(c, buf, cfgs)
}

func (s *testStatusAPISuite) TestStatus(c *C) {
numbers := []int{1, 3}

for _, num := range numbers {
s.testStatusInternal(c, num)
}
}

0 comments on commit 39c1555

Please sign in to comment.