test exec: waiting exit event after exec cmd
[src/xds/xds-server.git] / test / exec_test.go
index 8759dbd..884ba87 100644 (file)
@@ -22,14 +22,13 @@ import (
        "os/exec"
        "path"
        "testing"
-       "time"
 
        "gerrit.automotivelinux.org/gerrit/src/xds/xds-server/lib/xsapiv1"
        "github.com/stretchr/testify/assert"
 )
 
 func InitExec(t *testing.T) string {
-       t.Logf("Create helloworld directory with app-templates")
+       Debugf(t, "Create helloworld directory with app-templates")
        /*copy helloworld from fixtures to envRootCfgDir*/
        helloworldDir := path.Join(os.Getenv(envRootCfgDir), "helloworld")
        cmd := exec.Command("cp", "-r", helloworldFixturesDir, helloworldDir)
@@ -44,17 +43,6 @@ func InitExec(t *testing.T) string {
        return helloworldDir
 }
 
-/*flush channel with timeout*/
-func flushChannelExec(channel chan xsapiv1.ExecOutMsg, ms time.Duration) {
-       timeoutB := false
-       for !timeoutB {
-               select {
-               case <-channel:
-               case <-time.After(ms * time.Millisecond):
-                       timeoutB = true
-               }
-       }
-}
 func TestExec(t *testing.T) {
        helloworldDir := InitExec(t)
        /*channel for SDK events*/
@@ -88,10 +76,10 @@ func TestExec(t *testing.T) {
        assert.NotNil(t, cfg)
 
        /*channel for ExecOutMsg*/
-       chExec := make(chan xsapiv1.ExecOutMsg)
+       chExec := make(chan xsapiv1.ExecExitMsg)
        defer close(chExec)
        /*connect to ExecOutEvent*/
-       sCli.Conn.On(xsapiv1.ExecOutEvent, func(ev xsapiv1.ExecOutMsg) {
+       sCli.Conn.On(xsapiv1.ExecExitEvent, func(ev xsapiv1.ExecExitMsg) {
                chExec <- ev
        })
 
@@ -115,9 +103,10 @@ func TestExec(t *testing.T) {
                Cmd: cmd,
        }
        var execRes xsapiv1.ExecArgs
-       t.Logf("exec cmake cmd(%v)", cmd)
+       Debugf(t, "exec cmake cmd(%v)", cmd)
        assert.Nil(t, HTTPCli.Post("/exec", exec, &execRes))
-       flushChannelExec(chExec, 1000) //waiting for execOutMsg
+       exitMsg := <-chExec
+       assert.Equal(t, exitMsg.Code, 0)
 
        /*make helloworld project with fake sdk*/
        cmd = "source " + sdkSourceFile
@@ -131,12 +120,13 @@ func TestExec(t *testing.T) {
        cmd = cmd + "make"
        exec.Cmd = cmd
        /*post exec cmd make*/
-       t.Logf("exec make cmd(%v)", cmd)
+       Debugf(t, "exec make cmd(%v)", cmd)
        assert.Nil(t, HTTPCli.Post("/exec", exec, &execRes))
-       flushChannelExec(chExec, 1000) //waiting for execOutMsg
+       exitMsg = <-chExec
+       assert.Equal(t, exitMsg.Code, 0)
 
        /*check if helloworld.so exists*/
-       t.Log("check that helloworld.so exists")
+       Debug(t, "check that helloworld.so exists")
        _, err := os.Stat(path.Join(fPrj.ClientPath, "build/helloworld/helloworld.so"))
        assert.Nil(t, err)