From 3e5f0e1a81e66a29ca7c779450fc1183d8c98643 Mon Sep 17 00:00:00 2001 From: tongjingran Date: Thu, 9 Jul 2020 20:55:07 +0800 Subject: [PATCH] add flags for cmd profile --- cmd/profile.go | 11 ++++++- go.mod | 1 + pkg/cover/client.go | 17 +++++++--- pkg/cover/client_test.go | 5 ++- pkg/cover/server.go | 69 +++++++++++++++++++++++++++++++++++++--- 5 files changed, 92 insertions(+), 11 deletions(-) diff --git a/cmd/profile.go b/cmd/profile.go index 18ae1c6..865c116 100644 --- a/cmd/profile.go +++ b/cmd/profile.go @@ -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 `, 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 { 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 force bool func init() { 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()) rootCmd.AddCommand(profileCmd) } diff --git a/go.mod b/go.mod index 2d107a0..67c6e35 100644 --- a/go.mod +++ b/go.mod @@ -19,5 +19,6 @@ require ( golang.org/x/net v0.0.0-20200301022130-244492dfa37a golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d 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 ) diff --git a/pkg/cover/client.go b/pkg/cover/client.go index 2eb1cf0..456e668 100644 --- a/pkg/cover/client.go +++ b/pkg/cover/client.go @@ -17,6 +17,8 @@ package cover import ( + "bytes" + "encoding/json" "fmt" "io" "io/ioutil" @@ -29,7 +31,7 @@ import ( // Action provides methods to contact with the covered service under test type Action interface { - Profile() ([]byte, error) + Profile(param ProfileParam) ([]byte, error) Clear() ([]byte, error) InitSystem() ([]byte, error) ListServices() ([]byte, error) @@ -73,6 +75,8 @@ func (c *client) RegisterService(srv Service) ([]byte, error) { if strings.TrimSpace(srv.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) res, err := c.do("POST", u, nil) return res, err @@ -88,11 +92,16 @@ func (c *client) ListServices() ([]byte, error) { 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) - 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) { - profile, err = c.do("GET", u, nil) + profile, err = c.do("POST", u, bytes.NewReader(args)) } return profile, err diff --git a/pkg/cover/client_test.go b/pkg/cover/client_test.go index 273355f..a950bc7 100644 --- a/pkg/cover/client_test.go +++ b/pkg/cover/client_test.go @@ -22,6 +22,7 @@ import ( "testing" "github.com/stretchr/testify/assert" + "strings" ) func TestClientAction(t *testing.T) { @@ -31,9 +32,11 @@ func TestClientAction(t *testing.T) { // regsiter service into goc server var src Service - src.Name = "goc" + src.Name = "/home/goctest123/_package/newest/goc" src.Address = "http://127.0.0.1:7777" res, err := client.RegisterService(src) + srvPath := strings.Split(src.Name, "/") + src.Name = srvPath[len(srvPath)-1] assert.NoError(t, err) assert.Contains(t, string(res), "success") diff --git a/pkg/cover/server.go b/pkg/cover/server.go index 996946e..8f48132 100644 --- a/pkg/cover/server.go +++ b/pkg/cover/server.go @@ -26,10 +26,12 @@ import ( "net/url" "os" + "encoding/json" "github.com/gin-gonic/gin" log "github.com/sirupsen/logrus" "golang.org/x/tools/cover" "k8s.io/test-infra/gopherage/pkg/cov" + "strings" ) // DefaultStore implements the IPersistence interface @@ -67,7 +69,7 @@ func GocServer(w io.Writer) *gin.Engine { v1 := r.Group("/v1") { v1.POST("/cover/register", registerService) - v1.GET("/cover/profile", profile) + v1.POST("/cover/profile", profile) v1.POST("/cover/clear", clear) v1.POST("/cover/init", initSystem) v1.GET("/cover/list", listServices) @@ -82,6 +84,13 @@ type Service struct { 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 func listServices(c *gin.Context) { services := DefaultStore.GetAll() @@ -120,11 +129,61 @@ func registerService(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)) - for _, addrs := range svrsUnderTest { - for _, addr := range addrs { - pp, err := NewWorker(addr).Profile() + for _, svrs := range svrsUnderTest { + for _, addr := range svrs { + pp, err := NewWorker(addr).Profile(ProfileParam{}) if err != nil { c.JSON(http.StatusExpectationFailed, gin.H{"error": err.Error()}) return