1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-10-01 16:26:02 -04:00
v2fly/testing/scenarios/server_env.go

90 lines
1.9 KiB
Go
Raw Normal View History

2015-12-04 10:49:10 -05:00
package scenarios
import (
2015-12-04 10:49:45 -05:00
"os"
2016-06-03 18:38:22 -04:00
"os/exec"
2015-12-04 10:49:45 -05:00
"path/filepath"
2016-06-04 06:41:48 -04:00
"runtime"
2016-06-04 07:27:17 -04:00
"time"
2015-12-04 10:49:10 -05:00
2016-08-20 14:55:45 -04:00
"v2ray.com/core/common/log"
2015-12-04 10:49:10 -05:00
2016-02-05 16:04:43 -05:00
// The following are necessary as they register handlers in their init functions.
2016-08-20 14:55:45 -04:00
_ "v2ray.com/core/proxy/blackhole"
_ "v2ray.com/core/proxy/dokodemo"
_ "v2ray.com/core/proxy/freedom"
_ "v2ray.com/core/proxy/http"
_ "v2ray.com/core/proxy/shadowsocks"
_ "v2ray.com/core/proxy/socks"
_ "v2ray.com/core/proxy/vmess/inbound"
_ "v2ray.com/core/proxy/vmess/outbound"
2015-12-04 10:49:10 -05:00
)
2015-12-04 11:32:42 -05:00
var (
2016-06-03 18:38:22 -04:00
runningServers = make([]*exec.Cmd, 0, 10)
2015-12-04 11:32:42 -05:00
)
2016-08-09 16:13:16 -04:00
func GetTestBinaryPath() string {
file := filepath.Join(os.Getenv("GOPATH"), "out", "v2ray", "v2ray.test")
2016-06-04 06:41:48 -04:00
if runtime.GOOS == "windows" {
2016-08-09 16:13:16 -04:00
file += ".exe"
2016-06-04 06:41:48 -04:00
}
2016-08-09 16:13:16 -04:00
return file
}
func GetSourcePath() string {
2016-10-12 10:46:02 -04:00
return filepath.Join("v2ray.com", "core", "main")
2016-06-03 18:38:22 -04:00
}
2015-12-04 10:49:10 -05:00
func TestFile(filename string) string {
2016-08-20 15:03:50 -04:00
return filepath.Join(os.Getenv("GOPATH"), "src", "v2ray.com", "core", "testing", "scenarios", "data", filename)
2015-12-04 10:49:10 -05:00
}
2015-12-04 11:32:42 -05:00
func InitializeServerSetOnce(testcase string) error {
2016-01-28 07:40:00 -05:00
if err := InitializeServerServer(testcase); err != nil {
2015-12-04 11:32:42 -05:00
return err
}
2016-01-28 07:40:00 -05:00
if err := InitializeServerClient(testcase); err != nil {
2015-12-04 11:32:42 -05:00
return err
}
return nil
}
2016-01-28 07:40:00 -05:00
func InitializeServerServer(testcase string) error {
return InitializeServer(TestFile(testcase + "_server.json"))
}
func InitializeServerClient(testcase string) error {
return InitializeServer(TestFile(testcase + "_client.json"))
}
2015-12-04 10:49:10 -05:00
func InitializeServer(configFile string) error {
2016-06-03 18:38:22 -04:00
err := BuildV2Ray()
2015-12-04 10:49:10 -05:00
if err != nil {
return err
}
2016-08-08 18:55:36 -04:00
proc := RunV2Ray(configFile)
2015-12-04 10:49:10 -05:00
2016-06-03 18:38:22 -04:00
err = proc.Start()
2015-12-04 10:49:10 -05:00
if err != nil {
return err
}
2016-06-03 18:38:22 -04:00
2016-06-04 07:27:17 -04:00
time.Sleep(time.Second)
2016-06-03 18:38:22 -04:00
runningServers = append(runningServers, proc)
2015-12-04 10:49:10 -05:00
return nil
}
2016-01-03 18:33:25 -05:00
func CloseAllServers() {
2016-06-03 18:38:22 -04:00
log.Info("Closing all servers.")
2016-01-03 18:33:25 -05:00
for _, server := range runningServers {
2016-08-09 16:13:16 -04:00
server.Process.Signal(os.Interrupt)
server.Process.Wait()
2016-01-03 18:33:25 -05:00
}
2016-06-03 18:38:22 -04:00
runningServers = make([]*exec.Cmd, 0, 10)
log.Info("All server closed.")
2016-01-03 18:33:25 -05:00
}