2 * Copyright (C) 2017-2018 "IoT.bzh"
3 * Author Clément Bénier <clement.benier@iot.bzh>
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
26 "gerrit.automotivelinux.org/gerrit/src/xds/xds-server/lib/xsapiv1"
27 "github.com/stretchr/testify/assert"
30 func InitExec(t *testing.T) string {
31 Debugf(t, "Create helloworld directory with app-templates")
32 /*copy helloworld from fixtures to envRootCfgDir*/
33 helloworldDir := path.Join(os.Getenv(envRootCfgDir), "helloworld")
34 cmd := exec.Command("cp", "-r", helloworldFixturesDir, helloworldDir)
37 assert.Nil(t, cmd.Run())
39 /*clone submodules app templates into helloworld*/
40 subHelloworldAppTemplateDir := path.Join(helloworldDir, "conf.d", "app-templates")
41 cmd = exec.Command("git", "clone", "-b", "flounder",
42 "https://gerrit.automotivelinux.org/gerrit/p/apps/app-templates.git",
43 subHelloworldAppTemplateDir)
44 assert.Nil(t, cmd.Run())
48 func TestExec(t *testing.T) {
49 helloworldDir := InitExec(t)
50 /*channel for SDK events*/
51 chSdks := make(chan xsapiv1.SDK)
53 sdk := xsapiv1.SDKInstallArgs{
55 Filename: sdkFileName,
58 ConnectSDKStateChange(t, sCli, chSdks)
59 sdkRes := installFakeSdk(t, sdk, chSdks)
61 /*check there is no project*/
62 var cfgArray []xsapiv1.FolderConfig
63 assert.Nil(t, HTTPCli.Get("/folders", &cfgArray))
64 assert.Equal(t, len(cfgArray), 0)
66 fPrj := xsapiv1.FolderConfig{
68 ClientPath: helloworldDir,
69 Type: xsapiv1.TypePathMap,
70 ClientData: "clientdatatest",
71 DataPathMap: xsapiv1.PathMapConfig{
72 ServerPath: helloworldDir,
76 var cfg xsapiv1.FolderConfig
77 assert.Nil(t, HTTPCli.Post("/folders", fPrj, &cfg))
80 /*channel for ExecExitMsg*/
81 chExec := make(chan xsapiv1.ExecExitMsg)
83 sCli.Conn.On(xsapiv1.ExecExitEvent, func(ev xsapiv1.ExecExitMsg) {
87 /*Collect commands output in */
89 sCli.Conn.On(xsapiv1.ExecOutEvent, func(ev xsapiv1.ExecOutMsg) {
90 cmdOut += ev.Stdout + ev.Stderr
93 /*basic check: verify that environment is set correctly (use the right sdk)*/
94 var execRes xsapiv1.ExecArgs
95 cmd := "pwd && echo \"SDKTARGETSYSROOT=<$SDKTARGETSYSROOT>\""
96 exec := xsapiv1.ExecArgs{
101 Debugf(t, "exec cmake cmd(%v)", cmd)
103 assert.Nil(t, HTTPCli.Post("/exec", exec, &execRes))
105 assert.Equal(t, exitMsg.Code, 0)
107 /*cmake helloworld project with fake sdk*/
108 cmd = "cd " + fPrj.ClientPath
110 cmd = cmd + "mkdir -p build"
112 cmd = cmd + "cd build"
114 cmd = cmd + "cmake .."
116 /*post exec cmd cmake*/
117 exec = xsapiv1.ExecArgs{
121 Debugf(t, "exec cmake cmd(%v)", cmd)
122 assert.Nil(t, HTTPCli.Post("/exec", exec, &execRes))
124 assert.Equal(t, exitMsg.Code, 0)
126 /*make helloworld project with fake sdk*/
127 cmd = "cd " + fPrj.ClientPath
129 cmd = cmd + "cd build"
133 /*post exec cmd make*/
134 Debugf(t, "exec make cmd(%v)", cmd)
135 assert.Nil(t, HTTPCli.Post("/exec", exec, &execRes))
137 assert.Equal(t, exitMsg.Code, 0)
139 /*check if helloworld.so exists*/
140 Debug(t, "check that helloworld.so exists")
141 _, err := os.Stat(path.Join(fPrj.ClientPath, "build/helloworld/helloworld.so"))
145 assert.Nil(t, HTTPCli.Delete("/folders/"+cfg.ID, &cfg))
146 RemoveSdk(t, sdkRes, chSdks)
147 DisconnectSDKStateChange(t, sCli)