Taking in latest xapp-frame for alarm-go
[ric-plt/alarm-go.git] / manager / cmd / manager_test.go
index b75cae5..5f1cecc 100755 (executable)
@@ -50,7 +50,7 @@ var eventChan chan string
 func TestMain(M *testing.M) {
        alarmManager = NewAlarmManager("localhost:9093", 500, false)
        alarmManager.alertInterval = 20000
-       go alarmManager.Run(false)
+       go alarmManager.Run(false, 5)
        time.Sleep(time.Duration(10) * time.Second)
 
        // Wait until RMR is up-and-running
@@ -221,6 +221,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) {
@@ -353,7 +381,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
 
@@ -392,17 +420,17 @@ func TestDelayedAlarmRaiseAndClear(t *testing.T) {
        ts := CreatePromAlertSimulator(t, "POST", "/api/v2/alerts", http.StatusOK, models.LabelSet{})
        defer ts.Close()
 
-       // Raise alarm. Posting alert and updating alarm history should be delayed 
+       // Raise alarm. Posting alert and updating alarm history should be delayed
        a := alarmer.NewAlarm(9999, alarm.SeverityCritical, "Some App data", "eth 0 1")
        assert.Nil(t, alarmer.Raise(a), "raise failed")
-       VerifyAlarm(t, a, activeAlarmsBeforeTest + 1)
+       VerifyAlarm(t, a, activeAlarmsBeforeTest+1)
 
        // Clear the alarm and check the alarm is removed. Posting alert clear and updating alarm history should be delayed
        assert.Nil(t, alarmer.Clear(a), "clear failed")
 
        time.Sleep(time.Duration(2) * time.Second)
        assert.Equal(t, len(alarmManager.activeAlarms), activeAlarmsBeforeTest)
-       assert.Equal(t, len(alarmManager.alarmHistory), alarmHistoryBeforeTest + 2)
+       assert.Equal(t, len(alarmManager.alarmHistory), alarmHistoryBeforeTest+2)
 }
 
 func TestDelayedAlarmRaiseAndClear2(t *testing.T) {
@@ -417,11 +445,11 @@ func TestDelayedAlarmRaiseAndClear2(t *testing.T) {
        // Raise two alarms. The first should be delayed
        a := alarmer.NewAlarm(9999, alarm.SeverityCritical, "Some App data", "eth 0 1")
        assert.Nil(t, alarmer.Raise(a), "raise failed")
-       VerifyAlarm(t, a, activeAlarmsBeforeTest + 1)
+       VerifyAlarm(t, a, activeAlarmsBeforeTest+1)
 
        b := alarmer.NewAlarm(alarm.RIC_RT_DISTRIBUTION_FAILED, alarm.SeverityMajor, "Some App data", "eth 0 1")
        assert.Nil(t, alarmer.Raise(b), "raise failed")
-       VerifyAlarm(t, b, activeAlarmsBeforeTest + 2)
+       VerifyAlarm(t, b, activeAlarmsBeforeTest+2)
 
        // Clear two alarms. The first should be delayed. Check the alarms are removed
        assert.Nil(t, alarmer.Clear(a), "clear failed")
@@ -429,7 +457,7 @@ func TestDelayedAlarmRaiseAndClear2(t *testing.T) {
 
        time.Sleep(time.Duration(2) * time.Second)
        assert.Equal(t, len(alarmManager.activeAlarms), activeAlarmsBeforeTest)
-       assert.Equal(t, len(alarmManager.alarmHistory), alarmHistoryBeforeTest + 4)
+       assert.Equal(t, len(alarmManager.alarmHistory), alarmHistoryBeforeTest+4)
 }
 
 func TestDelayedAlarmRaiseAndClear3(t *testing.T) {
@@ -475,11 +503,11 @@ func TestDelayedAlarmRaiseAndClear3(t *testing.T) {
        // Raise two alarms. The first should be delayed
        a := alarmer.NewAlarm(9999, alarm.SeverityCritical, "Some App data", "eth 0 1")
        assert.Nil(t, alarmer.Raise(a), "raise failed")
-       VerifyAlarm(t, a, activeAlarmsBeforeTest + 1)
+       VerifyAlarm(t, a, activeAlarmsBeforeTest+1)
 
        b := alarmer.NewAlarm(alarm.RIC_RT_DISTRIBUTION_FAILED, alarm.SeverityMajor, "Some App data", "eth 0 1")
        assert.Nil(t, alarmer.Raise(b), "raise failed")
-       VerifyAlarm(t, b, activeAlarmsBeforeTest + 2)
+       VerifyAlarm(t, b, activeAlarmsBeforeTest+2)
 
        // Clear two alarms. The first should be delayed. Check the alarms are removed
        assert.Nil(t, alarmer.Clear(a), "clear failed")
@@ -487,7 +515,94 @@ func TestDelayedAlarmRaiseAndClear3(t *testing.T) {
 
        time.Sleep(time.Duration(2) * time.Second)
        assert.Equal(t, len(alarmManager.activeAlarms), activeAlarmsBeforeTest)
-       assert.Equal(t, len(alarmManager.alarmHistory), alarmHistoryBeforeTest + 4)
+       assert.Equal(t, len(alarmManager.alarmHistory), alarmHistoryBeforeTest+4)
+}
+
+func TestClearExpiredAlarms(t *testing.T) {
+       xapp.Logger.Info("TestClearExpiredAlarms")
+
+       a := alarm.AlarmMessage{
+               Alarm:       alarmer.NewAlarm(8007, alarm.SeverityWarning, "threshold", ""),
+               AlarmAction: alarm.AlarmActionRaise,
+               AlarmTime:   time.Now().UnixNano(),
+       }
+       d := alarm.RICAlarmDefinitions[8007]
+       n := AlarmNotification{a, *d}
+       alarmManager.activeAlarms = make([]AlarmNotification, 0)
+       alarmManager.UpdateActiveAlarmList(&n)
+
+       // Unknown SP
+       a.Alarm.SpecificProblem = 1234
+       assert.False(t, alarmManager.ClearExpiredAlarms(n, 0, false), "ClearExpiredAlarms failed")
+
+       // TTL is 0
+       d.TimeToLive = 0
+       assert.False(t, alarmManager.ClearExpiredAlarms(n, 0, false), "ClearExpiredAlarms failed")
+
+       // TTL not expired
+       a.Alarm.SpecificProblem = 8007
+       d.TimeToLive = 2
+       assert.False(t, alarmManager.ClearExpiredAlarms(n, 0, false), "ClearExpiredAlarms failed")
+
+       // TTL expired, alarm should be cleared
+       time.Sleep(time.Duration(3) * time.Second)
+       assert.Equal(t, len(alarmManager.activeAlarms), 1)
+       assert.True(t, alarmManager.ClearExpiredAlarms(n, 0, false), "ClearExpiredAlarms failed")
+       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 VerifyAlarm(t *testing.T, a alarm.Alarm, expectedCount int) string {