2020-05-13 08:27:19 +00:00
|
|
|
/*
|
2020-05-25 16:19:20 +00:00
|
|
|
Copyright 2020 Qiniu Cloud (qiniu.com)
|
2020-05-13 08:27:19 +00:00
|
|
|
|
|
|
|
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 cover
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
2020-06-12 11:07:21 +00:00
|
|
|
"net/url"
|
2020-07-10 07:17:14 +00:00
|
|
|
"strconv"
|
2020-06-12 11:07:21 +00:00
|
|
|
"strings"
|
2020-08-05 09:28:11 +00:00
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
2020-05-13 08:27:19 +00:00
|
|
|
)
|
|
|
|
|
2020-06-12 06:08:15 +00:00
|
|
|
// Action provides methods to contact with the covered service under test
|
2020-05-13 08:27:19 +00:00
|
|
|
type Action interface {
|
2020-07-09 12:55:07 +00:00
|
|
|
Profile(param ProfileParam) ([]byte, error)
|
2020-06-12 11:07:21 +00:00
|
|
|
Clear() ([]byte, error)
|
|
|
|
InitSystem() ([]byte, error)
|
|
|
|
ListServices() ([]byte, error)
|
|
|
|
RegisterService(svr Service) ([]byte, error)
|
2020-05-13 08:27:19 +00:00
|
|
|
}
|
|
|
|
|
2020-06-12 06:08:15 +00:00
|
|
|
const (
|
|
|
|
//CoverInitSystemAPI prepare a new round of testing
|
|
|
|
CoverInitSystemAPI = "/v1/cover/init"
|
|
|
|
//CoverProfileAPI is provided by the covered service to get profiles
|
|
|
|
CoverProfileAPI = "/v1/cover/profile"
|
|
|
|
//CoverProfileClearAPI is provided by the covered service to clear profiles
|
|
|
|
CoverProfileClearAPI = "/v1/cover/clear"
|
|
|
|
//CoverServicesListAPI list all the registered services
|
|
|
|
CoverServicesListAPI = "/v1/cover/list"
|
2020-06-12 11:07:21 +00:00
|
|
|
//CoverRegisterServiceAPI register a service into service center
|
|
|
|
CoverRegisterServiceAPI = "/v1/cover/register"
|
2020-06-12 06:08:15 +00:00
|
|
|
)
|
2020-05-13 08:27:19 +00:00
|
|
|
|
|
|
|
type client struct {
|
2020-06-12 11:07:21 +00:00
|
|
|
Host string
|
2020-05-13 08:27:19 +00:00
|
|
|
client *http.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewWorker creates a worker to contact with service
|
2020-06-12 11:07:21 +00:00
|
|
|
func NewWorker(host string) Action {
|
|
|
|
_, err := url.ParseRequestURI(host)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Parse url %s failed, err: %v", host, err)
|
|
|
|
}
|
2020-05-13 08:27:19 +00:00
|
|
|
return &client{
|
2020-06-12 11:07:21 +00:00
|
|
|
Host: host,
|
2020-05-13 08:27:19 +00:00
|
|
|
client: http.DefaultClient,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-12 11:07:21 +00:00
|
|
|
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)
|
2020-07-30 09:30:50 +00:00
|
|
|
_, res, err := c.do("POST", u, nil)
|
2020-06-12 11:07:21 +00:00
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *client) ListServices() ([]byte, error) {
|
|
|
|
u := fmt.Sprintf("%s%s", c.Host, CoverServicesListAPI)
|
2020-07-30 09:30:50 +00:00
|
|
|
_, services, err := c.do("GET", u, nil)
|
2020-06-12 06:08:15 +00:00
|
|
|
if err != nil && isNetworkError(err) {
|
2020-07-30 09:30:50 +00:00
|
|
|
_, services, err = c.do("GET", u, nil)
|
2020-06-12 06:08:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return services, err
|
|
|
|
}
|
|
|
|
|
2020-07-09 12:55:07 +00:00
|
|
|
func (c *client) Profile(param ProfileParam) ([]byte, error) {
|
2020-07-10 07:17:14 +00:00
|
|
|
u := fmt.Sprintf("%s%s?force=%s", c.Host, CoverProfileAPI, strconv.FormatBool(param.Force))
|
2020-07-16 04:19:26 +00:00
|
|
|
if len(param.Service) != 0 && len(param.Address) != 0 {
|
2020-07-16 12:58:58 +00:00
|
|
|
return nil, fmt.Errorf("use 'service' flag and 'address' flag at the same time may cause ambiguity, please use them separately")
|
2020-07-16 04:19:26 +00:00
|
|
|
}
|
|
|
|
|
2020-07-10 07:17:14 +00:00
|
|
|
for _, svr := range param.Service {
|
|
|
|
u = u + "&service=" + svr
|
2020-07-09 12:55:07 +00:00
|
|
|
}
|
2020-07-10 07:17:14 +00:00
|
|
|
for _, addr := range param.Address {
|
|
|
|
u = u + "&address=" + addr
|
|
|
|
}
|
2020-07-30 09:30:50 +00:00
|
|
|
res, profile, err := c.do("GET", u, nil)
|
2020-05-13 08:27:19 +00:00
|
|
|
if err != nil && isNetworkError(err) {
|
2020-07-30 09:30:50 +00:00
|
|
|
res, profile, err = c.do("GET", u, nil)
|
|
|
|
}
|
|
|
|
if err == nil && res.StatusCode != 200 {
|
|
|
|
err = fmt.Errorf(string(profile))
|
2020-05-13 08:27:19 +00:00
|
|
|
}
|
|
|
|
return profile, err
|
|
|
|
}
|
|
|
|
|
2020-06-12 11:07:21 +00:00
|
|
|
func (c *client) Clear() ([]byte, error) {
|
|
|
|
u := fmt.Sprintf("%s%s", c.Host, CoverProfileClearAPI)
|
2020-07-30 09:30:50 +00:00
|
|
|
_, resp, err := c.do("POST", u, nil)
|
2020-05-25 13:04:46 +00:00
|
|
|
if err != nil && isNetworkError(err) {
|
2020-07-30 09:30:50 +00:00
|
|
|
_, resp, err = c.do("POST", u, nil)
|
2020-05-25 13:04:46 +00:00
|
|
|
}
|
|
|
|
return resp, err
|
|
|
|
}
|
2020-05-13 08:27:19 +00:00
|
|
|
|
2020-06-12 11:07:21 +00:00
|
|
|
func (c *client) InitSystem() ([]byte, error) {
|
|
|
|
u := fmt.Sprintf("%s%s", c.Host, CoverInitSystemAPI)
|
2020-07-30 09:30:50 +00:00
|
|
|
_, body, err := c.do("POST", u, nil)
|
|
|
|
return body, err
|
2020-05-13 08:27:19 +00:00
|
|
|
}
|
|
|
|
|
2020-07-30 09:30:50 +00:00
|
|
|
func (c *client) do(method, url string, body io.Reader) (*http.Response, []byte, error) {
|
2020-05-13 08:27:19 +00:00
|
|
|
req, err := http.NewRequest(method, url, body)
|
|
|
|
if err != nil {
|
2020-07-30 09:30:50 +00:00
|
|
|
return nil, nil, err
|
2020-05-13 08:27:19 +00:00
|
|
|
}
|
2020-06-12 06:08:15 +00:00
|
|
|
res, err := c.client.Do(req)
|
2020-05-13 08:27:19 +00:00
|
|
|
if err != nil {
|
2020-07-30 09:30:50 +00:00
|
|
|
return nil, nil, err
|
2020-05-13 08:27:19 +00:00
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
responseBody, err := ioutil.ReadAll(res.Body)
|
|
|
|
if err != nil {
|
2020-07-30 09:30:50 +00:00
|
|
|
return res, nil, err
|
2020-05-13 08:27:19 +00:00
|
|
|
}
|
2020-07-30 09:30:50 +00:00
|
|
|
return res, responseBody, nil
|
2020-05-13 08:27:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func isNetworkError(err error) bool {
|
|
|
|
if err == io.EOF {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
_, ok := err.(net.Error)
|
|
|
|
return ok
|
|
|
|
}
|