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 // init function will run once before execution of test functions begins.
94 // isCommandAvailable verify if a command/utility is available
95 func isCommandAvailable(name string) bool {
96 cmd := exec.Command("/bin/sh", "-c", "command -v "+name)
97 if err := cmd.Run(); err != nil {
103 // checkTestDep checks if all dependency tools are available to be able to run tests
104 func checkTestDep() error {
105 for _, cmd := range dependency_tools {
106 if !isCommandAvailable(cmd) {
107 return fmt.Errorf(cmd + " is not installed and is mandatory to run tests")
114 func initEnv(launchProcess bool) {
116 /*kill xds-server if needed*/
117 cmd := exec.Command("pkill", "-9", "xds-server")
118 if err := cmd.Start(); err != nil {
123 /*set environment variable*/
124 rootTestLog := "/tmp/xds-server-test"
125 if err := os.Setenv(envRootCfgDir, rootTestLog); err != nil {
128 sdkDir := rootTestLog + "/sdks/"
129 if err := os.Setenv(envXdtSdk, sdkDir); err != nil {
132 if err := os.Setenv(envXdsServerWorkspaceDir, rootTestLog); err != nil {
135 if err := os.Setenv(envXdsServerRootCfgDir, rootTestLog); err != nil {
138 if err := os.Setenv("XDS_LOG_SILLY", "1"); err != nil {
141 /*remove and recreate working directories*/
142 os.RemoveAll(rootTestLog)
143 os.MkdirAll(rootTestLog, 0755)
144 logDir = rootTestLog + "/logs/"
145 os.MkdirAll(logDir, 0755)
148 /*prepare xds-server process*/
149 func launchXdsServer(proc **os.Process) *os.File {
150 logFile := logDir + logFileXdsServer
151 file, err := os.OpenFile(logFile, os.O_CREATE|os.O_WRONLY, 0644)
155 tmpProc, err := os.StartProcess(argsProcess[0], argsProcess, &os.ProcAttr{
156 Files: []*os.File{os.Stdin, file, file},
166 func getHTTPClient(lvl int) (*common.HTTPClient, *os.File) {
167 logFile := logDir + logFileClient
168 file, err := os.OpenFile(logFile, os.O_CREATE|os.O_WRONLY, 0644)
172 conf := common.HTTPClientConfig{
173 URLPrefix: "/api/v1",
174 HeaderClientKeyName: "Xds-Sid",
177 LogPrefix: "XDSSERVERTEST: ",
180 HTTPcli, err := common.HTTPNewClient(prefixURL, conf)
184 log.Printf("HTTP session ID : %v", HTTPcli.GetClientID())
185 var ver xsapiv1.Version
186 err = HTTPcli.Get("/version", &ver)
193 func NewIoSocketClient(url, clientID string) (*IOSockClient, error) {
196 sCli := &IOSockClient{
198 EmitMutex: &sync.Mutex{},
199 Options: &socketio_client.Options{
200 Transport: "websocket",
201 Header: make(map[string][]string),
204 sCli.Options.Header["XDS-SID"] = []string{clientID}
206 sCli.Conn, err = socketio_client.NewClient(url, sCli.Options)
208 return nil, fmt.Errorf("IO.socket connection error: " + err.Error())
211 sCli.Conn.On("connection", func() {
212 sCli.Connected = true
215 sCli.Conn.On("disconnection", func(err error) {
217 log.Printf("WS disconnection event with err: %v\n", err)
219 sCli.Connected = false
222 log.Printf("Connect websocket with url=%v clientId=%v\n", prefixURL, HTTPCli.GetClientID())
226 // TestMain is the main entry point of testsuite
227 func TestMain(m *testing.M) {
229 /* useful for debugging, preventing from launching xds-server
230 * it can be launch separately */
231 launchProcess := true
232 log.Printf("TestMain: launchProcess is %v, so launching xds-server", launchProcess)
233 initEnv(launchProcess)
236 var fileXdsServer *os.File
238 fileXdsServer = launchXdsServer(&proc)
239 go func(p *os.Process) {
240 log.Print("xds-server is launching")
241 if status, err := p.Wait(); err != nil {
242 log.Fatalf("status=%v\n err=%v\n", status, err)
245 defer fileXdsServer.Close()
247 time.Sleep(1 * time.Second)
249 lvl := common.HTTPLogLevelDebug
250 var fileHTTPClient *os.File
251 HTTPCli, fileHTTPClient = getHTTPClient(lvl)
252 defer fileHTTPClient.Close()
254 sCli, err = NewIoSocketClient(prefixURL, HTTPCli.GetClientID())
260 log.Fatal("HTTPCLi is nil")