summaryrefslogtreecommitdiffstats
path: root/test/target_test.go
blob: bc0be58a9ab03a060b221c2f6d38c836a952a5f1 (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
/*
 * 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 (
	"os"
	"path"
	"strconv"
	"strings"
	"testing"
	"time"

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

/*flush channel with timeout*/
func flushChannelTerm(channel chan xsapiv1.TerminalOutMsg, ms time.Duration) {
	timeoutB := false
	for !timeoutB {
		select {
		case <-channel:
		case <-time.After(ms * time.Millisecond):
			timeoutB = true
		}
	}
}

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}
	assert.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}
	assert.Nil(t, HTTPCli.Post("/events/register", args, nil))
}

func DisconnectTargetEvents(t *testing.T) {
	args := xsapiv1.EventRegisterArgs{Name: xsapiv1.EVTTargetAdd}
	assert.Nil(t, HTTPCli.Post("/events/unregister", args, nil))
	args = xsapiv1.EventRegisterArgs{Name: xsapiv1.EVTTargetRemove}
	assert.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}
	assert.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}
	assert.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}
	assert.Nil(t, HTTPCli.Post("/events/register", args, nil))
}

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

func TestTarget(t *testing.T) {
	chTarget := make(chan xsapiv1.TargetConfig)
	defer close(chTarget)
	ConnectTargetEvents(t, chTarget)

	chTermEvt := make(chan xsapiv1.TerminalConfig)
	defer close(chTermEvt)
	ConnectTermEvents(t, chTermEvt)

	var targetArray []xsapiv1.TargetConfig
	assert.Nil(t, HTTPCli.Get("/targets", &targetArray))
	assert.Equal(t, len(targetArray), 0)

	target := xsapiv1.TargetConfig{
		Name: "fakeTarget",
		Type: xsapiv1.TypeTgtStandard,
		IP:   "127.0.0.1",
	}
	var targetRes, targetResBis xsapiv1.TargetConfig
	assert.Nil(t, HTTPCli.Post("/targets", target, &targetRes))
	targetEvt := <-chTarget
	assert.Equal(t, targetRes.ID, targetEvt.ID)
	target.Name = "fakeTargetBis"
	assert.Nil(t, HTTPCli.Post("/targets", target, &targetResBis))
	targetEvt = <-chTarget
	assert.Equal(t, targetResBis.ID, targetEvt.ID)
	assert.Equal(t, targetRes.Status, "Enable")
	assert.Equal(t, targetResBis.Status, "Enable")

	term := xsapiv1.TerminalConfig{
		Name: "terminal",
		Type: xsapiv1.TypeTermSSH,
	}
	var termRes, termResBis xsapiv1.TerminalConfig
	assert.Nil(t, HTTPCli.Post("/targets/"+targetRes.ID+"/terminals", term, &termRes))
	termEvt := <-chTermEvt
	assert.Equal(t, termRes.ID, termEvt.ID)
	term.Name = "terminalBis"
	assert.Nil(t, HTTPCli.Post("/targets/"+targetResBis.ID+"/terminals", term, &termResBis))
	termEvt = <-chTermEvt
	assert.Equal(t, termResBis.ID, termEvt.ID)
	assert.Equal(t, termRes.Status, "Enable")
	assert.Equal(t, termResBis.Status, "Enable")

	chTerm := make(chan xsapiv1.TerminalOutMsg)
	defer close(chTerm)

	sCli.Conn.On(xsapiv1.TerminalOutEvent, func(ev xsapiv1.TerminalOutMsg) {
		chTerm <- ev
	})

	assert.Nil(t, HTTPCli.Post("/targets/"+targetRes.ID+"/terminals/"+termRes.ID+"/open", termRes, &termRes))
	termEvt = <-chTermEvt
	assert.Equal(t, termRes.ID, termEvt.ID)
	assert.Equal(t, termEvt.Status, "Open")
	assert.Nil(t, HTTPCli.Post("/targets/"+targetResBis.ID+"/terminals/"+termResBis.ID+"/open", termResBis, &termResBis))
	termEvt = <-chTermEvt
	assert.Equal(t, termResBis.ID, termEvt.ID)
	assert.Equal(t, termEvt.Status, "Open")
	assert.Equal(t, termRes.Status, "Open")
	assert.Equal(t, termResBis.Status, "Open")

	termOut := <-chTerm
	flushChannelTerm(chTerm, 50)
	stdoutMsg := string(termOut.Stdout)
	if strings.Contains(stdoutMsg, "Connection refused") {
		t.Fatalf("%vYou may have to launch ssh server", stdoutMsg)
	} else if strings.Contains(stdoutMsg, "password") {
		t.Fatalf("%vcopy your pub key in authorized_keys\ncat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys", stdoutMsg)
	}
	assert.True(t, strings.Contains(stdoutMsg, "Last login"))

	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")

		assert.Nil(t, sCli.Conn.Emit(xsapiv1.TerminalInEvent, data))
		flushChannelTerm(chTerm, 50)

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

		data = []byte("cat " + totoFileCurrent + "\n")
		assert.Nil(t, sCli.Conn.Emit(xsapiv1.TerminalInEvent, data))

		<-chTerm           //cmd sent
		termOut = <-chTerm //result of cat cmd
		flushChannelTerm(chTerm, 50)
		assert.Equal(t, string(termOut.Stdout), "helloWorld"+strconv.Itoa(i)+"\r\n")
	}

	assert.Nil(t, HTTPCli.Post("/targets/"+targetRes.ID+"/terminals/"+termRes.ID+"/close", termRes, &termRes))
	termEvt = <-chTermEvt
	assert.Equal(t, termRes.ID, termEvt.ID)
	assert.Equal(t, termEvt.Status, "Close")
	assert.Nil(t, HTTPCli.Post("/targets/"+targetResBis.ID+"/terminals/"+termResBis.ID+"/close", termResBis, &termResBis))
	termEvt = <-chTermEvt
	assert.Equal(t, termResBis.ID, termEvt.ID)
	assert.Equal(t, termEvt.Status, "Close")
	assert.Equal(t, termRes.Status, "Close")
	assert.Equal(t, termResBis.Status, "Close")

	/*remove targets and terms*/
	assert.Nil(t, HTTPCli.Get("/targets", &targetArray))
	for i := 0; i < len(targetArray); i++ {
		var termArray []xsapiv1.TerminalConfig
		assert.Nil(t, HTTPCli.Get("/targets/"+targetArray[i].ID+"/terminals", &termArray))
		for j := 0; j < len(termArray); j++ {
			assert.Nil(t, HTTPCli.Delete("/targets/"+targetArray[i].ID+"/terminals/"+termArray[j].ID, &termRes))
			termEvt = <-chTermEvt
			assert.Equal(t, termRes.ID, termEvt.ID)
			assert.NotNil(t, HTTPCli.Delete("/targets/"+targetArray[i].ID+"/terminals/"+termArray[j].ID, &termRes))
		}
		var tgtRes xsapiv1.TargetConfig
		assert.Nil(t, HTTPCli.Delete("/targets/"+targetArray[i].ID, &tgtRes))
		targetEvt = <-chTarget
		assert.Equal(t, tgtRes.ID, targetEvt.ID)
		assert.Equal(t, targetArray[i].ID, tgtRes.ID)
	}
	DisconnectTargetEvents(t)
	DisconnectTermEvents(t)
}

func TestTargetErrors(t *testing.T) {
	target := xsapiv1.TargetConfig{}
	var targetRes xsapiv1.TargetConfig
	assert.NotNil(t, HTTPCli.Post("/targets", target, &targetRes))
	target.Type = xsapiv1.TypeTgtStandard
	assert.NotNil(t, HTTPCli.Post("/targets", target, &targetRes))
	target.IP = "127.0.0.1"
	assert.Nil(t, HTTPCli.Post("/targets", target, &targetRes))

	term := xsapiv1.TerminalConfig{}
	var termRes xsapiv1.TerminalConfig
	assert.NotNil(t, HTTPCli.Post("/targets/"+targetRes.ID+"/terminals", term, &termRes))
	term.Type = xsapiv1.TypeTermSSH
	assert.NotNil(t, HTTPCli.Post("/targets/"+"1010"+"/terminals", term, &termRes))
	assert.Nil(t, HTTPCli.Post("/targets/"+targetRes.ID+"/terminals", term, &termRes))
	assert.Nil(t, HTTPCli.Post("/targets/"+targetRes.ID+"/terminals", term, &termRes))
	assert.Nil(t, HTTPCli.Post("/targets/"+targetRes.ID+"/terminals", term, &termRes))
	assert.Nil(t, HTTPCli.Post("/targets/"+targetRes.ID+"/terminals", term, &termRes))

	/*remove targets and terms*/
	var targetArray []xsapiv1.TargetConfig
	assert.Nil(t, HTTPCli.Get("/targets", &targetArray))
	for i := 0; i < len(targetArray); i++ {
		var termArray []xsapiv1.TerminalConfig
		assert.Nil(t, HTTPCli.Get("/targets/"+targetArray[i].ID+"/terminals", &termArray))
		for j := 0; j < len(termArray); j++ {
			assert.Nil(t, HTTPCli.Delete("/targets/"+targetArray[i].ID+"/terminals/"+termArray[j].ID, &termRes))
			assert.NotNil(t, HTTPCli.Delete("/targets/"+targetArray[i].ID+"/terminals/"+termArray[j].ID, &termRes))
		}
		var tgtRes xsapiv1.TargetConfig
		assert.Nil(t, HTTPCli.Delete("/targets/"+targetArray[i].ID, &tgtRes))
		assert.Equal(t, targetArray[i].ID, tgtRes.ID)
		assert.NotNil(t, HTTPCli.Delete("/targets/"+targetArray[i].ID, &tgtRes))
	}
}