package xdsservertest
import (
+ "encoding/json"
"fmt"
"io"
"log"
"time"
common "gerrit.automotivelinux.org/gerrit/src/xds/xds-common.git/golib"
+ "gerrit.automotivelinux.org/gerrit/src/xds/xds-server/lib/xdsconfig"
"gerrit.automotivelinux.org/gerrit/src/xds/xds-server/lib/xsapiv1"
socketio_client "github.com/sebd71/go-socket.io-client"
)
var HTTPCli *common.HTTPClient
var logDir string
var sCli *IOSockClient
+var prefixURL string
// Debug function used to print debug logs
func Debug(t *testing.T, args ...interface{}) {
if err != nil {
log.Fatal(err)
}
+ var argsProcess = []string{"../bin/xds-server",
+ "-l", "debug",
+ "-c", testConfigFile,
+ }
+
tmpProc, err := os.StartProcess(argsProcess[0], argsProcess, &os.ProcAttr{
Files: []*os.File{os.Stdin, file, file},
})
}
// getHTTPClient
-func getHTTPClient(lvl int) (*common.HTTPClient, *os.File) {
+func getHTTPClient(lvl int, url string) (*common.HTTPClient, *os.File) {
logFile := logDir + logFileClient
file, err := os.OpenFile(logFile, os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
LogPrefix: "XDSSERVERTEST: ",
LogLevel: lvl,
}
- HTTPcli, err := common.HTTPNewClient(prefixURL, conf)
- if err != nil {
- log.Fatal(err)
+
+ // Try to connect during 30 seconds
+ var HTTPcli *common.HTTPClient
+ retry := 30
+ for retry > 0 {
+ if HTTPcli, err = common.HTTPNewClient(url, conf); err == nil {
+ break
+ }
+ if retry < 25 {
+ log.Printf("Establishing connection to XDS Server (retry %d/10)", retry)
+ }
+ time.Sleep(time.Second)
+ retry--
+ }
+ if retry == 0 {
+ log.Fatalf("Cannot establish connection with xds-server:\n %v", err)
}
+
log.Printf("HTTP session ID : %v", HTTPcli.GetClientID())
+
+ // Basic check
var ver xsapiv1.Version
err = HTTPcli.Get("/version", &ver)
if err != nil {
sCli.Connected = false
})
- log.Printf("Connect websocket with url=%v clientId=%v\n", prefixURL, HTTPCli.GetClientID())
return sCli, nil
}
if status, err := p.Wait(); err != nil {
log.Fatalf("status=%v\n err=%v\n", status, err)
}
+ os.Exit(0)
}(proc)
defer fileXdsServer.Close()
}
- time.Sleep(1 * time.Second)
+ // Extract URL from _test-config.json file
+ fd, _ := os.Open(testConfigFile)
+ defer fd.Close()
+ fCfg := xdsconfig.FileConfig{}
+ if err := json.NewDecoder(fd).Decode(&fCfg); err != nil {
+ log.Fatalf("Cannot decode test config file %v : %v", testConfigFile, err)
+ }
+
+ if fCfg.HTTPPort == "" {
+ log.Fatalf("Cannot retrieve httpPort in test config file %v", testConfigFile)
+ }
+ prefixURL := "http://localhost:" + fCfg.HTTPPort
+
+ // Start HTTP client to send test commands
lvl := common.HTTPLogLevelDebug
var fileHTTPClient *os.File
- HTTPCli, fileHTTPClient = getHTTPClient(lvl)
+ HTTPCli, fileHTTPClient = getHTTPClient(lvl, prefixURL)
defer fileHTTPClient.Close()
var err error
sCli, err = NewIoSocketClient(prefixURL, HTTPCli.GetClientID())
if err != nil {
log.Fatal(err)
}
-
if HTTPCli == nil {
- log.Fatal("HTTPCLi is nil")
+ log.Fatal("HTTPCli is nil")
}
+
+ // Run tests
res := m.Run()
defer os.Exit(res)
+
+ // Gracefully stop xds-server process
+ if err := proc.Signal(os.Interrupt); err != nil {
+ log.Fatalf("xds-server proc interrupt error : %v", err)
+ }
+
+ // Should not be executed, normal exit must be above go routine after on p.Wait returns
+ time.Sleep(10 * time.Second)
+ log.Printf("xds-server not gravefully stop, kill it\n")
proc.Kill()
}