+package servicemgr
+
/*******************************************************************************
* Copyright 2019 Samsung Electronics All Rights Reserved.
*
* limitations under the License.
*
*******************************************************************************/
-package servicemgr
import (
- "encoding/json"
"log"
- "net/http"
"os"
"os/exec"
"restapi/httpclient"
- "strconv"
"strings"
)
}
func (p Service) notifyServiceStatus(status string) (err error) {
- statusNotificationRequest := make(map[string]interface{})
- statusNotificationRequest["ServiceID"] = p.serviceID
- statusNotificationRequest["Status"] = status
-
- reqbytes, _ := json.Marshal(statusNotificationRequest)
+ statusNotificationInfo := make(map[string]interface{})
+ statusNotificationInfo[ConstKeyServiceID] = p.serviceID
+ statusNotificationInfo[ConstKeyStatus] = status
if strings.Compare(p.notificationTargetURL, httpclient.ConstLocalTarget) == 0 {
- HandleNoti(statusNotificationRequest)
+ HandleNoti(statusNotificationInfo)
} else {
- targetURL := httpclient.ConstPrefixHTTP + p.notificationTargetURL + ConstServiceStatusNotiURI + strconv.FormatUint(p.serviceID, 10)
- _, statusCode, err := httpclient.DoPost(targetURL, reqbytes)
+ err = httpclient.DoNotifyAppStatusRemoteDevice(statusNotificationInfo, p.serviceID, p.notificationTargetURL)
if err != nil {
log.Println(logPrefix, err.Error())
}
- if statusCode != http.StatusOK {
- log.Println(logPrefix, statusCode)
- }
}
return
}
+package servicemgr
+
/*******************************************************************************
* Copyright 2019 Samsung Electronics All Rights Reserved.
*
* limitations under the License.
*
*******************************************************************************/
-package servicemgr
import (
"common"
- "encoding/json"
- "errors"
"log"
- "net/http"
"os/exec"
- "strconv"
"strings"
"restapi/httpclient"
appInfo[ConstKeyNotiTargetURL] = httpclient.ConstLocalTarget
err = executeLocalEnv(appInfo)
} else {
- appInfo[ConstKeyNotiTargetURL] = httpclient.ConstPrefixHTTP + target + ":" + strconv.Itoa(httpclient.ConstWellknownPort)
- err = executeRemoteEnv(appInfo, target)
+ appInfo[ConstKeyNotiTargetURL] = target
+ err = httpclient.DoExecuteRemoteDevice(appInfo, target)
}
return
return
}
-
-func executeRemoteEnv(appInfo map[string]interface{}, target string) (err error) {
-
- reqBytes, _ := json.Marshal(appInfo)
- executeTarget := httpclient.ConstPrefixHTTP + target + ":" + strconv.Itoa(httpclient.ConstWellknownPort) + ConstServiceExecuteURI
- respBytes, statusCode, err := httpclient.DoPost(executeTarget, reqBytes)
-
- var responseMsg map[string]interface{}
- err = json.Unmarshal(respBytes, &responseMsg)
- if err != nil {
- return
- }
- if statusCode != http.StatusOK {
- log.Println(statusCode)
- }
-
- log.Println("[JSON] : ", responseMsg)
-
- str := responseMsg[ConstKeyStatus].(string)
- if str == ConstServiceStatusFailed {
- err = errors.New(ConstServiceStatusFailed)
- }
-
- return
-}
+package servicemgr_test
+
/*******************************************************************************
* Copyright 2019 Samsung Electronics All Rights Reserved.
*
* limitations under the License.
*
*******************************************************************************/
-package servicemgr_test
import (
"common"
- "context"
- "fmt"
- "net/http"
- "securemgr"
+ "log"
"strings"
"testing"
"time"
- restapi "restapi/v1"
servicemgr "servicemgr"
)
+const (
+ serviceName = "ls"
+ serviceName2 = "main2"
+)
+
var (
- targetRemoteDeviceAddr = "127.0.0.1"
- targetLocalAddr, _ = common.GetOutboundIP()
- serviceID uint64
- notExistFileName = "NotExistFile"
- keyFilePath = "./../securemgr/test/key.txt"
+ targetLocalAddr, _ = common.GetOutboundIP()
+ serviceID uint64
+ // notExistFileName = "NotExistFile"
)
func init() {
servicemgr.Init()
- securemgr.Init(keyFilePath)
}
func TestExecuteApp(t *testing.T) {
notiChan := make(chan string)
_, err := servicemgr.ExecuteApp(targetLocalAddr, serviceName, nil, notiChan)
- retError(t, err)
+ checkError(t, err)
time.Sleep(time.Millisecond * 10)
}
args := []string{"-ail"}
_, err := servicemgr.ExecuteApp(targetLocalAddr, serviceName, args, notiChan)
- retError(t, err)
+ checkError(t, err)
+
+ select {
+ case retStr := <-notiChan:
+ log.Println("[retStr]", retStr)
+ if strings.Compare(retStr, servicemgr.ConstServiceStatusFinished) != 0 {
+ t.Fail()
+ }
+ }
+
+ time.Sleep(time.Millisecond * 10)
+}
+
+func TestExecuteAppWithInvalidArgs(t *testing.T) {
+ notiChan := make(chan string)
+
+ args := []string{"invalidArgs"}
+ _, err := servicemgr.ExecuteApp(targetLocalAddr, serviceName, args, notiChan)
+ checkError(t, err)
+
+ select {
+ case retStr := <-notiChan:
+ log.Println("[retStr]", retStr)
+ if strings.Compare(retStr, servicemgr.ConstServiceStatusFailed) != 0 {
+ t.Fail()
+ }
+ }
time.Sleep(time.Millisecond * 10)
}
notiChan := make(chan string, 1)
id, err := servicemgr.ExecuteApp(targetLocalAddr, serviceName, nil, notiChan)
- retError(t, err)
+ checkError(t, err)
statusNotificationRequest := make(map[string]interface{})
statusNotificationRequest[servicemgr.ConstKeyServiceID] = id
}
}
-func TestSystemtestExecuteRemoteApp(t *testing.T) {
- //init
- router := restapi.NewRouter()
- server := &http.Server{Addr: fmt.Sprintf(":%d", 56001), Handler: router}
- go server.ListenAndServe()
- time.Sleep(time.Duration(1) * time.Second)
-
- //setting
- _, err := servicemgr.ExecuteApp(targetRemoteDeviceAddr, serviceName, nil, nil)
- retError(t, err)
-
- //user scenario
- time.Sleep(time.Duration(3) * time.Second)
-
- //release
- if err := server.Shutdown(context.TODO()); err != nil {
- panic(err)
+func assertEqualStr(t *testing.T, a, b string) {
+ t.Helper()
+ if strings.Compare(a, b) != 0 {
+ t.Errorf("%s != %s", a, b)
}
-
- time.Sleep(time.Millisecond * 10)
}
-func TestSystemtestExecuteRemoteAppFailed(t *testing.T) {
- //init
- router := restapi.NewRouter()
- server := &http.Server{Addr: fmt.Sprintf(":%d", 56001), Handler: router}
- go server.ListenAndServe()
- time.Sleep(time.Duration(1) * time.Second)
-
- //setting
- _, err := servicemgr.ExecuteApp(targetRemoteDeviceAddr, notExistFileName, nil, nil)
- time.Sleep(time.Duration(3) * time.Second)
+func checkError(t *testing.T, err error) {
+ t.Helper()
- //user scenario
- if err.Error() != "Failed" {
+ if err != nil {
t.Error(err.Error())
}
- time.Sleep(time.Duration(1) * time.Second)
-
- //release
- if err := server.Shutdown(context.TODO()); err != nil {
- panic(err)
- }
- time.Sleep(time.Millisecond * 10)
}