summaryrefslogtreecommitdiffstats
path: root/test/target_test.go
diff options
context:
space:
mode:
authorClément Bénier <clement.benier@iot.bzh>2018-08-14 16:07:02 +0200
committerClément Bénier <clement.benier@iot.bzh>2018-08-21 11:08:36 +0200
commit4c01e5e3fad9455b97519380babdf7e8801d8643 (patch)
tree6656e3cbd2b56374a0ab1dde8869c6015c97bf67 /test/target_test.go
parent8b84b5cac636d639040fc9a7da485263ffbc9a09 (diff)
test target: add test for target and terminals
Change-Id: I3761debe4ffe8971a7e94a55136b5dc34a8c65c2 Signed-off-by: Clément Bénier <clement.benier@iot.bzh>
Diffstat (limited to 'test/target_test.go')
-rw-r--r--test/target_test.go175
1 files changed, 175 insertions, 0 deletions
diff --git a/test/target_test.go b/test/target_test.go
new file mode 100644
index 0000000..696c8fc
--- /dev/null
+++ b/test/target_test.go
@@ -0,0 +1,175 @@
+/*
+ * 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 TestTarget(t *testing.T) {
+ 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))
+ target.Name = "fakeTargetBis"
+ assert.Nil(t, HTTPCli.Post("/targets", target, &targetResBis))
+ 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))
+ term.Name = "terminalBis"
+ assert.Nil(t, HTTPCli.Post("/targets/"+targetResBis.ID+"/terminals", term, &termResBis))
+ assert.Equal(t, termRes.Status, "Enable")
+ assert.Equal(t, termResBis.Status, "Enable")
+
+ chTerm := make(chan xsapiv1.TerminalOutMsg)
+ defer close(chTerm)
+ sCli, err := NewIoSocketClient(prefixURL, HTTPCli.GetClientID())
+ if err != nil {
+ t.Fatal(err)
+ }
+ 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))
+ assert.Nil(t, HTTPCli.Post("/targets/"+targetResBis.ID+"/terminals/"+termResBis.ID+"/open", termResBis, &termResBis))
+ 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))
+ assert.Nil(t, HTTPCli.Post("/targets/"+targetResBis.ID+"/terminals/"+termResBis.ID+"/close", termResBis, &termResBis))
+ 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))
+ 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)
+ }
+}
+
+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))
+ }
+}