aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--test/config.go4
-rw-r--r--test/main_test.go65
2 files changed, 57 insertions, 12 deletions
diff --git a/test/config.go b/test/config.go
index 804b0b1..6d31559 100644
--- a/test/config.go
+++ b/test/config.go
@@ -17,11 +17,9 @@
package xdsservertest
-var argsProcess = []string{"../bin/xds-server", "-l", "debug", "-c", "_test-config.json"}
-
const (
+ testConfigFile = "_test-config.json"
envRootCfgDir = "XDS_SERVER_ROOT_CFG_DIR"
- prefixURL = "http://localhost:8000"
logFileXdsServer = "xdsserver-test.log"
logFileClient = "client-test.log"
logFileSSHd = "sshd.log"
diff --git a/test/main_test.go b/test/main_test.go
index bdea40c..1c577a9 100644
--- a/test/main_test.go
+++ b/test/main_test.go
@@ -17,6 +17,7 @@
package xdsservertest
import (
+ "encoding/json"
"fmt"
"io"
"log"
@@ -27,6 +28,7 @@ import (
"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"
)
@@ -46,6 +48,7 @@ type IOSockClient struct {
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{}) {
@@ -152,6 +155,11 @@ func launchXdsServer(proc **os.Process) *os.File {
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},
})
@@ -163,7 +171,7 @@ func launchXdsServer(proc **os.Process) *os.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 {
@@ -177,11 +185,27 @@ func getHTTPClient(lvl int) (*common.HTTPClient, *os.File) {
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 {
@@ -219,7 +243,6 @@ func NewIoSocketClient(url, clientID string) (*IOSockClient, error) {
sCli.Connected = false
})
- log.Printf("Connect websocket with url=%v clientId=%v\n", prefixURL, HTTPCli.GetClientID())
return sCli, nil
}
@@ -241,25 +264,49 @@ func TestMain(m *testing.M) {
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()
}