blob: 4f4cf6d536769698837f2cd865e7e2cc6d4e3416 [file] [log] [blame]
Mohamed Abukar4e7e7122020-03-04 10:01:45 +02001/*
2 * Copyright (c) 2020 AT&T Intellectual Property.
3 * Copyright (c) 2020 Nokia.
4 *
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
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
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.
16 *
17 * This source code is part of the near-RT RIC (RAN Intelligent Controller)
18 * platform project (RICP).
19 */
20
21package main
22
23import (
vipin4cedd502020-09-25 05:58:31 +000024 "bytes"
Mohamed Abukar4e7e7122020-03-04 10:01:45 +020025 "encoding/json"
26 "fmt"
Mohamed Abukar4e7e7122020-03-04 10:01:45 +020027 "io"
28 "io/ioutil"
29 "net"
30 "net/http"
31 "net/http/httptest"
32 "os"
Anssi Mannila00894a42020-10-19 11:36:26 +030033 "os/exec"
vipin4cedd502020-09-25 05:58:31 +000034 "strconv"
Mohamed Abukar4e7e7122020-03-04 10:01:45 +020035 "strings"
36 "testing"
37 "time"
Mohamed Abukar2d3cc6b2021-06-09 07:33:04 +000038
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"
Mohamed Abukar4e7e7122020-03-04 10:01:45 +020044)
45
Abukar Mohamed121e8b62020-09-18 11:41:33 +000046var alarmManager *AlarmManager
Mohamed Abukar4e7e7122020-03-04 10:01:45 +020047var alarmer *alarm.RICAlarm
48var eventChan chan string
49
50// Test cases
51func TestMain(M *testing.M) {
Mohamed Abukar105030f2020-10-22 18:08:34 +030052 alarmManager = NewAlarmManager("localhost:9093", 500, false)
vipin6f73fa32020-10-06 06:51:53 +000053 alarmManager.alertInterval = 20000
Mohamed Abukar3649fae2020-10-30 23:51:39 +020054 go alarmManager.Run(false, 5)
vipin14323a92020-09-25 10:03:43 +000055 time.Sleep(time.Duration(10) * time.Second)
Mohamed Abukar4e7e7122020-03-04 10:01:45 +020056
57 // Wait until RMR is up-and-running
58 for !xapp.Rmr.IsReady() {
59 time.Sleep(time.Duration(1) * time.Second)
60 }
61
62 alarmer, _ = alarm.InitAlarm("my-pod", "my-app")
vipin541eb502020-09-22 12:04:59 +000063 alarmManager.alarmClient = alarmer
Mohamed Abukareac44512020-03-31 09:46:04 +030064 time.Sleep(time.Duration(5) * time.Second)
Mohamed Abukar4e7e7122020-03-04 10:01:45 +020065 eventChan = make(chan string)
66
67 os.Exit(M.Run())
68}
69
vipin14323a92020-09-25 10:03:43 +000070func 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")
83 }
84}
85
vipin54a3a4f2020-09-23 12:19:58 +000086func 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"
Anssi Mannila18fd03c2020-10-29 10:01:00 +020093 alarm8004Definition.RaiseDelay = 0
94 alarm8004Definition.ClearDelay = 0
vipin54a3a4f2020-09-23 12:19:58 +000095
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"
Anssi Mannila18fd03c2020-10-29 10:01:00 +0200101 alarm8005Definition.RaiseDelay = 0
102 alarm8005Definition.ClearDelay = 0
vipin54a3a4f2020-09-23 12:19:58 +0000103
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"
Anssi Mannila18fd03c2020-10-29 10:01:00 +0200109 alarm8006Definition.RaiseDelay = 0
110 alarm8006Definition.ClearDelay = 0
vipin54a3a4f2020-09-23 12:19:58 +0000111
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"
Anssi Mannila18fd03c2020-10-29 10:01:00 +0200117 alarm8007Definition.RaiseDelay = 0
118 alarm8007Definition.ClearDelay = 0
vipin54a3a4f2020-09-23 12:19:58 +0000119
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"
vipin14323a92020-09-25 10:03:43 +0000124 alarm8008Definition.OperationInstructions = "Clear alarms or raise threshold"
Anssi Mannila18fd03c2020-10-29 10:01:00 +0200125 alarm8008Definition.RaiseDelay = 0
126 alarm8008Definition.ClearDelay = 0
vipin54a3a4f2020-09-23 12:19:58 +0000127
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"
vipin14323a92020-09-25 10:03:43 +0000132 alarm8009Definition.OperationInstructions = "Clear alarms or raise threshold"
Anssi Mannila18fd03c2020-10-29 10:01:00 +0200133 alarm8009Definition.RaiseDelay = 0
134 alarm8009Definition.ClearDelay = 0
vipin54a3a4f2020-09-23 12:19:58 +0000135
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)
143
144}
145
146func 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")
159 }
160}
161
162func TestDeleteAlarmDefinitions(t *testing.T) {
163 xapp.Logger.Info("TestDeleteAlarmDefinitions")
164 //Get all
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)
174 }
175
176 //Delete 8004
177 req, _ = http.NewRequest("DELETE", "/ric/v1/alarms/define", nil)
vipin4cedd502020-09-25 05:58:31 +0000178 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)
vipin54a3a4f2020-09-23 12:19:58 +0000183
184 //Get 8004 fail
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)
191
192 //Set 8004 success
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"
Anssi Mannila18fd03c2020-10-29 10:01:00 +0200198 alarm8004Definition.RaiseDelay = 0
199 alarm8004Definition.ClearDelay = 0
vipin54a3a4f2020-09-23 12:19:58 +0000200 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)
206
207 //Get 8004 success
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)
214}
215
Mohamed Abukar4e7e7122020-03-04 10:01:45 +0200216func TestNewAlarmStoredAndPostedSucess(t *testing.T) {
vipin54a3a4f2020-09-23 12:19:58 +0000217 xapp.Logger.Info("TestNewAlarmStoredAndPostedSucess")
Mohamed Abukar4e7e7122020-03-04 10:01:45 +0200218 ts := CreatePromAlertSimulator(t, "POST", "/api/v2/alerts", http.StatusOK, models.LabelSet{})
219 defer ts.Close()
220
Mohamed Abukar540ceee2020-09-09 08:07:40 +0300221 a := alarmer.NewAlarm(alarm.RIC_RT_DISTRIBUTION_FAILED, alarm.SeverityCritical, "Some App data", "eth 0 1")
Mohamed Abukar4e7e7122020-03-04 10:01:45 +0200222 assert.Nil(t, alarmer.Raise(a), "raise failed")
223
Mohamed Abukarebe58c22020-03-13 14:40:47 +0200224 VerifyAlarm(t, a, 1)
vipin01454d42020-11-10 08:13:51 +0000225
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)
233
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")
238 }
239
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)
247
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")
252 }
Mohamed Abukar4e7e7122020-03-04 10:01:45 +0200253}
254
255func TestAlarmClearedSucess(t *testing.T) {
vipin54a3a4f2020-09-23 12:19:58 +0000256 xapp.Logger.Info("TestAlarmClearedSucess")
Mohamed Abukar4e7e7122020-03-04 10:01:45 +0200257 ts := CreatePromAlertSimulator(t, "POST", "/api/v2/alerts", http.StatusOK, models.LabelSet{})
258 defer ts.Close()
259
260 // Raise the alarm
Mohamed Abukar540ceee2020-09-09 08:07:40 +0300261 a := alarmer.NewAlarm(alarm.RIC_RT_DISTRIBUTION_FAILED, alarm.SeverityCritical, "Some App data", "eth 0 1")
Mohamed Abukar4e7e7122020-03-04 10:01:45 +0200262 assert.Nil(t, alarmer.Raise(a), "raise failed")
263
Mohamed Abukarebe58c22020-03-13 14:40:47 +0200264 VerifyAlarm(t, a, 1)
Mohamed Abukar4e7e7122020-03-04 10:01:45 +0200265
266 // Now Clear the alarm and check alarm is removed
Mohamed Abukar540ceee2020-09-09 08:07:40 +0300267 a = alarmer.NewAlarm(alarm.RIC_RT_DISTRIBUTION_FAILED, alarm.SeverityCritical, "Some App data", "eth 0 1")
Mohamed Abukar4e7e7122020-03-04 10:01:45 +0200268 assert.Nil(t, alarmer.Clear(a), "clear failed")
269
270 time.Sleep(time.Duration(2) * time.Second)
Abukar Mohamed121e8b62020-09-18 11:41:33 +0000271 assert.Equal(t, len(alarmManager.activeAlarms), 0)
Mohamed Abukar4e7e7122020-03-04 10:01:45 +0200272}
273
274func TestMultipleAlarmsRaisedSucess(t *testing.T) {
vipin54a3a4f2020-09-23 12:19:58 +0000275 xapp.Logger.Info("TestMultipleAlarmsRaisedSucess")
Mohamed Abukar4e7e7122020-03-04 10:01:45 +0200276 ts := CreatePromAlertSimulator(t, "POST", "/api/v2/alerts", http.StatusOK, models.LabelSet{})
277 defer ts.Close()
278
279 // Raise two alarms
Mohamed Abukaraf0c5702020-03-11 10:29:40 +0200280 a := alarmer.NewAlarm(alarm.RIC_RT_DISTRIBUTION_FAILED, alarm.SeverityMajor, "Some App data", "eth 0 1")
Mohamed Abukar4e7e7122020-03-04 10:01:45 +0200281 assert.Nil(t, alarmer.Raise(a), "raise failed")
282
Mohamed Abukaraf0c5702020-03-11 10:29:40 +0200283 b := alarmer.NewAlarm(alarm.TCP_CONNECTIVITY_LOST_TO_DBAAS, alarm.SeverityMinor, "Hello", "abcd 11")
Mohamed Abukar4e7e7122020-03-04 10:01:45 +0200284 assert.Nil(t, alarmer.Raise(b), "raise failed")
285
Mohamed Abukar2d3cc6b2021-06-09 07:33:04 +0000286 time.Sleep(time.Duration(5) * time.Second)
287
288 xapp.Logger.Info("VerifyAlarm: %d %+v", len(alarmManager.activeAlarms), alarmManager.activeAlarms)
289 VerifyAlarm(t, a, 1)
290 xapp.Logger.Info("VerifyAlarm: %d %+v", len(alarmManager.activeAlarms), alarmManager.activeAlarms)
Mohamed Abukarebe58c22020-03-13 14:40:47 +0200291 VerifyAlarm(t, b, 2)
Mohamed Abukar4e7e7122020-03-04 10:01:45 +0200292}
293
294func TestMultipleAlarmsClearedSucess(t *testing.T) {
vipin54a3a4f2020-09-23 12:19:58 +0000295 xapp.Logger.Info("TestMultipleAlarmsClearedSucess")
Mohamed Abukar4e7e7122020-03-04 10:01:45 +0200296 ts := CreatePromAlertSimulator(t, "POST", "/api/v2/alerts", http.StatusOK, models.LabelSet{})
297 defer ts.Close()
298
299 // Raise two alarms
Mohamed Abukaraf0c5702020-03-11 10:29:40 +0200300 a := alarmer.NewAlarm(alarm.RIC_RT_DISTRIBUTION_FAILED, alarm.SeverityMajor, "Some App data", "eth 0 1")
Mohamed Abukar4e7e7122020-03-04 10:01:45 +0200301 assert.Nil(t, alarmer.Clear(a), "clear failed")
302
Mohamed Abukaraf0c5702020-03-11 10:29:40 +0200303 b := alarmer.NewAlarm(alarm.TCP_CONNECTIVITY_LOST_TO_DBAAS, alarm.SeverityMinor, "Hello", "abcd 11")
Mohamed Abukar4e7e7122020-03-04 10:01:45 +0200304 assert.Nil(t, alarmer.Clear(b), "clear failed")
305
306 time.Sleep(time.Duration(2) * time.Second)
Abukar Mohamed121e8b62020-09-18 11:41:33 +0000307 assert.Equal(t, len(alarmManager.activeAlarms), 0)
Mohamed Abukar4e7e7122020-03-04 10:01:45 +0200308}
309
310func TestAlarmsSuppresedSucess(t *testing.T) {
vipin54a3a4f2020-09-23 12:19:58 +0000311 xapp.Logger.Info("TestAlarmsSuppresedSucess")
Mohamed Abukar4e7e7122020-03-04 10:01:45 +0200312 ts := CreatePromAlertSimulator(t, "POST", "/api/v2/alerts", http.StatusOK, models.LabelSet{})
313 defer ts.Close()
314
315 // Raise two similar/matching alarms ... the second one suppresed
Mohamed Abukaraf0c5702020-03-11 10:29:40 +0200316 a := alarmer.NewAlarm(alarm.RIC_RT_DISTRIBUTION_FAILED, alarm.SeverityMajor, "Some App data", "eth 0 1")
Mohamed Abukar4e7e7122020-03-04 10:01:45 +0200317 assert.Nil(t, alarmer.Raise(a), "raise failed")
318 assert.Nil(t, alarmer.Raise(a), "raise failed")
319
Mohamed Abukarebe58c22020-03-13 14:40:47 +0200320 VerifyAlarm(t, a, 1)
vipin541eb502020-09-22 12:04:59 +0000321 assert.Nil(t, alarmer.Clear(a), "clear failed")
Mohamed Abukar4e7e7122020-03-04 10:01:45 +0200322}
323
324func TestInvalidAlarms(t *testing.T) {
vipin54a3a4f2020-09-23 12:19:58 +0000325 xapp.Logger.Info("TestInvalidAlarms")
Mohamed Abukar4e7e7122020-03-04 10:01:45 +0200326 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)
329}
330
331func TestAlarmHandlingErrorCases(t *testing.T) {
vipin54a3a4f2020-09-23 12:19:58 +0000332 xapp.Logger.Info("TestAlarmHandlingErrorCases")
Abukar Mohamed121e8b62020-09-18 11:41:33 +0000333 ok, err := alarmManager.HandleAlarms(&xapp.RMRParams{})
Mohamed Abukar4e7e7122020-03-04 10:01:45 +0200334 assert.Equal(t, err.Error(), "unexpected end of JSON input")
335 assert.Nil(t, ok, "raise failed")
336}
337
338func TestConsumeUnknownMessage(t *testing.T) {
vipin54a3a4f2020-09-23 12:19:58 +0000339 xapp.Logger.Info("TestConsumeUnknownMessage")
Abukar Mohamed121e8b62020-09-18 11:41:33 +0000340 err := alarmManager.Consume(&xapp.RMRParams{})
Mohamed Abukar4e7e7122020-03-04 10:01:45 +0200341 assert.Nil(t, err, "raise failed")
342}
343
344func TestStatusCallback(t *testing.T) {
vipin54a3a4f2020-09-23 12:19:58 +0000345 xapp.Logger.Info("TestStatusCallback")
Abukar Mohamed121e8b62020-09-18 11:41:33 +0000346 assert.Equal(t, true, alarmManager.StatusCB())
Mohamed Abukar4e7e7122020-03-04 10:01:45 +0200347}
348
vipin541eb502020-09-22 12:04:59 +0000349func 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
354
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")
357
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)
363
364 // Check HTTP Status Code
365 checkResponseCode(t, http.StatusOK, response.Code)
366
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")
371 }
372
373 time.Sleep(time.Duration(1) * time.Second)
374 alarmManager.maxActiveAlarms = 5000
375 alarmManager.maxAlarmHistory = 20000
376 VerifyAlarm(t, a, 2)
377 VerifyAlarm(t, a, 2)
378 ts.Close()
379}
380
Anssi Mannila00894a42020-10-19 11:36:26 +0300381func 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")
385
386 commandReady := make(chan bool, 1)
387 command := "cli/alarm-cli"
Anssi Mannila43fe17e2020-11-05 14:06:54 +0200388 args := []string{"alerts", "--active", "true", "--inhibited", "true", "--silenced", "--unprocessed", "true", "true", "--host", "localhost", "--port", "9093", "flushall"}
Anssi Mannila00894a42020-10-19 11:36:26 +0300389 ExecCLICommand(commandReady, command, args...)
390 <-commandReady
391
392 ts.Close()
393}
394
Anssi Mannila18fd03c2020-10-29 10:01:00 +0200395func TestDelayedAlarmRaiseAndClear(t *testing.T) {
396 xapp.Logger.Info("TestDelayedAlarmRaiseAndClear")
397
398 activeAlarmsBeforeTest := len(alarmManager.activeAlarms)
399 alarmHistoryBeforeTest := len(alarmManager.alarmHistory)
400
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)
415
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)
423
424 ts := CreatePromAlertSimulator(t, "POST", "/api/v2/alerts", http.StatusOK, models.LabelSet{})
425 defer ts.Close()
426
Mohamed Abukar2336a842020-10-30 16:19:38 +0200427 // Raise alarm. Posting alert and updating alarm history should be delayed
Anssi Mannila18fd03c2020-10-29 10:01:00 +0200428 a := alarmer.NewAlarm(9999, alarm.SeverityCritical, "Some App data", "eth 0 1")
429 assert.Nil(t, alarmer.Raise(a), "raise failed")
Mohamed Abukar2336a842020-10-30 16:19:38 +0200430 VerifyAlarm(t, a, activeAlarmsBeforeTest+1)
Anssi Mannila18fd03c2020-10-29 10:01:00 +0200431
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")
434
435 time.Sleep(time.Duration(2) * time.Second)
436 assert.Equal(t, len(alarmManager.activeAlarms), activeAlarmsBeforeTest)
Mohamed Abukar2336a842020-10-30 16:19:38 +0200437 assert.Equal(t, len(alarmManager.alarmHistory), alarmHistoryBeforeTest+2)
Anssi Mannila18fd03c2020-10-29 10:01:00 +0200438}
439
440func TestDelayedAlarmRaiseAndClear2(t *testing.T) {
441 xapp.Logger.Info("TestDelayedAlarmRaiseAndClear2")
442
443 activeAlarmsBeforeTest := len(alarmManager.activeAlarms)
444 alarmHistoryBeforeTest := len(alarmManager.alarmHistory)
445
446 ts := CreatePromAlertSimulator(t, "POST", "/api/v2/alerts", http.StatusOK, models.LabelSet{})
447 defer ts.Close()
448
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")
Mohamed Abukar2336a842020-10-30 16:19:38 +0200452 VerifyAlarm(t, a, activeAlarmsBeforeTest+1)
Anssi Mannila18fd03c2020-10-29 10:01:00 +0200453
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")
Mohamed Abukar2336a842020-10-30 16:19:38 +0200456 VerifyAlarm(t, b, activeAlarmsBeforeTest+2)
Anssi Mannila18fd03c2020-10-29 10:01:00 +0200457
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")
461
462 time.Sleep(time.Duration(2) * time.Second)
463 assert.Equal(t, len(alarmManager.activeAlarms), activeAlarmsBeforeTest)
Mohamed Abukar2336a842020-10-30 16:19:38 +0200464 assert.Equal(t, len(alarmManager.alarmHistory), alarmHistoryBeforeTest+4)
Anssi Mannila18fd03c2020-10-29 10:01:00 +0200465}
466
467func TestDelayedAlarmRaiseAndClear3(t *testing.T) {
468 xapp.Logger.Info("TestDelayedAlarmRaiseAndClear3")
469
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)
477
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)
492
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)
500
501 activeAlarmsBeforeTest := len(alarmManager.activeAlarms)
502 alarmHistoryBeforeTest := len(alarmManager.alarmHistory)
503
504 ts := CreatePromAlertSimulator(t, "POST", "/api/v2/alerts", http.StatusOK, models.LabelSet{})
505 defer ts.Close()
506
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")
Mohamed Abukar2336a842020-10-30 16:19:38 +0200510 VerifyAlarm(t, a, activeAlarmsBeforeTest+1)
Anssi Mannila18fd03c2020-10-29 10:01:00 +0200511
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")
Mohamed Abukar2336a842020-10-30 16:19:38 +0200514 VerifyAlarm(t, b, activeAlarmsBeforeTest+2)
Anssi Mannila18fd03c2020-10-29 10:01:00 +0200515
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")
519
520 time.Sleep(time.Duration(2) * time.Second)
521 assert.Equal(t, len(alarmManager.activeAlarms), activeAlarmsBeforeTest)
Mohamed Abukar2336a842020-10-30 16:19:38 +0200522 assert.Equal(t, len(alarmManager.alarmHistory), alarmHistoryBeforeTest+4)
Anssi Mannila18fd03c2020-10-29 10:01:00 +0200523}
524
Mohamed Abukar3649fae2020-10-30 23:51:39 +0200525func TestClearExpiredAlarms(t *testing.T) {
526 xapp.Logger.Info("TestClearExpiredAlarms")
527
528 a := alarm.AlarmMessage{
529 Alarm: alarmer.NewAlarm(8007, alarm.SeverityWarning, "threshold", ""),
530 AlarmAction: alarm.AlarmActionRaise,
531 AlarmTime: time.Now().UnixNano(),
532 }
533 d := alarm.RICAlarmDefinitions[8007]
534 n := AlarmNotification{a, *d}
535 alarmManager.activeAlarms = make([]AlarmNotification, 0)
536 alarmManager.UpdateActiveAlarmList(&n)
537
538 // Unknown SP
539 a.Alarm.SpecificProblem = 1234
540 assert.False(t, alarmManager.ClearExpiredAlarms(n, 0, false), "ClearExpiredAlarms failed")
541
542 // TTL is 0
543 d.TimeToLive = 0
544 assert.False(t, alarmManager.ClearExpiredAlarms(n, 0, false), "ClearExpiredAlarms failed")
545
546 // TTL not expired
547 a.Alarm.SpecificProblem = 8007
548 d.TimeToLive = 2
549 assert.False(t, alarmManager.ClearExpiredAlarms(n, 0, false), "ClearExpiredAlarms failed")
550
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)
556}
557
vipin01454d42020-11-10 08:13:51 +0000558func TestSetAlarmConfig(t *testing.T) {
559 xapp.Logger.Info("TestSetAlarmConfig")
560
561 var setAlarmConfig alarm.AlarmConfigParams
562 setAlarmConfig.MaxActiveAlarms = 500
563 setAlarmConfig.MaxAlarmHistory = 2000
564
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)
570
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)
577
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")
582 }
583
584 // Revert ot default
585 setAlarmConfig.MaxActiveAlarms = 5000
586 setAlarmConfig.MaxAlarmHistory = 20000
587
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)
593
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)
599
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")
604 }
605}
606
607func TestConfigChangeCB(t *testing.T) {
608 xapp.Logger.Info("TestConfigChangeCB")
609 alarmManager.ConfigChangeCB("AlarmManager")
610}
611
Mohamed Abukarebe58c22020-03-13 14:40:47 +0200612func VerifyAlarm(t *testing.T, a alarm.Alarm, expectedCount int) string {
Mohamed Abukar4e7e7122020-03-04 10:01:45 +0200613 receivedAlert := waitForEvent()
614
vipin6f73fa32020-10-06 06:51:53 +0000615 assert.Equal(t, expectedCount, len(alarmManager.activeAlarms))
Abukar Mohamed121e8b62020-09-18 11:41:33 +0000616 _, ok := alarmManager.IsMatchFound(a)
Mohamed Abukarebe58c22020-03-13 14:40:47 +0200617 assert.True(t, ok)
Mohamed Abukar4e7e7122020-03-04 10:01:45 +0200618
619 return receivedAlert
620}
621
622func VerifyAlert(t *testing.T, receivedAlert, expectedAlert string) {
623 receivedAlert = strings.Replace(fmt.Sprintf("%v", receivedAlert), "\r\n", " ", -1)
624 //assert.Equal(t, receivedAlert, e)
625}
626
627func CreatePromAlertSimulator(t *testing.T, method, url string, status int, respData interface{}) *httptest.Server {
628 l, err := net.Listen("tcp", "localhost:9093")
629 if err != nil {
630 t.Error("Failed to create listener: " + err.Error())
631 }
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)
635
636 fireEvent(t, r.Body)
637
638 w.Header().Add("Content-Type", "application/json")
639 w.WriteHeader(status)
640 b, _ := json.Marshal(respData)
641 w.Write(b)
642 }))
643 ts.Listener.Close()
644 ts.Listener = l
645
646 ts.Start()
647
648 return ts
649}
650
Anssi Mannila00894a42020-10-19 11:36:26 +0300651func CreatePromAlertSimulator2(t *testing.T, method, url string) *httptest.Server {
652 l, err := net.Listen("tcp", "localhost:9093")
653 if err != nil {
654 t.Error("Failed to create listener: " + err.Error())
655 }
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)
659
660 w.Header().Add("Content-Type", "application/json")
661 w.WriteHeader(200)
662 // Read alerts from file
663 payload, err := readJSONFromFile("../testresources/prometheus-alerts.json")
Mohamed Abukarf5a8e712020-10-19 16:58:17 +0300664 if err != nil {
665 t.Error("Failed to send response: ", err)
Anssi Mannila00894a42020-10-19 11:36:26 +0300666 }
667 _, err = w.Write(payload)
668 if err != nil {
669 t.Error("Failed to send response: " + err.Error())
670 }
671 }))
672 ts.Listener.Close()
673 ts.Listener = l
674 ts.Start()
675 return ts
676}
677
Mohamed Abukar4e7e7122020-03-04 10:01:45 +0200678func waitForEvent() string {
679 receivedAlert := <-eventChan
680 return receivedAlert
681}
682
683func 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")
687
688 eventChan <- fmt.Sprintf("%s", reqBody)
689}
vipin541eb502020-09-22 12:04:59 +0000690
691func executeRequest(req *http.Request, handleR http.HandlerFunc) *httptest.ResponseRecorder {
692 rr := httptest.NewRecorder()
693
694 handleR.ServeHTTP(rr, req)
695
696 return rr
697}
698
699func checkResponseCode(t *testing.T, expected, actual int) bool {
700 if expected != actual {
701 t.Errorf("Expected response code %d. Got %d\n", expected, actual)
702 return false
703 }
704 return true
705}
Anssi Mannila00894a42020-10-19 11:36:26 +0300706
707func ExecCLICommand(commandReady chan bool, command string, args ...string) {
708 go func() {
709 xapp.Logger.Info("Giving CLI command")
710 cmd := exec.Command(command, args...)
711 cmd.Dir = "../"
712 output, err := cmd.CombinedOutput()
713 if err != nil {
714 xapp.Logger.Info("CLI command failed out: %s", err)
715 }
716 xapp.Logger.Info("CLI command output: %s", output)
717 commandReady <- true
718 xapp.Logger.Info("CLI command completed")
719 }()
720}
721
722func readJSONFromFile(filename string) ([]byte, error) {
723 file, err := ioutil.ReadFile(filename)
724 if err != nil {
725 err := fmt.Errorf("readJSONFromFile() failed: Error: %v", err)
726 return nil, err
727 }
728 return file, nil
729}