frp/tests/ci/reconnect_test.go

116 lines
2.6 KiB
Go
Raw Normal View History

2018-07-12 00:31:21 +08:00
package ci
2018-07-12 15:23:34 +08:00
import (
"os"
"testing"
"time"
"github.com/fatedier/frp/tests/config"
"github.com/fatedier/frp/tests/consts"
"github.com/fatedier/frp/tests/util"
"github.com/stretchr/testify/assert"
)
const FRPS_RECONNECT_CONF = `
[common]
bind_addr = 0.0.0.0
bind_port = 20000
log_file = console
log_level = debug
token = 123456
`
const FRPC_RECONNECT_CONF = `
[common]
server_addr = 127.0.0.1
server_port = 20000
log_file = console
log_level = debug
token = 123456
admin_port = 21000
admin_user = abc
admin_pwd = abc
[tcp]
type = tcp
local_port = 10701
remote_port = 20801
`
func TestReconnect(t *testing.T) {
assert := assert.New(t)
frpsCfgPath, err := config.GenerateConfigFile(consts.FRPS_NORMAL_CONFIG, FRPS_RECONNECT_CONF)
if assert.NoError(err) {
defer os.Remove(frpsCfgPath)
}
frpcCfgPath, err := config.GenerateConfigFile(consts.FRPC_NORMAL_CONFIG, FRPC_RECONNECT_CONF)
if assert.NoError(err) {
defer os.Remove(frpcCfgPath)
}
frpsProcess := util.NewProcess(consts.FRPS_BIN_PATH, []string{"-c", frpsCfgPath})
err = frpsProcess.Start()
if assert.NoError(err) {
defer frpsProcess.Stop()
}
time.Sleep(500 * time.Millisecond)
2018-07-12 15:23:34 +08:00
frpcProcess := util.NewProcess(consts.FRPC_BIN_PATH, []string{"-c", frpcCfgPath})
err = frpcProcess.Start()
if assert.NoError(err) {
defer frpcProcess.Stop()
}
2019-03-11 17:11:26 +08:00
time.Sleep(500 * time.Millisecond)
2018-07-12 15:23:34 +08:00
// test tcp
2020-05-24 17:48:37 +08:00
res, err := util.SendTCPMsg("127.0.0.1:20801", consts.TEST_TCP_ECHO_STR)
2018-07-12 15:23:34 +08:00
assert.NoError(err)
assert.Equal(consts.TEST_TCP_ECHO_STR, res)
// stop frpc
frpcProcess.Stop()
2019-03-11 17:11:26 +08:00
time.Sleep(200 * time.Millisecond)
2018-07-12 15:23:34 +08:00
// test tcp, expect failed
2020-05-24 17:48:37 +08:00
_, err = util.SendTCPMsg("127.0.0.1:20801", consts.TEST_TCP_ECHO_STR)
2018-07-12 15:23:34 +08:00
assert.Error(err)
// restart frpc
newFrpcProcess := util.NewProcess(consts.FRPC_BIN_PATH, []string{"-c", frpcCfgPath})
err = newFrpcProcess.Start()
if assert.NoError(err) {
defer newFrpcProcess.Stop()
}
2019-03-11 17:11:26 +08:00
time.Sleep(500 * time.Millisecond)
2018-07-12 15:23:34 +08:00
// test tcp
2020-05-24 17:48:37 +08:00
res, err = util.SendTCPMsg("127.0.0.1:20801", consts.TEST_TCP_ECHO_STR)
2018-07-12 15:23:34 +08:00
assert.NoError(err)
assert.Equal(consts.TEST_TCP_ECHO_STR, res)
// stop frps
frpsProcess.Stop()
2019-03-11 17:11:26 +08:00
time.Sleep(200 * time.Millisecond)
2018-07-12 15:23:34 +08:00
// test tcp, expect failed
2020-05-24 17:48:37 +08:00
_, err = util.SendTCPMsg("127.0.0.1:20801", consts.TEST_TCP_ECHO_STR)
2018-07-12 15:23:34 +08:00
assert.Error(err)
// restart frps
newFrpsProcess := util.NewProcess(consts.FRPS_BIN_PATH, []string{"-c", frpsCfgPath})
err = newFrpsProcess.Start()
if assert.NoError(err) {
defer newFrpsProcess.Stop()
}
time.Sleep(2 * time.Second)
// test tcp
2020-05-24 17:48:37 +08:00
res, err = util.SendTCPMsg("127.0.0.1:20801", consts.TEST_TCP_ECHO_STR)
2018-07-12 15:23:34 +08:00
assert.NoError(err)
assert.Equal(consts.TEST_TCP_ECHO_STR, res)
}