1ab781a1fb764a6b399b514c556b9842d1ccd903
[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         "time"
26
27         "gerrit.automotivelinux.org/gerrit/src/xds/xds-server/lib/xsapiv1"
28         "github.com/stretchr/testify/assert"
29 )
30
31 func InitExec(t *testing.T) string {
32         Debugf(t, "Create helloworld directory with app-templates")
33         /*copy helloworld from fixtures to envRootCfgDir*/
34         helloworldDir := path.Join(os.Getenv(envRootCfgDir), "helloworld")
35         cmd := exec.Command("cp", "-r", helloworldFixturesDir, helloworldDir)
36         var out bytes.Buffer
37         cmd.Stdout = &out
38         assert.Nil(t, cmd.Run())
39
40         /*clone submodules app templates into helloworld*/
41         subHelloworldAppTemplateDir := path.Join(helloworldDir, "conf.d", "app-templates")
42         cmd = exec.Command("git", "clone", "https://gerrit.automotivelinux.org/gerrit/p/apps/app-templates.git", subHelloworldAppTemplateDir)
43         assert.Nil(t, cmd.Run())
44         return helloworldDir
45 }
46
47 /*flush channel with timeout*/
48 func flushChannelExec(channel chan xsapiv1.ExecOutMsg, ms time.Duration) {
49         timeoutB := false
50         for !timeoutB {
51                 select {
52                 case <-channel:
53                 case <-time.After(ms * time.Millisecond):
54                         timeoutB = true
55                 }
56         }
57 }
58 func TestExec(t *testing.T) {
59         helloworldDir := InitExec(t)
60         /*channel for SDK events*/
61         chSdks := make(chan xsapiv1.SDK)
62         defer close(chSdks)
63         sdk := xsapiv1.SDKInstallArgs{
64                 ID:       "",
65                 Filename: sdkFileName,
66                 Force:    false,
67         }
68         ConnectSDKStateChange(t, sCli, chSdks)
69         sdkRes := installFakeSdk(t, sdk, chSdks)
70
71         /*check there is no project*/
72         var cfgArray []xsapiv1.FolderConfig
73         assert.Nil(t, HTTPCli.Get("/folders", &cfgArray))
74         assert.Equal(t, len(cfgArray), 0)
75
76         fPrj := xsapiv1.FolderConfig{
77                 Label:      "testproject",
78                 ClientPath: helloworldDir,
79                 Type:       xsapiv1.TypePathMap,
80                 ClientData: "clientdatatest",
81                 DataPathMap: xsapiv1.PathMapConfig{
82                         ServerPath: helloworldDir,
83                 },
84         }
85         /*create project*/
86         var cfg xsapiv1.FolderConfig
87         assert.Nil(t, HTTPCli.Post("/folders", fPrj, &cfg))
88         assert.NotNil(t, cfg)
89
90         /*channel for ExecOutMsg*/
91         chExec := make(chan xsapiv1.ExecOutMsg)
92         defer close(chExec)
93         /*connect to ExecOutEvent*/
94         sCli.Conn.On(xsapiv1.ExecOutEvent, func(ev xsapiv1.ExecOutMsg) {
95                 chExec <- ev
96         })
97
98         /*cmake helloworld project with fake sdk*/
99         sdkSourceFile := path.Join(sdkRes.Path, "environment-setup-corei7-64-native-linux")
100         cmd := "source " + sdkSourceFile
101         cmd = cmd + " && "
102         cmd = cmd + "unset SDKTARGETSYSROOT"
103         cmd = cmd + " && "
104         cmd = cmd + "cd " + fPrj.ClientPath
105         cmd = cmd + " && "
106         cmd = cmd + "mkdir -p build"
107         cmd = cmd + " && "
108         cmd = cmd + "cd build"
109         cmd = cmd + " && "
110         cmd = cmd + "cmake .."
111
112         /*post exec cmd cmake*/
113         exec := xsapiv1.ExecArgs{
114                 ID:  cfg.ID,
115                 Cmd: cmd,
116         }
117         var execRes xsapiv1.ExecArgs
118         Debugf(t, "exec cmake cmd(%v)", cmd)
119         assert.Nil(t, HTTPCli.Post("/exec", exec, &execRes))
120         flushChannelExec(chExec, 1000) //waiting for execOutMsg
121
122         /*make helloworld project with fake sdk*/
123         cmd = "source " + sdkSourceFile
124         cmd = cmd + " && "
125         cmd = cmd + "unset SDKTARGETSYSROOT"
126         cmd = cmd + " && "
127         cmd = cmd + "cd " + fPrj.ClientPath
128         cmd = cmd + "&&"
129         cmd = cmd + "cd build"
130         cmd = cmd + "&&"
131         cmd = cmd + "make"
132         exec.Cmd = cmd
133         /*post exec cmd make*/
134         Debugf(t, "exec make cmd(%v)", cmd)
135         assert.Nil(t, HTTPCli.Post("/exec", exec, &execRes))
136         flushChannelExec(chExec, 1000) //waiting for execOutMsg
137
138         /*check if helloworld.so exists*/
139         Debug(t, "check that helloworld.so exists")
140         _, err := os.Stat(path.Join(fPrj.ClientPath, "build/helloworld/helloworld.so"))
141         assert.Nil(t, err)
142
143         /*deinit*/
144         assert.Nil(t, HTTPCli.Delete("/folders/"+cfg.ID, &cfg))
145         RemoveSdk(t, sdkRes, chSdks)
146         DisconnectSDKStateChange(t, sCli)
147 }