8fb6519ff7d9af0eada6c88f1e514931fdbf6d47
[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         _, err = NewIoSocketClient(prefixURL, HTTPCli.GetClientID())
75         if err != nil {
76                 t.Fatal(err)
77         }
78
79         cmd := "export PKG_CONFIG_PATH=/usr/local/lib64/pkgconfig/"
80         cmd = cmd + " && "
81         cmd = cmd + "cd " + fPrj.ClientPath
82         cmd = cmd + " && "
83         cmd = cmd + "mkdir -p build"
84         cmd = cmd + " && "
85         cmd = cmd + "cd build"
86         cmd = cmd + " && "
87         cmd = cmd + "cmake .."
88
89         exec := xsapiv1.ExecArgs{
90                 ID:  cfg.ID,
91                 Cmd: cmd,
92         }
93         var execRes xsapiv1.ExecArgs
94         assert.Nil(t, HTTPCli.Post("/exec", exec, &execRes))
95         time.Sleep(3 * time.Second) //maybe waiting for an event would be better
96
97         cmd = "export PKG_CONFIG_PATH=/usr/local/lib64/pkgconfig/"
98         cmd = cmd + "&&"
99         cmd = cmd + "cd " + fPrj.ClientPath
100         cmd = cmd + "&&"
101         cmd = cmd + "cd build"
102         cmd = cmd + "&&"
103         cmd = cmd + "make"
104         exec.Cmd = cmd
105         assert.Nil(t, HTTPCli.Post("/exec", exec, &execRes))
106         time.Sleep(3 * time.Second) //maybe waiting for an event would be better
107
108         /*check afb-helloworld.so exists*/
109         _, err = os.Stat(path.Join(fPrj.ClientPath, "build/helloworld-afb/afb-helloworld.so"))
110         assert.Nil(t, err)
111
112         assert.Nil(t, HTTPCli.Delete("/folders/"+cfg.ID, &cfg))
113 }