test exec: waiting exit event after exec cmd
[src/xds/xds-server.git] / test / exec_test.go
1 /*
2  * Copyright (C) 2017-2018 "IoT.bzh"
3  * Author Clément Bénier <clement.benier@iot.bzh>
4  *
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
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17 package xdsservertest
18
19 import (
20         "bytes"
21         "os"
22         "os/exec"
23         "path"
24         "testing"
25
26         "gerrit.automotivelinux.org/gerrit/src/xds/xds-server/lib/xsapiv1"
27         "github.com/stretchr/testify/assert"
28 )
29
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)
35         var out bytes.Buffer
36         cmd.Stdout = &out
37         assert.Nil(t, cmd.Run())
38
39         /*clone submodules app templates into helloworld*/
40         subHelloworldAppTemplateDir := path.Join(helloworldDir, "conf.d", "app-templates")
41         cmd = exec.Command("git", "clone", "https://gerrit.automotivelinux.org/gerrit/p/apps/app-templates.git", subHelloworldAppTemplateDir)
42         assert.Nil(t, cmd.Run())
43         return helloworldDir
44 }
45
46 func TestExec(t *testing.T) {
47         helloworldDir := InitExec(t)
48         /*channel for SDK events*/
49         chSdks := make(chan xsapiv1.SDK)
50         defer close(chSdks)
51         sdk := xsapiv1.SDKInstallArgs{
52                 ID:       "",
53                 Filename: sdkFileName,
54                 Force:    false,
55         }
56         ConnectSDKStateChange(t, sCli, chSdks)
57         sdkRes := installFakeSdk(t, sdk, chSdks)
58
59         /*check there is no project*/
60         var cfgArray []xsapiv1.FolderConfig
61         assert.Nil(t, HTTPCli.Get("/folders", &cfgArray))
62         assert.Equal(t, len(cfgArray), 0)
63
64         fPrj := xsapiv1.FolderConfig{
65                 Label:      "testproject",
66                 ClientPath: helloworldDir,
67                 Type:       xsapiv1.TypePathMap,
68                 ClientData: "clientdatatest",
69                 DataPathMap: xsapiv1.PathMapConfig{
70                         ServerPath: helloworldDir,
71                 },
72         }
73         /*create project*/
74         var cfg xsapiv1.FolderConfig
75         assert.Nil(t, HTTPCli.Post("/folders", fPrj, &cfg))
76         assert.NotNil(t, cfg)
77
78         /*channel for ExecOutMsg*/
79         chExec := make(chan xsapiv1.ExecExitMsg)
80         defer close(chExec)
81         /*connect to ExecOutEvent*/
82         sCli.Conn.On(xsapiv1.ExecExitEvent, func(ev xsapiv1.ExecExitMsg) {
83                 chExec <- ev
84         })
85
86         /*cmake helloworld project with fake sdk*/
87         sdkSourceFile := path.Join(sdkRes.Path, "environment-setup-corei7-64-native-linux")
88         cmd := "source " + sdkSourceFile
89         cmd = cmd + " && "
90         cmd = cmd + "unset SDKTARGETSYSROOT"
91         cmd = cmd + " && "
92         cmd = cmd + "cd " + fPrj.ClientPath
93         cmd = cmd + " && "
94         cmd = cmd + "mkdir -p build"
95         cmd = cmd + " && "
96         cmd = cmd + "cd build"
97         cmd = cmd + " && "
98         cmd = cmd + "cmake .."
99
100         /*post exec cmd cmake*/
101         exec := xsapiv1.ExecArgs{
102                 ID:  cfg.ID,
103                 Cmd: cmd,
104         }
105         var execRes xsapiv1.ExecArgs
106         Debugf(t, "exec cmake cmd(%v)", cmd)
107         assert.Nil(t, HTTPCli.Post("/exec", exec, &execRes))
108         exitMsg := <-chExec
109         assert.Equal(t, exitMsg.Code, 0)
110
111         /*make helloworld project with fake sdk*/
112         cmd = "source " + sdkSourceFile
113         cmd = cmd + " && "
114         cmd = cmd + "unset SDKTARGETSYSROOT"
115         cmd = cmd + " && "
116         cmd = cmd + "cd " + fPrj.ClientPath
117         cmd = cmd + "&&"
118         cmd = cmd + "cd build"
119         cmd = cmd + "&&"
120         cmd = cmd + "make"
121         exec.Cmd = cmd
122         /*post exec cmd make*/
123         Debugf(t, "exec make cmd(%v)", cmd)
124         assert.Nil(t, HTTPCli.Post("/exec", exec, &execRes))
125         exitMsg = <-chExec
126         assert.Equal(t, exitMsg.Code, 0)
127
128         /*check if helloworld.so exists*/
129         Debug(t, "check that helloworld.so exists")
130         _, err := os.Stat(path.Join(fPrj.ClientPath, "build/helloworld/helloworld.so"))
131         assert.Nil(t, err)
132
133         /*deinit*/
134         assert.Nil(t, HTTPCli.Delete("/folders/"+cfg.ID, &cfg))
135         RemoveSdk(t, sdkRes, chSdks)
136         DisconnectSDKStateChange(t, sCli)
137 }