test exec: waiting for events and timeout while building
[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         "log"
21         "os"
22         "path"
23         "testing"
24         "time"
25
26         "gerrit.automotivelinux.org/gerrit/src/xds/xds-server/lib/xsapiv1"
27         git "github.com/libgit2/git2go"
28         "github.com/stretchr/testify/assert"
29 )
30
31 /*flush channel with timeout*/
32 func flushChannelExec(channel chan xsapiv1.ExecOutMsg, ms time.Duration) {
33         timeoutB := false
34         for !timeoutB {
35                 select {
36                 case <-channel:
37                 case <-time.After(ms * time.Millisecond):
38                         timeoutB = true
39                 }
40         }
41 }
42 func TestExec(t *testing.T) {
43         cloneRepo := "https://github.com/iotbzh/helloworld-service.git"
44         cloneDir := path.Join(os.Getenv(envRootCfgDir), "testExec")
45         t.Logf("Cloning repo %v in %v\n...\n", cloneRepo, cloneDir)
46         var cloneOptions git.CloneOptions
47         repository, err := git.Clone(cloneRepo, cloneDir, &cloneOptions)
48         if err != nil {
49                 t.Fatal(err)
50         }
51
52         repository.Submodules.Foreach(func(sub *git.Submodule, name string) int {
53                 sub.Init(true)
54                 err := sub.Update(true, &git.SubmoduleUpdateOptions{
55                         &git.CheckoutOpts{
56                                 Strategy: git.CheckoutForce | git.CheckoutUpdateSubmodules,
57                         },
58                         &git.FetchOptions{},
59                 })
60                 if err != nil {
61                         log.Fatal(err)
62                 }
63                 return 0
64
65         })
66
67         t.Logf("repo cloned\n")
68
69         var cfgArray []xsapiv1.FolderConfig
70         assert.Nil(t, HTTPCli.Get("/folders", &cfgArray))
71         assert.Equal(t, len(cfgArray), 0)
72
73         fPrj := xsapiv1.FolderConfig{
74                 Label:      "testproject",
75                 ClientPath: cloneDir,
76                 Type:       xsapiv1.TypePathMap,
77                 ClientData: "clientdatatest",
78                 DataPathMap: xsapiv1.PathMapConfig{
79                         ServerPath: cloneDir,
80                 },
81         }
82         var cfg xsapiv1.FolderConfig
83         assert.Nil(t, HTTPCli.Post("/folders", fPrj, &cfg))
84         assert.NotNil(t, cfg)
85
86         chExec := make(chan xsapiv1.ExecOutMsg)
87         defer close(chExec)
88         sCli.Conn.On(xsapiv1.ExecOutEvent, func(ev xsapiv1.ExecOutMsg) {
89                 chExec <- ev
90         })
91
92         cmd := "export PKG_CONFIG_PATH=/usr/local/lib64/pkgconfig/"
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 = "export PKG_CONFIG_PATH=/usr/local/lib64/pkgconfig/"
111         cmd = cmd + "&&"
112         cmd = cmd + "cd " + fPrj.ClientPath
113         cmd = cmd + "&&"
114         cmd = cmd + "cd build"
115         cmd = cmd + "&&"
116         cmd = cmd + "make"
117         exec.Cmd = cmd
118         assert.Nil(t, HTTPCli.Post("/exec", exec, &execRes))
119         flushChannelExec(chExec, 1000)
120
121         /*check afb-helloworld.so exists*/
122         _, err = os.Stat(path.Join(fPrj.ClientPath, "build/helloworld-afb/afb-helloworld.so"))
123         assert.Nil(t, err)
124
125         assert.Nil(t, HTTPCli.Delete("/folders/"+cfg.ID, &cfg))
126 }