2 * Copyright (c) 2020 AT&T Intellectual Property.
3 * Copyright (c) 2020 Nokia.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * This source code is part of the near-RT RIC (RAN Intelligent Controller)
18 * platform project (RICP).
39 "gerrit.o-ran-sc.org/r/ric-plt/alarm-go/alarm"
40 "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
41 "github.com/gorilla/mux"
42 "github.com/prometheus/alertmanager/api/v2/models"
43 "github.com/stretchr/testify/assert"
46 var alarmManager *AlarmManager
47 var alarmer *alarm.RICAlarm
48 var eventChan chan string
51 func TestMain(M *testing.M) {
52 alarmManager = NewAlarmManager("localhost:9093", 500, false)
53 alarmManager.alertInterval = 20000
54 go alarmManager.Run(false, 5)
55 time.Sleep(time.Duration(10) * time.Second)
57 // Wait until RMR is up-and-running
58 for !xapp.Rmr.IsReady() {
59 time.Sleep(time.Duration(1) * time.Second)
62 alarmer, _ = alarm.InitAlarm("my-pod", "my-app")
63 alarmManager.alarmClient = alarmer
64 time.Sleep(time.Duration(5) * time.Second)
65 eventChan = make(chan string)
70 func TestGetPreDefinedAlarmDefinitions(t *testing.T) {
71 xapp.Logger.Info("TestGetPreDefinedAlarmDefinitions")
72 var alarmDefinition alarm.AlarmDefinition
73 req, _ := http.NewRequest("GET", "/ric/v1/alarms/define", nil)
74 vars := map[string]string{"alarmId": strconv.FormatUint(8004, 10)}
75 req = mux.SetURLVars(req, vars)
76 handleFunc := http.HandlerFunc(alarmManager.GetAlarmDefinition)
77 response := executeRequest(req, handleFunc)
78 checkResponseCode(t, http.StatusOK, response.Code)
79 json.NewDecoder(response.Body).Decode(&alarmDefinition)
80 xapp.Logger.Info("alarm definition = %v", alarmDefinition)
81 if alarmDefinition.AlarmId != alarm.RIC_RT_DISTRIBUTION_FAILED || alarmDefinition.AlarmText != "RIC ROUTING TABLE DISTRIBUTION FAILED" {
82 t.Errorf("Incorrect alarm definition")
86 func TestSetAlarmDefinitions(t *testing.T) {
87 xapp.Logger.Info("TestSetAlarmDefinitions")
88 var alarm8004Definition alarm.AlarmDefinition
89 alarm8004Definition.AlarmId = alarm.RIC_RT_DISTRIBUTION_FAILED
90 alarm8004Definition.AlarmText = "RIC ROUTING TABLE DISTRIBUTION FAILED"
91 alarm8004Definition.EventType = "Processing error"
92 alarm8004Definition.OperationInstructions = "Not defined"
93 alarm8004Definition.RaiseDelay = 0
94 alarm8004Definition.ClearDelay = 0
96 var alarm8005Definition alarm.AlarmDefinition
97 alarm8005Definition.AlarmId = alarm.TCP_CONNECTIVITY_LOST_TO_DBAAS
98 alarm8005Definition.AlarmText = "TCP CONNECTIVITY LOST TO DBAAS"
99 alarm8005Definition.EventType = "Communication error"
100 alarm8005Definition.OperationInstructions = "Not defined"
101 alarm8005Definition.RaiseDelay = 0
102 alarm8005Definition.ClearDelay = 0
104 var alarm8006Definition alarm.AlarmDefinition
105 alarm8006Definition.AlarmId = alarm.E2_CONNECTIVITY_LOST_TO_GNODEB
106 alarm8006Definition.AlarmText = "E2 CONNECTIVITY LOST TO G-NODEB"
107 alarm8006Definition.EventType = "Communication error"
108 alarm8006Definition.OperationInstructions = "Not defined"
109 alarm8006Definition.RaiseDelay = 0
110 alarm8006Definition.ClearDelay = 0
112 var alarm8007Definition alarm.AlarmDefinition
113 alarm8007Definition.AlarmId = alarm.E2_CONNECTIVITY_LOST_TO_ENODEB
114 alarm8007Definition.AlarmText = "E2 CONNECTIVITY LOST TO E-NODEB"
115 alarm8007Definition.EventType = "Communication error"
116 alarm8007Definition.OperationInstructions = "Not defined"
117 alarm8007Definition.RaiseDelay = 0
118 alarm8007Definition.ClearDelay = 0
120 var alarm8008Definition alarm.AlarmDefinition
121 alarm8008Definition.AlarmId = alarm.ACTIVE_ALARM_EXCEED_MAX_THRESHOLD
122 alarm8008Definition.AlarmText = "ACTIVE ALARM EXCEED MAX THRESHOLD"
123 alarm8008Definition.EventType = "storage warning"
124 alarm8008Definition.OperationInstructions = "Clear alarms or raise threshold"
125 alarm8008Definition.RaiseDelay = 0
126 alarm8008Definition.ClearDelay = 0
128 var alarm8009Definition alarm.AlarmDefinition
129 alarm8009Definition.AlarmId = alarm.ALARM_HISTORY_EXCEED_MAX_THRESHOLD
130 alarm8009Definition.AlarmText = "ALARM HISTORY EXCEED MAX THRESHOLD"
131 alarm8009Definition.EventType = "storage warning"
132 alarm8009Definition.OperationInstructions = "Clear alarms or raise threshold"
133 alarm8009Definition.RaiseDelay = 0
134 alarm8009Definition.ClearDelay = 0
136 pbodyParams := RicAlarmDefinitions{AlarmDefinitions: []*alarm.AlarmDefinition{&alarm8004Definition, &alarm8005Definition, &alarm8006Definition, &alarm8007Definition, &alarm8008Definition, &alarm8009Definition}}
137 pbodyEn, _ := json.Marshal(pbodyParams)
138 req, _ := http.NewRequest("POST", "/ric/v1/alarms/define", bytes.NewBuffer(pbodyEn))
139 handleFunc := http.HandlerFunc(alarmManager.SetAlarmDefinition)
140 response := executeRequest(req, handleFunc)
141 status := checkResponseCode(t, http.StatusOK, response.Code)
142 xapp.Logger.Info("status = %v", status)
146 func TestGetAlarmDefinitions(t *testing.T) {
147 xapp.Logger.Info("TestGetAlarmDefinitions")
148 var alarmDefinition alarm.AlarmDefinition
149 req, _ := http.NewRequest("GET", "/ric/v1/alarms/define", nil)
150 vars := map[string]string{"alarmId": strconv.FormatUint(8004, 10)}
151 req = mux.SetURLVars(req, vars)
152 handleFunc := http.HandlerFunc(alarmManager.GetAlarmDefinition)
153 response := executeRequest(req, handleFunc)
154 checkResponseCode(t, http.StatusOK, response.Code)
155 json.NewDecoder(response.Body).Decode(&alarmDefinition)
156 xapp.Logger.Info("alarm definition = %v", alarmDefinition)
157 if alarmDefinition.AlarmId != alarm.RIC_RT_DISTRIBUTION_FAILED || alarmDefinition.AlarmText != "RIC ROUTING TABLE DISTRIBUTION FAILED" {
158 t.Errorf("Incorrect alarm definition")
162 func TestDeleteAlarmDefinitions(t *testing.T) {
163 xapp.Logger.Info("TestDeleteAlarmDefinitions")
165 var ricAlarmDefinitions RicAlarmDefinitions
166 req, _ := http.NewRequest("GET", "/ric/v1/alarms/define", nil)
167 req = mux.SetURLVars(req, nil)
168 handleFunc := http.HandlerFunc(alarmManager.GetAlarmDefinition)
169 response := executeRequest(req, handleFunc)
170 checkResponseCode(t, http.StatusOK, response.Code)
171 json.NewDecoder(response.Body).Decode(&ricAlarmDefinitions)
172 for _, alarmDefinition := range ricAlarmDefinitions.AlarmDefinitions {
173 xapp.Logger.Info("alarm definition = %v", *alarmDefinition)
177 req, _ = http.NewRequest("DELETE", "/ric/v1/alarms/define", nil)
178 vars := map[string]string{"alarmId": strconv.FormatUint(8004, 10)}
179 req = mux.SetURLVars(req, vars)
180 handleFunc = http.HandlerFunc(alarmManager.DeleteAlarmDefinition)
181 response = executeRequest(req, handleFunc)
182 checkResponseCode(t, http.StatusOK, response.Code)
185 req, _ = http.NewRequest("GET", "/ric/v1/alarms/define", nil)
186 vars = map[string]string{"alarmId": strconv.FormatUint(8004, 10)}
187 req = mux.SetURLVars(req, vars)
188 handleFunc = http.HandlerFunc(alarmManager.GetAlarmDefinition)
189 response = executeRequest(req, handleFunc)
190 checkResponseCode(t, http.StatusBadRequest, response.Code)
193 var alarm8004Definition alarm.AlarmDefinition
194 alarm8004Definition.AlarmId = alarm.RIC_RT_DISTRIBUTION_FAILED
195 alarm8004Definition.AlarmText = "RIC ROUTING TABLE DISTRIBUTION FAILED"
196 alarm8004Definition.EventType = "Processing error"
197 alarm8004Definition.OperationInstructions = "Not defined"
198 alarm8004Definition.RaiseDelay = 0
199 alarm8004Definition.ClearDelay = 0
200 pbodyParams := RicAlarmDefinitions{AlarmDefinitions: []*alarm.AlarmDefinition{&alarm8004Definition}}
201 pbodyEn, _ := json.Marshal(pbodyParams)
202 req, _ = http.NewRequest("POST", "/ric/v1/alarms/define", bytes.NewBuffer(pbodyEn))
203 handleFunc = http.HandlerFunc(alarmManager.SetAlarmDefinition)
204 response = executeRequest(req, handleFunc)
205 checkResponseCode(t, http.StatusOK, response.Code)
208 req, _ = http.NewRequest("GET", "/ric/v1/alarms/define", nil)
209 vars = map[string]string{"alarmId": strconv.FormatUint(8004, 10)}
210 req = mux.SetURLVars(req, vars)
211 handleFunc = http.HandlerFunc(alarmManager.GetAlarmDefinition)
212 response = executeRequest(req, handleFunc)
213 checkResponseCode(t, http.StatusOK, response.Code)
216 func TestNewAlarmStoredAndPostedSucess(t *testing.T) {
217 xapp.Logger.Info("TestNewAlarmStoredAndPostedSucess")
218 ts := CreatePromAlertSimulator(t, "POST", "/api/v2/alerts", http.StatusOK, models.LabelSet{})
221 a := alarmer.NewAlarm(alarm.RIC_RT_DISTRIBUTION_FAILED, alarm.SeverityCritical, "Some App data", "eth 0 1")
222 assert.Nil(t, alarmer.Raise(a), "raise failed")
226 var activeAlarms []AlarmNotification
227 activeAlarms = make([]AlarmNotification, 1)
228 req, _ := http.NewRequest("GET", "/ric/v1/alarms/active", nil)
229 req = mux.SetURLVars(req, nil)
230 handleFunc := http.HandlerFunc(alarmManager.GetActiveAlarms)
231 response := executeRequest(req, handleFunc)
232 checkResponseCode(t, http.StatusOK, response.Code)
234 // Decode the json output from handler
235 json.NewDecoder(response.Body).Decode(activeAlarms)
236 if len(activeAlarms) != 1 {
237 t.Errorf("Incorrect alarm alarm count")
240 var alarmHistory []AlarmNotification
241 alarmHistory = make([]AlarmNotification, 1)
242 req, _ = http.NewRequest("GET", "/ric/v1/alarms/history", nil)
243 req = mux.SetURLVars(req, nil)
244 handleFunc = http.HandlerFunc(alarmManager.GetAlarmHistory)
245 response = executeRequest(req, handleFunc)
246 checkResponseCode(t, http.StatusOK, response.Code)
248 // Decode the json output from handler
249 json.NewDecoder(response.Body).Decode(alarmHistory)
250 if len(alarmHistory) != 1 {
251 t.Errorf("Incorrect alarm history count")
255 func TestAlarmClearedSucess(t *testing.T) {
256 xapp.Logger.Info("TestAlarmClearedSucess")
257 ts := CreatePromAlertSimulator(t, "POST", "/api/v2/alerts", http.StatusOK, models.LabelSet{})
261 a := alarmer.NewAlarm(alarm.RIC_RT_DISTRIBUTION_FAILED, alarm.SeverityCritical, "Some App data", "eth 0 1")
262 assert.Nil(t, alarmer.Raise(a), "raise failed")
266 // Now Clear the alarm and check alarm is removed
267 a = alarmer.NewAlarm(alarm.RIC_RT_DISTRIBUTION_FAILED, alarm.SeverityCritical, "Some App data", "eth 0 1")
268 assert.Nil(t, alarmer.Clear(a), "clear failed")
270 time.Sleep(time.Duration(2) * time.Second)
271 assert.Equal(t, len(alarmManager.activeAlarms), 0)
274 func TestMultipleAlarmsRaisedSucess(t *testing.T) {
275 xapp.Logger.Info("TestMultipleAlarmsRaisedSucess")
276 ts := CreatePromAlertSimulator(t, "POST", "/api/v2/alerts", http.StatusOK, models.LabelSet{})
280 a := alarmer.NewAlarm(alarm.RIC_RT_DISTRIBUTION_FAILED, alarm.SeverityMajor, "Some App data", "eth 0 1")
281 assert.Nil(t, alarmer.Raise(a), "raise failed")
283 b := alarmer.NewAlarm(alarm.TCP_CONNECTIVITY_LOST_TO_DBAAS, alarm.SeverityMinor, "Hello", "abcd 11")
284 assert.Nil(t, alarmer.Raise(b), "raise failed")
286 time.Sleep(time.Duration(5) * time.Second)
288 xapp.Logger.Info("VerifyAlarm: %d %+v", len(alarmManager.activeAlarms), alarmManager.activeAlarms)
290 xapp.Logger.Info("VerifyAlarm: %d %+v", len(alarmManager.activeAlarms), alarmManager.activeAlarms)
294 func TestMultipleAlarmsClearedSucess(t *testing.T) {
295 xapp.Logger.Info("TestMultipleAlarmsClearedSucess")
296 ts := CreatePromAlertSimulator(t, "POST", "/api/v2/alerts", http.StatusOK, models.LabelSet{})
300 a := alarmer.NewAlarm(alarm.RIC_RT_DISTRIBUTION_FAILED, alarm.SeverityMajor, "Some App data", "eth 0 1")
301 assert.Nil(t, alarmer.Clear(a), "clear failed")
303 b := alarmer.NewAlarm(alarm.TCP_CONNECTIVITY_LOST_TO_DBAAS, alarm.SeverityMinor, "Hello", "abcd 11")
304 assert.Nil(t, alarmer.Clear(b), "clear failed")
306 time.Sleep(time.Duration(2) * time.Second)
307 assert.Equal(t, len(alarmManager.activeAlarms), 0)
310 func TestAlarmsSuppresedSucess(t *testing.T) {
311 xapp.Logger.Info("TestAlarmsSuppresedSucess")
312 ts := CreatePromAlertSimulator(t, "POST", "/api/v2/alerts", http.StatusOK, models.LabelSet{})
315 // Raise two similar/matching alarms ... the second one suppresed
316 a := alarmer.NewAlarm(alarm.RIC_RT_DISTRIBUTION_FAILED, alarm.SeverityMajor, "Some App data", "eth 0 1")
317 assert.Nil(t, alarmer.Raise(a), "raise failed")
318 assert.Nil(t, alarmer.Raise(a), "raise failed")
321 assert.Nil(t, alarmer.Clear(a), "clear failed")
324 func TestInvalidAlarms(t *testing.T) {
325 xapp.Logger.Info("TestInvalidAlarms")
326 a := alarmer.NewAlarm(1111, alarm.SeverityMajor, "Some App data", "eth 0 1")
327 assert.Nil(t, alarmer.Raise(a), "raise failed")
328 time.Sleep(time.Duration(2) * time.Second)
331 func TestAlarmHandlingErrorCases(t *testing.T) {
332 xapp.Logger.Info("TestAlarmHandlingErrorCases")
333 ok, err := alarmManager.HandleAlarms(&xapp.RMRParams{})
334 assert.Equal(t, err.Error(), "unexpected end of JSON input")
335 assert.Nil(t, ok, "raise failed")
338 func TestConsumeUnknownMessage(t *testing.T) {
339 xapp.Logger.Info("TestConsumeUnknownMessage")
340 err := alarmManager.Consume(&xapp.RMRParams{})
341 assert.Nil(t, err, "raise failed")
344 func TestStatusCallback(t *testing.T) {
345 xapp.Logger.Info("TestStatusCallback")
346 assert.Equal(t, true, alarmManager.StatusCB())
349 func TestActiveAlarmMaxThresholds(t *testing.T) {
350 xapp.Logger.Info("TestActiveAlarmMaxThresholds")
351 ts := CreatePromAlertSimulator(t, "POST", "/api/v2/alerts", http.StatusOK, models.LabelSet{})
352 alarmManager.maxActiveAlarms = 0
353 alarmManager.maxAlarmHistory = 10
355 a := alarmer.NewAlarm(alarm.E2_CONNECTIVITY_LOST_TO_GNODEB, alarm.SeverityCritical, "Some Application data", "eth 0 2")
356 assert.Nil(t, alarmer.Raise(a), "raise failed")
358 var alarmConfigParams alarm.AlarmConfigParams
359 req, _ := http.NewRequest("GET", "/ric/v1/alarms/config", nil)
360 req = mux.SetURLVars(req, nil)
361 handleFunc := http.HandlerFunc(alarmManager.GetAlarmConfig)
362 response := executeRequest(req, handleFunc)
364 // Check HTTP Status Code
365 checkResponseCode(t, http.StatusOK, response.Code)
367 // Decode the json output from handler
368 json.NewDecoder(response.Body).Decode(&alarmConfigParams)
369 if alarmConfigParams.MaxActiveAlarms != 0 || alarmConfigParams.MaxAlarmHistory != 10 {
370 t.Errorf("Incorrect alarm thresholds")
373 time.Sleep(time.Duration(1) * time.Second)
374 alarmManager.maxActiveAlarms = 5000
375 alarmManager.maxAlarmHistory = 20000
381 func TestGetPrometheusAlerts(t *testing.T) {
382 time.Sleep(1 * time.Second)
383 xapp.Logger.Info("TestGetPrometheusAlerts")
384 ts := CreatePromAlertSimulator2(t, "GET", "/alerts?active=true&inhibited=true&silenced=true&unprocessed=true")
386 commandReady := make(chan bool, 1)
387 command := "cli/alarm-cli"
388 args := []string{"alerts", "--active", "true", "--inhibited", "true", "--silenced", "--unprocessed", "true", "true", "--host", "localhost", "--port", "9093", "flushall"}
389 ExecCLICommand(commandReady, command, args...)
395 func TestDelayedAlarmRaiseAndClear(t *testing.T) {
396 xapp.Logger.Info("TestDelayedAlarmRaiseAndClear")
398 activeAlarmsBeforeTest := len(alarmManager.activeAlarms)
399 alarmHistoryBeforeTest := len(alarmManager.alarmHistory)
401 // Add new alarm definition
402 var alarm9999Definition alarm.AlarmDefinition
403 alarm9999Definition.AlarmId = 9999
404 alarm9999Definition.AlarmText = "DELAYED TEST ALARM"
405 alarm9999Definition.EventType = "Test type"
406 alarm9999Definition.OperationInstructions = "Not defined"
407 alarm9999Definition.RaiseDelay = 1
408 alarm9999Definition.ClearDelay = 1
409 pbodyParams := RicAlarmDefinitions{AlarmDefinitions: []*alarm.AlarmDefinition{&alarm9999Definition}}
410 pbodyEn, _ := json.Marshal(pbodyParams)
411 req, _ := http.NewRequest("POST", "/ric/v1/alarms/define", bytes.NewBuffer(pbodyEn))
412 handleFunc := http.HandlerFunc(alarmManager.SetAlarmDefinition)
413 response := executeRequest(req, handleFunc)
414 checkResponseCode(t, http.StatusOK, response.Code)
416 // Verify 9999 alarm definition
417 req, _ = http.NewRequest("GET", "/ric/v1/alarms/define", nil)
418 vars := map[string]string{"alarmId": strconv.FormatUint(8004, 10)}
419 req = mux.SetURLVars(req, vars)
420 handleFunc = http.HandlerFunc(alarmManager.GetAlarmDefinition)
421 response = executeRequest(req, handleFunc)
422 checkResponseCode(t, http.StatusOK, response.Code)
424 ts := CreatePromAlertSimulator(t, "POST", "/api/v2/alerts", http.StatusOK, models.LabelSet{})
427 // Raise alarm. Posting alert and updating alarm history should be delayed
428 a := alarmer.NewAlarm(9999, alarm.SeverityCritical, "Some App data", "eth 0 1")
429 assert.Nil(t, alarmer.Raise(a), "raise failed")
430 VerifyAlarm(t, a, activeAlarmsBeforeTest+1)
432 // Clear the alarm and check the alarm is removed. Posting alert clear and updating alarm history should be delayed
433 assert.Nil(t, alarmer.Clear(a), "clear failed")
435 time.Sleep(time.Duration(2) * time.Second)
436 assert.Equal(t, len(alarmManager.activeAlarms), activeAlarmsBeforeTest)
437 assert.Equal(t, len(alarmManager.alarmHistory), alarmHistoryBeforeTest+2)
440 func TestDelayedAlarmRaiseAndClear2(t *testing.T) {
441 xapp.Logger.Info("TestDelayedAlarmRaiseAndClear2")
443 activeAlarmsBeforeTest := len(alarmManager.activeAlarms)
444 alarmHistoryBeforeTest := len(alarmManager.alarmHistory)
446 ts := CreatePromAlertSimulator(t, "POST", "/api/v2/alerts", http.StatusOK, models.LabelSet{})
449 // Raise two alarms. The first should be delayed
450 a := alarmer.NewAlarm(9999, alarm.SeverityCritical, "Some App data", "eth 0 1")
451 assert.Nil(t, alarmer.Raise(a), "raise failed")
452 VerifyAlarm(t, a, activeAlarmsBeforeTest+1)
454 b := alarmer.NewAlarm(alarm.RIC_RT_DISTRIBUTION_FAILED, alarm.SeverityMajor, "Some App data", "eth 0 1")
455 assert.Nil(t, alarmer.Raise(b), "raise failed")
456 VerifyAlarm(t, b, activeAlarmsBeforeTest+2)
458 // Clear two alarms. The first should be delayed. Check the alarms are removed
459 assert.Nil(t, alarmer.Clear(a), "clear failed")
460 assert.Nil(t, alarmer.Clear(b), "clear failed")
462 time.Sleep(time.Duration(2) * time.Second)
463 assert.Equal(t, len(alarmManager.activeAlarms), activeAlarmsBeforeTest)
464 assert.Equal(t, len(alarmManager.alarmHistory), alarmHistoryBeforeTest+4)
467 func TestDelayedAlarmRaiseAndClear3(t *testing.T) {
468 xapp.Logger.Info("TestDelayedAlarmRaiseAndClear3")
470 // Delete exisitng 9999 alarm definition
471 req, _ := http.NewRequest("DELETE", "/ric/v1/alarms/define", nil)
472 vars := map[string]string{"alarmId": strconv.FormatUint(9999, 10)}
473 req = mux.SetURLVars(req, vars)
474 handleFunc := http.HandlerFunc(alarmManager.DeleteAlarmDefinition)
475 response := executeRequest(req, handleFunc)
476 checkResponseCode(t, http.StatusOK, response.Code)
478 // Add updated 9999 alarm definition
479 var alarm9999Definition alarm.AlarmDefinition
480 alarm9999Definition.AlarmId = 9999
481 alarm9999Definition.AlarmText = "DELAYED TEST ALARM"
482 alarm9999Definition.EventType = "Test type"
483 alarm9999Definition.OperationInstructions = "Not defined"
484 alarm9999Definition.RaiseDelay = 1
485 alarm9999Definition.ClearDelay = 0
486 pbodyParams := RicAlarmDefinitions{AlarmDefinitions: []*alarm.AlarmDefinition{&alarm9999Definition}}
487 pbodyEn, _ := json.Marshal(pbodyParams)
488 req, _ = http.NewRequest("POST", "/ric/v1/alarms/define", bytes.NewBuffer(pbodyEn))
489 handleFunc = http.HandlerFunc(alarmManager.SetAlarmDefinition)
490 response = executeRequest(req, handleFunc)
491 checkResponseCode(t, http.StatusOK, response.Code)
493 // Verify 9999 alarm definition
494 req, _ = http.NewRequest("GET", "/ric/v1/alarms/define", nil)
495 vars = map[string]string{"alarmId": strconv.FormatUint(8004, 10)}
496 req = mux.SetURLVars(req, vars)
497 handleFunc = http.HandlerFunc(alarmManager.GetAlarmDefinition)
498 response = executeRequest(req, handleFunc)
499 checkResponseCode(t, http.StatusOK, response.Code)
501 activeAlarmsBeforeTest := len(alarmManager.activeAlarms)
502 alarmHistoryBeforeTest := len(alarmManager.alarmHistory)
504 ts := CreatePromAlertSimulator(t, "POST", "/api/v2/alerts", http.StatusOK, models.LabelSet{})
507 // Raise two alarms. The first should be delayed
508 a := alarmer.NewAlarm(9999, alarm.SeverityCritical, "Some App data", "eth 0 1")
509 assert.Nil(t, alarmer.Raise(a), "raise failed")
510 VerifyAlarm(t, a, activeAlarmsBeforeTest+1)
512 b := alarmer.NewAlarm(alarm.RIC_RT_DISTRIBUTION_FAILED, alarm.SeverityMajor, "Some App data", "eth 0 1")
513 assert.Nil(t, alarmer.Raise(b), "raise failed")
514 VerifyAlarm(t, b, activeAlarmsBeforeTest+2)
516 // Clear two alarms. The first should be delayed. Check the alarms are removed
517 assert.Nil(t, alarmer.Clear(a), "clear failed")
518 assert.Nil(t, alarmer.Clear(b), "clear failed")
520 time.Sleep(time.Duration(2) * time.Second)
521 assert.Equal(t, len(alarmManager.activeAlarms), activeAlarmsBeforeTest)
522 assert.Equal(t, len(alarmManager.alarmHistory), alarmHistoryBeforeTest+4)
525 func TestClearExpiredAlarms(t *testing.T) {
526 xapp.Logger.Info("TestClearExpiredAlarms")
528 a := alarm.AlarmMessage{
529 Alarm: alarmer.NewAlarm(8007, alarm.SeverityWarning, "threshold", ""),
530 AlarmAction: alarm.AlarmActionRaise,
531 AlarmTime: time.Now().UnixNano(),
533 d := alarm.RICAlarmDefinitions[8007]
534 n := AlarmNotification{a, *d}
535 alarmManager.activeAlarms = make([]AlarmNotification, 0)
536 alarmManager.UpdateActiveAlarmList(&n)
539 a.Alarm.SpecificProblem = 1234
540 assert.False(t, alarmManager.ClearExpiredAlarms(n, 0, false), "ClearExpiredAlarms failed")
544 assert.False(t, alarmManager.ClearExpiredAlarms(n, 0, false), "ClearExpiredAlarms failed")
547 a.Alarm.SpecificProblem = 8007
549 assert.False(t, alarmManager.ClearExpiredAlarms(n, 0, false), "ClearExpiredAlarms failed")
551 // TTL expired, alarm should be cleared
552 time.Sleep(time.Duration(3) * time.Second)
553 assert.Equal(t, len(alarmManager.activeAlarms), 1)
554 assert.True(t, alarmManager.ClearExpiredAlarms(n, 0, false), "ClearExpiredAlarms failed")
555 assert.Equal(t, len(alarmManager.activeAlarms), 0)
558 func TestSetAlarmConfig(t *testing.T) {
559 xapp.Logger.Info("TestSetAlarmConfig")
561 var setAlarmConfig alarm.AlarmConfigParams
562 setAlarmConfig.MaxActiveAlarms = 500
563 setAlarmConfig.MaxAlarmHistory = 2000
565 pbodyEn, _ := json.Marshal(setAlarmConfig)
566 req, _ := http.NewRequest("POST", "/ric/v1/alarms/config", bytes.NewBuffer(pbodyEn))
567 handleFunc := http.HandlerFunc(alarmManager.SetAlarmConfig)
568 response := executeRequest(req, handleFunc)
569 checkResponseCode(t, http.StatusOK, response.Code)
571 var getAlarmConfig alarm.AlarmConfigParams
572 req, _ = http.NewRequest("GET", "/ric/v1/alarms/config", nil)
573 req = mux.SetURLVars(req, nil)
574 handleFunc = http.HandlerFunc(alarmManager.GetAlarmConfig)
575 response = executeRequest(req, handleFunc)
576 checkResponseCode(t, http.StatusOK, response.Code)
578 // Decode the json output from handler
579 json.NewDecoder(response.Body).Decode(&getAlarmConfig)
580 if getAlarmConfig.MaxActiveAlarms != 500 || getAlarmConfig.MaxAlarmHistory != 2000 {
581 t.Errorf("Incorrect alarm thresholds")
585 setAlarmConfig.MaxActiveAlarms = 5000
586 setAlarmConfig.MaxAlarmHistory = 20000
588 pbodyEn, _ = json.Marshal(setAlarmConfig)
589 req, _ = http.NewRequest("POST", "/ric/v1/alarms/config", bytes.NewBuffer(pbodyEn))
590 handleFunc = http.HandlerFunc(alarmManager.SetAlarmConfig)
591 response = executeRequest(req, handleFunc)
592 checkResponseCode(t, http.StatusOK, response.Code)
594 req, _ = http.NewRequest("GET", "/ric/v1/alarms/config", nil)
595 req = mux.SetURLVars(req, nil)
596 handleFunc = http.HandlerFunc(alarmManager.GetAlarmConfig)
597 response = executeRequest(req, handleFunc)
598 checkResponseCode(t, http.StatusOK, response.Code)
600 // Decode the json output from handler
601 json.NewDecoder(response.Body).Decode(&getAlarmConfig)
602 if getAlarmConfig.MaxActiveAlarms != 5000 || getAlarmConfig.MaxAlarmHistory != 20000 {
603 t.Errorf("Incorrect alarm thresholds")
607 func TestConfigChangeCB(t *testing.T) {
608 xapp.Logger.Info("TestConfigChangeCB")
609 alarmManager.ConfigChangeCB("AlarmManager")
612 func VerifyAlarm(t *testing.T, a alarm.Alarm, expectedCount int) string {
613 receivedAlert := waitForEvent()
615 assert.Equal(t, expectedCount, len(alarmManager.activeAlarms))
616 _, ok := alarmManager.IsMatchFound(a)
622 func VerifyAlert(t *testing.T, receivedAlert, expectedAlert string) {
623 receivedAlert = strings.Replace(fmt.Sprintf("%v", receivedAlert), "\r\n", " ", -1)
624 //assert.Equal(t, receivedAlert, e)
627 func CreatePromAlertSimulator(t *testing.T, method, url string, status int, respData interface{}) *httptest.Server {
628 l, err := net.Listen("tcp", "localhost:9093")
630 t.Error("Failed to create listener: " + err.Error())
632 ts := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
633 assert.Equal(t, r.Method, method)
634 assert.Equal(t, r.URL.String(), url)
638 w.Header().Add("Content-Type", "application/json")
639 w.WriteHeader(status)
640 b, _ := json.Marshal(respData)
651 func CreatePromAlertSimulator2(t *testing.T, method, url string) *httptest.Server {
652 l, err := net.Listen("tcp", "localhost:9093")
654 t.Error("Failed to create listener: " + err.Error())
656 ts := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
657 assert.Equal(t, r.Method, method)
658 assert.Equal(t, r.URL.String(), url)
660 w.Header().Add("Content-Type", "application/json")
662 // Read alerts from file
663 payload, err := readJSONFromFile("../testresources/prometheus-alerts.json")
665 t.Error("Failed to send response: ", err)
667 _, err = w.Write(payload)
669 t.Error("Failed to send response: " + err.Error())
678 func waitForEvent() string {
679 receivedAlert := <-eventChan
683 func fireEvent(t *testing.T, body io.ReadCloser) {
684 reqBody, err := ioutil.ReadAll(body)
685 assert.Nil(t, err, "ioutil.ReadAll failed")
686 assert.NotNil(t, reqBody, "ioutil.ReadAll failed")
688 eventChan <- fmt.Sprintf("%s", reqBody)
691 func executeRequest(req *http.Request, handleR http.HandlerFunc) *httptest.ResponseRecorder {
692 rr := httptest.NewRecorder()
694 handleR.ServeHTTP(rr, req)
699 func checkResponseCode(t *testing.T, expected, actual int) bool {
700 if expected != actual {
701 t.Errorf("Expected response code %d. Got %d\n", expected, actual)
707 func ExecCLICommand(commandReady chan bool, command string, args ...string) {
709 xapp.Logger.Info("Giving CLI command")
710 cmd := exec.Command(command, args...)
712 output, err := cmd.CombinedOutput()
714 xapp.Logger.Info("CLI command failed out: %s", err)
716 xapp.Logger.Info("CLI command output: %s", output)
718 xapp.Logger.Info("CLI command completed")
722 func readJSONFromFile(filename string) ([]byte, error) {
723 file, err := ioutil.ReadFile(filename)
725 err := fmt.Errorf("readJSONFromFile() failed: Error: %v", err)