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 (
|
|
|
|
"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-06-12 11:22:34 +00:00
|
|
|
ts := httptest.NewServer(GocServer(os.Stdout))
|
|
|
|
defer ts.Close()
|
|
|
|
var client = NewWorker(ts.URL)
|
|
|
|
|
2020-07-16 04:19:26 +00:00
|
|
|
// mock profile server
|
|
|
|
profileMockResponse := "mode: count\nmockService/main.go:30.13,48.33 13 1"
|
|
|
|
profileSuccessMockSvr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
w.Write([]byte(profileMockResponse))
|
|
|
|
}))
|
|
|
|
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-06-12 11:22:34 +00:00
|
|
|
// regsiter service into goc server
|
|
|
|
var src Service
|
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-07-16 04:19:26 +00:00
|
|
|
// do list and check service
|
|
|
|
res, err = client.ListServices()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Contains(t, string(res), src.Address)
|
|
|
|
assert.Contains(t, string(res), src.Name)
|
|
|
|
|
2020-07-10 07:17:14 +00:00
|
|
|
// get porfile from goc server
|
|
|
|
profileItems := []struct {
|
2020-07-16 04:19:26 +00:00
|
|
|
service Service
|
|
|
|
param ProfileParam
|
|
|
|
res string
|
2020-07-10 07:17:14 +00:00
|
|
|
}{
|
|
|
|
{
|
2020-07-16 04:19:26 +00:00
|
|
|
service: Service{Name: "serviceOK", Address: profileSuccessMockSvr.URL},
|
|
|
|
param: ProfileParam{Force: false, Service: []string{"serviceOK"}, Address: []string{profileSuccessMockSvr.URL}},
|
2020-07-16 12:58:58 +00:00
|
|
|
res: "use 'service' flag and 'address' flag at the same time may cause ambiguity, please use them separately",
|
2020-07-10 07:17:14 +00:00
|
|
|
},
|
|
|
|
{
|
2020-07-16 04:19:26 +00:00
|
|
|
service: Service{Name: "serviceOK", Address: profileSuccessMockSvr.URL},
|
|
|
|
param: ProfileParam{},
|
|
|
|
res: profileMockResponse,
|
2020-07-10 07:17:14 +00:00
|
|
|
},
|
|
|
|
{
|
2020-07-16 04:19:26 +00:00
|
|
|
service: Service{Name: "serviceOK", Address: profileSuccessMockSvr.URL},
|
|
|
|
param: ProfileParam{Service: []string{"serviceOK"}},
|
|
|
|
res: profileMockResponse,
|
2020-07-10 07:17:14 +00:00
|
|
|
},
|
|
|
|
{
|
2020-07-16 04:19:26 +00:00
|
|
|
service: Service{Name: "serviceOK", Address: profileSuccessMockSvr.URL},
|
|
|
|
param: ProfileParam{Address: []string{profileSuccessMockSvr.URL}},
|
|
|
|
res: profileMockResponse,
|
2020-07-10 07:17:14 +00:00
|
|
|
},
|
|
|
|
{
|
2020-07-16 04:19:26 +00:00
|
|
|
service: Service{Name: "serviceOK", Address: profileSuccessMockSvr.URL},
|
|
|
|
param: ProfileParam{Service: []string{"unknown"}},
|
|
|
|
res: "service [unknown] not found",
|
2020-07-10 07:17:14 +00:00
|
|
|
},
|
|
|
|
{
|
2020-07-16 04:19:26 +00:00
|
|
|
service: Service{Name: "serviceErr", Address: profileErrMockSvr.URL},
|
|
|
|
res: "bad mode line: error",
|
2020-07-10 07:17:14 +00:00
|
|
|
},
|
|
|
|
{
|
2020-07-16 04:19:26 +00:00
|
|
|
service: Service{Name: "serviceNotExist", Address: "http://172.0.0.2:7777"},
|
|
|
|
res: "connection refused",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
service: Service{Name: "serviceNotExist", Address: "http://172.0.0.2:7777"},
|
|
|
|
param: ProfileParam{Force: true},
|
|
|
|
res: "no profiles",
|
2020-07-10 07:17:14 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, item := range profileItems {
|
2020-07-16 04:19:26 +00:00
|
|
|
// init server
|
2020-07-31 07:16:55 +00:00
|
|
|
_, err = client.InitSystem()
|
2020-07-16 04:19:26 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
// register server
|
|
|
|
res, err = client.RegisterService(item.service)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Contains(t, string(res), "success")
|
2020-07-10 07:17:14 +00:00
|
|
|
res, err = client.Profile(item.param)
|
|
|
|
if err != nil {
|
2020-07-16 04:19:26 +00:00
|
|
|
assert.Equal(t, err.Error(), item.res)
|
2020-07-10 07:17:14 +00:00
|
|
|
} else {
|
2020-07-16 04:19:26 +00:00
|
|
|
assert.Contains(t, string(res), item.res)
|
2020-07-10 07:17:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-12 11:22:34 +00:00
|
|
|
// init system and check service 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
|
|
|
|
testService1 := Service{
|
|
|
|
Address: "",
|
|
|
|
Name: "abc",
|
|
|
|
}
|
|
|
|
_, err := c.RegisterService(testService1)
|
|
|
|
assert.Contains(t, err.Error(), "empty url")
|
|
|
|
|
|
|
|
// client register with empty name
|
|
|
|
testService2 := Service{
|
|
|
|
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
|
|
|
}
|