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.git/lib/xsapiv1"
27 "github.com/stretchr/testify/require"
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 require.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 require.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 require.Nil(t, HTTPCli.Get("/folders", &cfgArray))
64 require.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 require.Nil(t, HTTPCli.Post("/folders", fPrj, &cfg))
78 require.NotNil(t, 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
92 /*error when exec with fakeid*/
93 var execRes xsapiv1.ExecArgs
94 cmd := "pwd && echo \"SDKTARGETSYSROOT=<$SDKTARGETSYSROOT>\""
95 exec := xsapiv1.ExecArgs{
98 SdkID: "11111-invalid",
100 Debugf(t, "exec cmake cmd(%v)", cmd)
102 require.NotNil(t, HTTPCli.Post("/exec", exec, &execRes))
104 /*basic check: verify that environment is set correctly (use the right sdk)*/
105 cmd = "pwd && echo \"SDKTARGETSYSROOT=<$SDKTARGETSYSROOT>\""
106 exec = xsapiv1.ExecArgs{
111 Debugf(t, "exec cmake cmd(%v)", cmd)
113 require.Nil(t, HTTPCli.Post("/exec", exec, &execRes))
115 require.Equal(t, exitMsg.Code, 0)
117 /*cmake helloworld project with fake sdk*/
118 cmd = "cd " + fPrj.ClientPath
120 cmd = cmd + "mkdir -p build"
122 cmd = cmd + "cd build"
124 cmd = cmd + "cmake .."
126 /*post exec cmd cmake*/
127 exec = xsapiv1.ExecArgs{
131 Debugf(t, "exec cmake cmd(%v)", cmd)
132 require.Nil(t, HTTPCli.Post("/exec", exec, &execRes))
134 require.Equal(t, exitMsg.Code, 0)
136 /*make helloworld project with fake sdk*/
137 cmd = "cd " + fPrj.ClientPath
139 cmd = cmd + "cd build"
143 /*post exec cmd make*/
144 Debugf(t, "exec make cmd(%v)", cmd)
145 require.Nil(t, HTTPCli.Post("/exec", exec, &execRes))
147 require.Equal(t, exitMsg.Code, 0)
149 /*check if helloworld.so exists*/
150 Debug(t, "check that helloworld.so exists")
151 _, err := os.Stat(path.Join(fPrj.ClientPath, "build/helloworld/helloworld.so"))
155 require.Nil(t, HTTPCli.Delete("/folders/"+cfg.ID, &cfg))
156 RemoveSdk(t, sdkRes, chSdks)
157 DisconnectSDKStateChange(t, sCli)