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