f2d48ce79d698ac6cb6ec12e407d8212a5b224e0
[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
57         var cfgArray []xsapiv1.FolderConfig
58         assert.Nil(t, HTTPCli.Get("/folders", &cfgArray))
59         assert.Equal(t, len(cfgArray), 0)
60
61         fPrj := xsapiv1.FolderConfig{
62                 Label:      "testproject",
63                 ClientPath: helloworldDir,
64                 Type:       xsapiv1.TypePathMap,
65                 ClientData: "clientdatatest",
66                 DataPathMap: xsapiv1.PathMapConfig{
67                         ServerPath: helloworldDir,
68                 },
69         }
70         var cfg xsapiv1.FolderConfig
71         assert.Nil(t, HTTPCli.Post("/folders", fPrj, &cfg))
72         assert.NotNil(t, cfg)
73
74         chExec := make(chan xsapiv1.ExecOutMsg)
75         defer close(chExec)
76         sCli.Conn.On(xsapiv1.ExecOutEvent, func(ev xsapiv1.ExecOutMsg) {
77                 chExec <- ev
78         })
79
80         cmd := "cd " + fPrj.ClientPath
81         cmd = cmd + " && "
82         cmd = cmd + "mkdir -p build"
83         cmd = cmd + " && "
84         cmd = cmd + "cd build"
85         cmd = cmd + " && "
86         cmd = cmd + "cmake .."
87
88         exec := xsapiv1.ExecArgs{
89                 ID:  cfg.ID,
90                 Cmd: cmd,
91         }
92         var execRes xsapiv1.ExecArgs
93         assert.Nil(t, HTTPCli.Post("/exec", exec, &execRes))
94         flushChannelExec(chExec, 1000)
95
96         cmd = "cd " + fPrj.ClientPath
97         cmd = cmd + "&&"
98         cmd = cmd + "cd build"
99         cmd = cmd + "&&"
100         cmd = cmd + "make"
101         exec.Cmd = cmd
102         assert.Nil(t, HTTPCli.Post("/exec", exec, &execRes))
103         flushChannelExec(chExec, 1000)
104
105         /*check afb-helloworld.so exists*/
106         _, err := os.Stat(path.Join(fPrj.ClientPath, "build/helloworld/helloworld.so"))
107         assert.Nil(t, err)
108
109         assert.Nil(t, HTTPCli.Delete("/folders/"+cfg.ID, &cfg))
110 }