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" |
vipin | 4cedd50 | 2020-09-25 05:58:31 +0000 | [diff] [blame^] | 27 | "gerrit.o-ran-sc.org/r/ric-plt/alarm-go/alarm" |
| 28 | "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp" |
| 29 | "github.com/gorilla/mux" |
| 30 | "github.com/prometheus/alertmanager/api/v2/models" |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 31 | "github.com/stretchr/testify/assert" |
| 32 | "io" |
| 33 | "io/ioutil" |
| 34 | "net" |
| 35 | "net/http" |
| 36 | "net/http/httptest" |
| 37 | "os" |
vipin | 4cedd50 | 2020-09-25 05:58:31 +0000 | [diff] [blame^] | 38 | "strconv" |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 39 | "strings" |
| 40 | "testing" |
| 41 | "time" |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 42 | ) |
| 43 | |
Abukar Mohamed | 121e8b6 | 2020-09-18 11:41:33 +0000 | [diff] [blame] | 44 | var alarmManager *AlarmManager |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 45 | var alarmer *alarm.RICAlarm |
| 46 | var eventChan chan string |
| 47 | |
| 48 | // Test cases |
| 49 | func TestMain(M *testing.M) { |
Mohamed Abukar | 540ceee | 2020-09-09 08:07:40 +0300 | [diff] [blame] | 50 | os.Setenv("ALARM_IF_RMR", "true") |
Abukar Mohamed | 121e8b6 | 2020-09-18 11:41:33 +0000 | [diff] [blame] | 51 | alarmManager = NewAlarmManager("localhost:9093", 500) |
| 52 | go alarmManager.Run(false) |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 53 | time.Sleep(time.Duration(2) * time.Second) |
| 54 | |
| 55 | // Wait until RMR is up-and-running |
| 56 | for !xapp.Rmr.IsReady() { |
| 57 | time.Sleep(time.Duration(1) * time.Second) |
| 58 | } |
| 59 | |
| 60 | alarmer, _ = alarm.InitAlarm("my-pod", "my-app") |
vipin | 541eb50 | 2020-09-22 12:04:59 +0000 | [diff] [blame] | 61 | alarmManager.alarmClient = alarmer |
Mohamed Abukar | eac4451 | 2020-03-31 09:46:04 +0300 | [diff] [blame] | 62 | time.Sleep(time.Duration(5) * time.Second) |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 63 | eventChan = make(chan string) |
| 64 | |
| 65 | os.Exit(M.Run()) |
| 66 | } |
| 67 | |
vipin | 54a3a4f | 2020-09-23 12:19:58 +0000 | [diff] [blame] | 68 | func TestSetAlarmDefinitions(t *testing.T) { |
| 69 | xapp.Logger.Info("TestSetAlarmDefinitions") |
| 70 | var alarm8004Definition alarm.AlarmDefinition |
| 71 | alarm8004Definition.AlarmId = alarm.RIC_RT_DISTRIBUTION_FAILED |
| 72 | alarm8004Definition.AlarmText = "RIC ROUTING TABLE DISTRIBUTION FAILED" |
| 73 | alarm8004Definition.EventType = "Processing error" |
| 74 | alarm8004Definition.OperationInstructions = "Not defined" |
| 75 | |
| 76 | var alarm8005Definition alarm.AlarmDefinition |
| 77 | alarm8005Definition.AlarmId = alarm.TCP_CONNECTIVITY_LOST_TO_DBAAS |
| 78 | alarm8005Definition.AlarmText = "TCP CONNECTIVITY LOST TO DBAAS" |
| 79 | alarm8005Definition.EventType = "Communication error" |
| 80 | alarm8005Definition.OperationInstructions = "Not defined" |
| 81 | |
| 82 | var alarm8006Definition alarm.AlarmDefinition |
| 83 | alarm8006Definition.AlarmId = alarm.E2_CONNECTIVITY_LOST_TO_GNODEB |
| 84 | alarm8006Definition.AlarmText = "E2 CONNECTIVITY LOST TO G-NODEB" |
| 85 | alarm8006Definition.EventType = "Communication error" |
| 86 | alarm8006Definition.OperationInstructions = "Not defined" |
| 87 | |
| 88 | var alarm8007Definition alarm.AlarmDefinition |
| 89 | alarm8007Definition.AlarmId = alarm.E2_CONNECTIVITY_LOST_TO_ENODEB |
| 90 | alarm8007Definition.AlarmText = "E2 CONNECTIVITY LOST TO E-NODEB" |
| 91 | alarm8007Definition.EventType = "Communication error" |
| 92 | alarm8007Definition.OperationInstructions = "Not defined" |
| 93 | |
| 94 | var alarm8008Definition alarm.AlarmDefinition |
| 95 | alarm8008Definition.AlarmId = alarm.ACTIVE_ALARM_EXCEED_MAX_THRESHOLD |
| 96 | alarm8008Definition.AlarmText = "ACTIVE ALARM EXCEED MAX THRESHOLD" |
| 97 | alarm8008Definition.EventType = "storage warning" |
| 98 | alarm8008Definition.OperationInstructions = "clear alarms or raise threshold" |
| 99 | |
| 100 | var alarm8009Definition alarm.AlarmDefinition |
| 101 | alarm8009Definition.AlarmId = alarm.ALARM_HISTORY_EXCEED_MAX_THRESHOLD |
| 102 | alarm8009Definition.AlarmText = "ALARM HISTORY EXCEED MAX THRESHOLD" |
| 103 | alarm8009Definition.EventType = "storage warning" |
| 104 | alarm8009Definition.OperationInstructions = "clear alarms or raise threshold" |
| 105 | |
| 106 | pbodyParams := RicAlarmDefinitions{AlarmDefinitions: []*alarm.AlarmDefinition{&alarm8004Definition, &alarm8005Definition, &alarm8006Definition, &alarm8007Definition, &alarm8008Definition, &alarm8009Definition}} |
| 107 | pbodyEn, _ := json.Marshal(pbodyParams) |
| 108 | req, _ := http.NewRequest("POST", "/ric/v1/alarms/define", bytes.NewBuffer(pbodyEn)) |
| 109 | handleFunc := http.HandlerFunc(alarmManager.SetAlarmDefinition) |
| 110 | response := executeRequest(req, handleFunc) |
| 111 | status := checkResponseCode(t, http.StatusOK, response.Code) |
| 112 | xapp.Logger.Info("status = %v", status) |
| 113 | |
| 114 | } |
| 115 | |
| 116 | func TestGetAlarmDefinitions(t *testing.T) { |
| 117 | xapp.Logger.Info("TestGetAlarmDefinitions") |
| 118 | var alarmDefinition alarm.AlarmDefinition |
| 119 | req, _ := http.NewRequest("GET", "/ric/v1/alarms/define", nil) |
| 120 | vars := map[string]string{"alarmId": strconv.FormatUint(8004, 10)} |
| 121 | req = mux.SetURLVars(req, vars) |
| 122 | handleFunc := http.HandlerFunc(alarmManager.GetAlarmDefinition) |
| 123 | response := executeRequest(req, handleFunc) |
| 124 | checkResponseCode(t, http.StatusOK, response.Code) |
| 125 | json.NewDecoder(response.Body).Decode(&alarmDefinition) |
| 126 | xapp.Logger.Info("alarm definition = %v", alarmDefinition) |
| 127 | if alarmDefinition.AlarmId != alarm.RIC_RT_DISTRIBUTION_FAILED || alarmDefinition.AlarmText != "RIC ROUTING TABLE DISTRIBUTION FAILED" { |
| 128 | t.Errorf("Incorrect alarm definition") |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | func TestDeleteAlarmDefinitions(t *testing.T) { |
| 133 | xapp.Logger.Info("TestDeleteAlarmDefinitions") |
| 134 | //Get all |
| 135 | var ricAlarmDefinitions RicAlarmDefinitions |
| 136 | req, _ := http.NewRequest("GET", "/ric/v1/alarms/define", nil) |
| 137 | req = mux.SetURLVars(req, nil) |
| 138 | handleFunc := http.HandlerFunc(alarmManager.GetAlarmDefinition) |
| 139 | response := executeRequest(req, handleFunc) |
| 140 | checkResponseCode(t, http.StatusOK, response.Code) |
| 141 | json.NewDecoder(response.Body).Decode(&ricAlarmDefinitions) |
| 142 | for _, alarmDefinition := range ricAlarmDefinitions.AlarmDefinitions { |
| 143 | xapp.Logger.Info("alarm definition = %v", *alarmDefinition) |
| 144 | } |
| 145 | |
| 146 | //Delete 8004 |
| 147 | req, _ = http.NewRequest("DELETE", "/ric/v1/alarms/define", nil) |
vipin | 4cedd50 | 2020-09-25 05:58:31 +0000 | [diff] [blame^] | 148 | vars := map[string]string{"alarmId": strconv.FormatUint(8004, 10)} |
| 149 | req = mux.SetURLVars(req, vars) |
| 150 | handleFunc = http.HandlerFunc(alarmManager.DeleteAlarmDefinition) |
| 151 | response = executeRequest(req, handleFunc) |
| 152 | checkResponseCode(t, http.StatusOK, response.Code) |
vipin | 54a3a4f | 2020-09-23 12:19:58 +0000 | [diff] [blame] | 153 | |
| 154 | //Get 8004 fail |
| 155 | req, _ = http.NewRequest("GET", "/ric/v1/alarms/define", nil) |
| 156 | vars = map[string]string{"alarmId": strconv.FormatUint(8004, 10)} |
| 157 | req = mux.SetURLVars(req, vars) |
| 158 | handleFunc = http.HandlerFunc(alarmManager.GetAlarmDefinition) |
| 159 | response = executeRequest(req, handleFunc) |
| 160 | checkResponseCode(t, http.StatusBadRequest, response.Code) |
| 161 | |
| 162 | //Set 8004 success |
| 163 | var alarm8004Definition alarm.AlarmDefinition |
| 164 | alarm8004Definition.AlarmId = alarm.RIC_RT_DISTRIBUTION_FAILED |
| 165 | alarm8004Definition.AlarmText = "RIC ROUTING TABLE DISTRIBUTION FAILED" |
| 166 | alarm8004Definition.EventType = "Processing error" |
| 167 | alarm8004Definition.OperationInstructions = "Not defined" |
| 168 | pbodyParams := RicAlarmDefinitions{AlarmDefinitions: []*alarm.AlarmDefinition{&alarm8004Definition}} |
| 169 | pbodyEn, _ := json.Marshal(pbodyParams) |
| 170 | req, _ = http.NewRequest("POST", "/ric/v1/alarms/define", bytes.NewBuffer(pbodyEn)) |
| 171 | handleFunc = http.HandlerFunc(alarmManager.SetAlarmDefinition) |
| 172 | response = executeRequest(req, handleFunc) |
| 173 | checkResponseCode(t, http.StatusOK, response.Code) |
| 174 | |
| 175 | //Get 8004 success |
| 176 | req, _ = http.NewRequest("GET", "/ric/v1/alarms/define", nil) |
| 177 | vars = map[string]string{"alarmId": strconv.FormatUint(8004, 10)} |
| 178 | req = mux.SetURLVars(req, vars) |
| 179 | handleFunc = http.HandlerFunc(alarmManager.GetAlarmDefinition) |
| 180 | response = executeRequest(req, handleFunc) |
| 181 | checkResponseCode(t, http.StatusOK, response.Code) |
| 182 | } |
| 183 | |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 184 | func TestNewAlarmStoredAndPostedSucess(t *testing.T) { |
vipin | 54a3a4f | 2020-09-23 12:19:58 +0000 | [diff] [blame] | 185 | xapp.Logger.Info("TestNewAlarmStoredAndPostedSucess") |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 186 | ts := CreatePromAlertSimulator(t, "POST", "/api/v2/alerts", http.StatusOK, models.LabelSet{}) |
| 187 | defer ts.Close() |
| 188 | |
Mohamed Abukar | 540ceee | 2020-09-09 08:07:40 +0300 | [diff] [blame] | 189 | 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] | 190 | assert.Nil(t, alarmer.Raise(a), "raise failed") |
| 191 | |
Mohamed Abukar | ebe58c2 | 2020-03-13 14:40:47 +0200 | [diff] [blame] | 192 | VerifyAlarm(t, a, 1) |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | func TestAlarmClearedSucess(t *testing.T) { |
vipin | 54a3a4f | 2020-09-23 12:19:58 +0000 | [diff] [blame] | 196 | xapp.Logger.Info("TestAlarmClearedSucess") |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 197 | ts := CreatePromAlertSimulator(t, "POST", "/api/v2/alerts", http.StatusOK, models.LabelSet{}) |
| 198 | defer ts.Close() |
| 199 | |
| 200 | // Raise the alarm |
Mohamed Abukar | 540ceee | 2020-09-09 08:07:40 +0300 | [diff] [blame] | 201 | 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] | 202 | assert.Nil(t, alarmer.Raise(a), "raise failed") |
| 203 | |
Mohamed Abukar | ebe58c2 | 2020-03-13 14:40:47 +0200 | [diff] [blame] | 204 | VerifyAlarm(t, a, 1) |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 205 | |
| 206 | // Now Clear the alarm and check alarm is removed |
Mohamed Abukar | 540ceee | 2020-09-09 08:07:40 +0300 | [diff] [blame] | 207 | 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] | 208 | assert.Nil(t, alarmer.Clear(a), "clear failed") |
| 209 | |
| 210 | time.Sleep(time.Duration(2) * time.Second) |
Abukar Mohamed | 121e8b6 | 2020-09-18 11:41:33 +0000 | [diff] [blame] | 211 | assert.Equal(t, len(alarmManager.activeAlarms), 0) |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | func TestMultipleAlarmsRaisedSucess(t *testing.T) { |
vipin | 54a3a4f | 2020-09-23 12:19:58 +0000 | [diff] [blame] | 215 | xapp.Logger.Info("TestMultipleAlarmsRaisedSucess") |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 216 | ts := CreatePromAlertSimulator(t, "POST", "/api/v2/alerts", http.StatusOK, models.LabelSet{}) |
| 217 | defer ts.Close() |
| 218 | |
| 219 | // Raise two alarms |
Mohamed Abukar | af0c570 | 2020-03-11 10:29:40 +0200 | [diff] [blame] | 220 | 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] | 221 | assert.Nil(t, alarmer.Raise(a), "raise failed") |
| 222 | |
Mohamed Abukar | af0c570 | 2020-03-11 10:29:40 +0200 | [diff] [blame] | 223 | 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] | 224 | assert.Nil(t, alarmer.Raise(b), "raise failed") |
| 225 | |
Mohamed Abukar | ebe58c2 | 2020-03-13 14:40:47 +0200 | [diff] [blame] | 226 | VerifyAlarm(t, a, 2) |
| 227 | VerifyAlarm(t, b, 2) |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | func TestMultipleAlarmsClearedSucess(t *testing.T) { |
vipin | 54a3a4f | 2020-09-23 12:19:58 +0000 | [diff] [blame] | 231 | xapp.Logger.Info("TestMultipleAlarmsClearedSucess") |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 232 | ts := CreatePromAlertSimulator(t, "POST", "/api/v2/alerts", http.StatusOK, models.LabelSet{}) |
| 233 | defer ts.Close() |
| 234 | |
| 235 | // Raise two alarms |
Mohamed Abukar | af0c570 | 2020-03-11 10:29:40 +0200 | [diff] [blame] | 236 | 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] | 237 | assert.Nil(t, alarmer.Clear(a), "clear failed") |
| 238 | |
Mohamed Abukar | af0c570 | 2020-03-11 10:29:40 +0200 | [diff] [blame] | 239 | 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] | 240 | assert.Nil(t, alarmer.Clear(b), "clear failed") |
| 241 | |
| 242 | time.Sleep(time.Duration(2) * time.Second) |
Abukar Mohamed | 121e8b6 | 2020-09-18 11:41:33 +0000 | [diff] [blame] | 243 | assert.Equal(t, len(alarmManager.activeAlarms), 0) |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | func TestAlarmsSuppresedSucess(t *testing.T) { |
vipin | 54a3a4f | 2020-09-23 12:19:58 +0000 | [diff] [blame] | 247 | xapp.Logger.Info("TestAlarmsSuppresedSucess") |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 248 | ts := CreatePromAlertSimulator(t, "POST", "/api/v2/alerts", http.StatusOK, models.LabelSet{}) |
| 249 | defer ts.Close() |
| 250 | |
| 251 | // Raise two similar/matching alarms ... the second one suppresed |
Mohamed Abukar | af0c570 | 2020-03-11 10:29:40 +0200 | [diff] [blame] | 252 | 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] | 253 | assert.Nil(t, alarmer.Raise(a), "raise failed") |
| 254 | assert.Nil(t, alarmer.Raise(a), "raise failed") |
| 255 | |
Mohamed Abukar | ebe58c2 | 2020-03-13 14:40:47 +0200 | [diff] [blame] | 256 | VerifyAlarm(t, a, 1) |
vipin | 541eb50 | 2020-09-22 12:04:59 +0000 | [diff] [blame] | 257 | assert.Nil(t, alarmer.Clear(a), "clear failed") |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | func TestInvalidAlarms(t *testing.T) { |
vipin | 54a3a4f | 2020-09-23 12:19:58 +0000 | [diff] [blame] | 261 | xapp.Logger.Info("TestInvalidAlarms") |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 262 | a := alarmer.NewAlarm(1111, alarm.SeverityMajor, "Some App data", "eth 0 1") |
| 263 | assert.Nil(t, alarmer.Raise(a), "raise failed") |
| 264 | time.Sleep(time.Duration(2) * time.Second) |
| 265 | } |
| 266 | |
| 267 | func TestAlarmHandlingErrorCases(t *testing.T) { |
vipin | 54a3a4f | 2020-09-23 12:19:58 +0000 | [diff] [blame] | 268 | xapp.Logger.Info("TestAlarmHandlingErrorCases") |
Abukar Mohamed | 121e8b6 | 2020-09-18 11:41:33 +0000 | [diff] [blame] | 269 | ok, err := alarmManager.HandleAlarms(&xapp.RMRParams{}) |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 270 | assert.Equal(t, err.Error(), "unexpected end of JSON input") |
| 271 | assert.Nil(t, ok, "raise failed") |
| 272 | } |
| 273 | |
| 274 | func TestConsumeUnknownMessage(t *testing.T) { |
vipin | 54a3a4f | 2020-09-23 12:19:58 +0000 | [diff] [blame] | 275 | xapp.Logger.Info("TestConsumeUnknownMessage") |
Abukar Mohamed | 121e8b6 | 2020-09-18 11:41:33 +0000 | [diff] [blame] | 276 | err := alarmManager.Consume(&xapp.RMRParams{}) |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 277 | assert.Nil(t, err, "raise failed") |
| 278 | } |
| 279 | |
| 280 | func TestStatusCallback(t *testing.T) { |
vipin | 54a3a4f | 2020-09-23 12:19:58 +0000 | [diff] [blame] | 281 | xapp.Logger.Info("TestStatusCallback") |
Abukar Mohamed | 121e8b6 | 2020-09-18 11:41:33 +0000 | [diff] [blame] | 282 | assert.Equal(t, true, alarmManager.StatusCB()) |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 283 | } |
| 284 | |
vipin | 541eb50 | 2020-09-22 12:04:59 +0000 | [diff] [blame] | 285 | func TestActiveAlarmMaxThresholds(t *testing.T) { |
| 286 | xapp.Logger.Info("TestActiveAlarmMaxThresholds") |
| 287 | ts := CreatePromAlertSimulator(t, "POST", "/api/v2/alerts", http.StatusOK, models.LabelSet{}) |
| 288 | alarmManager.maxActiveAlarms = 0 |
| 289 | alarmManager.maxAlarmHistory = 10 |
| 290 | |
| 291 | a := alarmer.NewAlarm(alarm.E2_CONNECTIVITY_LOST_TO_GNODEB, alarm.SeverityCritical, "Some Application data", "eth 0 2") |
| 292 | assert.Nil(t, alarmer.Raise(a), "raise failed") |
| 293 | |
| 294 | var alarmConfigParams alarm.AlarmConfigParams |
| 295 | req, _ := http.NewRequest("GET", "/ric/v1/alarms/config", nil) |
| 296 | req = mux.SetURLVars(req, nil) |
| 297 | handleFunc := http.HandlerFunc(alarmManager.GetAlarmConfig) |
| 298 | response := executeRequest(req, handleFunc) |
| 299 | |
| 300 | // Check HTTP Status Code |
| 301 | checkResponseCode(t, http.StatusOK, response.Code) |
| 302 | |
| 303 | // Decode the json output from handler |
| 304 | json.NewDecoder(response.Body).Decode(&alarmConfigParams) |
| 305 | if alarmConfigParams.MaxActiveAlarms != 0 || alarmConfigParams.MaxAlarmHistory != 10 { |
| 306 | t.Errorf("Incorrect alarm thresholds") |
| 307 | } |
| 308 | |
| 309 | time.Sleep(time.Duration(1) * time.Second) |
| 310 | alarmManager.maxActiveAlarms = 5000 |
| 311 | alarmManager.maxAlarmHistory = 20000 |
| 312 | VerifyAlarm(t, a, 2) |
| 313 | VerifyAlarm(t, a, 2) |
| 314 | ts.Close() |
| 315 | } |
| 316 | |
Mohamed Abukar | ebe58c2 | 2020-03-13 14:40:47 +0200 | [diff] [blame] | 317 | func VerifyAlarm(t *testing.T, a alarm.Alarm, expectedCount int) string { |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 318 | receivedAlert := waitForEvent() |
| 319 | |
Abukar Mohamed | 121e8b6 | 2020-09-18 11:41:33 +0000 | [diff] [blame] | 320 | assert.Equal(t, len(alarmManager.activeAlarms), expectedCount) |
| 321 | _, ok := alarmManager.IsMatchFound(a) |
Mohamed Abukar | ebe58c2 | 2020-03-13 14:40:47 +0200 | [diff] [blame] | 322 | assert.True(t, ok) |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 323 | |
| 324 | return receivedAlert |
| 325 | } |
| 326 | |
| 327 | func VerifyAlert(t *testing.T, receivedAlert, expectedAlert string) { |
| 328 | receivedAlert = strings.Replace(fmt.Sprintf("%v", receivedAlert), "\r\n", " ", -1) |
| 329 | //assert.Equal(t, receivedAlert, e) |
| 330 | } |
| 331 | |
| 332 | func CreatePromAlertSimulator(t *testing.T, method, url string, status int, respData interface{}) *httptest.Server { |
| 333 | l, err := net.Listen("tcp", "localhost:9093") |
| 334 | if err != nil { |
| 335 | t.Error("Failed to create listener: " + err.Error()) |
| 336 | } |
| 337 | ts := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 338 | assert.Equal(t, r.Method, method) |
| 339 | assert.Equal(t, r.URL.String(), url) |
| 340 | |
| 341 | fireEvent(t, r.Body) |
| 342 | |
| 343 | w.Header().Add("Content-Type", "application/json") |
| 344 | w.WriteHeader(status) |
| 345 | b, _ := json.Marshal(respData) |
| 346 | w.Write(b) |
| 347 | })) |
| 348 | ts.Listener.Close() |
| 349 | ts.Listener = l |
| 350 | |
| 351 | ts.Start() |
| 352 | |
| 353 | return ts |
| 354 | } |
| 355 | |
| 356 | func waitForEvent() string { |
| 357 | receivedAlert := <-eventChan |
| 358 | return receivedAlert |
| 359 | } |
| 360 | |
| 361 | func fireEvent(t *testing.T, body io.ReadCloser) { |
| 362 | reqBody, err := ioutil.ReadAll(body) |
| 363 | assert.Nil(t, err, "ioutil.ReadAll failed") |
| 364 | assert.NotNil(t, reqBody, "ioutil.ReadAll failed") |
| 365 | |
| 366 | eventChan <- fmt.Sprintf("%s", reqBody) |
| 367 | } |
vipin | 541eb50 | 2020-09-22 12:04:59 +0000 | [diff] [blame] | 368 | |
| 369 | func executeRequest(req *http.Request, handleR http.HandlerFunc) *httptest.ResponseRecorder { |
| 370 | rr := httptest.NewRecorder() |
| 371 | |
| 372 | handleR.ServeHTTP(rr, req) |
| 373 | |
| 374 | return rr |
| 375 | } |
| 376 | |
| 377 | func checkResponseCode(t *testing.T, expected, actual int) bool { |
| 378 | if expected != actual { |
| 379 | t.Errorf("Expected response code %d. Got %d\n", expected, actual) |
| 380 | return false |
| 381 | } |
| 382 | return true |
| 383 | } |