diff options
-rw-r--r-- | test/target_test.go | 189 |
1 files changed, 114 insertions, 75 deletions
diff --git a/test/target_test.go b/test/target_test.go index bc0be58..401c641 100644 --- a/test/target_test.go +++ b/test/target_test.go @@ -100,78 +100,140 @@ func DisconnectTermEvents(t *testing.T) { 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)) + 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)) + 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 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 + assert.Nil(t, HTTPCli.Post("/targets/"+targets[i].ID+"/terminals/"+terms[j].ID+"/"+post, term, &term)) + termEvt := <-chTermEvt //waiting for event terminalStateChange + assert.Equal(t, term.ID, termEvt.ID) + assert.True(t, strings.EqualFold(termEvt.Status, post)) + listTermsID[i] = term.ID + } + 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)) + } + } +} + +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)) + } + var tgtRes xsapiv1.TargetConfig + assert.Nil(t, HTTPCli.Delete("/targets/"+targets[i].ID, &tgtRes)) + targetEvt := <-chTarget + 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) - 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") + 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 }) - 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) + /*open first term*/ + PostTerms(t, "open", chTermEvt) + + /*just for the first term*/ + 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")) + 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") @@ -180,51 +242,28 @@ func TestTarget(t *testing.T) { 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) + 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") assert.Nil(t, sCli.Conn.Emit(xsapiv1.TerminalInEvent, data)) - <-chTerm //cmd sent - termOut = <-chTerm //result of cat cmd - flushChannelTerm(chTerm, 50) + <-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") } - 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") + PostTerms(t, "close", chTermEvt) /*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) - } + RemoveTermsTargets(t, chTarget, chTermEvt) DisconnectTargetEvents(t) DisconnectTermEvents(t) } |