clear coverage by service&addr

This commit is contained in:
lyyyuna 2020-09-11 15:45:45 +08:00
parent 1f1f3341a9
commit 15caa56c73
3 changed files with 40 additions and 16 deletions

View File

@ -18,9 +18,10 @@ package cmd
import (
"fmt"
log "github.com/sirupsen/logrus"
"os"
log "github.com/sirupsen/logrus"
"github.com/qiniu/goc/pkg/cover"
"github.com/spf13/cobra"
)
@ -37,7 +38,11 @@ goc clear
goc clear --center=http://192.168.1.1:8080
`,
Run: func(cmd *cobra.Command, args []string) {
res, err := cover.NewWorker(center).Clear()
p := cover.ProfileParam{
Service: svrList,
Address: addrList,
}
res, err := cover.NewWorker(center).Clear(p)
if err != nil {
log.Fatalf("call host %v failed, err: %v, response: %v", center, err, string(res))
}
@ -47,5 +52,7 @@ goc clear --center=http://192.168.1.1:8080
func init() {
addBasicFlags(clearCmd.Flags())
clearCmd.Flags().StringSliceVarP(&svrList, "service", "", nil, "service name to clear profile, see 'goc list' for all services.")
clearCmd.Flags().StringSliceVarP(&addrList, "address", "", nil, "address to clear profile, see 'goc list' for all addresses.")
rootCmd.AddCommand(clearCmd)
}

View File

@ -33,7 +33,7 @@ import (
// Action provides methods to contact with the covered service under test
type Action interface {
Profile(param ProfileParam) ([]byte, error)
Clear() ([]byte, error)
Clear(param ProfileParam) ([]byte, error)
InitSystem() ([]byte, error)
ListServices() ([]byte, error)
RegisterService(svr Service) ([]byte, error)
@ -113,11 +113,19 @@ func (c *client) Profile(param ProfileParam) ([]byte, error) {
return profile, err
}
func (c *client) Clear() ([]byte, error) {
func (c *client) Clear(param ProfileParam) ([]byte, error) {
u := fmt.Sprintf("%s%s", c.Host, CoverProfileClearAPI)
_, resp, err := c.do("POST", u, "", nil)
if len(param.Service) != 0 && len(param.Address) != 0 {
return nil, fmt.Errorf("use 'service' flag and 'address' flag at the same time may cause ambiguity, please use them separately")
}
body, err := json.Marshal(param)
if err != nil {
return nil, fmt.Errorf("json.Marshal failed, param: %v, err:%v", param, err)
}
_, resp, err := c.do("POST", u, "application/json", bytes.NewReader(body))
if err != nil && isNetworkError(err) {
_, resp, err = c.do("POST", u, "", nil)
_, resp, err = c.do("POST", u, "application/json", bytes.NewReader(body))
}
return resp, err
}

View File

@ -217,17 +217,26 @@ func filterProfile(coverFile []string, profiles []*cover.Profile) ([]*cover.Prof
}
func clear(c *gin.Context) {
svrsUnderTest := DefaultStore.GetAll()
for svc, addrs := range svrsUnderTest {
for _, addr := range addrs {
pp, err := NewWorker(addr).Clear()
if err != nil {
c.JSON(http.StatusExpectationFailed, gin.H{"error": err.Error()})
return
}
fmt.Fprintf(c.Writer, "Register service %s: %s coverage counter %s", svc, addr, string(pp))
}
var body ProfileParam
if err := c.ShouldBind(&body); err != nil {
c.JSON(http.StatusExpectationFailed, gin.H{"error": err.Error()})
return
}
svrsUnderTest := DefaultStore.GetAll()
filterAddrList, err := filterAddrs(body.Service, body.Address, true, svrsUnderTest)
if err != nil {
c.JSON(http.StatusExpectationFailed, gin.H{"error": err.Error()})
return
}
for _, addr := range filterAddrList {
pp, err := NewWorker(addr).Clear(ProfileParam{})
if err != nil {
c.JSON(http.StatusExpectationFailed, gin.H{"error": err.Error()})
return
}
fmt.Fprintf(c.Writer, "Register service %s coverage counter %s", addr, string(pp))
}
}
func initSystem(c *gin.Context) {