package servicemgr_test import ( "common" "context" "fmt" "net/http" "securemgr" "strings" "testing" "time" restapi "restapi/v1" servicemgr "servicemgr" ) var ( targetRemoteDeviceAddr = "127.0.0.1" targetLocalAddr, _ = common.GetOutboundIP() serviceID uint64 ) var keyFilePath string = "./../securemgr/test/key.txt" 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) time.Sleep(time.Millisecond * 10) } func TestExecuteAppWithArgs(t *testing.T) { notiChan := make(chan string) args := []string{"-ail"} _, err := servicemgr.ExecuteApp(targetLocalAddr, serviceName, args, notiChan) retError(t, err) time.Sleep(time.Millisecond * 10) } func TestHandleNoti(t *testing.T) { notiChan := make(chan string, 1) id, err := servicemgr.ExecuteApp(targetLocalAddr, serviceName, nil, notiChan) retError(t, err) statusNotificationRequest := make(map[string]interface{}) statusNotificationRequest[servicemgr.ConstKeyServiceID] = id statusNotificationRequest[servicemgr.ConstKeyStatus] = servicemgr.ConstServiceStatusFinished servicemgr.HandleNoti(statusNotificationRequest) select { case str := <-notiChan: t.Log(str) if strings.Compare(servicemgr.ConstServiceStatusFinished, str) != 0 { t.Error() } } } 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) } 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, "main", nil, nil) time.Sleep(time.Duration(3) * time.Second) //user scenario if err.Error() != "Failed" { 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) }