2 * Copyright (C) 2017-2018 "IoT.bzh"
3 * Author Clément Bénier <clement.benier@iot.bzh>
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
26 "gerrit.automotivelinux.org/gerrit/src/xds/xds-server/lib/xsapiv1"
27 git "github.com/libgit2/git2go"
28 "github.com/stretchr/testify/assert"
31 /*flush channel with timeout*/
32 func flushChannelExec(channel chan xsapiv1.ExecOutMsg, ms time.Duration) {
37 case <-time.After(ms * time.Millisecond):
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)
52 repository.Submodules.Foreach(func(sub *git.Submodule, name string) int {
54 err := sub.Update(true, &git.SubmoduleUpdateOptions{
56 Strategy: git.CheckoutForce | git.CheckoutUpdateSubmodules,
67 t.Logf("repo cloned\n")
69 var cfgArray []xsapiv1.FolderConfig
70 assert.Nil(t, HTTPCli.Get("/folders", &cfgArray))
71 assert.Equal(t, len(cfgArray), 0)
73 fPrj := xsapiv1.FolderConfig{
76 Type: xsapiv1.TypePathMap,
77 ClientData: "clientdatatest",
78 DataPathMap: xsapiv1.PathMapConfig{
82 var cfg xsapiv1.FolderConfig
83 assert.Nil(t, HTTPCli.Post("/folders", fPrj, &cfg))
86 chExec := make(chan xsapiv1.ExecOutMsg)
88 sCli.Conn.On(xsapiv1.ExecOutEvent, func(ev xsapiv1.ExecOutMsg) {
92 cmd := "export PKG_CONFIG_PATH=/usr/local/lib64/pkgconfig/"
94 cmd = cmd + "cd " + fPrj.ClientPath
96 cmd = cmd + "mkdir -p build"
98 cmd = cmd + "cd build"
100 cmd = cmd + "cmake .."
102 exec := xsapiv1.ExecArgs{
106 var execRes xsapiv1.ExecArgs
107 assert.Nil(t, HTTPCli.Post("/exec", exec, &execRes))
108 flushChannelExec(chExec, 1000)
110 cmd = "export PKG_CONFIG_PATH=/usr/local/lib64/pkgconfig/"
112 cmd = cmd + "cd " + fPrj.ClientPath
114 cmd = cmd + "cd build"
118 assert.Nil(t, HTTPCli.Post("/exec", exec, &execRes))
119 flushChannelExec(chExec, 1000)
121 /*check afb-helloworld.so exists*/
122 _, err = os.Stat(path.Join(fPrj.ClientPath, "build/helloworld-afb/afb-helloworld.so"))
125 assert.Nil(t, HTTPCli.Delete("/folders/"+cfg.ID, &cfg))