Add new rpc tests for Server.Add and Server.List, improve Version.Get RPC to provide more details

This commit is contained in:
Harshavardhana 2015-09-18 17:40:45 -07:00
parent b59d7882ef
commit 778f8cd222
5 changed files with 102 additions and 92 deletions

View File

@ -20,12 +20,15 @@ import (
"net/http" "net/http"
router "github.com/gorilla/mux" router "github.com/gorilla/mux"
jsonrpc "github.com/gorilla/rpc/v2"
"github.com/gorilla/rpc/v2/json"
"github.com/minio/minio/pkg/controller/rpc" "github.com/minio/minio/pkg/controller/rpc"
) )
// getRPCHandler rpc handler // getRPCHandler rpc handler
func getRPCHandler() http.Handler { func getRPCHandler() http.Handler {
s := rpc.NewServer() s := jsonrpc.NewServer()
s.RegisterCodec(json.NewCodec(), "application/json")
s.RegisterService(new(rpc.VersionService), "Version") s.RegisterService(new(rpc.VersionService), "Version")
s.RegisterService(new(rpc.DonutService), "Donut") s.RegisterService(new(rpc.DonutService), "Donut")
s.RegisterService(new(rpc.AuthService), "Auth") s.RegisterService(new(rpc.AuthService), "Auth")
@ -35,7 +38,7 @@ func getRPCHandler() http.Handler {
} }
// registerRPC - register rpc handlers // registerRPC - register rpc handlers
func registerRPC(mux *router.Router, s *rpc.Server) http.Handler { func registerRPC(mux *router.Router, s *jsonrpc.Server) http.Handler {
mux.Handle("/rpc", s) mux.Handle("/rpc", s)
return mux return mux
} }

View File

@ -1,35 +0,0 @@
/*
* Minio Cloud Storage, (C) 2015 Minio, 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,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package rpc
import (
"github.com/gorilla/rpc/v2"
"github.com/gorilla/rpc/v2/json"
)
// Server new rpc server container
type Server struct {
*rpc.Server
}
// NewServer - provide a new instance of RPC server
func NewServer() *Server {
s := &Server{}
s.Server = rpc.NewServer()
s.RegisterCodec(json.NewCodec(), "application/json")
return s
}

View File

@ -27,20 +27,20 @@ import (
// MinioServer - container for minio server data // MinioServer - container for minio server data
type MinioServer struct { type MinioServer struct {
Name string `json:"name"` IP string `json:"ip"`
IP string `json:"ip"` ID string `json:"id"`
ID string `json:"id"` Name string `json:"name"`
Status string `json:"status"`
} }
// ServerArg - server arg // ServerArgs - server arg
type ServerArg struct { type ServerArgs struct {
MinioServer MinioServers []MinioServer `json:"servers"`
} }
// ServerAddReply - server add reply // ServerAddReply - server add reply
type ServerAddReply struct { type ServerAddReply struct {
Server MinioServer `json:"server"` ServersAdded []MinioServer `json:"serversAdded"`
Status string `json:"status"`
} }
// MemStatsReply memory statistics // MemStatsReply memory statistics
@ -55,12 +55,12 @@ type DiskStatsReply struct {
// SysInfoReply system info // SysInfoReply system info
type SysInfoReply struct { type SysInfoReply struct {
Hostname string `json:"hostname"` Hostname string `json:"hostname"`
SysARCH string `json:"sys.arch"` SysARCH string `json:"sysArch"`
SysOS string `json:"sys.os"` SysOS string `json:"sysOS"`
SysCPUS int `json:"sys.ncpus"` SysCPUS int `json:"sysNCPUs"`
Routines int `json:"goroutines"` GORoutines int `json:"golangRoutines"`
GOVersion string `json:"goversion"` GOVersion string `json:"golangVersion"`
} }
// ServerListReply list of minio servers // ServerListReply list of minio servers
@ -74,32 +74,33 @@ type ServerService struct {
} }
// Add - add new server // Add - add new server
func (s *ServerService) Add(r *http.Request, arg *ServerArg, reply *ServerAddReply) error { func (s *ServerService) Add(r *http.Request, arg *ServerArgs, reply *ServerAddReply) error {
reply.Server = MinioServer{arg.Name, arg.IP, arg.ID} for _, server := range arg.MinioServers {
reply.Status = "connected" server.Status = "connected"
s.serverList = append(s.serverList, reply.Server) reply.ServersAdded = append(reply.ServersAdded, server)
}
return nil return nil
} }
// MemStats - memory statistics on the server // MemStats - memory statistics on the server
func (s *ServerService) MemStats(r *http.Request, arg *ServerArg, reply *MemStatsReply) error { func (s *ServerService) MemStats(r *http.Request, arg *ServerArgs, reply *MemStatsReply) error {
runtime.ReadMemStats(&reply.MemStats) runtime.ReadMemStats(&reply.MemStats)
return nil return nil
} }
// DiskStats - disk statistics on the server // DiskStats - disk statistics on the server
func (s *ServerService) DiskStats(r *http.Request, arg *ServerArg, reply *DiskStatsReply) error { func (s *ServerService) DiskStats(r *http.Request, arg *ServerArgs, reply *DiskStatsReply) error {
syscall.Statfs("/", &reply.DiskStats) syscall.Statfs("/", &reply.DiskStats)
return nil return nil
} }
// SysInfo - system info for the server // SysInfo - system info for the server
func (s *ServerService) SysInfo(r *http.Request, arg *ServerArg, reply *SysInfoReply) error { func (s *ServerService) SysInfo(r *http.Request, arg *ServerArgs, reply *SysInfoReply) error {
reply.SysARCH = runtime.GOARCH
reply.SysOS = runtime.GOOS reply.SysOS = runtime.GOOS
reply.SysARCH = runtime.GOARCH
reply.SysCPUS = runtime.NumCPU() reply.SysCPUS = runtime.NumCPU()
reply.Routines = runtime.NumGoroutine()
reply.GOVersion = runtime.Version() reply.GOVersion = runtime.Version()
reply.GORoutines = runtime.NumGoroutine()
var err error var err error
reply.Hostname, err = os.Hostname() reply.Hostname, err = os.Hostname()
if err != nil { if err != nil {
@ -109,11 +110,26 @@ func (s *ServerService) SysInfo(r *http.Request, arg *ServerArg, reply *SysInfoR
} }
// List of servers in the cluster // List of servers in the cluster
func (s *ServerService) List(r *http.Request, arg *ServerArg, reply *ServerListReply) error { func (s *ServerService) List(r *http.Request, arg *ServerArgs, reply *ServerListReply) error {
reply.ServerList = []MinioServer{ reply.ServerList = []MinioServer{
{"server.one", "192.168.1.1", "192.168.1.1"}, {
{"server.two", "192.168.1.2", "192.168.1.2"}, "server.one",
{"server.three", "192.168.1.3", "192.168.1.3"}, "192.168.1.1",
"192.168.1.1",
"connected",
},
{
"server.two",
"192.168.1.2",
"192.168.1.2",
"connected",
},
{
"server.three",
"192.168.1.3",
"192.168.1.3",
"connected",
},
} }
return nil return nil
} }

View File

@ -18,40 +18,30 @@ package rpc
import ( import (
"net/http" "net/http"
"runtime"
"github.com/minio/minio/pkg/version" "github.com/minio/minio/pkg/version"
) )
// Args basic json RPC params // VersionArgs basic json RPC params
type Args struct { type VersionArgs struct{}
Request string
} // VersionService get version service
type VersionService struct{}
// VersionReply version reply // VersionReply version reply
type VersionReply struct { type VersionReply struct {
Version string `json:"version"` Version string `json:"version"`
BuildDate string `json:"buildDate"` BuildDate string `json:"buildDate"`
Architecture string `json:"arch"`
OperatingSystem string `json:"os"`
} }
// VersionService - // Get version
type VersionService struct{} func (v *VersionService) Get(r *http.Request, args *VersionArgs, reply *VersionReply) error {
reply.Version = "0.0.1"
func getVersion() string { reply.BuildDate = version.Version
return "0.0.1" reply.Architecture = runtime.GOARCH
} reply.OperatingSystem = runtime.GOOS
func getBuildDate() string {
return version.Version
}
func setVersionReply(reply *VersionReply) {
reply.Version = getVersion()
reply.BuildDate = getBuildDate()
return
}
// Get method
func (v *VersionService) Get(r *http.Request, args *Args, reply *VersionReply) error {
setVersionReply(reply)
return nil return nil
} }

View File

@ -53,7 +53,7 @@ func (s *MySuite) TearDownSuite(c *C) {
func (s *MySuite) TestMemStats(c *C) { func (s *MySuite) TestMemStats(c *C) {
op := rpc.Operation{ op := rpc.Operation{
Method: "Server.MemStats", Method: "Server.MemStats",
Request: rpc.Args{Request: ""}, Request: rpc.ServerArgs{},
} }
req, err := rpc.NewRequest(testRPCServer.URL+"/rpc", op, http.DefaultTransport) req, err := rpc.NewRequest(testRPCServer.URL+"/rpc", op, http.DefaultTransport)
c.Assert(err, IsNil) c.Assert(err, IsNil)
@ -71,7 +71,7 @@ func (s *MySuite) TestMemStats(c *C) {
func (s *MySuite) TestSysInfo(c *C) { func (s *MySuite) TestSysInfo(c *C) {
op := rpc.Operation{ op := rpc.Operation{
Method: "Server.SysInfo", Method: "Server.SysInfo",
Request: rpc.Args{Request: ""}, Request: rpc.ServerArgs{},
} }
req, err := rpc.NewRequest(testRPCServer.URL+"/rpc", op, http.DefaultTransport) req, err := rpc.NewRequest(testRPCServer.URL+"/rpc", op, http.DefaultTransport)
c.Assert(err, IsNil) c.Assert(err, IsNil)
@ -86,6 +86,42 @@ func (s *MySuite) TestSysInfo(c *C) {
c.Assert(reply, Not(DeepEquals), rpc.SysInfoReply{}) c.Assert(reply, Not(DeepEquals), rpc.SysInfoReply{})
} }
func (s *MySuite) TestServerList(c *C) {
op := rpc.Operation{
Method: "Server.List",
Request: rpc.ServerArgs{},
}
req, err := rpc.NewRequest(testRPCServer.URL+"/rpc", op, http.DefaultTransport)
c.Assert(err, IsNil)
c.Assert(req.Get("Content-Type"), Equals, "application/json")
resp, err := req.Do()
c.Assert(err, IsNil)
c.Assert(resp.StatusCode, Equals, http.StatusOK)
var reply rpc.ServerListReply
c.Assert(jsonrpc.DecodeClientResponse(resp.Body, &reply), IsNil)
resp.Body.Close()
c.Assert(reply, Not(DeepEquals), rpc.ServerListReply{})
}
func (s *MySuite) TestServerAdd(c *C) {
op := rpc.Operation{
Method: "Server.Add",
Request: rpc.ServerArgs{MinioServers: []rpc.MinioServer{}},
}
req, err := rpc.NewRequest(testRPCServer.URL+"/rpc", op, http.DefaultTransport)
c.Assert(err, IsNil)
c.Assert(req.Get("Content-Type"), Equals, "application/json")
resp, err := req.Do()
c.Assert(err, IsNil)
c.Assert(resp.StatusCode, Equals, http.StatusOK)
var reply rpc.ServerAddReply
c.Assert(jsonrpc.DecodeClientResponse(resp.Body, &reply), IsNil)
resp.Body.Close()
c.Assert(reply, Not(DeepEquals), rpc.ServerAddReply{ServersAdded: []rpc.MinioServer{}})
}
func (s *MySuite) TestAuth(c *C) { func (s *MySuite) TestAuth(c *C) {
op := rpc.Operation{ op := rpc.Operation{
Method: "Auth.Generate", Method: "Auth.Generate",