Merge pull request #848 from harshavardhana/rpc-tests
Add new rpc tests for Server.Add and Server.List, improve Version.Get…
This commit is contained in:
commit
2141c9f70f
5 changed files with 102 additions and 92 deletions
|
@ -20,12 +20,15 @@ import (
|
|||
"net/http"
|
||||
|
||||
router "github.com/gorilla/mux"
|
||||
jsonrpc "github.com/gorilla/rpc/v2"
|
||||
"github.com/gorilla/rpc/v2/json"
|
||||
"github.com/minio/minio/pkg/controller/rpc"
|
||||
)
|
||||
|
||||
// getRPCHandler rpc 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.DonutService), "Donut")
|
||||
s.RegisterService(new(rpc.AuthService), "Auth")
|
||||
|
@ -35,7 +38,7 @@ func getRPCHandler() http.Handler {
|
|||
}
|
||||
|
||||
// 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)
|
||||
return mux
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
|
@ -27,20 +27,20 @@ import (
|
|||
|
||||
// MinioServer - container for minio server data
|
||||
type MinioServer struct {
|
||||
Name string `json:"name"`
|
||||
IP string `json:"ip"`
|
||||
ID string `json:"id"`
|
||||
IP string `json:"ip"`
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Status string `json:"status"`
|
||||
}
|
||||
|
||||
// ServerArg - server arg
|
||||
type ServerArg struct {
|
||||
MinioServer
|
||||
// ServerArgs - server arg
|
||||
type ServerArgs struct {
|
||||
MinioServers []MinioServer `json:"servers"`
|
||||
}
|
||||
|
||||
// ServerAddReply - server add reply
|
||||
type ServerAddReply struct {
|
||||
Server MinioServer `json:"server"`
|
||||
Status string `json:"status"`
|
||||
ServersAdded []MinioServer `json:"serversAdded"`
|
||||
}
|
||||
|
||||
// MemStatsReply memory statistics
|
||||
|
@ -55,12 +55,12 @@ type DiskStatsReply struct {
|
|||
|
||||
// SysInfoReply system info
|
||||
type SysInfoReply struct {
|
||||
Hostname string `json:"hostname"`
|
||||
SysARCH string `json:"sys.arch"`
|
||||
SysOS string `json:"sys.os"`
|
||||
SysCPUS int `json:"sys.ncpus"`
|
||||
Routines int `json:"goroutines"`
|
||||
GOVersion string `json:"goversion"`
|
||||
Hostname string `json:"hostname"`
|
||||
SysARCH string `json:"sysArch"`
|
||||
SysOS string `json:"sysOS"`
|
||||
SysCPUS int `json:"sysNCPUs"`
|
||||
GORoutines int `json:"golangRoutines"`
|
||||
GOVersion string `json:"golangVersion"`
|
||||
}
|
||||
|
||||
// ServerListReply list of minio servers
|
||||
|
@ -74,32 +74,33 @@ type ServerService struct {
|
|||
}
|
||||
|
||||
// Add - add new server
|
||||
func (s *ServerService) Add(r *http.Request, arg *ServerArg, reply *ServerAddReply) error {
|
||||
reply.Server = MinioServer{arg.Name, arg.IP, arg.ID}
|
||||
reply.Status = "connected"
|
||||
s.serverList = append(s.serverList, reply.Server)
|
||||
func (s *ServerService) Add(r *http.Request, arg *ServerArgs, reply *ServerAddReply) error {
|
||||
for _, server := range arg.MinioServers {
|
||||
server.Status = "connected"
|
||||
reply.ServersAdded = append(reply.ServersAdded, server)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// 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)
|
||||
return nil
|
||||
}
|
||||
|
||||
// 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)
|
||||
return nil
|
||||
}
|
||||
|
||||
// SysInfo - system info for the server
|
||||
func (s *ServerService) SysInfo(r *http.Request, arg *ServerArg, reply *SysInfoReply) error {
|
||||
reply.SysARCH = runtime.GOARCH
|
||||
func (s *ServerService) SysInfo(r *http.Request, arg *ServerArgs, reply *SysInfoReply) error {
|
||||
reply.SysOS = runtime.GOOS
|
||||
reply.SysARCH = runtime.GOARCH
|
||||
reply.SysCPUS = runtime.NumCPU()
|
||||
reply.Routines = runtime.NumGoroutine()
|
||||
reply.GOVersion = runtime.Version()
|
||||
reply.GORoutines = runtime.NumGoroutine()
|
||||
var err error
|
||||
reply.Hostname, err = os.Hostname()
|
||||
if err != nil {
|
||||
|
@ -109,11 +110,26 @@ func (s *ServerService) SysInfo(r *http.Request, arg *ServerArg, reply *SysInfoR
|
|||
}
|
||||
|
||||
// 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{
|
||||
{"server.one", "192.168.1.1", "192.168.1.1"},
|
||||
{"server.two", "192.168.1.2", "192.168.1.2"},
|
||||
{"server.three", "192.168.1.3", "192.168.1.3"},
|
||||
{
|
||||
"server.one",
|
||||
"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
|
||||
}
|
||||
|
|
|
@ -18,40 +18,30 @@ package rpc
|
|||
|
||||
import (
|
||||
"net/http"
|
||||
"runtime"
|
||||
|
||||
"github.com/minio/minio/pkg/version"
|
||||
)
|
||||
|
||||
// Args basic json RPC params
|
||||
type Args struct {
|
||||
Request string
|
||||
}
|
||||
// VersionArgs basic json RPC params
|
||||
type VersionArgs struct{}
|
||||
|
||||
// VersionService get version service
|
||||
type VersionService struct{}
|
||||
|
||||
// VersionReply version reply
|
||||
type VersionReply struct {
|
||||
Version string `json:"version"`
|
||||
BuildDate string `json:"buildDate"`
|
||||
Version string `json:"version"`
|
||||
BuildDate string `json:"buildDate"`
|
||||
Architecture string `json:"arch"`
|
||||
OperatingSystem string `json:"os"`
|
||||
}
|
||||
|
||||
// VersionService -
|
||||
type VersionService struct{}
|
||||
|
||||
func getVersion() string {
|
||||
return "0.0.1"
|
||||
}
|
||||
|
||||
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)
|
||||
// Get version
|
||||
func (v *VersionService) Get(r *http.Request, args *VersionArgs, reply *VersionReply) error {
|
||||
reply.Version = "0.0.1"
|
||||
reply.BuildDate = version.Version
|
||||
reply.Architecture = runtime.GOARCH
|
||||
reply.OperatingSystem = runtime.GOOS
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -53,7 +53,7 @@ func (s *MySuite) TearDownSuite(c *C) {
|
|||
func (s *MySuite) TestMemStats(c *C) {
|
||||
op := rpc.Operation{
|
||||
Method: "Server.MemStats",
|
||||
Request: rpc.Args{Request: ""},
|
||||
Request: rpc.ServerArgs{},
|
||||
}
|
||||
req, err := rpc.NewRequest(testRPCServer.URL+"/rpc", op, http.DefaultTransport)
|
||||
c.Assert(err, IsNil)
|
||||
|
@ -71,7 +71,7 @@ func (s *MySuite) TestMemStats(c *C) {
|
|||
func (s *MySuite) TestSysInfo(c *C) {
|
||||
op := rpc.Operation{
|
||||
Method: "Server.SysInfo",
|
||||
Request: rpc.Args{Request: ""},
|
||||
Request: rpc.ServerArgs{},
|
||||
}
|
||||
req, err := rpc.NewRequest(testRPCServer.URL+"/rpc", op, http.DefaultTransport)
|
||||
c.Assert(err, IsNil)
|
||||
|
@ -86,6 +86,42 @@ func (s *MySuite) TestSysInfo(c *C) {
|
|||
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) {
|
||||
op := rpc.Operation{
|
||||
Method: "Auth.Generate",
|
||||
|
|
Loading…
Reference in a new issue