summaryrefslogtreecommitdiffstats
path: root/test/target_test.go
diff options
context:
space:
mode:
authorClément Bénier <clement.benier@iot.bzh>2018-10-23 15:09:06 +0200
committerJan-Simon Moeller <jsmoeller@linuxfoundation.org>2018-10-24 10:43:37 +0000
commita740425411f120ff7263f16f268e7efd7c19b311 (patch)
tree4ef142dfb4b5c57648e4e00243a234b2ad31f086 /test/target_test.go
parent3e3222406a6f015a9fee3720e8ba76ecf58b087f (diff)
testify: assert replace by require
require handles tests to be aborted during an assertion contrary to assert Change-Id: I3062046515c74509ae7ea1b24b7242a42d0191c1 Signed-off-by: Clément Bénier <clement.benier@iot.bzh>
Diffstat (limited to 'test/target_test.go')
-rw-r--r--test/target_test.go128
1 files changed, 64 insertions, 64 deletions
diff --git a/test/target_test.go b/test/target_test.go
index d7576c6..4e863fc 100644
--- a/test/target_test.go
+++ b/test/target_test.go
@@ -31,7 +31,7 @@ import (
common "gerrit.automotivelinux.org/gerrit/src/xds/xds-common.git/golib"
"gerrit.automotivelinux.org/gerrit/src/xds/xds-server/lib/xsapiv1"
- "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
)
func launchSSHd(sshDir string, proc **os.Process, sshdCmd string) (*os.File, string) {
@@ -70,11 +70,11 @@ func InitSSH(t *testing.T, procSSHd **os.Process) (string, string) {
cmd := exec.Command("cp", "-r", sshFixturesDir, sshDir)
var out bytes.Buffer
cmd.Stdout = &out
- assert.Nil(t, cmd.Run())
+ require.Nil(t, cmd.Run())
cmd = exec.Command("ls", sshDir)
cmd.Stdout = &out
- assert.Nil(t, cmd.Run())
+ require.Nil(t, cmd.Run())
files := strings.Split(fmt.Sprint(cmd.Stdout), "\n")
@@ -83,7 +83,7 @@ func InitSSH(t *testing.T, procSSHd **os.Process) (string, string) {
file := sshDir + "/" + f
cmd = exec.Command("chmod", "600", file)
cmd.Stdout = &out
- assert.Nil(t, cmd.Run())
+ require.Nil(t, cmd.Run())
}
}
@@ -97,7 +97,7 @@ func InitSSH(t *testing.T, procSSHd **os.Process) (string, string) {
} else if common.Exists("/usr/bin/sshd") {
sshdCmd = "/usr/sbin/sshd"
} else {
- assert.FailNow(t, "Cannot find sshd command, please install it or set in your PATH")
+ require.FailNow(t, "Cannot find sshd command, please install it or set in your PATH")
}
} else {
sshdCmd = strings.TrimSpace(fmt.Sprint(cmd.Stdout))
@@ -137,7 +137,7 @@ func waitForPrompt(t *testing.T, channel chan xsapiv1.TerminalOutMsg, prompt str
case <-time.After(step):
current = current + step
if current >= timeout {
- assert.FailNow(t, "Never received prompt message from terminal (output:"+out+")")
+ require.FailNow(t, "Never received prompt message from terminal (output:"+out+")")
}
}
}
@@ -150,7 +150,7 @@ func ConnectTargetEvents(t *testing.T, channel chan xsapiv1.TargetConfig) {
})
args := xsapiv1.EventRegisterArgs{Name: xsapiv1.EVTTargetAdd}
- assert.Nil(t, HTTPCli.Post("/events/register", args, nil))
+ require.Nil(t, HTTPCli.Post("/events/register", args, nil))
sCli.Conn.On(xsapiv1.EVTTargetRemove, func(e xsapiv1.EventMsg) {
target, _ := e.DecodeTargetEvent()
@@ -158,14 +158,14 @@ func ConnectTargetEvents(t *testing.T, channel chan xsapiv1.TargetConfig) {
})
args = xsapiv1.EventRegisterArgs{Name: xsapiv1.EVTTargetRemove}
- assert.Nil(t, HTTPCli.Post("/events/register", args, nil))
+ require.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))
+ require.Nil(t, HTTPCli.Post("/events/unregister", args, nil))
args = xsapiv1.EventRegisterArgs{Name: xsapiv1.EVTTargetRemove}
- assert.Nil(t, HTTPCli.Post("/events/unregister", args, nil))
+ require.Nil(t, HTTPCli.Post("/events/unregister", args, nil))
}
func ConnectTermEvents(t *testing.T, channel chan xsapiv1.TerminalConfig) {
@@ -175,7 +175,7 @@ func ConnectTermEvents(t *testing.T, channel chan xsapiv1.TerminalConfig) {
})
args := xsapiv1.EventRegisterArgs{Name: xsapiv1.EVTTargetTerminalAdd}
- assert.Nil(t, HTTPCli.Post("/events/register", args, nil))
+ require.Nil(t, HTTPCli.Post("/events/register", args, nil))
sCli.Conn.On(xsapiv1.EVTTargetTerminalStateChange, func(e xsapiv1.EventMsg) {
termEvt, _ := e.DecodeTerminalEvent()
@@ -183,7 +183,7 @@ func ConnectTermEvents(t *testing.T, channel chan xsapiv1.TerminalConfig) {
})
args = xsapiv1.EventRegisterArgs{Name: xsapiv1.EVTTargetTerminalStateChange}
- assert.Nil(t, HTTPCli.Post("/events/register", args, nil))
+ require.Nil(t, HTTPCli.Post("/events/register", args, nil))
sCli.Conn.On(xsapiv1.EVTTargetTerminalRemove, func(e xsapiv1.EventMsg) {
termEvt, _ := e.DecodeTerminalEvent()
@@ -191,16 +191,16 @@ func ConnectTermEvents(t *testing.T, channel chan xsapiv1.TerminalConfig) {
})
args = xsapiv1.EventRegisterArgs{Name: xsapiv1.EVTTargetTerminalRemove}
- assert.Nil(t, HTTPCli.Post("/events/register", args, nil))
+ require.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))
+ require.Nil(t, HTTPCli.Post("/events/unregister", args, nil))
args = xsapiv1.EventRegisterArgs{Name: xsapiv1.EVTTargetTerminalStateChange}
- assert.Nil(t, HTTPCli.Post("/events/unregister", args, nil))
+ require.Nil(t, HTTPCli.Post("/events/unregister", args, nil))
args = xsapiv1.EventRegisterArgs{Name: xsapiv1.EVTTargetTerminalRemove}
- assert.Nil(t, HTTPCli.Post("/events/unregister", args, nil))
+ require.Nil(t, HTTPCli.Post("/events/unregister", args, nil))
}
func AddTargets(t *testing.T, nbTargets int, chTarget chan xsapiv1.TargetConfig) []string {
@@ -213,16 +213,16 @@ func AddTargets(t *testing.T, nbTargets int, chTarget chan xsapiv1.TargetConfig)
IP: "127.0.0.1",
}
/*add target*/
- assert.Nil(t, HTTPCli.Post("/targets", target, &target))
+ require.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)
+ require.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")
+ require.Nil(t, HTTPCli.Get("/targets/"+listID[i], &target))
+ require.Equal(t, target.Status, "Enable")
}
return listID
}
@@ -244,17 +244,17 @@ func AddTerms(t *testing.T, nbTerms int, listID []string, chTermEvt chan xsapiv1
},
}
/*add terminal on target*/
- assert.Nil(t, HTTPCli.Post("/targets/"+listID[j]+"/terminals", term, &term))
+ require.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)
+ require.Equal(t, term.ID, termEvt.ID)
listTermsID[i] = term.ID
}
- assert.Equal(t, len(listTermsID), nbTerms)
+ require.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")
+ require.Nil(t, HTTPCli.Get("/targets/"+listID[j]+"/terminals/"+listTermsID[i], &term))
+ require.Equal(t, term.Status, "Close")
}
}
}
@@ -269,32 +269,32 @@ func PostTerms(t *testing.T, post string, chTermEvt chan xsapiv1.TerminalConfig,
status = "Closing"
}
var targets []xsapiv1.TargetConfig
- assert.Nil(t, HTTPCli.Get("/targets", &targets))
+ require.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))
+ 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*/
- assert.Nil(t, HTTPCli.Post("/targets/"+targets[i].ID+"/terminals/"+terms[j].ID+"/"+post, terms[j], &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")
- assert.Nil(t, sCli.Conn.Emit(xsapiv1.TerminalInEvent, data))
+ require.Nil(t, sCli.Conn.Emit(xsapiv1.TerminalInEvent, data))
waitForPrompt(t, chTerm, prompt)
}
- assert.Equal(t, term.ID, termEvt.ID)
- assert.Equal(t, term.Status, status)
- assert.Equal(t, termEvt.Status, status)
+ 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
- assert.Nil(t, HTTPCli.Get("/targets/"+targets[i].ID+"/terminals/"+listTermsID[i], &term))
- assert.True(t, strings.EqualFold(term.Status, post))
+ 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)
}
}
@@ -302,23 +302,23 @@ func PostTerms(t *testing.T, post string, chTermEvt chan xsapiv1.TerminalConfig,
func RemoveTermsTargets(t *testing.T, chTarget chan xsapiv1.TargetConfig, chTermEvt chan xsapiv1.TerminalConfig) {
var targets []xsapiv1.TargetConfig
- assert.Nil(t, HTTPCli.Get("/targets", &targets))
+ require.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))
+ require.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))
+ require.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))
+ 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
- assert.Nil(t, HTTPCli.Delete("/targets/"+targets[i].ID, &tgtRes))
+ require.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)
+ require.Equal(t, tgtRes.ID, targetEvt.ID)
+ require.Equal(t, targets[i].ID, tgtRes.ID)
}
}
func TestTarget(t *testing.T) {
@@ -341,8 +341,8 @@ func TestTarget(t *testing.T) {
/*check that targetArray is empty at startup*/
var targetArray []xsapiv1.TargetConfig
- assert.Nil(t, HTTPCli.Get("/targets", &targetArray))
- assert.Equal(t, len(targetArray), 0)
+ 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)
@@ -369,23 +369,23 @@ func TestTarget(t *testing.T) {
/*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))
+ 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)
- assert.Nil(t, err)
+ require.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))
+ 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*/
- assert.Equal(t, string(termOut.Stdout), "helloWorld"+strconv.Itoa(i)+"\r\n")
+ require.Equal(t, string(termOut.Stdout), "helloWorld"+strconv.Itoa(i)+"\r\n")
Debugf(t, "check terminal output msg: %v", string(termOut.Stdout))
}
@@ -401,47 +401,47 @@ func TestTargetErrors(t *testing.T) {
/*cannot create empty target*/
target := xsapiv1.TargetConfig{}
var targetRes xsapiv1.TargetConfig
- assert.NotNil(t, HTTPCli.Post("/targets", target, &targetRes))
+ 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
- assert.NotNil(t, HTTPCli.Post("/targets", target, &targetRes))
+ require.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))
+ 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
- assert.NotNil(t, HTTPCli.Post("/targets/"+targetRes.ID+"/terminals", term, &termRes))
+ require.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))
+ require.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))
+ 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
- assert.Nil(t, HTTPCli.Get("/targets", &targetArray))
+ require.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))
+ require.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))
+ require.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))
+ 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
- assert.Nil(t, HTTPCli.Delete("/targets/"+targetArray[i].ID, &tgtRes))
+ require.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))
+ 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)
}
}