Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 1 | /* |
| 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 | |
| 21 | package main |
| 22 | |
| 23 | import ( |
vipin | 4cedd50 | 2020-09-25 05:58:31 +0000 | [diff] [blame] | 24 | "bytes" |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 25 | "encoding/json" |
| 26 | "fmt" |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 27 | "io" |
| 28 | "io/ioutil" |
| 29 | "net" |
| 30 | "net/http" |
| 31 | "net/http/httptest" |
| 32 | "os" |
Anssi Mannila | 00894a4 | 2020-10-19 11:36:26 +0300 | [diff] [blame] | 33 | "os/exec" |
vipin | 4cedd50 | 2020-09-25 05:58:31 +0000 | [diff] [blame] | 34 | "strconv" |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 35 | "strings" |
| 36 | "testing" |
| 37 | "time" |
Mohamed Abukar | 2d3cc6b | 2021-06-09 07:33:04 +0000 | [diff] [blame^] | 38 | |
| 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 Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 44 | ) |
| 45 | |
Abukar Mohamed | 121e8b6 | 2020-09-18 11:41:33 +0000 | [diff] [blame] | 46 | var alarmManager *AlarmManager |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 47 | var alarmer *alarm.RICAlarm |
| 48 | var eventChan chan string |
| 49 | |
| 50 | // Test cases |
| 51 | func TestMain(M *testing.M) { |
Mohamed Abukar | 105030f | 2020-10-22 18:08:34 +0300 | [diff] [blame] | 52 | alarmManager = NewAlarmManager("localhost:9093", 500, false) |
vipin | 6f73fa3 | 2020-10-06 06:51:53 +0000 | [diff] [blame] | 53 | alarmManager.alertInterval = 20000 |
Mohamed Abukar | 3649fae | 2020-10-30 23:51:39 +0200 | [diff] [blame] | 54 | go alarmManager.Run(false, 5) |
vipin | 14323a9 | 2020-09-25 10:03:43 +0000 | [diff] [blame] | 55 | time.Sleep(time.Duration(10) * time.Second) |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 56 | |
| 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") |
vipin | 541eb50 | 2020-09-22 12:04:59 +0000 | [diff] [blame] | 63 | alarmManager.alarmClient = alarmer |
Mohamed Abukar | eac4451 | 2020-03-31 09:46:04 +0300 | [diff] [blame] | 64 | time.Sleep(time.Duration(5) * time.Second) |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 65 | eventChan = make(chan string) |
| 66 | |
| 67 | os.Exit(M.Run()) |
| 68 | } |
| 69 | |
vipin | 14323a9 | 2020-09-25 10:03:43 +0000 | [diff] [blame] | 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") |
| 83 | } |
| 84 | } |
| 85 | |
vipin | 54a3a4f | 2020-09-23 12:19:58 +0000 | [diff] [blame] | 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" |
Anssi Mannila | 18fd03c | 2020-10-29 10:01:00 +0200 | [diff] [blame] | 93 | alarm8004Definition.RaiseDelay = 0 |
| 94 | alarm8004Definition.ClearDelay = 0 |
vipin | 54a3a4f | 2020-09-23 12:19:58 +0000 | [diff] [blame] | 95 | |
| 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 Mannila | 18fd03c | 2020-10-29 10:01:00 +0200 | [diff] [blame] | 101 | alarm8005Definition.RaiseDelay = 0 |
| 102 | alarm8005Definition.ClearDelay = 0 |
vipin | 54a3a4f | 2020-09-23 12:19:58 +0000 | [diff] [blame] | 103 | |
| 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 Mannila | 18fd03c | 2020-10-29 10:01:00 +0200 | [diff] [blame] | 109 | alarm8006Definition.RaiseDelay = 0 |
| 110 | alarm8006Definition.ClearDelay = 0 |
vipin | 54a3a4f | 2020-09-23 12:19:58 +0000 | [diff] [blame] | 111 | |
| 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 Mannila | 18fd03c | 2020-10-29 10:01:00 +0200 | [diff] [blame] | 117 | alarm8007Definition.RaiseDelay = 0 |
| 118 | alarm8007Definition.ClearDelay = 0 |
vipin | 54a3a4f | 2020-09-23 12:19:58 +0000 | [diff] [blame] | 119 | |
| 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" |
vipin | 14323a9 | 2020-09-25 10:03:43 +0000 | [diff] [blame] | 124 | alarm8008Definition.OperationInstructions = "Clear alarms or raise threshold" |
Anssi Mannila | 18fd03c | 2020-10-29 10:01:00 +0200 | [diff] [blame] | 125 | alarm8008Definition.RaiseDelay = 0 |
| 126 | alarm8008Definition.ClearDelay = 0 |
vipin | 54a3a4f | 2020-09-23 12:19:58 +0000 | [diff] [blame] | 127 | |
| 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" |
vipin | 14323a9 | 2020-09-25 10:03:43 +0000 | [diff] [blame] | 132 | alarm8009Definition.OperationInstructions = "Clear alarms or raise threshold" |
Anssi Mannila | 18fd03c | 2020-10-29 10:01:00 +0200 | [diff] [blame] | 133 | alarm8009Definition.RaiseDelay = 0 |
| 134 | alarm8009Definition.ClearDelay = 0 |
vipin | 54a3a4f | 2020-09-23 12:19:58 +0000 | [diff] [blame] | 135 | |
| 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 | |
| 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") |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | func 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) |
vipin | 4cedd50 | 2020-09-25 05:58:31 +0000 | [diff] [blame] | 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) |
vipin | 54a3a4f | 2020-09-23 12:19:58 +0000 | [diff] [blame] | 183 | |
| 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 Mannila | 18fd03c | 2020-10-29 10:01:00 +0200 | [diff] [blame] | 198 | alarm8004Definition.RaiseDelay = 0 |
| 199 | alarm8004Definition.ClearDelay = 0 |
vipin | 54a3a4f | 2020-09-23 12:19:58 +0000 | [diff] [blame] | 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) |
| 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 Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 216 | func TestNewAlarmStoredAndPostedSucess(t *testing.T) { |
vipin | 54a3a4f | 2020-09-23 12:19:58 +0000 | [diff] [blame] | 217 | xapp.Logger.Info("TestNewAlarmStoredAndPostedSucess") |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 218 | ts := CreatePromAlertSimulator(t, "POST", "/api/v2/alerts", http.StatusOK, models.LabelSet{}) |
| 219 | defer ts.Close() |
| 220 | |
Mohamed Abukar | 540ceee | 2020-09-09 08:07:40 +0300 | [diff] [blame] | 221 | a := alarmer.NewAlarm(alarm.RIC_RT_DISTRIBUTION_FAILED, alarm.SeverityCritical, "Some App data", "eth 0 1") |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 222 | assert.Nil(t, alarmer.Raise(a), "raise failed") |
| 223 | |
Mohamed Abukar | ebe58c2 | 2020-03-13 14:40:47 +0200 | [diff] [blame] | 224 | VerifyAlarm(t, a, 1) |
vipin | 01454d4 | 2020-11-10 08:13:51 +0000 | [diff] [blame] | 225 | |
| 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 Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | func TestAlarmClearedSucess(t *testing.T) { |
vipin | 54a3a4f | 2020-09-23 12:19:58 +0000 | [diff] [blame] | 256 | xapp.Logger.Info("TestAlarmClearedSucess") |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 257 | ts := CreatePromAlertSimulator(t, "POST", "/api/v2/alerts", http.StatusOK, models.LabelSet{}) |
| 258 | defer ts.Close() |
| 259 | |
| 260 | // Raise the alarm |
Mohamed Abukar | 540ceee | 2020-09-09 08:07:40 +0300 | [diff] [blame] | 261 | a := alarmer.NewAlarm(alarm.RIC_RT_DISTRIBUTION_FAILED, alarm.SeverityCritical, "Some App data", "eth 0 1") |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 262 | assert.Nil(t, alarmer.Raise(a), "raise failed") |
| 263 | |
Mohamed Abukar | ebe58c2 | 2020-03-13 14:40:47 +0200 | [diff] [blame] | 264 | VerifyAlarm(t, a, 1) |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 265 | |
| 266 | // Now Clear the alarm and check alarm is removed |
Mohamed Abukar | 540ceee | 2020-09-09 08:07:40 +0300 | [diff] [blame] | 267 | a = alarmer.NewAlarm(alarm.RIC_RT_DISTRIBUTION_FAILED, alarm.SeverityCritical, "Some App data", "eth 0 1") |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 268 | assert.Nil(t, alarmer.Clear(a), "clear failed") |
| 269 | |
| 270 | time.Sleep(time.Duration(2) * time.Second) |
Abukar Mohamed | 121e8b6 | 2020-09-18 11:41:33 +0000 | [diff] [blame] | 271 | assert.Equal(t, len(alarmManager.activeAlarms), 0) |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | func TestMultipleAlarmsRaisedSucess(t *testing.T) { |
vipin | 54a3a4f | 2020-09-23 12:19:58 +0000 | [diff] [blame] | 275 | xapp.Logger.Info("TestMultipleAlarmsRaisedSucess") |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 276 | ts := CreatePromAlertSimulator(t, "POST", "/api/v2/alerts", http.StatusOK, models.LabelSet{}) |
| 277 | defer ts.Close() |
| 278 | |
| 279 | // Raise two alarms |
Mohamed Abukar | af0c570 | 2020-03-11 10:29:40 +0200 | [diff] [blame] | 280 | a := alarmer.NewAlarm(alarm.RIC_RT_DISTRIBUTION_FAILED, alarm.SeverityMajor, "Some App data", "eth 0 1") |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 281 | assert.Nil(t, alarmer.Raise(a), "raise failed") |
| 282 | |
Mohamed Abukar | af0c570 | 2020-03-11 10:29:40 +0200 | [diff] [blame] | 283 | b := alarmer.NewAlarm(alarm.TCP_CONNECTIVITY_LOST_TO_DBAAS, alarm.SeverityMinor, "Hello", "abcd 11") |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 284 | assert.Nil(t, alarmer.Raise(b), "raise failed") |
| 285 | |
Mohamed Abukar | 2d3cc6b | 2021-06-09 07:33:04 +0000 | [diff] [blame^] | 286 | 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 Abukar | ebe58c2 | 2020-03-13 14:40:47 +0200 | [diff] [blame] | 291 | VerifyAlarm(t, b, 2) |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | func TestMultipleAlarmsClearedSucess(t *testing.T) { |
vipin | 54a3a4f | 2020-09-23 12:19:58 +0000 | [diff] [blame] | 295 | xapp.Logger.Info("TestMultipleAlarmsClearedSucess") |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 296 | ts := CreatePromAlertSimulator(t, "POST", "/api/v2/alerts", http.StatusOK, models.LabelSet{}) |
| 297 | defer ts.Close() |
| 298 | |
| 299 | // Raise two alarms |
Mohamed Abukar | af0c570 | 2020-03-11 10:29:40 +0200 | [diff] [blame] | 300 | a := alarmer.NewAlarm(alarm.RIC_RT_DISTRIBUTION_FAILED, alarm.SeverityMajor, "Some App data", "eth 0 1") |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 301 | assert.Nil(t, alarmer.Clear(a), "clear failed") |
| 302 | |
Mohamed Abukar | af0c570 | 2020-03-11 10:29:40 +0200 | [diff] [blame] | 303 | b := alarmer.NewAlarm(alarm.TCP_CONNECTIVITY_LOST_TO_DBAAS, alarm.SeverityMinor, "Hello", "abcd 11") |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 304 | assert.Nil(t, alarmer.Clear(b), "clear failed") |
| 305 | |
| 306 | time.Sleep(time.Duration(2) * time.Second) |
Abukar Mohamed | 121e8b6 | 2020-09-18 11:41:33 +0000 | [diff] [blame] | 307 | assert.Equal(t, len(alarmManager.activeAlarms), 0) |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | func TestAlarmsSuppresedSucess(t *testing.T) { |
vipin | 54a3a4f | 2020-09-23 12:19:58 +0000 | [diff] [blame] | 311 | xapp.Logger.Info("TestAlarmsSuppresedSucess") |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 312 | 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 Abukar | af0c570 | 2020-03-11 10:29:40 +0200 | [diff] [blame] | 316 | a := alarmer.NewAlarm(alarm.RIC_RT_DISTRIBUTION_FAILED, alarm.SeverityMajor, "Some App data", "eth 0 1") |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 317 | assert.Nil(t, alarmer.Raise(a), "raise failed") |
| 318 | assert.Nil(t, alarmer.Raise(a), "raise failed") |
| 319 | |
Mohamed Abukar | ebe58c2 | 2020-03-13 14:40:47 +0200 | [diff] [blame] | 320 | VerifyAlarm(t, a, 1) |
vipin | 541eb50 | 2020-09-22 12:04:59 +0000 | [diff] [blame] | 321 | assert.Nil(t, alarmer.Clear(a), "clear failed") |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | func TestInvalidAlarms(t *testing.T) { |
vipin | 54a3a4f | 2020-09-23 12:19:58 +0000 | [diff] [blame] | 325 | xapp.Logger.Info("TestInvalidAlarms") |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 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) |
| 329 | } |
| 330 | |
| 331 | func TestAlarmHandlingErrorCases(t *testing.T) { |
vipin | 54a3a4f | 2020-09-23 12:19:58 +0000 | [diff] [blame] | 332 | xapp.Logger.Info("TestAlarmHandlingErrorCases") |
Abukar Mohamed | 121e8b6 | 2020-09-18 11:41:33 +0000 | [diff] [blame] | 333 | ok, err := alarmManager.HandleAlarms(&xapp.RMRParams{}) |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 334 | assert.Equal(t, err.Error(), "unexpected end of JSON input") |
| 335 | assert.Nil(t, ok, "raise failed") |
| 336 | } |
| 337 | |
| 338 | func TestConsumeUnknownMessage(t *testing.T) { |
vipin | 54a3a4f | 2020-09-23 12:19:58 +0000 | [diff] [blame] | 339 | xapp.Logger.Info("TestConsumeUnknownMessage") |
Abukar Mohamed | 121e8b6 | 2020-09-18 11:41:33 +0000 | [diff] [blame] | 340 | err := alarmManager.Consume(&xapp.RMRParams{}) |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 341 | assert.Nil(t, err, "raise failed") |
| 342 | } |
| 343 | |
| 344 | func TestStatusCallback(t *testing.T) { |
vipin | 54a3a4f | 2020-09-23 12:19:58 +0000 | [diff] [blame] | 345 | xapp.Logger.Info("TestStatusCallback") |
Abukar Mohamed | 121e8b6 | 2020-09-18 11:41:33 +0000 | [diff] [blame] | 346 | assert.Equal(t, true, alarmManager.StatusCB()) |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 347 | } |
| 348 | |
vipin | 541eb50 | 2020-09-22 12:04:59 +0000 | [diff] [blame] | 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 |
| 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 Mannila | 00894a4 | 2020-10-19 11:36:26 +0300 | [diff] [blame] | 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") |
| 385 | |
| 386 | commandReady := make(chan bool, 1) |
| 387 | command := "cli/alarm-cli" |
Anssi Mannila | 43fe17e | 2020-11-05 14:06:54 +0200 | [diff] [blame] | 388 | args := []string{"alerts", "--active", "true", "--inhibited", "true", "--silenced", "--unprocessed", "true", "true", "--host", "localhost", "--port", "9093", "flushall"} |
Anssi Mannila | 00894a4 | 2020-10-19 11:36:26 +0300 | [diff] [blame] | 389 | ExecCLICommand(commandReady, command, args...) |
| 390 | <-commandReady |
| 391 | |
| 392 | ts.Close() |
| 393 | } |
| 394 | |
Anssi Mannila | 18fd03c | 2020-10-29 10:01:00 +0200 | [diff] [blame] | 395 | func 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 Abukar | 2336a84 | 2020-10-30 16:19:38 +0200 | [diff] [blame] | 427 | // Raise alarm. Posting alert and updating alarm history should be delayed |
Anssi Mannila | 18fd03c | 2020-10-29 10:01:00 +0200 | [diff] [blame] | 428 | a := alarmer.NewAlarm(9999, alarm.SeverityCritical, "Some App data", "eth 0 1") |
| 429 | assert.Nil(t, alarmer.Raise(a), "raise failed") |
Mohamed Abukar | 2336a84 | 2020-10-30 16:19:38 +0200 | [diff] [blame] | 430 | VerifyAlarm(t, a, activeAlarmsBeforeTest+1) |
Anssi Mannila | 18fd03c | 2020-10-29 10:01:00 +0200 | [diff] [blame] | 431 | |
| 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 Abukar | 2336a84 | 2020-10-30 16:19:38 +0200 | [diff] [blame] | 437 | assert.Equal(t, len(alarmManager.alarmHistory), alarmHistoryBeforeTest+2) |
Anssi Mannila | 18fd03c | 2020-10-29 10:01:00 +0200 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | func 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 Abukar | 2336a84 | 2020-10-30 16:19:38 +0200 | [diff] [blame] | 452 | VerifyAlarm(t, a, activeAlarmsBeforeTest+1) |
Anssi Mannila | 18fd03c | 2020-10-29 10:01:00 +0200 | [diff] [blame] | 453 | |
| 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 Abukar | 2336a84 | 2020-10-30 16:19:38 +0200 | [diff] [blame] | 456 | VerifyAlarm(t, b, activeAlarmsBeforeTest+2) |
Anssi Mannila | 18fd03c | 2020-10-29 10:01:00 +0200 | [diff] [blame] | 457 | |
| 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 Abukar | 2336a84 | 2020-10-30 16:19:38 +0200 | [diff] [blame] | 464 | assert.Equal(t, len(alarmManager.alarmHistory), alarmHistoryBeforeTest+4) |
Anssi Mannila | 18fd03c | 2020-10-29 10:01:00 +0200 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | func 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 Abukar | 2336a84 | 2020-10-30 16:19:38 +0200 | [diff] [blame] | 510 | VerifyAlarm(t, a, activeAlarmsBeforeTest+1) |
Anssi Mannila | 18fd03c | 2020-10-29 10:01:00 +0200 | [diff] [blame] | 511 | |
| 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 Abukar | 2336a84 | 2020-10-30 16:19:38 +0200 | [diff] [blame] | 514 | VerifyAlarm(t, b, activeAlarmsBeforeTest+2) |
Anssi Mannila | 18fd03c | 2020-10-29 10:01:00 +0200 | [diff] [blame] | 515 | |
| 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 Abukar | 2336a84 | 2020-10-30 16:19:38 +0200 | [diff] [blame] | 522 | assert.Equal(t, len(alarmManager.alarmHistory), alarmHistoryBeforeTest+4) |
Anssi Mannila | 18fd03c | 2020-10-29 10:01:00 +0200 | [diff] [blame] | 523 | } |
| 524 | |
Mohamed Abukar | 3649fae | 2020-10-30 23:51:39 +0200 | [diff] [blame] | 525 | func 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 | |
vipin | 01454d4 | 2020-11-10 08:13:51 +0000 | [diff] [blame] | 558 | func 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 | |
| 607 | func TestConfigChangeCB(t *testing.T) { |
| 608 | xapp.Logger.Info("TestConfigChangeCB") |
| 609 | alarmManager.ConfigChangeCB("AlarmManager") |
| 610 | } |
| 611 | |
Mohamed Abukar | ebe58c2 | 2020-03-13 14:40:47 +0200 | [diff] [blame] | 612 | func VerifyAlarm(t *testing.T, a alarm.Alarm, expectedCount int) string { |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 613 | receivedAlert := waitForEvent() |
| 614 | |
vipin | 6f73fa3 | 2020-10-06 06:51:53 +0000 | [diff] [blame] | 615 | assert.Equal(t, expectedCount, len(alarmManager.activeAlarms)) |
Abukar Mohamed | 121e8b6 | 2020-09-18 11:41:33 +0000 | [diff] [blame] | 616 | _, ok := alarmManager.IsMatchFound(a) |
Mohamed Abukar | ebe58c2 | 2020-03-13 14:40:47 +0200 | [diff] [blame] | 617 | assert.True(t, ok) |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 618 | |
| 619 | return receivedAlert |
| 620 | } |
| 621 | |
| 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) |
| 625 | } |
| 626 | |
| 627 | func 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 Mannila | 00894a4 | 2020-10-19 11:36:26 +0300 | [diff] [blame] | 651 | func 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 Abukar | f5a8e71 | 2020-10-19 16:58:17 +0300 | [diff] [blame] | 664 | if err != nil { |
| 665 | t.Error("Failed to send response: ", err) |
Anssi Mannila | 00894a4 | 2020-10-19 11:36:26 +0300 | [diff] [blame] | 666 | } |
| 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 Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 678 | func waitForEvent() string { |
| 679 | receivedAlert := <-eventChan |
| 680 | return receivedAlert |
| 681 | } |
| 682 | |
| 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") |
| 687 | |
| 688 | eventChan <- fmt.Sprintf("%s", reqBody) |
| 689 | } |
vipin | 541eb50 | 2020-09-22 12:04:59 +0000 | [diff] [blame] | 690 | |
| 691 | func 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 | |
| 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) |
| 702 | return false |
| 703 | } |
| 704 | return true |
| 705 | } |
Anssi Mannila | 00894a4 | 2020-10-19 11:36:26 +0300 | [diff] [blame] | 706 | |
| 707 | func 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 | |
| 722 | func 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 | } |