From 57417c83ae8695e43e8b0cb2f7ba88660a3eb8e5 Mon Sep 17 00:00:00 2001 From: fatedier Date: Thu, 12 Jul 2018 15:23:34 +0800 Subject: [PATCH] add ci case of reload and reconnect --- tests/ci/cmd_test.go | 2 + tests/ci/normal_test.go | 35 +++++------ tests/ci/reconnect_test.go | 116 +++++++++++++++++++++++++++++++++++++ tests/ci/reload_test.go | 60 +++++++++++++++---- tests/consts/consts.go | 3 + tests/util/util.go | 22 +++++++ 6 files changed, 208 insertions(+), 30 deletions(-) diff --git a/tests/ci/cmd_test.go b/tests/ci/cmd_test.go index 006e04c2..77803975 100644 --- a/tests/ci/cmd_test.go +++ b/tests/ci/cmd_test.go @@ -1 +1,3 @@ package ci + +// TODO diff --git a/tests/ci/normal_test.go b/tests/ci/normal_test.go index 1ee0f58d..c5283460 100644 --- a/tests/ci/normal_test.go +++ b/tests/ci/normal_test.go @@ -4,6 +4,7 @@ import ( "fmt" "net/http" "net/url" + "os" "strings" "testing" "time" @@ -20,40 +21,36 @@ import ( gnet "github.com/fatedier/golib/net" ) -func init() { +func TestMain(m *testing.M) { go mock.StartTcpEchoServer(consts.TEST_TCP_PORT) go mock.StartTcpEchoServer2(consts.TEST_TCP2_PORT) go mock.StartUdpEchoServer(consts.TEST_UDP_PORT) go mock.StartUnixDomainServer(consts.TEST_UNIX_DOMAIN_ADDR) go mock.StartHttpServer(consts.TEST_HTTP_PORT) - if err := runFrps(); err != nil { + var err error + p1 := util.NewProcess(consts.FRPS_BIN_PATH, []string{"-c", "./auto_test_frps.ini"}) + if err = p1.Start(); err != nil { panic(err) } - time.Sleep(200 * time.Millisecond) - if err := runFrpc(); err != nil { + time.Sleep(200 * time.Millisecond) + p2 := util.NewProcess(consts.FRPC_BIN_PATH, []string{"-c", "./auto_test_frpc.ini"}) + if err = p2.Start(); err != nil { panic(err) } - if err := runFrpcVisitor(); err != nil { + + p3 := util.NewProcess(consts.FRPC_BIN_PATH, []string{"-c", "./auto_test_frpc_visitor.ini"}) + if err = p3.Start(); err != nil { panic(err) } time.Sleep(500 * time.Millisecond) -} -func runFrps() error { - p := util.NewProcess(consts.FRPS_BIN_PATH, []string{"-c", "./auto_test_frps.ini"}) - return p.Start() -} - -func runFrpc() error { - p := util.NewProcess(consts.FRPC_BIN_PATH, []string{"-c", "./auto_test_frpc.ini"}) - return p.Start() -} - -func runFrpcVisitor() error { - p := util.NewProcess(consts.FRPC_BIN_PATH, []string{"-c", "./auto_test_frpc_visitor.ini"}) - return p.Start() + exitCode := m.Run() + p1.Stop() + p2.Stop() + p3.Stop() + os.Exit(exitCode) } func TestTcp(t *testing.T) { diff --git a/tests/ci/reconnect_test.go b/tests/ci/reconnect_test.go index 006e04c2..2378a080 100644 --- a/tests/ci/reconnect_test.go +++ b/tests/ci/reconnect_test.go @@ -1 +1,117 @@ package ci + +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 +# debug, info, warn, error +log_level = debug +token = 123456 +` + +const FRPC_RECONNECT_CONF = ` +[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 +` + +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(200 * time.Millisecond) + + frpcProcess := util.NewProcess(consts.FRPC_BIN_PATH, []string{"-c", frpcCfgPath}) + err = frpcProcess.Start() + if assert.NoError(err) { + defer frpcProcess.Stop() + } + time.Sleep(250 * time.Millisecond) + + // test tcp + res, err := util.SendTcpMsg("127.0.0.1:20801", consts.TEST_TCP_ECHO_STR) + assert.NoError(err) + assert.Equal(consts.TEST_TCP_ECHO_STR, res) + + // stop frpc + frpcProcess.Stop() + time.Sleep(100 * time.Millisecond) + + // test tcp, expect failed + _, err = util.SendTcpMsg("127.0.0.1:20801", consts.TEST_TCP_ECHO_STR) + 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() + } + time.Sleep(250 * time.Millisecond) + + // test tcp + res, err = util.SendTcpMsg("127.0.0.1:20801", consts.TEST_TCP_ECHO_STR) + assert.NoError(err) + assert.Equal(consts.TEST_TCP_ECHO_STR, res) + + // stop frps + frpsProcess.Stop() + time.Sleep(100 * time.Millisecond) + + // test tcp, expect failed + _, err = util.SendTcpMsg("127.0.0.1:20801", consts.TEST_TCP_ECHO_STR) + 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 + res, err = util.SendTcpMsg("127.0.0.1:20801", consts.TEST_TCP_ECHO_STR) + assert.NoError(err) + assert.Equal(consts.TEST_TCP_ECHO_STR, res) +} diff --git a/tests/ci/reload_test.go b/tests/ci/reload_test.go index 0a89f89d..5ba358a3 100644 --- a/tests/ci/reload_test.go +++ b/tests/ci/reload_test.go @@ -12,20 +12,17 @@ import ( "github.com/fatedier/frp/tests/util" ) -const FRPS_CONF = ` +const FRPS_RELOAD_CONF = ` [common] -server_addr = 127.0.0.1 -server_port = 10700 +bind_addr = 0.0.0.0 +bind_port = 20000 log_file = console # debug, info, warn, error log_level = debug token = 123456 -admin_port = 10600 -admin_user = abc -admin_pwd = abc ` -const FRPC_CONF_1 = ` +const FRPC_RELOAD_CONF_1 = ` [common] server_addr = 127.0.0.1 server_port = 20000 @@ -55,7 +52,7 @@ local_port = 10701 remote_port = 20803 ` -const FRPC_CONF_2 = ` +const FRPC_RELOAD_CONF_2 = ` [common] server_addr = 127.0.0.1 server_port = 20000 @@ -80,12 +77,12 @@ remote_port = 20902 func TestReload(t *testing.T) { assert := assert.New(t) - frpsCfgPath, err := config.GenerateConfigFile("./auto_test_frps.ini", FRPS_CONF) + frpsCfgPath, err := config.GenerateConfigFile(consts.FRPS_NORMAL_CONFIG, FRPS_RELOAD_CONF) if assert.NoError(err) { defer os.Remove(frpsCfgPath) } - frpcCfgPath, err := config.GenerateConfigFile("./auto_test_frpc.ini", FRPC_CONF_1) + frpcCfgPath, err := config.GenerateConfigFile(consts.FRPC_NORMAL_CONFIG, FRPC_RELOAD_CONF_1) if assert.NoError(err) { defer os.Remove(frpcCfgPath) } @@ -104,5 +101,46 @@ func TestReload(t *testing.T) { defer frpcProcess.Stop() } - // TODO + time.Sleep(250 * time.Millisecond) + + // test tcp1 + res, err := util.SendTcpMsg("127.0.0.1:20801", consts.TEST_TCP_ECHO_STR) + assert.NoError(err) + assert.Equal(consts.TEST_TCP_ECHO_STR, res) + + // test tcp2 + res, err = util.SendTcpMsg("127.0.0.1:20802", consts.TEST_TCP_ECHO_STR) + assert.NoError(err) + assert.Equal(consts.TEST_TCP_ECHO_STR, res) + + // test tcp3 + res, err = util.SendTcpMsg("127.0.0.1:20803", consts.TEST_TCP_ECHO_STR) + 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) + assert.NoError(err) + err = util.ReloadConf("127.0.0.1:21000", "abc", "abc") + assert.NoError(err) + + time.Sleep(time.Second) + + // test tcp1 + res, err = util.SendTcpMsg("127.0.0.1:20801", consts.TEST_TCP_ECHO_STR) + assert.NoError(err) + assert.Equal(consts.TEST_TCP_ECHO_STR, res) + + // test origin tcp2, expect failed + res, err = util.SendTcpMsg("127.0.0.1:20802", consts.TEST_TCP_ECHO_STR) + assert.Error(err) + + // test new origin tcp2 with different port + res, err = util.SendTcpMsg("127.0.0.1:20902", consts.TEST_TCP_ECHO_STR) + assert.NoError(err) + assert.Equal(consts.TEST_TCP_ECHO_STR, res) + + // test tcp3, expect failed + res, err = util.SendTcpMsg("127.0.0.1:20803", consts.TEST_TCP_ECHO_STR) + assert.Error(err) } diff --git a/tests/consts/consts.go b/tests/consts/consts.go index 9deae3a8..60dcffee 100644 --- a/tests/consts/consts.go +++ b/tests/consts/consts.go @@ -6,6 +6,9 @@ var ( FRPS_BIN_PATH = "../../bin/frps" FRPC_BIN_PATH = "../../bin/frpc" + FRPS_NORMAL_CONFIG = "./auto_test_frps.ini" + FRPC_NORMAL_CONFIG = "./auto_test_frpc.ini" + SERVER_ADDR = "127.0.0.1" ADMIN_ADDR = "127.0.0.1:10600" ADMIN_USER = "abc" diff --git a/tests/util/util.go b/tests/util/util.go index ac314ec7..2070ce31 100644 --- a/tests/util/util.go +++ b/tests/util/util.go @@ -5,6 +5,7 @@ import ( "encoding/json" "errors" "fmt" + "io" "io/ioutil" "net" "net/http" @@ -75,6 +76,27 @@ func GetProxyStatus(statusAddr string, user string, passwd string, name string) return status, errors.New("no proxy status found") } +func ReloadConf(reloadAddr string, user string, passwd string) error { + req, err := http.NewRequest("GET", "http://"+reloadAddr+"/api/reload", nil) + if err != nil { + return err + } + + authStr := "Basic " + base64.StdEncoding.EncodeToString([]byte(user+":"+passwd)) + req.Header.Add("Authorization", authStr) + resp, err := http.DefaultClient.Do(req) + if err != nil { + return err + } else { + if resp.StatusCode != 200 { + return fmt.Errorf("admin api status code [%d]", resp.StatusCode) + } + defer resp.Body.Close() + io.Copy(ioutil.Discard, resp.Body) + } + return nil +} + func SendTcpMsg(addr string, msg string) (res string, err error) { c, err := frpNet.ConnectTcpServer(addr) if err != nil {