add flags for cmd profile
This commit is contained in:
parent
fee726b165
commit
3e5f0e1a81
@ -45,7 +45,12 @@ goc profile --center=http://192.168.1.1:8080 -o ./coverage.cov
|
|||||||
goc profile --center=http://192.168.1.1:8080 --output=./coverage.cov
|
goc profile --center=http://192.168.1.1:8080 --output=./coverage.cov
|
||||||
`,
|
`,
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
res, err := cover.NewWorker(center).Profile()
|
p := cover.ProfileParam{
|
||||||
|
Force: force,
|
||||||
|
Name: name,
|
||||||
|
Address: address,
|
||||||
|
}
|
||||||
|
res, err := cover.NewWorker(center).Profile(p)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("call host %v failed, err: %v, response: %v", center, err, string(res))
|
log.Fatalf("call host %v failed, err: %v, response: %v", center, err, string(res))
|
||||||
}
|
}
|
||||||
@ -67,9 +72,13 @@ goc profile --center=http://192.168.1.1:8080 --output=./coverage.cov
|
|||||||
}
|
}
|
||||||
|
|
||||||
var output string
|
var output string
|
||||||
|
var force bool
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
profileCmd.Flags().StringVarP(&output, "output", "o", "", "download cover profile")
|
profileCmd.Flags().StringVarP(&output, "output", "o", "", "download cover profile")
|
||||||
|
profileCmd.Flags().StringVarP(&name, "name", "n", "", "name list to get cover profile")
|
||||||
|
profileCmd.Flags().StringVarP(&address, "address", "a", "", "address list to get cover proflie")
|
||||||
|
profileCmd.Flags().BoolVarP(&force, "force", "f", false, "force")
|
||||||
addBasicFlags(profileCmd.Flags())
|
addBasicFlags(profileCmd.Flags())
|
||||||
rootCmd.AddCommand(profileCmd)
|
rootCmd.AddCommand(profileCmd)
|
||||||
}
|
}
|
||||||
|
1
go.mod
1
go.mod
@ -19,5 +19,6 @@ require (
|
|||||||
golang.org/x/net v0.0.0-20200301022130-244492dfa37a
|
golang.org/x/net v0.0.0-20200301022130-244492dfa37a
|
||||||
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d
|
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d
|
||||||
golang.org/x/tools v0.0.0-20200329025819-fd4102a86c65
|
golang.org/x/tools v0.0.0-20200329025819-fd4102a86c65
|
||||||
|
k8s.io/kubernetes v1.13.0 // indirect
|
||||||
k8s.io/test-infra v0.0.0-20200511080351-8ac9dbfab055
|
k8s.io/test-infra v0.0.0-20200511080351-8ac9dbfab055
|
||||||
)
|
)
|
||||||
|
@ -17,6 +17,8 @@
|
|||||||
package cover
|
package cover
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
@ -29,7 +31,7 @@ import (
|
|||||||
|
|
||||||
// Action provides methods to contact with the covered service under test
|
// Action provides methods to contact with the covered service under test
|
||||||
type Action interface {
|
type Action interface {
|
||||||
Profile() ([]byte, error)
|
Profile(param ProfileParam) ([]byte, error)
|
||||||
Clear() ([]byte, error)
|
Clear() ([]byte, error)
|
||||||
InitSystem() ([]byte, error)
|
InitSystem() ([]byte, error)
|
||||||
ListServices() ([]byte, error)
|
ListServices() ([]byte, error)
|
||||||
@ -73,6 +75,8 @@ func (c *client) RegisterService(srv Service) ([]byte, error) {
|
|||||||
if strings.TrimSpace(srv.Name) == "" {
|
if strings.TrimSpace(srv.Name) == "" {
|
||||||
return nil, fmt.Errorf("invalid service name")
|
return nil, fmt.Errorf("invalid service name")
|
||||||
}
|
}
|
||||||
|
srvPath := strings.Split(srv.Name, "/")
|
||||||
|
srv.Name = srvPath[len(srvPath)-1]
|
||||||
u := fmt.Sprintf("%s%s?name=%s&address=%s", c.Host, CoverRegisterServiceAPI, srv.Name, srv.Address)
|
u := fmt.Sprintf("%s%s?name=%s&address=%s", c.Host, CoverRegisterServiceAPI, srv.Name, srv.Address)
|
||||||
res, err := c.do("POST", u, nil)
|
res, err := c.do("POST", u, nil)
|
||||||
return res, err
|
return res, err
|
||||||
@ -88,11 +92,16 @@ func (c *client) ListServices() ([]byte, error) {
|
|||||||
return services, err
|
return services, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *client) Profile() ([]byte, error) {
|
func (c *client) Profile(param ProfileParam) ([]byte, error) {
|
||||||
|
log.Printf("param:%+v", param)
|
||||||
u := fmt.Sprintf("%s%s", c.Host, CoverProfileAPI)
|
u := fmt.Sprintf("%s%s", c.Host, CoverProfileAPI)
|
||||||
profile, err := c.do("GET", u, nil)
|
args, err := json.Marshal(param)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
profile, err := c.do("POST", u, bytes.NewReader(args))
|
||||||
if err != nil && isNetworkError(err) {
|
if err != nil && isNetworkError(err) {
|
||||||
profile, err = c.do("GET", u, nil)
|
profile, err = c.do("POST", u, bytes.NewReader(args))
|
||||||
}
|
}
|
||||||
|
|
||||||
return profile, err
|
return profile, err
|
||||||
|
@ -22,6 +22,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestClientAction(t *testing.T) {
|
func TestClientAction(t *testing.T) {
|
||||||
@ -31,9 +32,11 @@ func TestClientAction(t *testing.T) {
|
|||||||
|
|
||||||
// regsiter service into goc server
|
// regsiter service into goc server
|
||||||
var src Service
|
var src Service
|
||||||
src.Name = "goc"
|
src.Name = "/home/goctest123/_package/newest/goc"
|
||||||
src.Address = "http://127.0.0.1:7777"
|
src.Address = "http://127.0.0.1:7777"
|
||||||
res, err := client.RegisterService(src)
|
res, err := client.RegisterService(src)
|
||||||
|
srvPath := strings.Split(src.Name, "/")
|
||||||
|
src.Name = srvPath[len(srvPath)-1]
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Contains(t, string(res), "success")
|
assert.Contains(t, string(res), "success")
|
||||||
|
|
||||||
|
@ -26,10 +26,12 @@ import (
|
|||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"encoding/json"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"golang.org/x/tools/cover"
|
"golang.org/x/tools/cover"
|
||||||
"k8s.io/test-infra/gopherage/pkg/cov"
|
"k8s.io/test-infra/gopherage/pkg/cov"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// DefaultStore implements the IPersistence interface
|
// DefaultStore implements the IPersistence interface
|
||||||
@ -67,7 +69,7 @@ func GocServer(w io.Writer) *gin.Engine {
|
|||||||
v1 := r.Group("/v1")
|
v1 := r.Group("/v1")
|
||||||
{
|
{
|
||||||
v1.POST("/cover/register", registerService)
|
v1.POST("/cover/register", registerService)
|
||||||
v1.GET("/cover/profile", profile)
|
v1.POST("/cover/profile", profile)
|
||||||
v1.POST("/cover/clear", clear)
|
v1.POST("/cover/clear", clear)
|
||||||
v1.POST("/cover/init", initSystem)
|
v1.POST("/cover/init", initSystem)
|
||||||
v1.GET("/cover/list", listServices)
|
v1.GET("/cover/list", listServices)
|
||||||
@ -82,6 +84,13 @@ type Service struct {
|
|||||||
Address string `form:"address" json:"address" binding:"required"`
|
Address string `form:"address" json:"address" binding:"required"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ProfileParam is param of profile API (TODO)
|
||||||
|
type ProfileParam struct {
|
||||||
|
Force bool `form:"force"`
|
||||||
|
Name string `form:"name" json:"name"`
|
||||||
|
Address string `form:"address" json:"address"`
|
||||||
|
}
|
||||||
|
|
||||||
//listServices list all the registered services
|
//listServices list all the registered services
|
||||||
func listServices(c *gin.Context) {
|
func listServices(c *gin.Context) {
|
||||||
services := DefaultStore.GetAll()
|
services := DefaultStore.GetAll()
|
||||||
@ -120,11 +129,61 @@ func registerService(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func profile(c *gin.Context) {
|
func profile(c *gin.Context) {
|
||||||
svrsUnderTest := DefaultStore.GetAll()
|
respByte, err := ioutil.ReadAll(c.Request.Body)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var param ProfileParam
|
||||||
|
json.Unmarshal(respByte, ¶m)
|
||||||
|
if param.Name != "" && param.Address != "" {
|
||||||
|
c.JSON(http.StatusExpectationFailed, gin.H{"error": "invalid param"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
nameList := strings.Split(param.Name, "&")
|
||||||
|
addrList := strings.Split(param.Address, "&")
|
||||||
|
svrsAll := DefaultStore.GetAll()
|
||||||
|
svrsUnderTest := make(map[string][]string)
|
||||||
|
if param.Name == "" && param.Address == "" {
|
||||||
|
svrsUnderTest = svrsAll
|
||||||
|
} else {
|
||||||
|
if param.Name != "" {
|
||||||
|
for _, name := range nameList {
|
||||||
|
miss := true
|
||||||
|
for svr, addrs := range svrsAll {
|
||||||
|
if svr == name {
|
||||||
|
svrsUnderTest[svr] = addrs
|
||||||
|
miss = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if miss && !param.Force {
|
||||||
|
c.JSON(http.StatusNotFound, fmt.Sprintf("service [%s] not found!", name))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if param.Address != "" {
|
||||||
|
for _, addr := range addrList {
|
||||||
|
miss := true
|
||||||
|
for svr, addrs := range svrsAll {
|
||||||
|
for _, a := range addrs {
|
||||||
|
if a == addr {
|
||||||
|
svrsUnderTest[svr] = append(svrsUnderTest[svr], a)
|
||||||
|
miss = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if miss && !param.Force {
|
||||||
|
c.JSON(http.StatusNotFound, fmt.Sprintf("address [%s] not found!", addr))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
var mergedProfiles = make([][]*cover.Profile, len(svrsUnderTest))
|
var mergedProfiles = make([][]*cover.Profile, len(svrsUnderTest))
|
||||||
for _, addrs := range svrsUnderTest {
|
for _, svrs := range svrsUnderTest {
|
||||||
for _, addr := range addrs {
|
for _, addr := range svrs {
|
||||||
pp, err := NewWorker(addr).Profile()
|
pp, err := NewWorker(addr).Profile(ProfileParam{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusExpectationFailed, gin.H{"error": err.Error()})
|
c.JSON(http.StatusExpectationFailed, gin.H{"error": err.Error()})
|
||||||
return
|
return
|
||||||
|
Loading…
Reference in New Issue
Block a user