--- /dev/null
+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()
+ }
+}
+++ /dev/null
-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()
- }
- }
-}
+++ /dev/null
-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
-}
--- /dev/null
+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)
+ }
+}
package test
import (
+ "log"
+ "net"
"reflect"
sm "servicemgr"
"testing"
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)
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()
}
}
--- /dev/null
+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()
+ }
+ }
+}