add case for profile api with coverfile flag

This commit is contained in:
jichangjun 2020-09-05 18:32:55 +08:00
parent c00f871f80
commit ff8f840b14
4 changed files with 69 additions and 62 deletions

View File

@ -106,6 +106,7 @@ func (c *client) Profile(param ProfileParam) ([]byte, error) {
if err != nil && isNetworkError(err) { if err != nil && isNetworkError(err) {
res, profile, err = c.do("POST", u, "application/json", bytes.NewReader(body)) res, profile, err = c.do("POST", u, "application/json", bytes.NewReader(body))
} }
if err == nil && res.StatusCode != 200 { if err == nil && res.StatusCode != 200 {
err = fmt.Errorf(string(profile)) err = fmt.Errorf(string(profile))
} }

View File

@ -33,10 +33,10 @@ func TestClientAction(t *testing.T) {
var client = NewWorker(ts.URL) var client = NewWorker(ts.URL)
// mock profile server // mock profile server
profileMockResponse := "mode: count\nmockService/main.go:30.13,48.33 13 1" profileMockResponse := []byte("mode: count\nmockService/main.go:30.13,48.33 13 1\nb/b.go:30.13,48.33 13 1")
profileSuccessMockSvr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { profileSuccessMockSvr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
w.Write([]byte(profileMockResponse)) w.Write(profileMockResponse)
})) }))
defer profileSuccessMockSvr.Close() defer profileSuccessMockSvr.Close()
@ -60,65 +60,89 @@ func TestClientAction(t *testing.T) {
assert.Contains(t, string(res), src.Address) assert.Contains(t, string(res), src.Address)
assert.Contains(t, string(res), src.Name) assert.Contains(t, string(res), src.Name)
// get porfile from goc server // get profile from goc server
profileItems := []struct { tcs := []struct {
service Service name string
param ProfileParam service Service
res string param ProfileParam
expected string
expectedErr bool
}{ }{
{ {
service: Service{Name: "serviceOK", Address: profileSuccessMockSvr.URL}, name: "both service and address existed",
param: ProfileParam{Force: false, Service: []string{"serviceOK"}, Address: []string{profileSuccessMockSvr.URL}}, service: Service{Name: "serviceOK", Address: profileSuccessMockSvr.URL},
res: "use 'service' flag and 'address' flag at the same time may cause ambiguity, please use them separately", param: ProfileParam{Force: false, Service: []string{"serviceOK"}, Address: []string{profileSuccessMockSvr.URL}},
expectedErr: true,
}, },
{ {
service: Service{Name: "serviceOK", Address: profileSuccessMockSvr.URL}, name: "valid test with no service flag provied",
param: ProfileParam{}, service: Service{Name: "serviceOK", Address: profileSuccessMockSvr.URL},
res: profileMockResponse, param: ProfileParam{},
expected: "mockService/main.go:30.13,48.33 13 1",
}, },
{ {
service: Service{Name: "serviceOK", Address: profileSuccessMockSvr.URL}, name: "valid test with service flag provied",
param: ProfileParam{Service: []string{"serviceOK"}}, service: Service{Name: "serviceOK", Address: profileSuccessMockSvr.URL},
res: profileMockResponse, param: ProfileParam{Service: []string{"serviceOK"}},
expected: "mockService/main.go:30.13,48.33 13 1",
}, },
{ {
service: Service{Name: "serviceOK", Address: profileSuccessMockSvr.URL}, name: "valid test with address flag provied",
param: ProfileParam{Address: []string{profileSuccessMockSvr.URL}}, service: Service{Name: "serviceOK", Address: profileSuccessMockSvr.URL},
res: profileMockResponse, param: ProfileParam{Address: []string{profileSuccessMockSvr.URL}},
expected: "mockService/main.go:30.13,48.33 13 1",
}, },
{ {
service: Service{Name: "serviceOK", Address: profileSuccessMockSvr.URL}, service: Service{Name: "serviceOK", Address: profileSuccessMockSvr.URL},
param: ProfileParam{Service: []string{"unknown"}}, param: ProfileParam{Service: []string{"unknown"}},
res: "service [unknown] not found", expected: "service [unknown] not found",
expectedErr: true,
}, },
{ {
service: Service{Name: "serviceErr", Address: profileErrMockSvr.URL}, service: Service{Name: "serviceErr", Address: profileErrMockSvr.URL},
res: "bad mode line: error", expected: "bad mode line: error",
expectedErr: true,
}, },
{ {
service: Service{Name: "serviceNotExist", Address: "http://172.0.0.2:7777"}, service: Service{Name: "serviceNotExist", Address: "http://172.0.0.2:7777"},
res: "connection refused", expected: "connection refused",
expectedErr: true,
}, },
{ {
service: Service{Name: "serviceNotExist", Address: "http://172.0.0.2:7777"}, service: Service{Name: "serviceNotExist", Address: "http://172.0.0.2:7777"},
param: ProfileParam{Force: true}, param: ProfileParam{Force: true},
res: "no profiles", expected: `{"message":"no profiles"}`,
},
{
name: "valid test with coverfile flag provied",
service: Service{Name: "serviceOK", Address: profileSuccessMockSvr.URL},
param: ProfileParam{CoverFilePatterns: []string{"b.go$"}},
expected: "b/b.go",
}, },
} }
for _, item := range profileItems { for _, tc := range tcs {
// init server t.Run(tc.name, func(t *testing.T) {
_, err = client.InitSystem() // init server
assert.NoError(t, err) _, err = client.InitSystem()
// register server assert.NoError(t, err)
res, err = client.RegisterService(item.service) // register server
assert.NoError(t, err) res, err = client.RegisterService(tc.service)
assert.Contains(t, string(res), "success") assert.NoError(t, err)
res, err = client.Profile(item.param) assert.Contains(t, string(res), "success")
if err != nil { res, err = client.Profile(tc.param)
assert.Contains(t, err.Error(), item.res) if err != nil {
} else { if !tc.expectedErr {
assert.Contains(t, string(res), item.res) t.Errorf("unexpected err got: %v", err)
} }
return
}
if tc.expectedErr {
t.Errorf("Expected an error, but got value %s", string(res))
}
assert.Regexp(t, tc.expected, string(res))
})
} }
// init system and check service again // init system and check service again

View File

@ -173,7 +173,7 @@ func profile(c *gin.Context) {
} }
if len(mergedProfiles) == 0 { if len(mergedProfiles) == 0 {
c.JSON(http.StatusOK, "no profiles") c.JSON(http.StatusOK, gin.H{"message": "no profiles"})
return return
} }
@ -307,16 +307,3 @@ func filterAddrs(serviceList, addressList []string, force bool, allInfos map[str
// Return all servers when all param is nil // Return all servers when all param is nil
return filterAddrList, nil return filterAddrList, nil
} }
// removeDuplicateElement remove duplicate element in slice
func removeDuplicateElement(addrs []string) []string {
result := make([]string, 0, len(addrs))
temp := map[string]struct{}{}
for _, item := range addrs {
if _, ok := temp[item]; !ok {
temp[item] = struct{}{}
result = append(result, item)
}
}
return result
}

View File

@ -110,11 +110,6 @@ func TestFilterAddrs(t *testing.T) {
} }
} }
func TestRemoveDuplicateElement(t *testing.T) {
strArr := []string{"a", "a", "b"}
assert.Equal(t, removeDuplicateElement(strArr), []string{"a", "b"})
}
func TestRegisterService(t *testing.T) { func TestRegisterService(t *testing.T) {
router := GocServer(os.Stdout) router := GocServer(os.Stdout)