From 81485d7702424a26adeb1f91a0a4ed3d1f7d0e82 Mon Sep 17 00:00:00 2001 From: lyyyuna Date: Fri, 11 Sep 2020 22:28:53 +0800 Subject: [PATCH] add test --- pkg/cover/client.go | 14 ++++++-------- pkg/cover/client_test.go | 13 +++++++++++++ pkg/cover/server_test.go | 26 +++++++++++++++++++++++++- tests/clear.bats | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 78 insertions(+), 9 deletions(-) diff --git a/pkg/cover/client.go b/pkg/cover/client.go index c1df32b..c45166f 100644 --- a/pkg/cover/client.go +++ b/pkg/cover/client.go @@ -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)) diff --git a/pkg/cover/client_test.go b/pkg/cover/client_test.go index 1663599..e5cc340 100644 --- a/pkg/cover/client_test.go +++ b/pkg/cover/client_test.go @@ -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") +} diff --git a/pkg/cover/server_test.go b/pkg/cover/server_test.go index 10d4d49..fb7feb7 100644 --- a/pkg/cover/server_test.go +++ b/pkg/cover/server_test.go @@ -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) { diff --git a/tests/clear.bats b/tests/clear.bats index 24583c7..f8cb637 100755 --- a/tests/clear.bats +++ b/tests/clear.bats @@ -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 } \ No newline at end of file