health: add more ci cases and fix bugs

This commit is contained in:
fatedier
2018-12-09 21:56:46 +08:00
parent 08c17c3247
commit aea9f9fbcc
14 changed files with 409 additions and 66 deletions

View File

@@ -0,0 +1,247 @@
package health
import (
"net/http"
"os"
"strings"
"sync"
"testing"
"time"
"github.com/fatedier/frp/tests/config"
"github.com/fatedier/frp/tests/consts"
"github.com/fatedier/frp/tests/mock"
"github.com/fatedier/frp/tests/util"
"github.com/stretchr/testify/assert"
)
const FRPS_CONF = `
[common]
bind_addr = 0.0.0.0
bind_port = 14000
vhost_http_port = 14000
log_file = console
log_level = debug
token = 123456
`
const FRPC_CONF = `
[common]
server_addr = 127.0.0.1
server_port = 14000
log_file = console
log_level = debug
token = 123456
[tcp1]
type = tcp
local_port = 15001
remote_port = 15000
group = test
group_key = 123
health_check_type = tcp
health_check_interval_s = 1
[tcp2]
type = tcp
local_port = 15002
remote_port = 15000
group = test
group_key = 123
health_check_type = tcp
health_check_interval_s = 1
[http1]
type = http
local_port = 15003
custom_domains = test1.com
health_check_type = http
health_check_interval_s = 1
health_check_url = /health
[http2]
type = http
local_port = 15004
custom_domains = test2.com
health_check_type = http
health_check_interval_s = 1
health_check_url = /health
`
func TestHealthCheck(t *testing.T) {
assert := assert.New(t)
// ****** start backgroud services ******
echoSvc1 := mock.NewEchoServer(15001, 1, "echo1")
err := echoSvc1.Start()
if assert.NoError(err) {
defer echoSvc1.Stop()
}
echoSvc2 := mock.NewEchoServer(15002, 1, "echo2")
err = echoSvc2.Start()
if assert.NoError(err) {
defer echoSvc2.Stop()
}
var healthMu sync.RWMutex
svc1Health := true
svc2Health := true
httpSvc1 := mock.NewHttpServer(15003, func(w http.ResponseWriter, r *http.Request) {
if strings.Contains(r.URL.Path, "health") {
healthMu.RLock()
defer healthMu.RUnlock()
if svc1Health {
w.WriteHeader(200)
} else {
w.WriteHeader(500)
}
} else {
w.Write([]byte("http1"))
}
})
err = httpSvc1.Start()
if assert.NoError(err) {
defer httpSvc1.Stop()
}
httpSvc2 := mock.NewHttpServer(15004, func(w http.ResponseWriter, r *http.Request) {
if strings.Contains(r.URL.Path, "health") {
healthMu.RLock()
defer healthMu.RUnlock()
if svc2Health {
w.WriteHeader(200)
} else {
w.WriteHeader(500)
}
} else {
w.Write([]byte("http2"))
}
})
err = httpSvc2.Start()
if assert.NoError(err) {
defer httpSvc2.Stop()
}
time.Sleep(200 * time.Millisecond)
// ****** start frps and frpc ******
frpsCfgPath, err := config.GenerateConfigFile(consts.FRPS_NORMAL_CONFIG, FRPS_CONF)
if assert.NoError(err) {
defer os.Remove(frpsCfgPath)
}
frpcCfgPath, err := config.GenerateConfigFile(consts.FRPC_NORMAL_CONFIG, FRPC_CONF)
if assert.NoError(err) {
defer os.Remove(frpcCfgPath)
}
frpsProcess := util.NewProcess(consts.FRPS_SUB_BIN_PATH, []string{"-c", frpsCfgPath})
err = frpsProcess.Start()
if assert.NoError(err) {
defer frpsProcess.Stop()
}
time.Sleep(100 * time.Millisecond)
frpcProcess := util.NewProcess(consts.FRPC_SUB_BIN_PATH, []string{"-c", frpcCfgPath})
err = frpcProcess.Start()
if assert.NoError(err) {
defer frpcProcess.Stop()
}
time.Sleep(1000 * time.Millisecond)
// ****** healcheck type tcp ******
// echo1 and echo2 is ok
result := make([]string, 0)
res, err := util.SendTcpMsg("127.0.0.1:15000", "echo")
assert.NoError(err)
result = append(result, res)
res, err = util.SendTcpMsg("127.0.0.1:15000", "echo")
assert.NoError(err)
result = append(result, res)
assert.Contains(result, "echo1")
assert.Contains(result, "echo2")
// close echo2 server, echo1 is work
echoSvc2.Stop()
time.Sleep(1200 * time.Millisecond)
result = make([]string, 0)
res, err = util.SendTcpMsg("127.0.0.1:15000", "echo")
assert.NoError(err)
result = append(result, res)
res, err = util.SendTcpMsg("127.0.0.1:15000", "echo")
assert.NoError(err)
result = append(result, res)
assert.NotContains(result, "echo2")
// resume echo2 server, all services are ok
echoSvc2 = mock.NewEchoServer(15002, 1, "echo2")
err = echoSvc2.Start()
if assert.NoError(err) {
defer echoSvc2.Stop()
}
time.Sleep(1200 * time.Millisecond)
result = make([]string, 0)
res, err = util.SendTcpMsg("127.0.0.1:15000", "echo")
assert.NoError(err)
result = append(result, res)
res, err = util.SendTcpMsg("127.0.0.1:15000", "echo")
assert.NoError(err)
result = append(result, res)
assert.Contains(result, "echo1")
assert.Contains(result, "echo2")
// ****** healcheck type http ******
// http1 and http2 is ok
code, body, _, err := util.SendHttpMsg("GET", "http://127.0.0.1:14000/xxx", "test1.com", nil, "")
assert.NoError(err)
assert.Equal(200, code)
assert.Equal("http1", body)
code, body, _, err = util.SendHttpMsg("GET", "http://127.0.0.1:14000/xxx", "test2.com", nil, "")
assert.NoError(err)
assert.Equal(200, code)
assert.Equal("http2", body)
// http2 health check error
healthMu.Lock()
svc2Health = false
healthMu.Unlock()
time.Sleep(1200 * time.Millisecond)
code, body, _, err = util.SendHttpMsg("GET", "http://127.0.0.1:14000/xxx", "test1.com", nil, "")
assert.NoError(err)
assert.Equal(200, code)
assert.Equal("http1", body)
code, _, _, err = util.SendHttpMsg("GET", "http://127.0.0.1:14000/xxx", "test2.com", nil, "")
assert.NoError(err)
assert.Equal(404, code)
// resume http2 service, http1 and http2 are ok
healthMu.Lock()
svc2Health = true
healthMu.Unlock()
time.Sleep(1200 * time.Millisecond)
code, body, _, err = util.SendHttpMsg("GET", "http://127.0.0.1:14000/xxx", "test1.com", nil, "")
assert.NoError(err)
assert.Equal(200, code)
assert.Equal("http1", body)
code, body, _, err = util.SendHttpMsg("GET", "http://127.0.0.1:14000/xxx", "test2.com", nil, "")
assert.NoError(err)
assert.Equal(200, code)
assert.Equal("http2", body)
}

View File

@@ -22,13 +22,21 @@ import (
)
func TestMain(m *testing.M) {
go mock.StartTcpEchoServer(consts.TEST_TCP_PORT)
go mock.StartTcpEchoServer2(consts.TEST_TCP2_PORT)
var err error
tcpEcho1 := mock.NewEchoServer(consts.TEST_TCP_PORT, 1, "")
tcpEcho2 := mock.NewEchoServer(consts.TEST_TCP2_PORT, 2, "")
if err = tcpEcho1.Start(); err != nil {
panic(err)
}
if err = tcpEcho2.Start(); err != nil {
panic(err)
}
go mock.StartUdpEchoServer(consts.TEST_UDP_PORT)
go mock.StartUnixDomainServer(consts.TEST_UNIX_DOMAIN_ADDR)
go mock.StartHttpServer(consts.TEST_HTTP_PORT)
var err error
p1 := util.NewProcess(consts.FRPS_BIN_PATH, []string{"-c", "./auto_test_frps.ini"})
if err = p1.Start(); err != nil {
panic(err)

View File

@@ -17,7 +17,6 @@ const FRPS_RECONNECT_CONF = `
bind_addr = 0.0.0.0
bind_port = 20000
log_file = console
# debug, info, warn, error
log_level = debug
token = 123456
`
@@ -27,7 +26,6 @@ const FRPC_RECONNECT_CONF = `
server_addr = 127.0.0.1
server_port = 20000
log_file = console
# debug, info, warn, error
log_level = debug
token = 123456
admin_port = 21000

View File

@@ -84,7 +84,8 @@ func TestReload(t *testing.T) {
frpcCfgPath, err := config.GenerateConfigFile(consts.FRPC_NORMAL_CONFIG, FRPC_RELOAD_CONF_1)
if assert.NoError(err) {
defer os.Remove(frpcCfgPath)
rmFile1 := frpcCfgPath
defer os.Remove(rmFile1)
}
frpsProcess := util.NewProcess(consts.FRPS_BIN_PATH, []string{"-c", frpsCfgPath})
@@ -120,7 +121,10 @@ func TestReload(t *testing.T) {
// reload frpc config
frpcCfgPath, err = config.GenerateConfigFile(consts.FRPC_NORMAL_CONFIG, FRPC_RELOAD_CONF_2)
assert.NoError(err)
if assert.NoError(err) {
rmFile2 := frpcCfgPath
defer os.Remove(rmFile2)
}
err = util.ReloadConf("127.0.0.1:21000", "abc", "abc")
assert.NoError(err)