Merge "Update available SDL version to v0.7.0"
[ric-plt/alarm-go.git] / manager / cmd / manager_test.go
index 00df224..7efbab2 100755 (executable)
@@ -24,11 +24,6 @@ import (
        "bytes"
        "encoding/json"
        "fmt"
-       "gerrit.o-ran-sc.org/r/ric-plt/alarm-go/alarm"
-       "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
-       "github.com/gorilla/mux"
-       "github.com/prometheus/alertmanager/api/v2/models"
-       "github.com/stretchr/testify/assert"
        "io"
        "io/ioutil"
        "net"
@@ -40,6 +35,12 @@ import (
        "strings"
        "testing"
        "time"
+
+       "gerrit.o-ran-sc.org/r/ric-plt/alarm-go/alarm"
+       "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
+       "github.com/gorilla/mux"
+       "github.com/prometheus/alertmanager/api/v2/models"
+       "github.com/stretchr/testify/assert"
 )
 
 var alarmManager *AlarmManager
@@ -139,7 +140,56 @@ func TestSetAlarmDefinitions(t *testing.T) {
        response := executeRequest(req, handleFunc)
        status := checkResponseCode(t, http.StatusOK, response.Code)
        xapp.Logger.Info("status = %v", status)
+}
+
+
+func TestSetAlarmConfigDecodeError(t *testing.T) {
+        xapp.Logger.Info("TestSetAlarmConfigDecodeError")
+
+       var jsonStr = []byte(`{"test":"Invalid Alarm Data", "test1" 123}`)
+        req, _ := http.NewRequest("POST", "/ric/v1/alarms", bytes.NewBuffer(jsonStr))
+        handleFunc := http.HandlerFunc(alarmManager.SetAlarmConfig)
+        response := executeRequest(req, handleFunc)
+        status := checkResponseCode(t,http.StatusOK,response.Code)
+        xapp.Logger.Info("status = %v", status)
+}
+
+func TestSetAlarmDefinitionDecodeError(t *testing.T) {
+        xapp.Logger.Info("TestSetAlarmDefinitionDecodeError")
+
+        var jsonStr = []byte(`{"test":"Invalid Alarm Data", "test1" 123}`)
+        req, _ := http.NewRequest("POST", "/ric/v1/alarms/define", bytes.NewBuffer(jsonStr))
+        handleFunc := http.HandlerFunc(alarmManager.SetAlarmDefinition)
+        response := executeRequest(req, handleFunc)
+        status := checkResponseCode(t,http.StatusBadRequest,response.Code)
+        xapp.Logger.Info("status = %v", status)
+}
+
+func TestRaiseAlarmEmptyBody(t *testing.T) {
+        xapp.Logger.Info("TestRaiseAlarmEmptyBody")
+        req, _ := http.NewRequest("POST", "/ric/v1/alarms", nil)
+        handleFunc := http.HandlerFunc(alarmManager.RaiseAlarm)
+        response := executeRequest(req, handleFunc)
+        status := checkResponseCode(t,http.StatusOK,response.Code)
+        xapp.Logger.Info("status = %v", status)
+}
+
+func TestSetAlarmDefinitionsEmptyBody(t *testing.T) {
+        xapp.Logger.Info("TestSetAlarmDefinitionsEmptyBody")
+        req, _ := http.NewRequest("POST", "/ric/v1/alarms/define", nil)
+        handleFunc := http.HandlerFunc(alarmManager.SetAlarmDefinition)
+        response := executeRequest(req, handleFunc)
+        status := checkResponseCode(t,http.StatusBadRequest,response.Code)
+        xapp.Logger.Info("status = %v", status)
+}
 
+func TestClearAlarmEmptyBody(t *testing.T) {
+        xapp.Logger.Info("TestClearAlarmEmptyBody")
+        req, _ := http.NewRequest("DELETE", "/ric/v1/alarms", nil)
+        handleFunc := http.HandlerFunc(alarmManager.ClearAlarm)
+        response := executeRequest(req, handleFunc)
+        status := checkResponseCode(t,http.StatusOK,response.Code)
+        xapp.Logger.Info("status = %v", status)
 }
 
 func TestGetAlarmDefinitions(t *testing.T) {
@@ -221,6 +271,34 @@ func TestNewAlarmStoredAndPostedSucess(t *testing.T) {
        assert.Nil(t, alarmer.Raise(a), "raise failed")
 
        VerifyAlarm(t, a, 1)
+
+       var activeAlarms []AlarmNotification
+       activeAlarms = make([]AlarmNotification, 1)
+       req, _ := http.NewRequest("GET", "/ric/v1/alarms/active", nil)
+       req = mux.SetURLVars(req, nil)
+       handleFunc := http.HandlerFunc(alarmManager.GetActiveAlarms)
+       response := executeRequest(req, handleFunc)
+       checkResponseCode(t, http.StatusOK, response.Code)
+
+       // Decode the json output from handler
+       json.NewDecoder(response.Body).Decode(activeAlarms)
+       if len(activeAlarms) != 1 {
+               t.Errorf("Incorrect alarm alarm count")
+       }
+
+       var alarmHistory []AlarmNotification
+       alarmHistory = make([]AlarmNotification, 1)
+       req, _ = http.NewRequest("GET", "/ric/v1/alarms/history", nil)
+       req = mux.SetURLVars(req, nil)
+       handleFunc = http.HandlerFunc(alarmManager.GetAlarmHistory)
+       response = executeRequest(req, handleFunc)
+       checkResponseCode(t, http.StatusOK, response.Code)
+
+       // Decode the json output from handler
+       json.NewDecoder(response.Body).Decode(alarmHistory)
+       if len(alarmHistory) != 1 {
+               t.Errorf("Incorrect alarm history count")
+       }
 }
 
 func TestAlarmClearedSucess(t *testing.T) {
@@ -254,8 +332,11 @@ func TestMultipleAlarmsRaisedSucess(t *testing.T) {
        b := alarmer.NewAlarm(alarm.TCP_CONNECTIVITY_LOST_TO_DBAAS, alarm.SeverityMinor, "Hello", "abcd 11")
        assert.Nil(t, alarmer.Raise(b), "raise failed")
 
-       time.Sleep(time.Duration(2) * time.Second)
-       VerifyAlarm(t, a, 2)
+       time.Sleep(time.Duration(5) * time.Second)
+
+       xapp.Logger.Info("VerifyAlarm: %d %+v", len(alarmManager.activeAlarms), alarmManager.activeAlarms)
+       VerifyAlarm(t, a, 1)
+       xapp.Logger.Info("VerifyAlarm: %d %+v", len(alarmManager.activeAlarms), alarmManager.activeAlarms)
        VerifyAlarm(t, b, 2)
 }
 
@@ -353,7 +434,7 @@ func TestGetPrometheusAlerts(t *testing.T) {
 
        commandReady := make(chan bool, 1)
        command := "cli/alarm-cli"
-       args := []string{"gapam", "--active", "true", "--inhibited", "true", "--silenced", "--unprocessed", "true", "true", "--host", "localhost", "--port", "9093", "flushall"}
+       args := []string{"alerts", "--active", "true", "--inhibited", "true", "--silenced", "--unprocessed", "true", "true", "--host", "localhost", "--port", "9093", "flushall"}
        ExecCLICommand(commandReady, command, args...)
        <-commandReady
 
@@ -523,6 +604,114 @@ func TestClearExpiredAlarms(t *testing.T) {
        assert.Equal(t, len(alarmManager.activeAlarms), 0)
 }
 
+func TestSetAlarmConfig(t *testing.T) {
+       xapp.Logger.Info("TestSetAlarmConfig")
+
+       var setAlarmConfig alarm.AlarmConfigParams
+       setAlarmConfig.MaxActiveAlarms = 500
+       setAlarmConfig.MaxAlarmHistory = 2000
+
+       pbodyEn, _ := json.Marshal(setAlarmConfig)
+       req, _ := http.NewRequest("POST", "/ric/v1/alarms/config", bytes.NewBuffer(pbodyEn))
+       handleFunc := http.HandlerFunc(alarmManager.SetAlarmConfig)
+       response := executeRequest(req, handleFunc)
+       checkResponseCode(t, http.StatusOK, response.Code)
+
+       var getAlarmConfig alarm.AlarmConfigParams
+       req, _ = http.NewRequest("GET", "/ric/v1/alarms/config", nil)
+       req = mux.SetURLVars(req, nil)
+       handleFunc = http.HandlerFunc(alarmManager.GetAlarmConfig)
+       response = executeRequest(req, handleFunc)
+       checkResponseCode(t, http.StatusOK, response.Code)
+
+       // Decode the json output from handler
+       json.NewDecoder(response.Body).Decode(&getAlarmConfig)
+       if getAlarmConfig.MaxActiveAlarms != 500 || getAlarmConfig.MaxAlarmHistory != 2000 {
+               t.Errorf("Incorrect alarm thresholds")
+       }
+
+       // Revert ot default
+       setAlarmConfig.MaxActiveAlarms = 5000
+       setAlarmConfig.MaxAlarmHistory = 20000
+
+       pbodyEn, _ = json.Marshal(setAlarmConfig)
+       req, _ = http.NewRequest("POST", "/ric/v1/alarms/config", bytes.NewBuffer(pbodyEn))
+       handleFunc = http.HandlerFunc(alarmManager.SetAlarmConfig)
+       response = executeRequest(req, handleFunc)
+       checkResponseCode(t, http.StatusOK, response.Code)
+
+       req, _ = http.NewRequest("GET", "/ric/v1/alarms/config", nil)
+       req = mux.SetURLVars(req, nil)
+       handleFunc = http.HandlerFunc(alarmManager.GetAlarmConfig)
+       response = executeRequest(req, handleFunc)
+       checkResponseCode(t, http.StatusOK, response.Code)
+
+       // Decode the json output from handler
+       json.NewDecoder(response.Body).Decode(&getAlarmConfig)
+       if getAlarmConfig.MaxActiveAlarms != 5000 || getAlarmConfig.MaxAlarmHistory != 20000 {
+               t.Errorf("Incorrect alarm thresholds")
+       }
+}
+
+func TestConfigChangeCB(t *testing.T) {
+       xapp.Logger.Info("TestConfigChangeCB")
+       alarmManager.ConfigChangeCB("AlarmManager")
+}
+
+func TestPersistentStorage(t *testing.T) {
+    xapp.Logger.Info("TestPersistentStorage")
+    alarmManager.alarmInfoPvFile = "../../definitions/sample.json"
+    alarmManager.ReadAlarmInfoFromPersistentVolume()
+}
+
+func TestReadAlarmDefinitionFromJsonWrongFilename(t *testing.T) {
+  // use   to set wrong file name os.Setenv("SITE_TITLE", "Test Site")
+    xapp.Logger.Info("TestReadAlarmDefinitionFromJsonWrongFilename")
+    os.Setenv("DEF_FILE","test.json")
+    alarmManager.ReadAlarmDefinitionFromJson()
+  // correct the filename
+}
+
+func TestReadAlarmDefinitionFromJsonInvalidFilename(t *testing.T) {
+  // use   to set wrong file name os.Setenv("SITE_TITLE", "Test Site")
+    xapp.Logger.Info("TestReadAlarmDefinitionFromJsonInvalidFilename")
+    os.Setenv("DEF_FILE","../../definitions/test.json")
+    alarmManager.ReadAlarmDefinitionFromJson()
+  // correct the filename
+}
+
+func TestPostAlarm(t *testing.T) {
+        xapp.Logger.Info("TestPostAlarm")
+        var activeAlarms []AlarmNotification
+        activeAlarms = make([]AlarmNotification, 1)
+        alarmManager.PostAlarm(&activeAlarms[0])
+}
+
+func TestPostAlarm1(t *testing.T) {
+        xapp.Logger.Info("TestPostAlarm")
+        var activeAlarms []AlarmNotification
+        activeAlarms = make([]AlarmNotification, 2)
+        alarmManager.PostAlarm(&activeAlarms[0])
+}
+
+func TestNewAlarmManagerOther(t *testing.T){
+    NewAlarmManager("", 0, true)
+}
+
+func TestStatusCallbackFailure(t *testing.T) {
+        xapp.Logger.Info("TestStatusCallbackFailure")
+       alarmManager.rmrReady = false 
+        assert.Equal(t, false, alarmManager.StatusCB())
+}
+
+func TestConfigChangeCBFailure(t *testing.T) {
+        xapp.Logger.Info("TestConfigChangeCBFailure")
+       alarmManager.maxActiveAlarms = 0
+       alarmManager.maxAlarmHistory = 0
+       alarmManager.ConfigChangeCB("AlarmManager")
+}
+
+
 func VerifyAlarm(t *testing.T, a alarm.Alarm, expectedCount int) string {
        receivedAlert := waitForEvent()
 
@@ -641,3 +830,5 @@ func readJSONFromFile(filename string) ([]byte, error) {
        }
        return file, nil
 }
+
+