2018-07-12 00:31:21 +08:00
|
|
|
package ci
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
|
|
|
"github.com/fatedier/frp/tests/config"
|
|
|
|
"github.com/fatedier/frp/tests/consts"
|
|
|
|
"github.com/fatedier/frp/tests/util"
|
|
|
|
)
|
|
|
|
|
2018-07-12 15:23:34 +08:00
|
|
|
const FRPS_RELOAD_CONF = `
|
2018-07-12 00:31:21 +08:00
|
|
|
[common]
|
2018-07-12 15:23:34 +08:00
|
|
|
bind_addr = 0.0.0.0
|
|
|
|
bind_port = 20000
|
2018-07-12 00:31:21 +08:00
|
|
|
log_file = console
|
|
|
|
# debug, info, warn, error
|
|
|
|
log_level = debug
|
|
|
|
token = 123456
|
|
|
|
`
|
|
|
|
|
2018-07-12 15:23:34 +08:00
|
|
|
const FRPC_RELOAD_CONF_1 = `
|
2018-07-12 00:31:21 +08:00
|
|
|
[common]
|
|
|
|
server_addr = 127.0.0.1
|
|
|
|
server_port = 20000
|
|
|
|
log_file = console
|
|
|
|
# debug, info, warn, error
|
|
|
|
log_level = debug
|
|
|
|
token = 123456
|
|
|
|
admin_port = 21000
|
|
|
|
admin_user = abc
|
|
|
|
admin_pwd = abc
|
|
|
|
|
|
|
|
[tcp]
|
|
|
|
type = tcp
|
|
|
|
local_port = 10701
|
|
|
|
remote_port = 20801
|
|
|
|
|
|
|
|
# change remote port
|
|
|
|
[tcp2]
|
|
|
|
type = tcp
|
|
|
|
local_port = 10701
|
|
|
|
remote_port = 20802
|
|
|
|
|
|
|
|
# delete
|
|
|
|
[tcp3]
|
|
|
|
type = tcp
|
|
|
|
local_port = 10701
|
|
|
|
remote_port = 20803
|
|
|
|
`
|
|
|
|
|
2018-07-12 15:23:34 +08:00
|
|
|
const FRPC_RELOAD_CONF_2 = `
|
2018-07-12 00:31:21 +08:00
|
|
|
[common]
|
|
|
|
server_addr = 127.0.0.1
|
|
|
|
server_port = 20000
|
|
|
|
log_file = console
|
|
|
|
# debug, info, warn, error
|
|
|
|
log_level = debug
|
|
|
|
token = 123456
|
|
|
|
admin_port = 21000
|
|
|
|
admin_user = abc
|
|
|
|
admin_pwd = abc
|
|
|
|
|
|
|
|
[tcp]
|
|
|
|
type = tcp
|
|
|
|
local_port = 10701
|
|
|
|
remote_port = 20801
|
|
|
|
|
|
|
|
[tcp2]
|
|
|
|
type = tcp
|
|
|
|
local_port = 10701
|
|
|
|
remote_port = 20902
|
|
|
|
`
|
|
|
|
|
|
|
|
func TestReload(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
2018-07-12 15:23:34 +08:00
|
|
|
frpsCfgPath, err := config.GenerateConfigFile(consts.FRPS_NORMAL_CONFIG, FRPS_RELOAD_CONF)
|
2018-07-12 00:31:21 +08:00
|
|
|
if assert.NoError(err) {
|
|
|
|
defer os.Remove(frpsCfgPath)
|
|
|
|
}
|
|
|
|
|
2018-07-12 15:23:34 +08:00
|
|
|
frpcCfgPath, err := config.GenerateConfigFile(consts.FRPC_NORMAL_CONFIG, FRPC_RELOAD_CONF_1)
|
2018-07-12 00:31:21 +08:00
|
|
|
if assert.NoError(err) {
|
2018-12-09 21:56:46 +08:00
|
|
|
rmFile1 := frpcCfgPath
|
|
|
|
defer os.Remove(rmFile1)
|
2018-07-12 00:31:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
frpsProcess := util.NewProcess(consts.FRPS_BIN_PATH, []string{"-c", frpsCfgPath})
|
|
|
|
err = frpsProcess.Start()
|
|
|
|
if assert.NoError(err) {
|
|
|
|
defer frpsProcess.Stop()
|
|
|
|
}
|
|
|
|
|
2021-02-18 16:15:35 +08:00
|
|
|
time.Sleep(500 * time.Millisecond)
|
2018-07-12 00:31:21 +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 tcp1
|
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)
|
|
|
|
|
|
|
|
// test tcp2
|
2020-05-24 17:48:37 +08:00
|
|
|
res, err = util.SendTCPMsg("127.0.0.1:20802", consts.TEST_TCP_ECHO_STR)
|
2018-07-12 15:23:34 +08:00
|
|
|
assert.NoError(err)
|
|
|
|
assert.Equal(consts.TEST_TCP_ECHO_STR, res)
|
|
|
|
|
|
|
|
// test tcp3
|
2020-05-24 17:48:37 +08:00
|
|
|
res, err = util.SendTCPMsg("127.0.0.1:20803", consts.TEST_TCP_ECHO_STR)
|
2018-07-12 15:23:34 +08:00
|
|
|
assert.NoError(err)
|
|
|
|
assert.Equal(consts.TEST_TCP_ECHO_STR, res)
|
|
|
|
|
|
|
|
// reload frpc config
|
|
|
|
frpcCfgPath, err = config.GenerateConfigFile(consts.FRPC_NORMAL_CONFIG, FRPC_RELOAD_CONF_2)
|
2018-12-09 21:56:46 +08:00
|
|
|
if assert.NoError(err) {
|
|
|
|
rmFile2 := frpcCfgPath
|
|
|
|
defer os.Remove(rmFile2)
|
|
|
|
}
|
2018-07-12 15:23:34 +08:00
|
|
|
err = util.ReloadConf("127.0.0.1:21000", "abc", "abc")
|
|
|
|
assert.NoError(err)
|
|
|
|
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
|
|
|
|
// test tcp1
|
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)
|
|
|
|
|
|
|
|
// test origin tcp2, expect failed
|
2020-05-24 17:48:37 +08:00
|
|
|
res, err = util.SendTCPMsg("127.0.0.1:20802", consts.TEST_TCP_ECHO_STR)
|
2018-07-12 15:23:34 +08:00
|
|
|
assert.Error(err)
|
|
|
|
|
|
|
|
// test new origin tcp2 with different port
|
2020-05-24 17:48:37 +08:00
|
|
|
res, err = util.SendTCPMsg("127.0.0.1:20902", consts.TEST_TCP_ECHO_STR)
|
2018-07-12 15:23:34 +08:00
|
|
|
assert.NoError(err)
|
|
|
|
assert.Equal(consts.TEST_TCP_ECHO_STR, res)
|
|
|
|
|
|
|
|
// test tcp3, expect failed
|
2020-05-24 17:48:37 +08:00
|
|
|
res, err = util.SendTCPMsg("127.0.0.1:20803", consts.TEST_TCP_ECHO_STR)
|
2018-07-12 15:23:34 +08:00
|
|
|
assert.Error(err)
|
2018-07-12 00:31:21 +08:00
|
|
|
}
|