add test
This commit is contained in:
parent
c4e12e6d86
commit
81485d7702
@ -97,10 +97,9 @@ func (c *client) Profile(param ProfileParam) ([]byte, error) {
|
||||
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)
|
||||
}
|
||||
// the json.Marshal function can return two types of errors: UnsupportedTypeError or UnsupportedValueError
|
||||
// so no need to check here
|
||||
body, _ := json.Marshal(param)
|
||||
|
||||
res, profile, err := c.do("POST", u, "application/json", bytes.NewReader(body))
|
||||
if err != nil && isNetworkError(err) {
|
||||
@ -119,10 +118,9 @@ func (c *client) Clear(param ProfileParam) ([]byte, error) {
|
||||
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)
|
||||
}
|
||||
// the json.Marshal function can return two types of errors: UnsupportedTypeError or UnsupportedValueError
|
||||
// so no need to check here
|
||||
body, _ := json.Marshal(param)
|
||||
_, resp, err := c.do("POST", u, "application/json", bytes.NewReader(body))
|
||||
if err != nil && isNetworkError(err) {
|
||||
_, resp, err = c.do("POST", u, "application/json", bytes.NewReader(body))
|
||||
|
@ -189,3 +189,16 @@ func TestClientDo(t *testing.T) {
|
||||
_, _, err := c.do(" ", "http://127.0.0.1:7777", "", nil) // a invalid method
|
||||
assert.Contains(t, err.Error(), "invalid method")
|
||||
}
|
||||
|
||||
func TestClientClearWithInvalidParam(t *testing.T) {
|
||||
p := ProfileParam{
|
||||
Service: []string{"goc"},
|
||||
Address: []string{"http://127.0.0.1:777"},
|
||||
}
|
||||
c := &client{
|
||||
client: http.DefaultClient,
|
||||
}
|
||||
_, err := c.Clear(p)
|
||||
assert.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "use 'service' flag and 'address' flag at the same time may cause ambiguity, please use them separately")
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ package cover
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
@ -187,13 +188,36 @@ func TestClearService(t *testing.T) {
|
||||
|
||||
router := GocServer(os.Stdout)
|
||||
|
||||
// get profile with invalid force parameter
|
||||
// clear profile with non-exist port
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("POST", "/v1/cover/clear", bytes.NewBuffer([]byte(`{}`)))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(t, http.StatusExpectationFailed, w.Code)
|
||||
assert.Contains(t, w.Body.String(), "invalid port")
|
||||
|
||||
// clear profile with invalid service
|
||||
w = httptest.NewRecorder()
|
||||
req, _ = http.NewRequest("POST", "/v1/cover/clear", nil)
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
router.ServeHTTP(w, req)
|
||||
assert.Equal(t, http.StatusExpectationFailed, w.Code)
|
||||
assert.Contains(t, w.Body.String(), "invalid request")
|
||||
|
||||
// clear profile with service and address set at at the same time
|
||||
p := ProfileParam{
|
||||
Service: []string{"goc"},
|
||||
Address: []string{"http://127.0.0.1:3333"},
|
||||
}
|
||||
encoded, err := json.Marshal(p)
|
||||
assert.NoError(t, err)
|
||||
w = httptest.NewRecorder()
|
||||
req, _ = http.NewRequest("POST", "/v1/cover/clear", bytes.NewBuffer(encoded))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
router.ServeHTTP(w, req)
|
||||
assert.Equal(t, http.StatusExpectationFailed, w.Code)
|
||||
assert.Contains(t, w.Body.String(), "use 'service' flag and 'address' flag at the same time may cause ambiguity, please use them separately")
|
||||
}
|
||||
|
||||
func TestInitService(t *testing.T) {
|
||||
|
@ -38,6 +38,7 @@ setup_file() {
|
||||
}
|
||||
|
||||
teardown_file() {
|
||||
rm *_profile_listen_addr
|
||||
kill -9 $GOC_PID
|
||||
kill -9 $GOCC_PID
|
||||
kill -9 $SAMPLE_PID
|
||||
@ -65,4 +66,37 @@ teardown_file() {
|
||||
[[ "$output" == *"coverage counter clear call successfully"* ]]
|
||||
|
||||
wait $profile_pid
|
||||
}
|
||||
|
||||
@test "test clear by service name" {
|
||||
goc build --output=./test-service
|
||||
./test-service 3>&- &
|
||||
TEST_SERVICE=$!
|
||||
sleep 1
|
||||
|
||||
# clear by wrong service name
|
||||
run goc clear --service="test-servicej"
|
||||
[ "$status" -eq 0 ]
|
||||
[ "$output" = "" ]
|
||||
|
||||
# check by goc profile, as the last step is wrong
|
||||
# the coverage count should be 1
|
||||
run goc profile --coverfile="simple-project/a/a.go" --force
|
||||
info clear3 output: $output
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" =~ "example.com/simple-project/a/a.go:4.12,6.2 1 1" ]]
|
||||
|
||||
# clear by right service name
|
||||
run goc clear --service="test-service"
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" =~ "coverage counter clear call successfully" ]]
|
||||
|
||||
# check by goc profile, the coverage count should be reset to 0
|
||||
run goc profile --coverfile="simple-project/a/a.go" --force
|
||||
info clear4 output: $output
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" =~ "example.com/simple-project/a/a.go:4.12,6.2 1 0" ]]
|
||||
|
||||
|
||||
kill -9 $TEST_SERVICE
|
||||
}
|
Loading…
Reference in New Issue
Block a user