diff --git a/testing/scenarios/common.go b/testing/scenarios/common.go index 4433ec354..8d9e609fe 100644 --- a/testing/scenarios/common.go +++ b/testing/scenarios/common.go @@ -1,14 +1,23 @@ package scenarios import ( + "fmt" "io" + "io/ioutil" "net" + "os" + "os/exec" + "path/filepath" + "runtime" + "sync" "time" "github.com/golang/protobuf/proto" "v2ray.com/core" + "v2ray.com/core/app/log" "v2ray.com/core/common" v2net "v2ray.com/core/common/net" + "v2ray.com/core/common/retry" ) func pickPort() v2net.Port { @@ -59,3 +68,46 @@ func InitializeServerConfig(config *core.Config) error { return nil } + +var ( + runningServers = make([]*exec.Cmd, 0, 10) + testBinaryPath string + testBinaryPathGen sync.Once +) + +func genTestBinaryPath() { + testBinaryPathGen.Do(func() { + var tempDir string + err := retry.Timed(5, 100).On(func() error { + dir, err := ioutil.TempDir("", "v2ray") + if err != nil { + return err + } + tempDir = dir + return nil + }) + if err != nil { + panic(err) + } + file := filepath.Join(tempDir, "v2ray.test") + if runtime.GOOS == "windows" { + file += ".exe" + } + testBinaryPath = file + fmt.Printf("Generated binary path: %s\n", file) + }) +} + +func GetSourcePath() string { + return filepath.Join("v2ray.com", "core", "main") +} + +func CloseAllServers() { + log.Info("Closing all servers.") + for _, server := range runningServers { + server.Process.Signal(os.Interrupt) + server.Process.Wait() + } + runningServers = make([]*exec.Cmd, 0, 10) + log.Info("All server closed.") +} diff --git a/testing/scenarios/server_env_coverage.go b/testing/scenarios/common_coverage.go similarity index 64% rename from testing/scenarios/server_env_coverage.go rename to testing/scenarios/common_coverage.go index 8a9529906..c29aceeae 100644 --- a/testing/scenarios/server_env_coverage.go +++ b/testing/scenarios/common_coverage.go @@ -3,16 +3,16 @@ package scenarios import ( + "bytes" "os" "os/exec" "path/filepath" - "bytes" "v2ray.com/core/common/uuid" ) func BuildV2Ray() error { - GenTestBinaryPath() + genTestBinaryPath() if _, err := os.Stat(testBinaryPath); err == nil { return nil } @@ -21,21 +21,8 @@ func BuildV2Ray() error { return cmd.Run() } -func RunV2Ray(configFile string) *exec.Cmd { - GenTestBinaryPath() - - covDir := filepath.Join(os.Getenv("GOPATH"), "out", "v2ray", "cov") - os.MkdirAll(covDir, os.ModeDir) - profile := uuid.New().String() + ".out" - proc := exec.Command(testBinaryPath, "-config", configFile, "-test.run", "TestRunMainForCoverage", "-test.coverprofile", profile, "-test.outputdir", covDir) - proc.Stderr = os.Stderr - proc.Stdout = os.Stdout - - return proc -} - func RunV2RayProtobuf(config []byte) *exec.Cmd { - GenTestBinaryPath() + genTestBinaryPath() covDir := filepath.Join(os.Getenv("GOPATH"), "out", "v2ray", "cov") os.MkdirAll(covDir, os.ModeDir) diff --git a/testing/scenarios/server_env_regular.go b/testing/scenarios/common_regular.go similarity index 70% rename from testing/scenarios/server_env_regular.go rename to testing/scenarios/common_regular.go index 23b35761e..6c5662d82 100644 --- a/testing/scenarios/server_env_regular.go +++ b/testing/scenarios/common_regular.go @@ -10,7 +10,7 @@ import ( ) func BuildV2Ray() error { - GenTestBinaryPath() + genTestBinaryPath() if _, err := os.Stat(testBinaryPath); err == nil { return nil } @@ -20,17 +20,8 @@ func BuildV2Ray() error { return cmd.Run() } -func RunV2Ray(configFile string) *exec.Cmd { - GenTestBinaryPath() - proc := exec.Command(testBinaryPath, "-config", configFile) - proc.Stderr = os.Stderr - proc.Stdout = os.Stdout - - return proc -} - func RunV2RayProtobuf(config []byte) *exec.Cmd { - GenTestBinaryPath() + genTestBinaryPath() proc := exec.Command(testBinaryPath, "-config=stdin:", "-format=pb") proc.Stdin = bytes.NewBuffer(config) proc.Stderr = os.Stderr diff --git a/testing/scenarios/server_env.go b/testing/scenarios/server_env.go deleted file mode 100644 index 0aac09116..000000000 --- a/testing/scenarios/server_env.go +++ /dev/null @@ -1,102 +0,0 @@ -package scenarios - -import ( - "os" - "os/exec" - "path/filepath" - "runtime" - "time" - - "v2ray.com/core/app/log" - - "fmt" - "io/ioutil" - "sync" - _ "v2ray.com/core" - "v2ray.com/core/common/retry" -) - -var ( - runningServers = make([]*exec.Cmd, 0, 10) - testBinaryPath string - testBinaryPathGen sync.Once -) - -func GenTestBinaryPath() { - testBinaryPathGen.Do(func() { - var tempDir string - err := retry.Timed(5, 100).On(func() error { - dir, err := ioutil.TempDir("", "v2ray") - if err != nil { - return err - } - tempDir = dir - return nil - }) - if err != nil { - panic(err) - } - file := filepath.Join(tempDir, "v2ray.test") - if runtime.GOOS == "windows" { - file += ".exe" - } - testBinaryPath = file - fmt.Printf("Generated binary path: %s\n", file) - }) -} - -func GetSourcePath() string { - return filepath.Join("v2ray.com", "core", "main") -} - -func TestFile(filename string) string { - return filepath.Join(os.Getenv("GOPATH"), "src", "v2ray.com", "core", "testing", "scenarios", "data", filename) -} - -func InitializeServerSetOnce(testcase string) error { - if err := InitializeServerServer(testcase); err != nil { - return err - } - if err := InitializeServerClient(testcase); err != nil { - return err - } - return nil -} - -func InitializeServerServer(testcase string) error { - return InitializeServer(TestFile(testcase + "_server.json")) -} - -func InitializeServerClient(testcase string) error { - return InitializeServer(TestFile(testcase + "_client.json")) -} - -func InitializeServer(configFile string) error { - err := BuildV2Ray() - if err != nil { - return err - } - - proc := RunV2Ray(configFile) - - err = proc.Start() - if err != nil { - return err - } - - time.Sleep(time.Second) - - runningServers = append(runningServers, proc) - - return nil -} - -func CloseAllServers() { - log.Info("Closing all servers.") - for _, server := range runningServers { - server.Process.Signal(os.Interrupt) - server.Process.Wait() - } - runningServers = make([]*exec.Cmd, 0, 10) - log.Info("All server closed.") -}