2018-10-14 21:38:14 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-03-07 04:52:41 +02:00
|
|
|
"context"
|
2018-10-14 21:38:14 +02:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
2019-06-19 23:58:56 +02:00
|
|
|
"net"
|
2018-12-22 06:14:41 +02:00
|
|
|
"os"
|
2019-03-07 04:52:41 +02:00
|
|
|
"os/signal"
|
2018-12-22 06:14:41 +02:00
|
|
|
"path/filepath"
|
2019-06-19 23:58:56 +02:00
|
|
|
|
|
|
|
"github.com/MontFerret/ferret/e2e/runner"
|
|
|
|
"github.com/MontFerret/ferret/e2e/server"
|
|
|
|
|
|
|
|
"github.com/rs/zerolog"
|
2018-10-14 21:38:14 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
testsDir = flag.String(
|
|
|
|
"tests",
|
|
|
|
"./tests",
|
|
|
|
"root directory with test scripts",
|
|
|
|
)
|
|
|
|
|
|
|
|
pagesDir = flag.String(
|
|
|
|
"pages",
|
|
|
|
"./pages",
|
|
|
|
"root directory with test pages",
|
|
|
|
)
|
|
|
|
|
|
|
|
cdp = flag.String(
|
|
|
|
"cdp",
|
|
|
|
"http://0.0.0.0:9222",
|
|
|
|
"address of remote Chrome instance",
|
|
|
|
)
|
2018-12-22 06:14:41 +02:00
|
|
|
|
|
|
|
filter = flag.String(
|
|
|
|
"filter",
|
|
|
|
"",
|
|
|
|
"regexp expression to filter out tests",
|
|
|
|
)
|
2018-10-14 21:38:14 +02:00
|
|
|
)
|
|
|
|
|
2019-06-19 23:58:56 +02:00
|
|
|
func getOutboundIP() (net.IP, error) {
|
|
|
|
conn, err := net.Dial("udp", "8.8.8.8:80")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
localAddr := conn.LocalAddr().(*net.UDPAddr)
|
|
|
|
|
|
|
|
return localAddr.IP, nil
|
|
|
|
}
|
|
|
|
|
2018-10-14 21:38:14 +02:00
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
logger := zerolog.New(zerolog.ConsoleWriter{Out: os.Stderr})
|
|
|
|
|
2018-11-12 21:53:36 +02:00
|
|
|
staticPort := uint64(8080)
|
|
|
|
static := server.New(server.Settings{
|
|
|
|
Port: staticPort,
|
|
|
|
Dir: filepath.Join(*pagesDir, "static"),
|
2018-10-14 21:38:14 +02:00
|
|
|
})
|
|
|
|
|
2018-11-12 21:53:36 +02:00
|
|
|
dynamicPort := uint64(8081)
|
|
|
|
dynamic := server.New(server.Settings{
|
|
|
|
Port: dynamicPort,
|
|
|
|
Dir: filepath.Join(*pagesDir, "dynamic"),
|
|
|
|
})
|
2018-10-14 21:38:14 +02:00
|
|
|
|
|
|
|
go func() {
|
2018-11-12 21:53:36 +02:00
|
|
|
if err := static.Start(); err != nil {
|
|
|
|
logger.Info().Timestamp().Msg("shutting down the static pages server")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
if err := dynamic.Start(); err != nil {
|
|
|
|
logger.Info().Timestamp().Msg("shutting down the dynamic pages server")
|
2018-10-14 21:38:14 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2019-05-03 23:10:34 +02:00
|
|
|
if *testsDir == "" {
|
|
|
|
_, err := filepath.Abs(filepath.Dir(os.Args[0]))
|
2018-10-14 21:38:14 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
logger.Fatal().Timestamp().Err(err).Msg("failed to get testsDir")
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-19 23:58:56 +02:00
|
|
|
var ipAddr string
|
|
|
|
|
|
|
|
// we need it in those cases when a Chrome instance is running inside a container
|
|
|
|
// and it needs an external IP to get access to our static web server
|
|
|
|
outIP, err := getOutboundIP()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
ipAddr = "0.0.0.0"
|
|
|
|
logger.Warn().Err(err).Msg("Failed to get outbound IP address")
|
|
|
|
} else {
|
|
|
|
ipAddr = outIP.String()
|
|
|
|
}
|
|
|
|
|
2018-10-14 21:38:14 +02:00
|
|
|
r := runner.New(logger, runner.Settings{
|
2019-06-19 23:58:56 +02:00
|
|
|
StaticServerAddress: fmt.Sprintf("http://%s:%d", ipAddr, staticPort),
|
|
|
|
DynamicServerAddress: fmt.Sprintf("http://%s:%d", ipAddr, dynamicPort),
|
2018-11-12 21:53:36 +02:00
|
|
|
CDPAddress: *cdp,
|
|
|
|
Dir: *testsDir,
|
2019-06-19 23:58:56 +02:00
|
|
|
Filter: *filter,
|
2018-10-14 21:38:14 +02:00
|
|
|
})
|
|
|
|
|
2019-03-07 04:52:41 +02:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
c := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(c, os.Interrupt)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
<-c
|
|
|
|
cancel()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2019-06-19 23:58:56 +02:00
|
|
|
err = r.Run(ctx)
|
2018-10-14 21:38:14 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2018-11-12 21:53:36 +02:00
|
|
|
|
|
|
|
os.Exit(0)
|
2018-10-14 21:38:14 +02:00
|
|
|
}
|