2 * Copyright (C) 2017-2018 "IoT.bzh"
3 * Author Clément Bénier <clement.benier@iot.bzh>
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
29 common "gerrit.automotivelinux.org/gerrit/src/xds/xds-common.git/golib"
30 "gerrit.automotivelinux.org/gerrit/src/xds/xds-server/lib/xsapiv1"
31 socketio_client "github.com/sebd71/go-socket.io-client"
35 type IOSockClient struct {
37 Conn *socketio_client.Client
38 Options *socketio_client.Options
41 //ServerDiscoChan chan Disconnection
46 var HTTPCli *common.HTTPClient
48 var sCli *IOSockClient
50 // Debug function used to print debug logs
51 func Debug(t *testing.T, args ...interface{}) {
52 if os.Getenv("VERBOSE") != "" {
57 // Debugf function used to print debug logs
58 func Debugf(t *testing.T, format string, args ...interface{}) {
59 if os.Getenv("VERBOSE") != "" {
60 t.Logf(format, args...)
64 // Copy copies from src to dst until either EOF
65 func Copy(src, dst string) error {
66 in, err := os.Open(src)
72 out, err := os.Create(dst)
78 _, err = io.Copy(out, in)
85 func initEnv(launchProcess bool) {
87 /*kill xds-server if needed*/
88 cmd := exec.Command("killall", "-9", "xds-server")
89 if err := cmd.Start(); err != nil {
94 /*set environment variable*/
95 rootTestLog := "/tmp/xds-server-test"
96 if err := os.Setenv(envRootCfgDir, rootTestLog); err != nil {
99 sdkDir := rootTestLog + "/sdks/"
100 if err := os.Setenv(envXdtSdk, sdkDir); err != nil {
103 if err := os.Setenv(envXdsServerWorkspaceDir, rootTestLog); err != nil {
106 if err := os.Setenv(envXdsServerRootCfgDir, rootTestLog); err != nil {
109 if err := os.Setenv("XDS_LOG_SILLY", "1"); err != nil {
112 /*remove and recreate working directories*/
113 os.RemoveAll(rootTestLog)
114 os.MkdirAll(rootTestLog, 0755)
115 logDir = rootTestLog + "/logs/"
116 os.MkdirAll(logDir, 0755)
119 /*prepare xds-server process*/
120 func launchXdsServer(proc **os.Process) *os.File {
121 logFile := logDir + logFileXdsServer
122 file, err := os.OpenFile(logFile, os.O_CREATE|os.O_WRONLY, 0644)
126 tmpProc, err := os.StartProcess(argsProcess[0], argsProcess, &os.ProcAttr{
127 Files: []*os.File{os.Stdin, file, file},
136 func getHTTPClient(lvl int) (*common.HTTPClient, *os.File) {
137 logFile := logDir + logFileClient
138 file, err := os.OpenFile(logFile, os.O_CREATE|os.O_WRONLY, 0644)
142 conf := common.HTTPClientConfig{
143 URLPrefix: "/api/v1",
144 HeaderClientKeyName: "Xds-Sid",
147 LogPrefix: "XDSSERVERTEST: ",
150 HTTPcli, err := common.HTTPNewClient(prefixURL, conf)
154 log.Printf("HTTP session ID : %v", HTTPcli.GetClientID())
155 var ver xsapiv1.Version
156 err = HTTPcli.Get("/version", &ver)
163 func NewIoSocketClient(url, clientID string) (*IOSockClient, error) {
166 sCli := &IOSockClient{
168 EmitMutex: &sync.Mutex{},
169 Options: &socketio_client.Options{
170 Transport: "websocket",
171 Header: make(map[string][]string),
174 sCli.Options.Header["XDS-SID"] = []string{clientID}
176 sCli.Conn, err = socketio_client.NewClient(url, sCli.Options)
178 return nil, fmt.Errorf("IO.socket connection error: " + err.Error())
181 sCli.Conn.On("connection", func() {
182 sCli.Connected = true
185 sCli.Conn.On("disconnection", func(err error) {
187 log.Printf("WS disconnection event with err: %v\n", err)
189 sCli.Connected = false
192 log.Printf("Connect websocket with url=%v clientId=%v\n", prefixURL, HTTPCli.GetClientID())
195 func TestMain(m *testing.M) {
196 /* useful for debugging, preventing from launching xds-server
197 * it can be launch separately */
198 launchProcess := true
199 log.Printf("TestMain: launchProcess is %v, so launching xds-server", launchProcess)
200 initEnv(launchProcess)
203 var fileXdsServer *os.File
205 fileXdsServer = launchXdsServer(&proc)
206 go func(p *os.Process) {
207 log.Print("xds-server is launching")
208 if status, err := p.Wait(); err != nil {
209 log.Fatalf("status=%v\n err=%v\n", status, err)
212 defer fileXdsServer.Close()
214 time.Sleep(1 * time.Second)
216 lvl := common.HTTPLogLevelDebug
217 var fileHTTPClient *os.File
218 HTTPCli, fileHTTPClient = getHTTPClient(lvl)
219 defer fileHTTPClient.Close()
221 sCli, err = NewIoSocketClient(prefixURL, HTTPCli.GetClientID())
227 log.Fatal("HTTPCLi is nil")