summaryrefslogtreecommitdiffstats
path: root/test/target_test.go
blob: 010411db34b57621755761d988b5ca8cb1756c9c (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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/*
 * 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 (
	"bytes"
	"fmt"
	"log"
	"os"
	"os/exec"
	"path"
	"regexp"
	"strconv"
	"strings"
	"testing"
	"time"

	common "gerrit.automotivelinux.org/gerrit/src/xds/xds-common.git"
	"gerrit.automotivelinux.org/gerrit/src/xds/xds-server.git/lib/xsapiv1"
	"github.com/stretchr/testify/require"
)

func launchSSHd(sshDir string, proc **os.Process, sshdCmd string) (*os.File, string) {
	port := "22222"
	argsProcessSSHd := []string{
		sshdCmd,
		"-f",
		sshDir + "/sshd_config",
		"-D",
		"-h",
		sshDir + "/ssh_host_rsa_key",
		"-o",
		"AuthorizedKeysFile=" + sshDir + "/authorized_keys",
		"-p",
		port,
	}
	logFile := logDir + logFileSSHd
	file, err := os.OpenFile(logFile, os.O_CREATE|os.O_WRONLY, 0644)
	if err != nil {
		log.Fatal(err)
	}
	defer file.Close()
	tmpProc, err := os.StartProcess(argsProcessSSHd[0], argsProcessSSHd, &os.ProcAttr{
		Files: []*os.File{os.Stdin, file, file},
	})
	if err != nil {
		log.Fatal(err)
	}
	*proc = tmpProc
	return file, port
}

func InitSSH(t *testing.T, procSSHd **os.Process) (string, string) {
	Debug(t, "Initialise ssh with local user")
	sshDir := path.Join(os.Getenv(envRootCfgDir), "ssh")
	cmd := exec.Command("cp", "-r", sshFixturesDir, sshDir)
	var out bytes.Buffer
	cmd.Stdout = &out
	require.Nil(t, cmd.Run())

	cmd = exec.Command("ls", sshDir)
	cmd.Stdout = &out
	require.Nil(t, cmd.Run())

	files := strings.Split(fmt.Sprint(cmd.Stdout), "\n")

	for _, f := range files {
		if f != "" {
			file := sshDir + "/" + f
			cmd = exec.Command("chmod", "600", file)
			cmd.Stdout = &out
			require.Nil(t, cmd.Run())
		}
	}

	var outSSHd bytes.Buffer
	var sshdCmd string
	cmd = exec.Command("which", "sshd")
	cmd.Stdout = &outSSHd
	if cmd.Run() != nil {
		if common.Exists("/usr/sbin/sshd") {
			sshdCmd = "/usr/sbin/sshd"
		} else if common.Exists("/usr/bin/sshd") {
			sshdCmd = "/usr/sbin/sshd"
		} else {
			require.FailNow(t, "Cannot find sshd command, please install it or set in your PATH")
		}
	} else {
		sshdCmd = strings.TrimSpace(fmt.Sprint(cmd.Stdout))
	}

	var port string
	_, port = launchSSHd(sshDir, procSSHd, sshdCmd)
	go func(p *os.Process) {
		Debug(t, "sshd is launching")
		if status, err := p.Wait(); err != nil {
			log.Fatalf("status=%v\n err=%v\n", status, err)
		}
	}(*procSSHd)
	return sshDir, port
}

/*wait for terminal prompt*/
func waitForPrompt(t *testing.T, channel chan xsapiv1.TerminalOutMsg, prompt string) string {
	step := 1 * time.Millisecond
	timeout := 10 * time.Second
	current := 0 * time.Second
	out := ""
	re := regexp.MustCompile("^" + prompt)

	for {
		select {
		case outMsg := <-channel:
			out += string(outMsg.Stdout)
			if string(outMsg.Stderr) != "" {
				out += string(outMsg.Stderr)
			}
			for _, line := range strings.Split(out, "\n") {
				if re.MatchString(line) {
					return line
				}
			}
		case <-time.After(step):
			current = current + step
			if current >= timeout {
				require.FailNow(t, "Never received prompt message from terminal (output:"+out+")")
			}
		}
	}
}

func ConnectTargetEvents(t *testing.T, channel chan xsapiv1.TargetConfig) {
	sCli.Conn.On(xsapiv1.EVTTargetAdd, func(e xsapiv1.EventMsg) {
		target, _ := e.DecodeTargetEvent()
		channel <- target
	})

	args := xsapiv1.EventRegisterArgs{Name: xsapiv1.EVTTargetAdd}
	require.Nil(t, HTTPCli.Post("/events/register", args, nil))

	sCli.Conn.On(xsapiv1.EVTTargetRemove, func(e xsapiv1.EventMsg) {
		target, _ := e.DecodeTargetEvent()
		channel <- target
	})

	args = xsapiv1.EventRegisterArgs{Name: xsapiv1.EVTTargetRemove}
	require.Nil(t, HTTPCli.Post("/events/register", args, nil))
}

func DisconnectTargetEvents(t *testing.T) {
	args := xsapiv1.EventRegisterArgs{Name: xsapiv1.EVTTargetAdd}
	require.Nil(t, HTTPCli.Post("/events/unregister", args, nil))
	args = xsapiv1.EventRegisterArgs{Name: xsapiv1.EVTTargetRemove}
	require.Nil(t, HTTPCli.Post("/events/unregister", args, nil))
}

func ConnectTermEvents(t *testing.T, channel chan xsapiv1.TerminalConfig) {
	sCli.Conn.On(xsapiv1.EVTTargetTerminalAdd, func(e xsapiv1.EventMsg) {
		termEvt, _ := e.DecodeTerminalEvent()
		channel <- termEvt
	})

	args := xsapiv1.EventRegisterArgs{Name: xsapiv1.EVTTargetTerminalAdd}
	require.Nil(t, HTTPCli.Post("/events/register", args, nil))

	sCli.Conn.On(xsapiv1.EVTTargetTerminalStateChange, func(e xsapiv1.EventMsg) {
		termEvt, _ := e.DecodeTerminalEvent()
		channel <- termEvt
	})

	args = xsapiv1.EventRegisterArgs{Name: xsapiv1.EVTTargetTerminalStateChange}
	require.Nil(t, HTTPCli.Post("/events/register", args, nil))

	sCli.Conn.On(xsapiv1.EVTTargetTerminalRemove, func(e xsapiv1.EventMsg) {
		termEvt, _ := e.DecodeTerminalEvent()
		channel <- termEvt
	})

	args = xsapiv1.EventRegisterArgs{Name: xsapiv1.EVTTargetTerminalRemove}
	require.Nil(t, HTTPCli.Post("/events/register", args, nil))
}

func DisconnectTermEvents(t *testing.T) {
	args := xsapiv1.EventRegisterArgs{Name: xsapiv1.EVTTargetTerminalAdd}
	require.Nil(t, HTTPCli.Post("/events/unregister", args, nil))
	args = xsapiv1.EventRegisterArgs{Name: xsapiv1.EVTTargetTerminalStateChange}
	require.Nil(t, HTTPCli.Post("/events/unregister", args, nil))
	args = xsapiv1.EventRegisterArgs{Name: xsapiv1.EVTTargetTerminalRemove}
	require.Nil(t, HTTPCli.Post("/events/unregister", args, nil))
}

func AddTargets(t *testing.T, nbTargets int, chTarget chan xsapiv1.TargetConfig) []string {
	listID := make([]string, nbTargets)
	for i := 0; i < nbTargets; i++ {
		/*target is local*/
		target := xsapiv1.TargetConfig{
			Name: "fakeTarget" + strconv.Itoa(i),
			Type: xsapiv1.TypeTgtStandard,
			IP:   "127.0.0.1",
		}
		/*add target*/
		require.Nil(t, HTTPCli.Post("/targets", target, &target))
		Debugf(t, "add target %v", target.Name)
		targetEvt := <-chTarget //waiting for event targetAdd
		require.Equal(t, target.ID, targetEvt.ID)
		listID[i] = target.ID
	}
	for i := 0; i < nbTargets; i++ {
		var target xsapiv1.TargetConfig
		require.Nil(t, HTTPCli.Get("/targets/"+listID[i], &target))
		require.Equal(t, target.Status, "Enable")
	}
	return listID
}

func AddTerms(t *testing.T, nbTerms int, listID []string, chTermEvt chan xsapiv1.TerminalConfig, sshDir string, port string) {
	for j := 0; j < len(listID); j++ {
		listTermsID := make([]string, nbTerms)
		for i := 0; i < nbTerms; i++ {
			term := xsapiv1.TerminalConfig{
				Name: "terminal" + strconv.Itoa(i),
				Type: xsapiv1.TypeTermSSH,
				Options: []string{
					"-p",
					port,
					"-i",
					sshDir + "/ssh",
					"-o",
					"StrictHostKeyChecking=no",
				},
			}
			/*add terminal on target*/
			require.Nil(t, HTTPCli.Post("/targets/"+listID[j]+"/terminals", term, &term))
			Debugf(t, "add terminal %v", term.Name)
			termEvt := <-chTermEvt //waiting for event terminalAdd*/
			require.Equal(t, term.ID, termEvt.ID)
			listTermsID[i] = term.ID
		}
		require.Equal(t, len(listTermsID), nbTerms)
		for i := 0; i < nbTerms; i++ {
			var term xsapiv1.TerminalConfig
			require.Nil(t, HTTPCli.Get("/targets/"+listID[j]+"/terminals/"+listTermsID[i], &term))
			require.Equal(t, term.Status, "Close")
		}
	}
}

func PostTerms(t *testing.T, post string, chTermEvt chan xsapiv1.TerminalConfig, chTerm chan xsapiv1.TerminalOutMsg,
	prompt string) {
	var status string
	switch post {
	case "open":
		status = "Open"
	case "close":
		status = "Closing"
	}
	var targets []xsapiv1.TargetConfig
	require.Nil(t, HTTPCli.Get("/targets", &targets))
	for i := 0; i < len(targets); i++ {
		var terms []xsapiv1.TerminalConfig
		require.Nil(t, HTTPCli.Get("/targets/"+targets[i].ID+"/terminals", &terms))
		listTermsID := make([]string, len(terms))
		for j := 0; j < len(terms); j++ {
			var term xsapiv1.TerminalConfig
			/*post action on term*/
			require.Nil(t, HTTPCli.Post("/targets/"+targets[i].ID+"/terminals/"+terms[j].ID+"/"+post, terms[j], &term))
			Debugf(t, "%v terminal %v", post, term.Name)
			termEvt := <-chTermEvt //waiting for event terminalStateChange
			if post == "open" {
				data := []byte("PS1=" + prompt + " bash -norc\n")
				require.Nil(t, sCli.Conn.Emit(xsapiv1.TerminalInEvent, data))
				waitForPrompt(t, chTerm, prompt)
			}
			require.Equal(t, term.ID, termEvt.ID)
			require.Equal(t, term.Status, status)
			require.Equal(t, termEvt.Status, status)
			listTermsID[i] = term.ID
		}
		time.Sleep(10 * time.Millisecond)
		for j := 0; j < len(listTermsID); j++ {
			var term xsapiv1.TerminalConfig
			require.Nil(t, HTTPCli.Get("/targets/"+targets[i].ID+"/terminals/"+listTermsID[i], &term))
			require.True(t, strings.EqualFold(term.Status, post))
			Debugf(t, "check that term status %v is %v", term.Name, post)
		}
	}
}

func RemoveTermsTargets(t *testing.T, chTarget chan xsapiv1.TargetConfig, chTermEvt chan xsapiv1.TerminalConfig) {
	var targets []xsapiv1.TargetConfig
	require.Nil(t, HTTPCli.Get("/targets", &targets))
	for i := 0; i < len(targets); i++ {
		var terms []xsapiv1.TerminalConfig
		require.Nil(t, HTTPCli.Get("/targets/"+targets[i].ID+"/terminals", &terms))
		for j := 0; j < len(terms); j++ {
			var term xsapiv1.TerminalConfig
			require.Nil(t, HTTPCli.Delete("/targets/"+targets[i].ID+"/terminals/"+terms[j].ID, &term))
			termEvt := <-chTermEvt
			require.Equal(t, term.ID, termEvt.ID)
			require.NotNil(t, HTTPCli.Delete("/targets/"+targets[i].ID+"/terminals/"+terms[j].ID, &term))
			Debugf(t, "remove terminal %v", term.Name)
		}
		var tgtRes xsapiv1.TargetConfig
		require.Nil(t, HTTPCli.Delete("/targets/"+targets[i].ID, &tgtRes))
		targetEvt := <-chTarget //waiting for remove terminal event
		require.Equal(t, tgtRes.ID, targetEvt.ID)
		require.Equal(t, targets[i].ID, tgtRes.ID)
	}
}
func TestTarget(t *testing.T) {
	prompt := "--PROMPT--"
	var procSSHd *os.Process
	sshDir, port := InitSSH(t, &procSSHd)
	defer procSSHd.Kill()

	nbTargets := 2
	nbTermsByTarget := 2
	/*channel for target events*/
	chTarget := make(chan xsapiv1.TargetConfig)
	defer close(chTarget)
	ConnectTargetEvents(t, chTarget)

	/*channel for terminal events*/
	chTermEvt := make(chan xsapiv1.TerminalConfig)
	defer close(chTermEvt)
	ConnectTermEvents(t, chTermEvt)

	/*check that targetArray is empty at startup*/
	var targetArray []xsapiv1.TargetConfig
	require.Nil(t, HTTPCli.Get("/targets", &targetArray))
	require.Equal(t, len(targetArray), 0)

	listID := AddTargets(t, nbTargets, chTarget)
	AddTerms(t, nbTermsByTarget, listID, chTermEvt, sshDir, port)

	/*channel for TerminalOutMsg*/
	chTerm := make(chan xsapiv1.TerminalOutMsg)
	defer close(chTerm)

	/*connect on terminalOutMsg event*/
	sCli.Conn.On(xsapiv1.TerminalOutEvent, func(ev xsapiv1.TerminalOutMsg) {
		chTerm <- ev
	})

	/*open terminals*/
	PostTerms(t, "open", chTermEvt, chTerm, prompt)

	/*create toto file through terminals*/
	rootCfgDir := os.Getenv(envRootCfgDir)
	totoFile := path.Join(rootCfgDir, "toto")

	/*test with 2 terminals*/
	for i := 0; i < 2; i++ {
		totoFileCurrent := totoFile + strconv.Itoa(i)
		/*send cmd though term*/
		data := []byte("echo helloWorld" + strconv.Itoa(i) + " >> " + totoFileCurrent + "\n")
		Debugf(t, "send following command through terminal: %v", string(data))
		require.Nil(t, sCli.Conn.Emit(xsapiv1.TerminalInEvent, data))
		waitForPrompt(t, chTerm, prompt) //waiting for terminal prompt

		/*check that toto file is created*/
		_, err := os.Stat(totoFileCurrent)
		require.Nil(t, err)

		/*send cmd though term*/
		data = []byte("cat " + totoFileCurrent + "\n")
		Debugf(t, "send following command through terminal: %v", string(data))
		require.Nil(t, sCli.Conn.Emit(xsapiv1.TerminalInEvent, data))

		<-chTerm                         //cmd sent
		termOut := <-chTerm              //result of cat cmd
		waitForPrompt(t, chTerm, prompt) //wait for terminal prompt
		/*check that terminal msg is what was written before*/
		require.Equal(t, string(termOut.Stdout), "helloWorld"+strconv.Itoa(i)+"\r\n")
		Debugf(t, "check terminal output msg: %v", string(termOut.Stdout))
	}

	PostTerms(t, "close", chTermEvt, nil, prompt)

	/*remove targets and terms*/
	RemoveTermsTargets(t, chTarget, chTermEvt)
	DisconnectTargetEvents(t)
	DisconnectTermEvents(t)
}

func TestTargetErrors(t *testing.T) {
	/*cannot create empty target*/
	target := xsapiv1.TargetConfig{}
	var targetRes xsapiv1.TargetConfig
	require.NotNil(t, HTTPCli.Post("/targets", target, &targetRes))
	Debugf(t, "error while creating empty target")
	/*check cannot create target with no IP*/
	target.Type = xsapiv1.TypeTgtStandard
	require.NotNil(t, HTTPCli.Post("/targets", target, &targetRes))
	Debugf(t, "error while creating target without IP")
	target.IP = "127.0.0.1"
	require.Nil(t, HTTPCli.Post("/targets", target, &targetRes))
	Debugf(t, "create target %v", targetRes.Name)

	/*cannot create empty terminal*/
	term := xsapiv1.TerminalConfig{}
	var termRes xsapiv1.TerminalConfig
	require.NotNil(t, HTTPCli.Post("/targets/"+targetRes.ID+"/terminals", term, &termRes))
	Debugf(t, "error while creating empty terminal")
	term.Type = xsapiv1.TypeTermSSH
	require.NotNil(t, HTTPCli.Post("/targets/"+"1010"+"/terminals", term, &termRes))
	Debugf(t, "error while creating terminal on an non existing target")
	require.Nil(t, HTTPCli.Post("/targets/"+targetRes.ID+"/terminals", term, &termRes))
	require.Nil(t, HTTPCli.Post("/targets/"+targetRes.ID+"/terminals", term, &termRes))
	require.Nil(t, HTTPCli.Post("/targets/"+targetRes.ID+"/terminals", term, &termRes))
	require.Nil(t, HTTPCli.Post("/targets/"+targetRes.ID+"/terminals", term, &termRes))
	Debugf(t, "create several terminals")

	/*remove targets and terms*/
	var targetArray []xsapiv1.TargetConfig
	require.Nil(t, HTTPCli.Get("/targets", &targetArray))
	for i := 0; i < len(targetArray); i++ {
		var termArray []xsapiv1.TerminalConfig
		require.Nil(t, HTTPCli.Get("/targets/"+targetArray[i].ID+"/terminals", &termArray))
		for j := 0; j < len(termArray); j++ {
			require.Nil(t, HTTPCli.Delete("/targets/"+targetArray[i].ID+"/terminals/"+termArray[j].ID, &termRes))
			Debugf(t, "delete terminal %v", termRes.Name)
			require.NotNil(t, HTTPCli.Delete("/targets/"+targetArray[i].ID+"/terminals/"+termArray[j].ID, &termRes))
			Debugf(t, "error while deleting an already deleted terminal %v", termRes.Name)
		}
		var tgtRes xsapiv1.TargetConfig
		require.Nil(t, HTTPCli.Delete("/targets/"+targetArray[i].ID, &tgtRes))
		Debugf(t, "delete target %v", tgtRes.Name)
		require.Equal(t, targetArray[i].ID, tgtRes.ID)
		require.NotNil(t, HTTPCli.Delete("/targets/"+targetArray[i].ID, &tgtRes))
		Debugf(t, "error while deleting an already deleted target %v", tgtRes.Name)
	}
}