writeJSONResponse(w, data, http.StatusOK)
}
-// APIV1DiscoverymgrDevicesDeviceIDGet function
-func APIV1DiscoverymgrDevicesDeviceIDGet(w http.ResponseWriter, r *http.Request) {
- log.Printf("[%s] APIV1DiscoverymgrDevicesDeviceIDGet", logPrefix)
-}
-
// APIV1DiscoverymgrDevicesGet function
func APIV1DiscoverymgrDevicesGet(w http.ResponseWriter, r *http.Request) {
log.Printf("[%s] APIV1DiscoverymgrDevicesGet", logPrefix)
return
}
- // var distService map[string]string
- // decoder := json.NewDecoder(r.Body)
- // err := decoder.Decode(&distService)
- // if err != nil {
- // return
- // }
target := distService["ServiceName"]
+ ret := make(map[string]interface{})
+
err = discoverymgr.SetServiceNames(target.(string))
if err != nil {
- writeJSONResponse(w, nil, http.StatusBadRequest)
+ ret["Return"] = err.Error()
+ bdbytes, _ := json.Marshal(ret)
+ w.WriteHeader(http.StatusBadRequest)
+ writeJSONResponse(w, bdbytes, http.StatusBadRequest)
} else {
- writeJSONResponse(w, nil, http.StatusOK)
+ ret["Return"] = "OK"
+ w.WriteHeader(http.StatusOK)
+ bdbytes, _ := json.Marshal(ret)
+ writeJSONResponse(w, bdbytes, http.StatusOK)
}
}
-// APIV1ServicemgrServicesDelete function
-func APIV1ServicemgrServicesDelete(w http.ResponseWriter, r *http.Request) {
- log.Printf("[%s] APIV1ServicemgrServicesDelete", logPrefix)
-}
-
// APIV1ServicemgrServicesPost function
func APIV1ServicemgrServicesPost(w http.ResponseWriter, r *http.Request) {
log.Printf("[%s] APIV1ServicemgrServicesPost", logPrefix)
}
}
-// APIV1ServicemgrEventServiceIDPost function
-func APIV1ServicemgrEventServiceIDPost(w http.ResponseWriter, r *http.Request) {
- log.Printf("[%s] APIV1ServicemgrEventServiceIDPost", logPrefix)
- writeJSONResponse(w, nil, http.StatusNotImplemented)
-}
-
// APIV1ServicemgrServicesNotificationServiceIDPost function
func APIV1ServicemgrServicesNotificationServiceIDPost(w http.ResponseWriter, r *http.Request) {
log.Printf("[%s] APIV1ServicemgrServicesNotificationServiceIDPost", logPrefix)
return
}
- // decoder := json.NewDecoder(req.Body)
- // err = decoder.Decode(&distService)
- // if err != nil {
- // return
- // }
-
_, err = exec.LookPath(distService["ServiceName"].(string))
if err != nil {
err = errors.New("It is Invalid Service")
)
var (
- testURL string
+ testURL string
+ keyFilePath = "./../../securemgr/test/key.txt"
)
const (
ConstDeviceResourceUsageNetworkGet = "/api/v1/device/resource/usage/network"
// discovery mgr URI
- ConstDiscoverymgrDevicesGet = "/api/v1/discoverymgr/devices"
+ ConstDiscoverymgrDevices = "/api/v1/discoverymgr/devices"
+ ConstDiscoverymgrDevicesTXT = "/api/v1/discoverymgr/devices/TXT"
// Service mgr URI
ConstServicemgrServices = "/api/v1/servicemgr/services"
// Scoring mgr URI
ConstScoremgrScore = "/api/v1/scoringmgr/score"
- ConstAppName = "AppName"
- ConstServiceName = "ls"
+ ConstAppName = "ls"
+ ConstNotExistAppName = "NotExistFile"
+ ConstTooLongAppName = `00000000000000000000000000000000000000000000000000000000000000000000000000000000000
+ 00000000000000000000000000000000000000000000000000000000000000000000000000000000000
+ 00000000000000000000000000000000000000000000000000000000000000000000000000000000000
+ 00000000000000000000000000000000000000000000000000000000000000000000000000000000000
+ 00000000000000000000000000000000000000000000000000000000000000000000000000000000000`
)
type Response struct {
Code int
}
+func init() {
+ devicemgr.InitDeviceMgr()
+ servicemgr.Init()
+ discoverymgr.InitDiscovery()
+ scoringmgr.Init()
+ securemgr.Init(keyFilePath)
+
+ router := NewRouter()
+ server := httptest.NewServer(router)
+
+ testURL = server.URL
+
+ log.Println("testURL:", testURL)
+}
+
+func TestAPIV1DeviceResourceUsageCPUGet(t *testing.T) {
+ targetURI := ConstDeviceResourceUsageCPUGet
+ testGet(t, targetURI, http.StatusOK)
+}
+
+func TestAPIV1DeviceResourceUsageDiskGet(t *testing.T) {
+ targetURI := ConstDeviceResourceUsageDiskGet
+ testGet(t, targetURI, http.StatusOK)
+}
+
+func TestAPIV1DeviceResourceUsageMemoryGet(t *testing.T) {
+ targetURI := ConstDeviceResourceUsageMemoryGet
+ testGet(t, targetURI, http.StatusOK)
+}
+
+func TestAPIV1DeviceResourceUsageNetworkGet(t *testing.T) {
+ targetURI := ConstDeviceResourceUsageNetworkGet
+ testGet(t, targetURI, http.StatusOK)
+}
+
+func TestAPIV1DiscoverymgrDevicesGet(t *testing.T) {
+ targetURI := ConstDiscoverymgrDevices
+ testGet(t, targetURI, http.StatusOK)
+}
+
+func TestAPIV1DiscoverymgrDevicesPost(t *testing.T) {
+ getDevice(t, ConstDiscoverymgrDevices, ConstAppName, http.StatusOK)
+}
+
+func TestAPIV1DiscoverymgrDevicesTXTPost(t *testing.T) {
+ getDevice(t, ConstDiscoverymgrDevicesTXT, ConstAppName, http.StatusOK)
+}
+
+func TestAPIV1DiscoverymgrDevicesTXTPostWithInvalidAppName(t *testing.T) {
+ getDevice(t, ConstDiscoverymgrDevicesTXT, ConstTooLongAppName, http.StatusBadRequest)
+}
+
+func TestAPIV1ServicemgrServicesPost(t *testing.T) {
+ executeService(t, ConstAppName, http.StatusOK)
+}
+
+func TestAPIV1ServicemgrServicesPostWithNotExistExecutionFile(t *testing.T) {
+ executeService(t, ConstNotExistAppName, http.StatusBadRequest)
+}
+
+func TestAPIV1ScoringmgrScoreLibnameGet(t *testing.T) {
+ targetURI := ConstScoremgrScore
+ testGet(t, targetURI+"/mysum", http.StatusOK)
+}
+
+func TestAPIV1ServicemgrServicesNotificationServiceIDPost(t *testing.T) {
+ statusNotificationRequest := make(map[string]interface{})
+ statusNotificationRequest["ServiceID"] = 1
+ statusNotificationRequest["Status"] = "Failed"
+
+ snbytes, _ := json.Marshal(statusNotificationRequest)
+
+ targetURI := ConstServicemgrServicesNoti
+
+ _, resStatusCode, err := httpclient.DoPost(testURL+targetURI+"1", snbytes)
+ if err != nil {
+ t.Log(err.Error())
+ t.Error()
+ }
+
+ AssertEqualInt(t, resStatusCode, http.StatusOK)
+}
+
func AssertEqualStr(t *testing.T, a, b string) {
t.Helper()
if strings.Compare(a, b) != 0 {
return
}
-func executeService(t *testing.T) {
+func executeService(t *testing.T, appName string, expectedStatus int) {
t.Helper()
targetURI := ConstServicemgrServices
appInfo := make(map[string]interface{})
- appInfo[servicemgr.ConstKeyServiceID] = 1
- appInfo[servicemgr.ConstKeyServiceName] = ConstServiceName
- appInfo[servicemgr.ConstKeyNotiTargetURL] = "URL"
+ appInfo["ServiceID"] = 1
+ appInfo["ServiceName"] = appName
+ appInfo["NotificationTargetURL"] = "URL"
userArgs := []string{"-ail"}
- appInfo[servicemgr.ConstKeyUserArgs] = userArgs
+ appInfo["UserArgs"] = userArgs
bdbytes, err := json.Marshal(appInfo)
if err != nil {
t.Log(err.Error())
- t.Error()
+ t.Fatal()
}
- testPost(t, targetURI, bdbytes, http.StatusOK)
-}
-
-var keyFilePath string = "./../../securemgr/test/key.txt"
-
-func init() {
- devicemgr.InitDeviceMgr()
- servicemgr.Init()
- discoverymgr.InitDiscovery()
- scoringmgr.Init()
- securemgr.Init(keyFilePath)
-
- router := NewRouter()
- server := httptest.NewServer(router)
-
- testURL = server.URL
-
- log.Println("testURL:", testURL)
-}
-
-func TestAPIV1DeviceResourceUsageCPUGet(t *testing.T) {
- targetURI := ConstDeviceResourceUsageCPUGet
- testGet(t, targetURI, http.StatusOK)
-}
-
-func TestAPIV1DeviceResourceUsageDiskGet(t *testing.T) {
- targetURI := ConstDeviceResourceUsageDiskGet
- testGet(t, targetURI, http.StatusOK)
-}
-
-func TestAPIV1DeviceResourceUsageMemoryGet(t *testing.T) {
- targetURI := ConstDeviceResourceUsageMemoryGet
- testGet(t, targetURI, http.StatusOK)
-}
-
-func TestAPIV1DeviceResourceUsageNetworkGet(t *testing.T) {
- targetURI := ConstDeviceResourceUsageNetworkGet
- testGet(t, targetURI, http.StatusOK)
-}
-
-func TestAPIV1DiscoverymgrDevicesGet(t *testing.T) {
- targetURI := ConstDiscoverymgrDevicesGet
- testGet(t, targetURI, http.StatusOK)
-}
-
-func TestAPIV1ServicemgrServicesPost(t *testing.T) {
- executeService(t)
+ testPost(t, targetURI, bdbytes, expectedStatus)
}
-func TestAPIV1ScoringmgrScoreLibnameGet(t *testing.T) {
- targetURI := ConstScoremgrScore
- testGet(t, targetURI+"/mysum", http.StatusOK)
-}
+func getDevice(t *testing.T, targetURI string, appName string, expectedStatus int) {
+ t.Helper()
-func TestAPIV1ServicemgrServicesNotificationServiceIDPost(t *testing.T) {
- statusNotificationRequest := make(map[string]interface{})
- statusNotificationRequest[servicemgr.ConstKeyServiceID] = 1
- statusNotificationRequest[servicemgr.ConstKeyStatus] = servicemgr.ConstServiceStatusFailed
+ appInfo := make(map[string]interface{})
- snbytes, _ := json.Marshal(statusNotificationRequest)
+ appInfo["ServiceName"] = appName
- targetURI := ConstServicemgrServicesNoti
+ bdbytes, err := json.Marshal(appInfo)
- _, resStatusCode, err := httpclient.DoPost(testURL+targetURI+"1", snbytes)
if err != nil {
t.Log(err.Error())
- t.Error()
+ t.Fatal()
}
- AssertEqualInt(t, resStatusCode, http.StatusOK)
+ testPost(t, targetURI, bdbytes, expectedStatus)
}