tests: better handle websocket client
[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 func TestExec(t *testing.T) {
32         cloneRepo := "https://github.com/iotbzh/helloworld-service.git"
33         cloneDir := path.Join(os.Getenv(envRootCfgDir), "testExec")
34         t.Logf("Cloning repo %v in %v\n...\n", cloneRepo, cloneDir)
35         var cloneOptions git.CloneOptions
36         repository, err := git.Clone(cloneRepo, cloneDir, &cloneOptions)
37         if err != nil {
38                 t.Fatal(err)
39         }
40
41         repository.Submodules.Foreach(func(sub *git.Submodule, name string) int {
42                 sub.Init(true)
43                 err := sub.Update(true, &git.SubmoduleUpdateOptions{
44                         &git.CheckoutOpts{
45                                 Strategy: git.CheckoutForce | git.CheckoutUpdateSubmodules,
46                         },
47                         &git.FetchOptions{},
48                 })
49                 if err != nil {
50                         log.Fatal(err)
51                 }
52                 return 0
53
54         })
55
56         t.Logf("repo cloned\n")
57
58         var cfgArray []xsapiv1.FolderConfig
59         assert.Nil(t, HTTPCli.Get("/folders", &cfgArray))
60         assert.Equal(t, len(cfgArray), 0)
61
62         fPrj := xsapiv1.FolderConfig{
63                 Label:      "testproject",
64                 ClientPath: cloneDir,
65                 Type:       xsapiv1.TypePathMap,
66                 ClientData: "clientdatatest",
67                 DataPathMap: xsapiv1.PathMapConfig{
68                         ServerPath: cloneDir,
69                 },
70         }
71         var cfg xsapiv1.FolderConfig
72         assert.Nil(t, HTTPCli.Post("/folders", fPrj, &cfg))
73         assert.NotNil(t, cfg)
74
75         cmd := "export PKG_CONFIG_PATH=/usr/local/lib64/pkgconfig/"
76         cmd = cmd + " && "
77         cmd = cmd + "cd " + fPrj.ClientPath
78         cmd = cmd + " && "
79         cmd = cmd + "mkdir -p build"
80         cmd = cmd + " && "
81         cmd = cmd + "cd build"
82         cmd = cmd + " && "
83         cmd = cmd + "cmake .."
84
85         exec := xsapiv1.ExecArgs{
86                 ID:  cfg.ID,
87                 Cmd: cmd,
88         }
89         var execRes xsapiv1.ExecArgs
90         assert.Nil(t, HTTPCli.Post("/exec", exec, &execRes))
91         time.Sleep(3 * time.Second) //maybe waiting for an event would be better
92
93         cmd = "export PKG_CONFIG_PATH=/usr/local/lib64/pkgconfig/"
94         cmd = cmd + "&&"
95         cmd = cmd + "cd " + fPrj.ClientPath
96         cmd = cmd + "&&"
97         cmd = cmd + "cd build"
98         cmd = cmd + "&&"
99         cmd = cmd + "make"
100         exec.Cmd = cmd
101         assert.Nil(t, HTTPCli.Post("/exec", exec, &execRes))
102         time.Sleep(3 * time.Second) //maybe waiting for an event would be better
103
104         /*check afb-helloworld.so exists*/
105         _, err = os.Stat(path.Join(fPrj.ClientPath, "build/helloworld-afb/afb-helloworld.so"))
106         assert.Nil(t, err)
107
108         assert.Nil(t, HTTPCli.Delete("/folders/"+cfg.ID, &cfg))
109 }