1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-09-05 03:24:21 -04:00
v2fly/testing/scenarios/server_env.go

100 lines
2.3 KiB
Go
Raw Normal View History

2015-12-04 10:49:10 -05:00
package scenarios
import (
2016-06-03 18:38:22 -04:00
"io/ioutil"
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
_ "github.com/v2ray/v2ray-core/app/router/rules"
"github.com/v2ray/v2ray-core/common/log"
2016-02-05 16:04:43 -05:00
// The following are necessary as they register handlers in their init functions.
2015-12-04 10:49:10 -05:00
_ "github.com/v2ray/v2ray-core/proxy/blackhole"
_ "github.com/v2ray/v2ray-core/proxy/dokodemo"
_ "github.com/v2ray/v2ray-core/proxy/freedom"
2016-01-24 15:48:38 -05:00
_ "github.com/v2ray/v2ray-core/proxy/http"
2016-01-28 07:40:00 -05:00
_ "github.com/v2ray/v2ray-core/proxy/shadowsocks"
2015-12-04 10:49:10 -05:00
_ "github.com/v2ray/v2ray-core/proxy/socks"
2015-12-07 14:32:38 -05:00
_ "github.com/v2ray/v2ray-core/proxy/vmess/inbound"
_ "github.com/v2ray/v2ray-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)
binaryPath string
2015-12-04 11:32:42 -05:00
)
2016-06-03 18:38:22 -04:00
func BuildV2Ray() error {
if len(binaryPath) > 0 {
return nil
}
dir, err := ioutil.TempDir("", "v2ray")
if err != nil {
return err
}
2016-06-04 06:41:48 -04:00
binaryPath = filepath.Join(dir, "v2ray")
if runtime.GOOS == "windows" {
binaryPath += ".exe"
}
2016-06-03 18:38:22 -04:00
cmd := exec.Command("go", "build", "-tags=json", "-o="+binaryPath, filepath.Join("github.com", "v2ray", "v2ray-core", "release", "server"))
return cmd.Run()
}
2015-12-04 10:49:10 -05:00
func TestFile(filename string) string {
2015-12-04 10:49:45 -05:00
return filepath.Join(os.Getenv("GOPATH"), "src", "github.com", "v2ray", "v2ray-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-06-03 18:38:22 -04:00
proc := exec.Command(binaryPath, "-config="+configFile)
proc.Stderr = os.Stderr
proc.Stdout = os.Stdout
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-06-03 18:38:22 -04:00
server.Process.Kill()
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
}