2020-06-12 11:22:34 +00:00
|
|
|
/*
|
|
|
|
Copyright 2020 Qiniu Cloud (qiniu.com)
|
|
|
|
|
|
|
|
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 (
|
2020-09-20 12:29:17 +00:00
|
|
|
"fmt"
|
2020-06-12 11:22:34 +00:00
|
|
|
"net/http/httptest"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
2020-07-16 04:19:26 +00:00
|
|
|
"net/http"
|
2020-07-21 03:49:57 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2020-06-12 11:22:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestClientAction(t *testing.T) {
|
2020-07-16 04:19:26 +00:00
|
|
|
// mock goc server
|
2020-09-25 07:13:36 +00:00
|
|
|
server, err := NewFileBasedServer("_svrs_address.txt")
|
|
|
|
assert.NoError(t, err)
|
2020-09-23 06:14:01 +00:00
|
|
|
ts := httptest.NewServer(server.Route(os.Stdout))
|
2020-06-12 11:22:34 +00:00
|
|
|
defer ts.Close()
|
|
|
|
var client = NewWorker(ts.URL)
|
|
|
|
|
2020-07-16 04:19:26 +00:00
|
|
|
// mock profile server
|
2020-09-05 10:32:55 +00:00
|
|
|
profileMockResponse := []byte("mode: count\nmockService/main.go:30.13,48.33 13 1\nb/b.go:30.13,48.33 13 1")
|
2020-07-16 04:19:26 +00:00
|
|
|
profileSuccessMockSvr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.WriteHeader(http.StatusOK)
|
2020-09-05 10:49:29 +00:00
|
|
|
_, _ = w.Write(profileMockResponse)
|
2020-07-16 04:19:26 +00:00
|
|
|
}))
|
|
|
|
defer profileSuccessMockSvr.Close()
|
|
|
|
|
|
|
|
profileErrMockSvr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
w.Write([]byte("error"))
|
|
|
|
}))
|
|
|
|
defer profileErrMockSvr.Close()
|
|
|
|
|
2020-09-25 07:13:36 +00:00
|
|
|
// register service into goc server
|
2020-09-23 06:14:01 +00:00
|
|
|
var src ServiceUnderTest
|
2020-07-16 04:19:26 +00:00
|
|
|
src.Name = "serviceSuccess"
|
|
|
|
src.Address = profileSuccessMockSvr.URL
|
2020-06-12 11:22:34 +00:00
|
|
|
res, err := client.RegisterService(src)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Contains(t, string(res), "success")
|
|
|
|
|
2020-09-23 06:14:01 +00:00
|
|
|
// do list and check server
|
2020-07-16 04:19:26 +00:00
|
|
|
res, err = client.ListServices()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Contains(t, string(res), src.Address)
|
|
|
|
assert.Contains(t, string(res), src.Name)
|
|
|
|
|
2020-09-05 10:32:55 +00:00
|
|
|
// get profile from goc server
|
|
|
|
tcs := []struct {
|
|
|
|
name string
|
2020-09-23 06:14:01 +00:00
|
|
|
service ServiceUnderTest
|
2020-09-05 10:32:55 +00:00
|
|
|
param ProfileParam
|
|
|
|
expected string
|
|
|
|
expectedErr bool
|
2020-07-10 07:17:14 +00:00
|
|
|
}{
|
|
|
|
{
|
2020-09-23 06:14:01 +00:00
|
|
|
name: "both server and address existed",
|
|
|
|
service: ServiceUnderTest{Name: "serviceOK", Address: profileSuccessMockSvr.URL},
|
2020-09-05 10:32:55 +00:00
|
|
|
param: ProfileParam{Force: false, Service: []string{"serviceOK"}, Address: []string{profileSuccessMockSvr.URL}},
|
|
|
|
expectedErr: true,
|
2020-07-10 07:17:14 +00:00
|
|
|
},
|
|
|
|
{
|
2020-09-23 06:14:01 +00:00
|
|
|
name: "valid test with no server flag provide",
|
|
|
|
service: ServiceUnderTest{Name: "serviceOK", Address: profileSuccessMockSvr.URL},
|
2020-09-05 10:32:55 +00:00
|
|
|
param: ProfileParam{},
|
|
|
|
expected: "mockService/main.go:30.13,48.33 13 1",
|
2020-07-10 07:17:14 +00:00
|
|
|
},
|
|
|
|
{
|
2020-09-23 06:14:01 +00:00
|
|
|
name: "valid test with server flag provide",
|
|
|
|
service: ServiceUnderTest{Name: "serviceOK", Address: profileSuccessMockSvr.URL},
|
2020-09-05 10:32:55 +00:00
|
|
|
param: ProfileParam{Service: []string{"serviceOK"}},
|
|
|
|
expected: "mockService/main.go:30.13,48.33 13 1",
|
2020-07-10 07:17:14 +00:00
|
|
|
},
|
|
|
|
{
|
2020-09-09 11:38:16 +00:00
|
|
|
name: "valid test with address flag provide",
|
2020-09-23 06:14:01 +00:00
|
|
|
service: ServiceUnderTest{Name: "serviceOK", Address: profileSuccessMockSvr.URL},
|
2020-09-05 10:32:55 +00:00
|
|
|
param: ProfileParam{Address: []string{profileSuccessMockSvr.URL}},
|
|
|
|
expected: "mockService/main.go:30.13,48.33 13 1",
|
2020-07-10 07:17:14 +00:00
|
|
|
},
|
|
|
|
{
|
2020-09-23 06:14:01 +00:00
|
|
|
name: "invalid test with invalid server flag provide",
|
|
|
|
service: ServiceUnderTest{Name: "serviceOK", Address: profileSuccessMockSvr.URL},
|
2020-09-05 10:32:55 +00:00
|
|
|
param: ProfileParam{Service: []string{"unknown"}},
|
2020-09-23 06:14:01 +00:00
|
|
|
expected: "server [unknown] not found",
|
2020-09-05 10:32:55 +00:00
|
|
|
expectedErr: true,
|
2020-07-10 07:17:14 +00:00
|
|
|
},
|
|
|
|
{
|
2020-09-23 06:14:01 +00:00
|
|
|
name: "invalid test with invalid profile got by server",
|
|
|
|
service: ServiceUnderTest{Name: "serviceErr", Address: profileErrMockSvr.URL},
|
2020-09-05 10:32:55 +00:00
|
|
|
expected: "bad mode line: error",
|
|
|
|
expectedErr: true,
|
2020-07-10 07:17:14 +00:00
|
|
|
},
|
|
|
|
{
|
2020-09-23 06:14:01 +00:00
|
|
|
name: "invalid test with disconnected server",
|
|
|
|
service: ServiceUnderTest{Name: "serviceNotExist", Address: "http://172.0.0.2:7777"},
|
2020-09-05 10:32:55 +00:00
|
|
|
expected: "connection refused",
|
|
|
|
expectedErr: true,
|
2020-07-16 04:19:26 +00:00
|
|
|
},
|
|
|
|
{
|
2020-09-09 11:38:16 +00:00
|
|
|
name: "invalid test with empty profile",
|
2020-09-23 06:14:01 +00:00
|
|
|
service: ServiceUnderTest{Name: "serviceNotExist", Address: "http://172.0.0.2:7777"},
|
2020-09-09 11:38:16 +00:00
|
|
|
param: ProfileParam{Force: true},
|
|
|
|
expectedErr: true,
|
|
|
|
expected: "no profiles",
|
2020-09-05 10:32:55 +00:00
|
|
|
},
|
|
|
|
{
|
2020-09-09 11:38:16 +00:00
|
|
|
name: "valid test with coverfile flag provide",
|
2020-09-23 06:14:01 +00:00
|
|
|
service: ServiceUnderTest{Name: "serviceOK", Address: profileSuccessMockSvr.URL},
|
2020-09-05 10:32:55 +00:00
|
|
|
param: ProfileParam{CoverFilePatterns: []string{"b.go$"}},
|
|
|
|
expected: "b/b.go",
|
2020-07-10 07:17:14 +00:00
|
|
|
},
|
|
|
|
}
|
2020-09-05 10:32:55 +00:00
|
|
|
for _, tc := range tcs {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
// init server
|
|
|
|
_, err = client.InitSystem()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
// register server
|
|
|
|
res, err = client.RegisterService(tc.service)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Contains(t, string(res), "success")
|
|
|
|
res, err = client.Profile(tc.param)
|
|
|
|
if err != nil {
|
|
|
|
if !tc.expectedErr {
|
|
|
|
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))
|
|
|
|
})
|
2020-07-10 07:17:14 +00:00
|
|
|
}
|
|
|
|
|
2020-09-23 06:14:01 +00:00
|
|
|
// init system and check server again
|
2020-07-31 07:16:55 +00:00
|
|
|
_, err = client.InitSystem()
|
2020-06-12 11:22:34 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
res, err = client.ListServices()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "{}", string(res))
|
|
|
|
}
|
|
|
|
|
2020-07-21 03:49:57 +00:00
|
|
|
func TestClientRegisterService(t *testing.T) {
|
|
|
|
c := &client{}
|
|
|
|
|
|
|
|
// client register with empty address
|
2020-09-23 06:14:01 +00:00
|
|
|
testService1 := ServiceUnderTest{
|
2020-07-21 03:49:57 +00:00
|
|
|
Address: "",
|
|
|
|
Name: "abc",
|
|
|
|
}
|
|
|
|
_, err := c.RegisterService(testService1)
|
|
|
|
assert.Contains(t, err.Error(), "empty url")
|
|
|
|
|
|
|
|
// client register with empty name
|
2020-09-23 06:14:01 +00:00
|
|
|
testService2 := ServiceUnderTest{
|
2020-07-21 03:49:57 +00:00
|
|
|
Address: "http://127.0.0.1:444",
|
|
|
|
Name: "",
|
|
|
|
}
|
|
|
|
_, err = c.RegisterService(testService2)
|
|
|
|
assert.EqualError(t, err, "invalid service name")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClientListServices(t *testing.T) {
|
|
|
|
c := &client{
|
|
|
|
Host: "http://127.0.0.1:64445", // a invalid host
|
|
|
|
client: http.DefaultClient,
|
|
|
|
}
|
|
|
|
_, err := c.ListServices()
|
|
|
|
assert.Contains(t, err.Error(), "connect: connection refused")
|
2020-06-12 11:22:34 +00:00
|
|
|
}
|
2020-07-30 09:30:50 +00:00
|
|
|
|
|
|
|
func TestClientDo(t *testing.T) {
|
|
|
|
c := &client{
|
|
|
|
client: http.DefaultClient,
|
|
|
|
}
|
2020-09-04 13:16:42 +00:00
|
|
|
_, _, err := c.do(" ", "http://127.0.0.1:7777", "", nil) // a invalid method
|
2020-07-30 09:30:50 +00:00
|
|
|
assert.Contains(t, err.Error(), "invalid method")
|
|
|
|
}
|
2020-09-11 14:28:53 +00:00
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
2020-09-20 12:29:17 +00:00
|
|
|
|
|
|
|
func TestClientRemove(t *testing.T) {
|
|
|
|
// remove by invalid param
|
|
|
|
p := ProfileParam{
|
|
|
|
Service: []string{"goc"},
|
|
|
|
Address: []string{"http://127.0.0.1:777"},
|
|
|
|
}
|
|
|
|
c := &client{
|
|
|
|
client: http.DefaultClient,
|
|
|
|
}
|
|
|
|
_, err := c.Remove(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")
|
|
|
|
|
|
|
|
// remove by valid param
|
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
fmt.Fprintln(w, "Hello, client")
|
|
|
|
}))
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
c.Host = ts.URL
|
|
|
|
p = ProfileParam{
|
|
|
|
Address: []string{"http://127.0.0.1:777"},
|
|
|
|
}
|
|
|
|
res, err := c.Remove(p)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, string(res), "Hello, client\n")
|
|
|
|
|
|
|
|
// remove from a invalid center
|
|
|
|
c.Host = "http://127.0.0.1:11111"
|
|
|
|
_, err = c.Remove(p)
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|