add register command

This commit is contained in:
jichangjun 2020-06-12 19:07:21 +08:00
parent 45612d6f63
commit 8f95da9041
8 changed files with 119 additions and 30 deletions

View File

@ -37,7 +37,7 @@ goc clear
goc clear --center=http://192.168.1.1:8080
`,
Run: func(cmd *cobra.Command, args []string) {
res, err := cover.NewWorker().Clear(center)
res, err := cover.NewWorker(center).Clear()
if err != nil {
log.Fatalf("call host %v failed, err: %v, response: %v", center, err, string(res))
}

View File

@ -27,7 +27,7 @@ var initCmd = &cobra.Command{
Use: "init",
Short: "Clear the register information in order to start a new round of tests",
Run: func(cmd *cobra.Command, args []string) {
if res, err := cover.NewWorker().InitSystem(center); err != nil {
if res, err := cover.NewWorker(center).InitSystem(); err != nil {
log.Fatalf("call host %v failed, err: %v, response: %v", center, err, string(res))
}
},

View File

@ -33,7 +33,7 @@ var listCmd = &cobra.Command{
goc list [flags]
`,
Run: func(cmd *cobra.Command, args []string) {
res, err := cover.NewWorker().ListServices(center)
res, err := cover.NewWorker(center).ListServices()
if err != nil {
log.Fatalf("list failed, err: %v", err)
}

View File

@ -45,7 +45,7 @@ goc profile --center=http://192.168.1.1:8080 -o ./coverage.cov
goc profile --center=http://192.168.1.1:8080 --output=./coverage.cov
`,
Run: func(cmd *cobra.Command, args []string) {
res, err := cover.NewWorker().Profile(center)
res, err := cover.NewWorker(center).Profile()
if err != nil {
log.Fatalf("call host %v failed, err: %v, response: %v", center, err, string(res))
}

60
cmd/register.go Normal file
View File

@ -0,0 +1,60 @@
/*
Copyright 2020 Qiniu Cloud (qiniu.com)
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 cmd
import (
"fmt"
"log"
"os"
"github.com/qiniu/goc/pkg/cover"
"github.com/spf13/cobra"
)
var registerCmd = &cobra.Command{
Use: "register",
Short: "Register a service into service center",
Long: "Register a service into service center",
Example: `
goc register [flags]
`,
Run: func(cmd *cobra.Command, args []string) {
s := cover.Service{
Name: name,
Address: address,
}
res, err := cover.NewWorker(center).RegisterService(s)
if err != nil {
log.Fatalf("register service failed, err: %v", err)
}
fmt.Fprint(os.Stdout, string(res))
},
}
var (
name string
address string
)
func init() {
registerCmd.Flags().StringVarP(&center, "center", "", "http://127.0.0.1:7777", "cover profile host center")
registerCmd.Flags().StringVarP(&name, "name", "n", "", "service name")
registerCmd.Flags().StringVarP(&address, "address", "a", "", "service address")
registerCmd.MarkFlagRequired("name")
registerCmd.MarkFlagRequired("address")
rootCmd.AddCommand(registerCmd)
}

View File

@ -36,7 +36,7 @@ goc server --port=:8080
goc server --port=localhost:8080
`,
Run: func(cmd *cobra.Command, args []string) {
cover.StartServer(port)
cover.Run(port)
},
}

View File

@ -20,16 +20,20 @@ import (
"fmt"
"io"
"io/ioutil"
"log"
"net"
"net/http"
"net/url"
"strings"
)
// Action provides methods to contact with the covered service under test
type Action interface {
Profile(host string) ([]byte, error)
Clear(host string) ([]byte, error)
InitSystem(host string) ([]byte, error)
ListServices(host string) ([]byte, error)
Profile() ([]byte, error)
Clear() ([]byte, error)
InitSystem() ([]byte, error)
ListServices() ([]byte, error)
RegisterService(svr Service) ([]byte, error)
}
const (
@ -41,21 +45,41 @@ const (
CoverProfileClearAPI = "/v1/cover/clear"
//CoverServicesListAPI list all the registered services
CoverServicesListAPI = "/v1/cover/list"
//CoverRegisterServiceAPI register a service into service center
CoverRegisterServiceAPI = "/v1/cover/register"
)
type client struct {
Host string
client *http.Client
}
// NewWorker creates a worker to contact with service
func NewWorker() Action {
func NewWorker(host string) Action {
_, err := url.ParseRequestURI(host)
if err != nil {
log.Fatalf("Parse url %s failed, err: %v", host, err)
}
return &client{
Host: host,
client: http.DefaultClient,
}
}
func (c *client) ListServices(host string) ([]byte, error) {
u := fmt.Sprintf("%s%s", host, CoverServicesListAPI)
func (c *client) RegisterService(srv Service) ([]byte, error) {
if _, err := url.ParseRequestURI(srv.Address); err != nil {
return nil, err
}
if strings.TrimSpace(srv.Name) == "" {
return nil, fmt.Errorf("invalid service name")
}
u := fmt.Sprintf("%s%s?name=%s&address=%s", c.Host, CoverRegisterServiceAPI, srv.Name, srv.Address)
res, err := c.do("POST", u, nil)
return res, err
}
func (c *client) ListServices() ([]byte, error) {
u := fmt.Sprintf("%s%s", c.Host, CoverServicesListAPI)
services, err := c.do("GET", u, nil)
if err != nil && isNetworkError(err) {
services, err = c.do("GET", u, nil)
@ -64,8 +88,8 @@ func (c *client) ListServices(host string) ([]byte, error) {
return services, err
}
func (c *client) Profile(host string) ([]byte, error) {
u := fmt.Sprintf("%s%s", host, CoverProfileAPI)
func (c *client) Profile() ([]byte, error) {
u := fmt.Sprintf("%s%s", c.Host, CoverProfileAPI)
profile, err := c.do("GET", u, nil)
if err != nil && isNetworkError(err) {
profile, err = c.do("GET", u, nil)
@ -74,8 +98,8 @@ func (c *client) Profile(host string) ([]byte, error) {
return profile, err
}
func (c *client) Clear(host string) ([]byte, error) {
u := fmt.Sprintf("%s%s", host, CoverProfileClearAPI)
func (c *client) Clear() ([]byte, error) {
u := fmt.Sprintf("%s%s", c.Host, CoverProfileClearAPI)
resp, err := c.do("POST", u, nil)
if err != nil && isNetworkError(err) {
resp, err = c.do("POST", u, nil)
@ -83,8 +107,8 @@ func (c *client) Clear(host string) ([]byte, error) {
return resp, err
}
func (c *client) InitSystem(host string) ([]byte, error) {
u := fmt.Sprintf("%s%s", host, CoverInitSystemAPI)
func (c *client) InitSystem() ([]byte, error) {
u := fmt.Sprintf("%s%s", c.Host, CoverInitSystemAPI)
return c.do("POST", u, nil)
}

View File

@ -35,25 +35,30 @@ import (
// LocalStore implements the IPersistence interface
var LocalStore Store
// Client implements the Action interface
var Client Action
// LogFile a file to save log.
const LogFile = "goc.log"
// StartServer starts coverage host center
func StartServer(port string) {
func init() {
LocalStore = NewStore()
Client = NewWorker()
}
// Run starts coverage host center
func Run(port string) {
f, err := os.Create(LogFile)
if err != nil {
log.Fatalf("failed to create log file %s, err: %v", LogFile, err)
}
r := GocServer(f)
log.Fatal(r.Run(port))
}
// GocServer init goc server engine
func GocServer(w io.Writer) *gin.Engine {
if w != nil && w != os.Stdout {
gin.DefaultWriter = io.MultiWriter(w, os.Stdout)
}
gin.DefaultWriter = io.MultiWriter(f, os.Stdout)
r := gin.Default()
// api to show the registered services
r.StaticFile(PersistenceFile, "./"+PersistenceFile)
@ -66,7 +71,7 @@ func StartServer(port string) {
v1.GET("/cover/list", listServices)
}
log.Fatal(r.Run(port))
return r
}
// Service is a entry under being tested
@ -109,7 +114,7 @@ func registerService(c *gin.Context) {
return
}
c.JSON(http.StatusOK, gin.H{"name": service.Name, "address": service.Address})
c.JSON(http.StatusOK, gin.H{"result": "success"})
}
func profile(c *gin.Context) {
@ -117,7 +122,7 @@ func profile(c *gin.Context) {
var mergedProfiles = make([][]*cover.Profile, len(svrsUnderTest))
for _, addrs := range svrsUnderTest {
for _, addr := range addrs {
pp, err := Client.Profile(addr)
pp, err := NewWorker(addr).Profile()
if err != nil {
c.JSON(http.StatusExpectationFailed, gin.H{"error": err.Error()})
return
@ -152,7 +157,7 @@ func clear(c *gin.Context) {
svrsUnderTest := LocalStore.GetAll()
for svc, addrs := range svrsUnderTest {
for _, addr := range addrs {
pp, err := Client.Clear(addr)
pp, err := NewWorker(addr).Clear()
if err != nil {
c.JSON(http.StatusExpectationFailed, gin.H{"error": err.Error()})
return