X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=test%2Fexec_test.go;h=8759dbd7e3ce1f29233f6e5b61bfb244da048968;hb=72bb1c521aff5bb166db287ef426b243166d8927;hp=8fb6519ff7d9af0eada6c88f1e514931fdbf6d47;hpb=8b84b5cac636d639040fc9a7da485263ffbc9a09;p=src%2Fxds%2Fxds-server.git diff --git a/test/exec_test.go b/test/exec_test.go index 8fb6519..8759dbd 100644 --- a/test/exec_test.go +++ b/test/exec_test.go @@ -17,66 +17,89 @@ package xdsservertest import ( - "log" + "bytes" "os" + "os/exec" "path" "testing" "time" "gerrit.automotivelinux.org/gerrit/src/xds/xds-server/lib/xsapiv1" - git "github.com/libgit2/git2go" "github.com/stretchr/testify/assert" ) -func TestExec(t *testing.T) { - cloneRepo := "https://github.com/iotbzh/helloworld-service.git" - cloneDir := path.Join(os.Getenv(envRootCfgDir), "testExec") - t.Logf("Cloning repo %v in %v\n...\n", cloneRepo, cloneDir) - var cloneOptions git.CloneOptions - repository, err := git.Clone(cloneRepo, cloneDir, &cloneOptions) - if err != nil { - t.Fatal(err) - } +func InitExec(t *testing.T) string { + t.Logf("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) + var out bytes.Buffer + cmd.Stdout = &out + assert.Nil(t, cmd.Run()) - repository.Submodules.Foreach(func(sub *git.Submodule, name string) int { - sub.Init(true) - err := sub.Update(true, &git.SubmoduleUpdateOptions{ - &git.CheckoutOpts{ - Strategy: git.CheckoutForce | git.CheckoutUpdateSubmodules, - }, - &git.FetchOptions{}, - }) - if err != nil { - log.Fatal(err) - } - return 0 - - }) + /*clone submodules app templates into helloworld*/ + subHelloworldAppTemplateDir := path.Join(helloworldDir, "conf.d", "app-templates") + cmd = exec.Command("git", "clone", "https://gerrit.automotivelinux.org/gerrit/p/apps/app-templates.git", subHelloworldAppTemplateDir) + assert.Nil(t, cmd.Run()) + return helloworldDir +} - t.Logf("repo cloned\n") +/*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*/ + chSdks := make(chan xsapiv1.SDK) + defer close(chSdks) + sdk := xsapiv1.SDKInstallArgs{ + ID: "", + Filename: sdkFileName, + Force: false, + } + ConnectSDKStateChange(t, sCli, chSdks) + sdkRes := installFakeSdk(t, sdk, chSdks) + /*check there is no project*/ var cfgArray []xsapiv1.FolderConfig assert.Nil(t, HTTPCli.Get("/folders", &cfgArray)) assert.Equal(t, len(cfgArray), 0) fPrj := xsapiv1.FolderConfig{ Label: "testproject", - ClientPath: cloneDir, + ClientPath: helloworldDir, Type: xsapiv1.TypePathMap, ClientData: "clientdatatest", DataPathMap: xsapiv1.PathMapConfig{ - ServerPath: cloneDir, + ServerPath: helloworldDir, }, } + /*create project*/ var cfg xsapiv1.FolderConfig assert.Nil(t, HTTPCli.Post("/folders", fPrj, &cfg)) assert.NotNil(t, cfg) - _, err = NewIoSocketClient(prefixURL, HTTPCli.GetClientID()) - if err != nil { - t.Fatal(err) - } - cmd := "export PKG_CONFIG_PATH=/usr/local/lib64/pkgconfig/" + /*channel for ExecOutMsg*/ + chExec := make(chan xsapiv1.ExecOutMsg) + defer close(chExec) + /*connect to ExecOutEvent*/ + sCli.Conn.On(xsapiv1.ExecOutEvent, func(ev xsapiv1.ExecOutMsg) { + chExec <- ev + }) + + /*cmake helloworld project with fake sdk*/ + sdkSourceFile := path.Join(sdkRes.Path, "environment-setup-corei7-64-native-linux") + cmd := "source " + sdkSourceFile + cmd = cmd + " && " + cmd = cmd + "unset SDKTARGETSYSROOT" cmd = cmd + " && " cmd = cmd + "cd " + fPrj.ClientPath cmd = cmd + " && " @@ -86,28 +109,39 @@ func TestExec(t *testing.T) { cmd = cmd + " && " cmd = cmd + "cmake .." + /*post exec cmd cmake*/ exec := xsapiv1.ExecArgs{ ID: cfg.ID, Cmd: cmd, } var execRes xsapiv1.ExecArgs + t.Logf("exec cmake cmd(%v)", cmd) assert.Nil(t, HTTPCli.Post("/exec", exec, &execRes)) - time.Sleep(3 * time.Second) //maybe waiting for an event would be better + flushChannelExec(chExec, 1000) //waiting for execOutMsg - cmd = "export PKG_CONFIG_PATH=/usr/local/lib64/pkgconfig/" - cmd = cmd + "&&" + /*make helloworld project with fake sdk*/ + cmd = "source " + sdkSourceFile + cmd = cmd + " && " + cmd = cmd + "unset SDKTARGETSYSROOT" + cmd = cmd + " && " cmd = cmd + "cd " + fPrj.ClientPath cmd = cmd + "&&" cmd = cmd + "cd build" cmd = cmd + "&&" cmd = cmd + "make" exec.Cmd = cmd + /*post exec cmd make*/ + t.Logf("exec make cmd(%v)", cmd) assert.Nil(t, HTTPCli.Post("/exec", exec, &execRes)) - time.Sleep(3 * time.Second) //maybe waiting for an event would be better + flushChannelExec(chExec, 1000) //waiting for execOutMsg - /*check afb-helloworld.so exists*/ - _, err = os.Stat(path.Join(fPrj.ClientPath, "build/helloworld-afb/afb-helloworld.so")) + /*check if helloworld.so exists*/ + t.Log("check that helloworld.so exists") + _, err := os.Stat(path.Join(fPrj.ClientPath, "build/helloworld/helloworld.so")) assert.Nil(t, err) + /*deinit*/ assert.Nil(t, HTTPCli.Delete("/folders/"+cfg.ID, &cfg)) + RemoveSdk(t, sdkRes, chSdks) + DisconnectSDKStateChange(t, sCli) }