Apply moving internal restapi logic in servicemgr 88/203288/1
authorjw_wonny.cha <jw_wonny.cha@samsung.com>
Thu, 11 Apr 2019 08:42:56 +0000 (17:42 +0900)
committerjw_wonny.cha <jw_wonny.cha@samsung.com>
Thu, 11 Apr 2019 08:42:56 +0000 (17:42 +0900)
Change-Id: I660a7dcd91bd604981e23d07b3ece2e6fdac1a9c
Signed-off-by: jw_wonny.cha <jw_wonny.cha@samsung.com>
src/servicemgr/service_execution.go
src/servicemgr/servicemgr.go
src/servicemgr/servicemgr_test.go

index 1989342..99ac8a0 100755 (executable)
@@ -1,3 +1,5 @@
+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"
 )
 
@@ -98,23 +96,17 @@ func (p Service) waitService(executeCh <-chan error) (status string) {
 }
 
 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
 }
index 95d1877..e1d9868 100755 (executable)
@@ -1,3 +1,5 @@
+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"
@@ -55,8 +52,8 @@ func ExecuteApp(target string, name string, args []string, notiChan chan string)
                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
@@ -138,28 +135,3 @@ func executeLocalEnv(appInfo map[string]interface{}) (err error) {
 
        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
-}
index daf49ae..bd652ee 100755 (executable)
@@ -1,3 +1,5 @@
+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)
 }
@@ -57,7 +56,33 @@ func TestExecuteAppWithArgs(t *testing.T) {
 
        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)
 }
@@ -66,7 +91,7 @@ func TestHandleNoti(t *testing.T) {
        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
@@ -83,48 +108,17 @@ func TestHandleNoti(t *testing.T) {
        }
 }
 
-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)
 }