/* * Copyright (C) 2017-2018 "IoT.bzh" * Author Clément Bénier * * 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 ( "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 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*/ assert.Nil(t, HTTPCli.Post("/targets", target, &target)) Debugf(t, "add target %v", target.Name) targetEvt := <-chTarget //waiting for event targetAdd assert.Equal(t, target.ID, targetEvt.ID) listID[i] = target.ID } for i := 0; i < nbTargets; i++ { var target xsapiv1.TargetConfig assert.Nil(t, HTTPCli.Get("/targets/"+listID[i], &target)) assert.Equal(t, target.Status, "Enable") } return listID } func AddTerms(t *testing.T, nbTerms int, listID []string, chTermEvt chan xsapiv1.TerminalConfig) { 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, } /*add terminal on target*/ assert.Nil(t, HTTPCli.Post("/targets/"+listID[j]+"/terminals", term, &term)) Debugf(t, "add terminal %v", term.Name) termEvt := <-chTermEvt //waiting for event terminalAdd*/ assert.Equal(t, term.ID, termEvt.ID) listTermsID[i] = term.ID } assert.Equal(t, len(listTermsID), nbTerms) for i := 0; i < nbTerms; i++ { var term xsapiv1.TerminalConfig assert.Nil(t, HTTPCli.Get("/targets/"+listID[j]+"/terminals/"+listTermsID[i], &term)) assert.Equal(t, term.Status, "Close") } } } func PostTerms(t *testing.T, post string, chTermEvt chan xsapiv1.TerminalConfig) { var status string switch post { case "open": status = "Open" case "close": status = "Closing" } var targets []xsapiv1.TargetConfig assert.Nil(t, HTTPCli.Get("/targets", &targets)) for i := 0; i < len(targets); i++ { var terms []xsapiv1.TerminalConfig assert.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*/ assert.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 assert.Equal(t, term.ID, termEvt.ID) assert.Equal(t, term.Status, status) assert.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 assert.Nil(t, HTTPCli.Get("/targets/"+targets[i].ID+"/terminals/"+listTermsID[i], &term)) assert.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 assert.Nil(t, HTTPCli.Get("/targets", &targets)) for i := 0; i < len(targets); i++ { var terms []xsapiv1.TerminalConfig assert.Nil(t, HTTPCli.Get("/targets/"+targets[i].ID+"/terminals", &terms)) for j := 0; j < len(terms); j++ { var term xsapiv1.TerminalConfig assert.Nil(t, HTTPCli.Delete("/targets/"+targets[i].ID+"/terminals/"+terms[j].ID, &term)) termEvt := <-chTermEvt assert.Equal(t, term.ID, termEvt.ID) assert.NotNil(t, HTTPCli.Delete("/targets/"+targets[i].ID+"/terminals/"+terms[j].ID, &term)) Debugf(t, "remove terminal %v", term.Name) } var tgtRes xsapiv1.TargetConfig assert.Nil(t, HTTPCli.Delete("/targets/"+targets[i].ID, &tgtRes)) targetEvt := <-chTarget //waiting for remove terminal event assert.Equal(t, tgtRes.ID, targetEvt.ID) assert.Equal(t, targets[i].ID, tgtRes.ID) } } func TestTarget(t *testing.T) { nbTargets := 3 nbTermsByTarget := 3 /*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 assert.Nil(t, HTTPCli.Get("/targets", &targetArray)) assert.Equal(t, len(targetArray), 0) listID := AddTargets(t, nbTargets, chTarget) AddTerms(t, nbTermsByTarget, listID, chTermEvt) ///*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 //}) ///*just for the first term*/ //var terms []xsapiv1.TerminalConfig //var term xsapiv1.TerminalConfig //assert.Nil(t, HTTPCli.Get("/targets/"+listID[0]+"/terminals", &terms)) //assert.Nil(t, HTTPCli.Post("/targets/"+listID[0]+"/terminals/"+terms[0].ID+"/open", terms[0], &term)) //<-chTermEvt //waiting for event terminalStateChange //termOut := <-chTerm //waiting for terminalOutMsg //flushChannelTerm(chTerm, 50) //flushing all terminalOutMsg //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")) //first terminal msg should be Last Login //assert.Nil(t, HTTPCli.Post("/targets/"+listID[0]+"/terminals/"+terms[0].ID+"/close", terms[0], &term)) //<-chTermEvt //waiting for event terminalStateChange ///*open terminals*/ //PostTerms(t, "open", chTermEvt) //termOut = <-chTerm //waiting for terminalOutMsg //flushChannelTerm(chTerm, 50) //flushing all terminalOutMsg //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")) //first terminal msg should be Last Login ///*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)) // assert.Nil(t, sCli.Conn.Emit(xsapiv1.TerminalInEvent, data)) // flushChannelTerm(chTerm, 50) //waiting for terminal msg // /*check that toto file is created*/ // _, err := os.Stat(totoFileCurrent) // assert.Nil(t, err) // /*send cmd though term*/ // data = []byte("cat " + totoFileCurrent + "\n") // Debugf(t, "send following command through terminal: %v", string(data)) // assert.Nil(t, sCli.Conn.Emit(xsapiv1.TerminalInEvent, data)) // <-chTerm //cmd sent // termOut = <-chTerm //result of cat cmd // flushChannelTerm(chTerm, 50) //flushing what remains // /*check that terminal msg is what was written before*/ // assert.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) /*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 assert.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 assert.NotNil(t, HTTPCli.Post("/targets", target, &targetRes)) Debugf(t, "error while creating target without IP") target.IP = "127.0.0.1" assert.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 assert.NotNil(t, HTTPCli.Post("/targets/"+targetRes.ID+"/terminals", term, &termRes)) Debugf(t, "error while creating empty terminal") term.Type = xsapiv1.TypeTermSSH assert.NotNil(t, HTTPCli.Post("/targets/"+"1010"+"/terminals", term, &termRes)) Debugf(t, "error while creating terminal on an non existing target") 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)) Debugf(t, "create several terminals") /*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)) Debugf(t, "delete terminal %v", termRes.Name) assert.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 assert.Nil(t, HTTPCli.Delete("/targets/"+targetArray[i].ID, &tgtRes)) Debugf(t, "delete target %v", tgtRes.Name) assert.Equal(t, targetArray[i].ID, tgtRes.ID) assert.NotNil(t, HTTPCli.Delete("/targets/"+targetArray[i].ID, &tgtRes)) Debugf(t, "error while deleting an already deleted target %v", tgtRes.Name) } }