source sync 20190409
[platform/core/system/edge-orchestration.git] / src / servicemgr / servicemgr_test.go
1 package servicemgr_test
2
3 import (
4         "common"
5         "context"
6         "fmt"
7         "net/http"
8         "securemgr"
9         "strings"
10         "testing"
11         "time"
12
13         restapi "restapi/v1"
14         servicemgr "servicemgr"
15 )
16
17 var (
18         targetRemoteDeviceAddr = "127.0.0.1"
19         targetLocalAddr, _     = common.GetOutboundIP()
20         serviceID              uint64
21 )
22
23 var keyFilePath string = "./../securemgr/test/key.txt"
24
25 func init() {
26         servicemgr.Init()
27         securemgr.Init(keyFilePath)
28 }
29
30 func TestExecuteApp(t *testing.T) {
31         notiChan := make(chan string)
32
33         _, err := servicemgr.ExecuteApp(targetLocalAddr, serviceName, nil, notiChan)
34         retError(t, err)
35
36         time.Sleep(time.Millisecond * 10)
37 }
38
39 func TestExecuteAppWithArgs(t *testing.T) {
40         notiChan := make(chan string)
41
42         args := []string{"-ail"}
43         _, err := servicemgr.ExecuteApp(targetLocalAddr, serviceName, args, notiChan)
44         retError(t, err)
45
46         time.Sleep(time.Millisecond * 10)
47 }
48
49 func TestHandleNoti(t *testing.T) {
50         notiChan := make(chan string, 1)
51
52         id, err := servicemgr.ExecuteApp(targetLocalAddr, serviceName, nil, notiChan)
53         retError(t, err)
54
55         statusNotificationRequest := make(map[string]interface{})
56         statusNotificationRequest[servicemgr.ConstKeyServiceID] = id
57         statusNotificationRequest[servicemgr.ConstKeyStatus] = servicemgr.ConstServiceStatusFinished
58
59         servicemgr.HandleNoti(statusNotificationRequest)
60
61         select {
62         case str := <-notiChan:
63                 t.Log(str)
64                 if strings.Compare(servicemgr.ConstServiceStatusFinished, str) != 0 {
65                         t.Error()
66                 }
67         }
68 }
69
70 func TestSystemtestExecuteRemoteApp(t *testing.T) {
71         //init
72         router := restapi.NewRouter()
73         server := &http.Server{Addr: fmt.Sprintf(":%d", 56001), Handler: router}
74         go server.ListenAndServe()
75         time.Sleep(time.Duration(1) * time.Second)
76
77         //setting
78         _, err := servicemgr.ExecuteApp(targetRemoteDeviceAddr, serviceName, nil, nil)
79         retError(t, err)
80
81         //user scenario
82         time.Sleep(time.Duration(3) * time.Second)
83
84         //release
85         if err := server.Shutdown(context.TODO()); err != nil {
86                 panic(err)
87         }
88
89         time.Sleep(time.Millisecond * 10)
90 }
91
92 func TestSystemtestExecuteRemoteAppFailed(t *testing.T) {
93         //init
94         router := restapi.NewRouter()
95         server := &http.Server{Addr: fmt.Sprintf(":%d", 56001), Handler: router}
96         go server.ListenAndServe()
97         time.Sleep(time.Duration(1) * time.Second)
98
99         //setting
100         _, err := servicemgr.ExecuteApp(targetRemoteDeviceAddr, "main", nil, nil)
101         time.Sleep(time.Duration(3) * time.Second)
102
103         //user scenario
104         if err.Error() != "Failed" {
105                 t.Error(err.Error())
106         }
107         time.Sleep(time.Duration(1) * time.Second)
108
109         //release
110         if err := server.Shutdown(context.TODO()); err != nil {
111                 panic(err)
112         }
113         time.Sleep(time.Millisecond * 10)
114 }