summaryrefslogtreecommitdiffstats
path: root/test/xdsserver_test.go
blob: ba56321fe5ad2980e56440b4c74c3b12115577ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
 * Copyright (C) 2017-2018 "IoT.bzh"
 * Author Clément Bénier <clement.benier@iot.bzh>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package xdsservertest

import (
	"fmt"
	"io"
	"log"
	"os"
	"os/exec"
	"sync"
	"testing"
	"time"

	common "gerrit.automotivelinux.org/gerrit/src/xds/xds-common.git/golib"
	"gerrit.automotivelinux.org/gerrit/src/xds/xds-server/lib/xsapiv1"
	socketio_client "github.com/sebd71/go-socket.io-client"
)

// IOSockClient
type IOSockClient struct {
	URL       string
	Conn      *socketio_client.Client
	Options   *socketio_client.Options
	EmitMutex *sync.Mutex
	Connected bool
	//ServerDiscoChan chan Disconnection
	EscapeKeys []byte
}

//global client
var HTTPCli *common.HTTPClient
var logDir string
var sCli *IOSockClient

func Copy(src, dst string) error {
	in, err := os.Open(src)
	if err != nil {
		return err
	}
	defer in.Close()

	out, err := os.Create(dst)
	if err != nil {
		return err
	}
	defer out.Close()

	_, err = io.Copy(out, in)
	if err != nil {
		return err
	}
	return out.Close()
}

func initEnv(launchProcess bool) {
	if launchProcess {
		cmd := exec.Command("killall", "-9", "xds-server")
		if err := cmd.Start(); err != nil {
			log.Fatal(err)
		}
		cmd.Wait()
	}
	rootTestLog := "/tmp/xds-server-test"
	if err := os.Setenv(envRootCfgDir, rootTestLog); err != nil {
		log.Fatal(err)
	}
	sdkDir := rootTestLog + "/sdks/"
	if err := os.Setenv(envXdtSdk, sdkDir); err != nil {
		log.Fatal(err)
	}
	if err := os.Setenv(envXdsServerWorkspaceDir, rootTestLog); err != nil {
		log.Fatal(err)
	}
	if err := os.Setenv(envXdsServerRootCfgDir, rootTestLog); err != nil {
		log.Fatal(err)
	}
	if err := os.Setenv("XDS_LOG_SILLY", "1"); err != nil {
		log.Fatal(err)
	}
	os.RemoveAll(rootTestLog)
	os.MkdirAll(rootTestLog, 0755)
	logDir = rootTestLog + "/logs/"
	os.MkdirAll(logDir, 0755)
}

func launchXdsServer(proc **os.Process) *os.File {
	logFile := logDir + logFileXdsServer
	file, err := os.OpenFile(logFile, os.O_CREATE|os.O_WRONLY, 0644)
	if err != nil {
		log.Fatal(err)
	}
	tmpProc, err := os.StartProcess(argsProcess[0], argsProcess, &os.ProcAttr{
		Files: []*os.File{os.Stdin, file, file},
	})
	if err != nil {
		log.Fatal(err)
	}
	*proc = tmpProc
	return file
}

func getHTTPClient(lvl int) (*common.HTTPClient, *os.File) {
	logFile := logDir + logFileClient
	file, err := os.OpenFile(logFile, os.O_CREATE|os.O_WRONLY, 0644)
	if err != nil {
		log.Fatal(err)
	}
	conf := common.HTTPClientConfig{
		URLPrefix:           "/api/v1",
		HeaderClientKeyName: "Xds-Sid",
		CsrfDisable:         true,
		LogOut:              file,
		LogPrefix:           "XDSSERVERTEST: ",
		LogLevel:            lvl,
	}
	cli, err := common.HTTPNewClient(prefixURL, conf)
	if err != nil {
		log.Fatal(err)
	}
	log.Printf("HTTP session ID : %v", cli.GetClientID())
	var ver xsapiv1.Version
	err = cli.Get("/version", &ver)
	if err != nil {
		log.Fatal(err)
	}
	return cli, file
}

func NewIoSocketClient(url, clientID string) (*IOSockClient, error) {

	var err error

	sCli := &IOSockClient{
		URL:       url,
		EmitMutex: &sync.Mutex{},
		Options: &socketio_client.Options{
			Transport: "websocket",
			Header:    make(map[string][]string),
		},
		//ServerDiscoChan: make(chan Disconnection, 1),
	}
	sCli.Options.Header["XDS-SID"] = []string{clientID}

	sCli.Conn, err = socketio_client.NewClient(url, sCli.Options)
	if err != nil {
		return nil, fmt.Errorf("IO.socket connection error: " + err.Error())
	}

	sCli.Conn.On("connection", func() {
		sCli.Connected = true
	})

	sCli.Conn.On("disconnection", func(err error) {
		log.Printf("WS disconnection event with err: %v\n", err)
		sCli.Connected = false
	})

	log.Printf("Connect websocket with url=%v clientId=%v\n", prefixURL, HTTPCli.GetClientID())
	return sCli, nil
}
func TestMain(m *testing.M) {
	launchProcess := true
	log.Printf("TestMain: launchProcess is %v, so launching xds-server", launchProcess)
	initEnv(launchProcess)

	var proc *os.Process
	var fileXdsServer *os.File
	if launchProcess {
		fileXdsServer = launchXdsServer(&proc)
		go func(p *os.Process) {
			if status, err := p.Wait(); err != nil {
				log.Fatalf("status=%v\n err=%v\n", status, err)
			}
		}(proc)
		defer proc.Kill()
		defer fileXdsServer.Close()
	}
	time.Sleep(1 * time.Second)

	lvl := common.HTTPLogLevelDebug
	var fileHTTPClient *os.File
	HTTPCli, fileHTTPClient = getHTTPClient(lvl)
	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")
	}
	res := m.Run()
	defer os.Exit(res)
}

func init() {
}