Use go module as dependency tool instead of glide
[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.git/lib/xsapiv1"
27         "github.com/stretchr/testify/require"
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         require.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", "-b", "flounder",
42                 "https://gerrit.automotivelinux.org/gerrit/p/apps/app-templates.git",
43                 subHelloworldAppTemplateDir)
44         require.Nil(t, cmd.Run())
45         return helloworldDir
46 }
47
48 func TestExec(t *testing.T) {
49         helloworldDir := InitExec(t)
50         /*channel for SDK events*/
51         chSdks := make(chan xsapiv1.SDK)
52         defer close(chSdks)
53         sdk := xsapiv1.SDKInstallArgs{
54                 ID:       "",
55                 Filename: sdkFileName,
56                 Force:    false,
57         }
58         ConnectSDKStateChange(t, sCli, chSdks)
59         sdkRes := installFakeSdk(t, sdk, chSdks)
60
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)
65
66         fPrj := xsapiv1.FolderConfig{
67                 Label:      "testproject",
68                 ClientPath: helloworldDir,
69                 Type:       xsapiv1.TypePathMap,
70                 ClientData: "clientdatatest",
71                 DataPathMap: xsapiv1.PathMapConfig{
72                         ServerPath: helloworldDir,
73                 },
74         }
75         /*create project*/
76         var cfg xsapiv1.FolderConfig
77         require.Nil(t, HTTPCli.Post("/folders", fPrj, &cfg))
78         require.NotNil(t, cfg)
79
80         /*channel for ExecExitMsg*/
81         chExec := make(chan xsapiv1.ExecExitMsg)
82         defer close(chExec)
83         sCli.Conn.On(xsapiv1.ExecExitEvent, func(ev xsapiv1.ExecExitMsg) {
84                 chExec <- ev
85         })
86
87         /*Collect commands output in */
88         cmdOut := ""
89         sCli.Conn.On(xsapiv1.ExecOutEvent, func(ev xsapiv1.ExecOutMsg) {
90                 cmdOut += ev.Stdout + ev.Stderr
91         })
92         /*error when exec with fakeid*/
93         var execRes xsapiv1.ExecArgs
94         cmd := "pwd && echo \"SDKTARGETSYSROOT=<$SDKTARGETSYSROOT>\""
95         exec := xsapiv1.ExecArgs{
96                 ID:    cfg.ID,
97                 Cmd:   cmd,
98                 SdkID: "11111-invalid",
99         }
100         Debugf(t, "exec cmake cmd(%v)", cmd)
101         cmdOut = ""
102         require.NotNil(t, HTTPCli.Post("/exec", exec, &execRes))
103
104         /*basic check: verify that environment is set correctly (use the right sdk)*/
105         cmd = "pwd && echo \"SDKTARGETSYSROOT=<$SDKTARGETSYSROOT>\""
106         exec = xsapiv1.ExecArgs{
107                 ID:    cfg.ID,
108                 Cmd:   cmd,
109                 SdkID: sdkRes.ID,
110         }
111         Debugf(t, "exec cmake cmd(%v)", cmd)
112         cmdOut = ""
113         require.Nil(t, HTTPCli.Post("/exec", exec, &execRes))
114         exitMsg := <-chExec
115         require.Equal(t, exitMsg.Code, 0)
116
117         /*cmake helloworld project with fake sdk*/
118         cmd = "cd " + fPrj.ClientPath
119         cmd = cmd + " && "
120         cmd = cmd + "mkdir -p build"
121         cmd = cmd + " && "
122         cmd = cmd + "cd build"
123         cmd = cmd + " && "
124         cmd = cmd + "cmake .."
125
126         /*post exec cmd cmake*/
127         exec = xsapiv1.ExecArgs{
128                 ID:  cfg.ID,
129                 Cmd: cmd,
130         }
131         Debugf(t, "exec cmake cmd(%v)", cmd)
132         require.Nil(t, HTTPCli.Post("/exec", exec, &execRes))
133         exitMsg = <-chExec
134         require.Equal(t, exitMsg.Code, 0)
135
136         /*make helloworld project with fake sdk*/
137         cmd = "cd " + fPrj.ClientPath
138         cmd = cmd + "&&"
139         cmd = cmd + "cd build"
140         cmd = cmd + "&&"
141         cmd = cmd + "make"
142         exec.Cmd = cmd
143         /*post exec cmd make*/
144         Debugf(t, "exec make cmd(%v)", cmd)
145         require.Nil(t, HTTPCli.Post("/exec", exec, &execRes))
146         exitMsg = <-chExec
147         require.Equal(t, exitMsg.Code, 0)
148
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"))
152         require.Nil(t, err)
153
154         /*deinit*/
155         require.Nil(t, HTTPCli.Delete("/folders/"+cfg.ID, &cfg))
156         RemoveSdk(t, sdkRes, chSdks)
157         DisconnectSDKStateChange(t, sCli)
158 }