Add test of servicemgr
authorjw_wonny.cha <jw_wonny.cha@samsung.com>
Mon, 25 Mar 2019 07:56:54 +0000 (16:56 +0900)
committerjw_wonny.cha <jw_wonny.cha@samsung.com>
Mon, 25 Mar 2019 07:56:54 +0000 (16:56 +0900)
src/servicemgr/service_execution_test.go [new file with mode: 0644]
src/servicemgr/test/concurrent_map_test.go [deleted file]
src/servicemgr/test/mq_test.go [deleted file]
src/servicemgr/test/service_agent_test.go [new file with mode: 0644]
src/servicemgr/test/service_discovery_test.go
src/servicemgr/test/service_id_test.go
src/servicemgr/test/types_concurrent_map_test.go [new file with mode: 0644]

diff --git a/src/servicemgr/service_execution_test.go b/src/servicemgr/service_execution_test.go
new file mode 100644 (file)
index 0000000..d5913ab
--- /dev/null
@@ -0,0 +1,119 @@
+package servicemgr
+
+import (
+       "encoding/json"
+       "net/http"
+       "net/http/httptest"
+       "os/exec"
+       "strings"
+       "testing"
+)
+
+func AssertEqualStr(t *testing.T, a, b string) {
+       t.Helper()
+       if strings.Compare(a, b) != 0 {
+               t.Errorf("%s != %s", a, b)
+       }
+}
+
+func TestSetService(t *testing.T) {
+       InitServiceMap()
+
+       var cmd *exec.Cmd
+       idx, _, _ := CreateServiceMap(cmd, "ls", "TestApps")
+
+       var args []string
+       args = append(args, "-ail")
+       service := Service{idx, "ls", args, ""}
+
+       _, pid, _ := service.setService()
+
+       cmd, _ = GetCmd(idx)
+       if cmd.Process.Pid != pid {
+               t.Error()
+       }
+
+}
+
+func TestRegisterRemoveService(t *testing.T) {
+       InitServiceMap()
+
+       var cmd *exec.Cmd
+       idx, _, _ := CreateServiceMap(cmd, "ls", "TestApps")
+
+       var args []string
+       args = append(args, "-ail")
+       service := Service{idx, "ls", args, ""}
+
+       service.registerService()
+       serviceInfo, _ := FindServiceByID(idx)
+
+       t.Log(idx)
+       if serviceInfo.ServiceID != idx {
+               t.Error()
+       }
+
+       AssertEqualStr(t, serviceInfo.ServiceName, "ls")
+       AssertEqualStr(t, serviceInfo.Status, ConstServiceStatusStarted)
+
+       service.removeService()
+
+       // @TODO FindServiceByID set timeout 30s
+       // _, err := FindServiceByID(idx)
+       // if err == nil {
+       //      t.Error()
+       // }
+}
+
+func TestWaitService(t *testing.T) {
+       InitServiceMap()
+
+       var cmd *exec.Cmd
+       idx, _, _ := CreateServiceMap(cmd, "ls", "TestApps")
+
+       var args []string
+       args = append(args, "-ail")
+       service := Service{idx, "ls", args, "`"}
+
+       service.registerService()
+
+       executeCh := make(chan error)
+
+       go func() {
+               executeCh <- nil
+       }()
+
+       service.waitService(executeCh)
+
+       // @TODO make error
+}
+
+func TestNotifyServiceStatus(t *testing.T) {
+       server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
+               decoder := json.NewDecoder(req.Body)
+               var statusNotificationRequest map[string]interface{}
+
+               decoder.Decode(&statusNotificationRequest)
+               t.Log(statusNotificationRequest["Status"].(string))
+               AssertEqualStr(t, statusNotificationRequest["Status"].(string), ConstServiceStatusFinished)
+
+               rw.Write([]byte(`OK`))
+       }))
+       defer server.Close()
+
+       InitServiceMap()
+
+       var cmd *exec.Cmd
+       idx, _, _ := CreateServiceMap(cmd, "ls", "TestApps")
+
+       var args []string
+       args = append(args, "-ail")
+       service := Service{idx, "ls", args, server.URL}
+
+       service.registerService()
+
+       err := service.notifyServiceStatus(ConstServiceStatusFinished)
+       if err != nil {
+               t.Error()
+       }
+}
diff --git a/src/servicemgr/test/concurrent_map_test.go b/src/servicemgr/test/concurrent_map_test.go
deleted file mode 100644 (file)
index ccd9dfd..0000000
+++ /dev/null
@@ -1,75 +0,0 @@
-package test
-
-import (
-       "servicemgr"
-       "strings"
-       "testing"
-)
-
-const (
-       serviceName1 = "hello1"
-       serviceName2 = "hello2"
-)
-
-func TestMapSetGet(t *testing.T) {
-       servicemgr.InitServiceMap()
-       servicemgr.ServiceMap.Set(uint64(1), serviceName1)
-
-       str, _ := servicemgr.ServiceMap.Get(uint64(1))
-       AssertEqualStr(t, str.(string), serviceName1)
-}
-
-func TestMapRemove(t *testing.T) {
-       servicemgr.InitServiceMap()
-       servicemgr.ServiceMap.Set(uint64(1), serviceName1)
-
-       servicemgr.ServiceMap.Remove(uint64(1))
-
-       exist, _ := servicemgr.ServiceMap.Get(uint64(1))
-       if exist != nil {
-               t.Error("ConcurrentMap Remove API is failed")
-       }
-}
-
-func TestMapModify(t *testing.T) {
-       servicemgr.InitServiceMap()
-       servicemgr.ServiceMap.Set(uint64(1), serviceName1)
-       servicemgr.ServiceMap.Set(uint64(1), serviceName2)
-
-       str, _ := servicemgr.ServiceMap.Get(uint64(1))
-       AssertEqualStr(t, str.(string), serviceName2)
-}
-
-func TestMapIter(t *testing.T) {
-       servicemgr.InitServiceMap()
-       servicemgr.ServiceMap.Set(uint64(1), serviceName1)
-       servicemgr.ServiceMap.Set(uint64(2), serviceName2)
-
-       mapItem := servicemgr.ServiceMap.Iter()
-       compareStr := make([]string, 10)
-       idx := 0
-
-       for {
-               msg := <-mapItem
-
-               t.Log(msg.Key, msg.Value)
-
-               if msg.Value == nil && msg.Key == 0 {
-                       break
-               } else {
-                       compareStr[idx] = msg.Value.(string)
-               }
-       }
-
-       for _, str := range compareStr {
-               if len(str) == 0 {
-                       break
-               }
-
-               if strings.Compare(str, serviceName1) == 0 || strings.Compare(str, serviceName2) == 0 {
-                       continue
-               } else {
-                       t.Fail()
-               }
-       }
-}
diff --git a/src/servicemgr/test/mq_test.go b/src/servicemgr/test/mq_test.go
deleted file mode 100644 (file)
index 81e1716..0000000
+++ /dev/null
@@ -1,67 +0,0 @@
-package test
-
-import (
-       "encoding/json"
-       "fmt"
-       "log"
-       "servicemgr"
-       "strconv"
-       "testing"
-
-       z4 "github.com/alecthomas/gozmq"
-)
-
-func TestPushMsg(t *testing.T) {
-       msgChan := make(chan servicemgr.MsgFormat, servicemgr.IPCMsgChan)
-       ctlChan := make(chan bool, 1)
-
-       go servicemgr.PushIpcZmq(uint64(1), msgChan, ctlChan)
-       ctx, err := z4.NewContext()
-       if err != nil {
-               log.Println("Failed to create new context")
-       }
-       defer ctx.Close()
-
-       pullSock, err := ctx.NewSocket(z4.PULL)
-       if err != nil {
-               t.Logf(err.Error())
-       }
-       defer pullSock.Close()
-
-       pullSock.Bind(fmt.Sprintf("ipc://%d", uint64(1)))
-
-       for index := 0; index < 20; index++ {
-               msgChan <- servicemgr.MsgFormat{Header: servicemgr.MsgHeader{Type: "Data"}, Body: strconv.Itoa(index)}
-
-               msg, _ := pullSock.Recv(0)
-
-               parsedMsg := servicemgr.MsgFormat{}
-               json.Unmarshal(msg, &parsedMsg)
-
-               AssertEqualStr(t, parsedMsg.Body.(string), strconv.Itoa(index))
-       }
-
-       ctlChan <- true
-}
-
-func TestPullMsg(t *testing.T) {
-       msgChan := make(chan servicemgr.MsgFormat, servicemgr.IPCMsgChan)
-       ctlPushChan := make(chan bool, 1)
-
-       outputChan := make(chan servicemgr.MsgFormat, servicemgr.IPCMsgChan)
-       ctlPullChan := make(chan bool, 1)
-
-       go servicemgr.PullIpcZmq(uint64(1), outputChan, ctlPullChan)
-       go servicemgr.PushIpcZmq(uint64(1), msgChan, ctlPushChan)
-
-       for index := 0; index < 20; index++ {
-               msgChan <- servicemgr.MsgFormat{Header: servicemgr.MsgHeader{Type: "Data"}, Body: strconv.Itoa(index)}
-               select {
-               case msg := <-outputChan:
-                       AssertEqualStr(t, msg.Body.(string), strconv.Itoa(index))
-               }
-       }
-
-       ctlPullChan <- true
-       ctlPushChan <- true
-}
diff --git a/src/servicemgr/test/service_agent_test.go b/src/servicemgr/test/service_agent_test.go
new file mode 100644 (file)
index 0000000..0e86c12
--- /dev/null
@@ -0,0 +1,27 @@
+package test
+
+import (
+       "encoding/json"
+       "servicemgr"
+       "testing"
+)
+
+func TestServiceCreate(t *testing.T) {
+       servicemgr.InitServiceMap()
+
+       serviceParam := servicemgr.ServiceParam{AppName: appName, ServiceName: serviceName, Count: 1}
+
+       sibytes := servicemgr.Create(serviceParam)
+
+       ret := servicemgr.ServiceCreationResponse{}
+       json.Unmarshal(sibytes, &ret)
+
+       for _, service := range ret.ServiceList {
+               idx := service.ID
+               name, _ := servicemgr.GetAppName(idx)
+               AssertEqualStr(t, name, appName)
+
+               name, _ = servicemgr.GetServiceName(idx)
+               AssertEqualStr(t, name, serviceName)
+       }
+}
index 02100f8cbbe0d0a9088a3de493456a8836aee7ed..c6964ecde59d4e61fe7faeded837789cc0190d2d 100644 (file)
@@ -1,6 +1,8 @@
 package test
 
 import (
+       "log"
+       "net"
        "reflect"
        sm "servicemgr"
        "testing"
@@ -13,17 +15,29 @@ var data = sm.AppReturnInfo{
                        ServiceID:   1,
                        ServiceName: "hello world#1",
                        Status:      "started",
-                       DeviceIP:    "10.113.175.144",
+                       DeviceIP:    GetOutboundIP().String(),
                },
                {
                        ServiceID:   2,
                        ServiceName: "hello world#2",
                        Status:      "progressing",
-                       DeviceIP:    "10.113.175.144",
+                       DeviceIP:    GetOutboundIP().String(),
                },
        },
 }
 
+func GetOutboundIP() net.IP {
+       conn, err := net.Dial("udp", "8.8.8.8:80")
+       if err != nil {
+               log.Fatal(err)
+       }
+       defer conn.Close()
+
+       localAddr := conn.LocalAddr().(*net.UDPAddr)
+
+       return localAddr.IP
+}
+
 func TestServiceRegisterRemove(t *testing.T) {
        ret := make(chan error)
 
index 3a054137f3259a4334145959b6508412cf2e0531..58b89ed3f780ae2ad5152c9d462c8828dc4788ef 100644 (file)
@@ -23,10 +23,10 @@ func TestCreateServiceID(t *testing.T) {
        servicemgr.InitServiceMap()
 
        var cmd *exec.Cmd
-       idx, _, _ := servicemgr.CreateServiceMap(cmd, serviceName, appName)
-       t.Log(idx)
+       var idx uint64
+       idx, _, _ = servicemgr.CreateServiceMap(cmd, serviceName, appName)
 
-       if idx != 1 {
+       if idx == 0 {
                t.Fail()
        }
 }
diff --git a/src/servicemgr/test/types_concurrent_map_test.go b/src/servicemgr/test/types_concurrent_map_test.go
new file mode 100644 (file)
index 0000000..ccd9dfd
--- /dev/null
@@ -0,0 +1,75 @@
+package test
+
+import (
+       "servicemgr"
+       "strings"
+       "testing"
+)
+
+const (
+       serviceName1 = "hello1"
+       serviceName2 = "hello2"
+)
+
+func TestMapSetGet(t *testing.T) {
+       servicemgr.InitServiceMap()
+       servicemgr.ServiceMap.Set(uint64(1), serviceName1)
+
+       str, _ := servicemgr.ServiceMap.Get(uint64(1))
+       AssertEqualStr(t, str.(string), serviceName1)
+}
+
+func TestMapRemove(t *testing.T) {
+       servicemgr.InitServiceMap()
+       servicemgr.ServiceMap.Set(uint64(1), serviceName1)
+
+       servicemgr.ServiceMap.Remove(uint64(1))
+
+       exist, _ := servicemgr.ServiceMap.Get(uint64(1))
+       if exist != nil {
+               t.Error("ConcurrentMap Remove API is failed")
+       }
+}
+
+func TestMapModify(t *testing.T) {
+       servicemgr.InitServiceMap()
+       servicemgr.ServiceMap.Set(uint64(1), serviceName1)
+       servicemgr.ServiceMap.Set(uint64(1), serviceName2)
+
+       str, _ := servicemgr.ServiceMap.Get(uint64(1))
+       AssertEqualStr(t, str.(string), serviceName2)
+}
+
+func TestMapIter(t *testing.T) {
+       servicemgr.InitServiceMap()
+       servicemgr.ServiceMap.Set(uint64(1), serviceName1)
+       servicemgr.ServiceMap.Set(uint64(2), serviceName2)
+
+       mapItem := servicemgr.ServiceMap.Iter()
+       compareStr := make([]string, 10)
+       idx := 0
+
+       for {
+               msg := <-mapItem
+
+               t.Log(msg.Key, msg.Value)
+
+               if msg.Value == nil && msg.Key == 0 {
+                       break
+               } else {
+                       compareStr[idx] = msg.Value.(string)
+               }
+       }
+
+       for _, str := range compareStr {
+               if len(str) == 0 {
+                       break
+               }
+
+               if strings.Compare(str, serviceName1) == 0 || strings.Compare(str, serviceName2) == 0 {
+                       continue
+               } else {
+                       t.Fail()
+               }
+       }
+}