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 ( |
| 24 | "encoding/json" |
| 25 | "fmt" |
| 26 | "github.com/stretchr/testify/assert" |
| 27 | "io" |
| 28 | "io/ioutil" |
| 29 | "net" |
| 30 | "net/http" |
| 31 | "net/http/httptest" |
| 32 | "os" |
| 33 | "strings" |
| 34 | "testing" |
| 35 | "time" |
| 36 | |
| 37 | "gerrit.o-ran-sc.org/r/ric-plt/alarm-go/alarm" |
| 38 | "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp" |
| 39 | "github.com/prometheus/alertmanager/api/v2/models" |
| 40 | ) |
| 41 | |
Abukar Mohamed | 121e8b6 | 2020-09-18 11:41:33 +0000 | [diff] [blame^] | 42 | var alarmManager *AlarmManager |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 43 | var alarmer *alarm.RICAlarm |
| 44 | var eventChan chan string |
| 45 | |
| 46 | // Test cases |
| 47 | func TestMain(M *testing.M) { |
Mohamed Abukar | 540ceee | 2020-09-09 08:07:40 +0300 | [diff] [blame] | 48 | os.Setenv("ALARM_IF_RMR", "true") |
Abukar Mohamed | 121e8b6 | 2020-09-18 11:41:33 +0000 | [diff] [blame^] | 49 | alarmManager = NewAlarmManager("localhost:9093", 500) |
| 50 | go alarmManager.Run(false) |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 51 | time.Sleep(time.Duration(2) * time.Second) |
| 52 | |
| 53 | // Wait until RMR is up-and-running |
| 54 | for !xapp.Rmr.IsReady() { |
| 55 | time.Sleep(time.Duration(1) * time.Second) |
| 56 | } |
| 57 | |
| 58 | alarmer, _ = alarm.InitAlarm("my-pod", "my-app") |
Mohamed Abukar | eac4451 | 2020-03-31 09:46:04 +0300 | [diff] [blame] | 59 | time.Sleep(time.Duration(5) * time.Second) |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 60 | eventChan = make(chan string) |
| 61 | |
| 62 | os.Exit(M.Run()) |
| 63 | } |
| 64 | |
| 65 | func TestNewAlarmStoredAndPostedSucess(t *testing.T) { |
| 66 | ts := CreatePromAlertSimulator(t, "POST", "/api/v2/alerts", http.StatusOK, models.LabelSet{}) |
| 67 | defer ts.Close() |
| 68 | |
Mohamed Abukar | 540ceee | 2020-09-09 08:07:40 +0300 | [diff] [blame] | 69 | 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] | 70 | assert.Nil(t, alarmer.Raise(a), "raise failed") |
| 71 | |
Mohamed Abukar | ebe58c2 | 2020-03-13 14:40:47 +0200 | [diff] [blame] | 72 | VerifyAlarm(t, a, 1) |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | func TestAlarmClearedSucess(t *testing.T) { |
| 76 | ts := CreatePromAlertSimulator(t, "POST", "/api/v2/alerts", http.StatusOK, models.LabelSet{}) |
| 77 | defer ts.Close() |
| 78 | |
| 79 | // Raise the alarm |
Mohamed Abukar | 540ceee | 2020-09-09 08:07:40 +0300 | [diff] [blame] | 80 | 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] | 81 | assert.Nil(t, alarmer.Raise(a), "raise failed") |
| 82 | |
Mohamed Abukar | ebe58c2 | 2020-03-13 14:40:47 +0200 | [diff] [blame] | 83 | VerifyAlarm(t, a, 1) |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 84 | |
| 85 | // Now Clear the alarm and check alarm is removed |
Mohamed Abukar | 540ceee | 2020-09-09 08:07:40 +0300 | [diff] [blame] | 86 | 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] | 87 | assert.Nil(t, alarmer.Clear(a), "clear failed") |
| 88 | |
| 89 | time.Sleep(time.Duration(2) * time.Second) |
Abukar Mohamed | 121e8b6 | 2020-09-18 11:41:33 +0000 | [diff] [blame^] | 90 | assert.Equal(t, len(alarmManager.activeAlarms), 0) |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | func TestMultipleAlarmsRaisedSucess(t *testing.T) { |
| 94 | ts := CreatePromAlertSimulator(t, "POST", "/api/v2/alerts", http.StatusOK, models.LabelSet{}) |
| 95 | defer ts.Close() |
| 96 | |
| 97 | // Raise two alarms |
Mohamed Abukar | af0c570 | 2020-03-11 10:29:40 +0200 | [diff] [blame] | 98 | 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] | 99 | assert.Nil(t, alarmer.Raise(a), "raise failed") |
| 100 | |
Mohamed Abukar | af0c570 | 2020-03-11 10:29:40 +0200 | [diff] [blame] | 101 | 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] | 102 | assert.Nil(t, alarmer.Raise(b), "raise failed") |
| 103 | |
Mohamed Abukar | ebe58c2 | 2020-03-13 14:40:47 +0200 | [diff] [blame] | 104 | VerifyAlarm(t, a, 2) |
| 105 | VerifyAlarm(t, b, 2) |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | func TestMultipleAlarmsClearedSucess(t *testing.T) { |
| 109 | ts := CreatePromAlertSimulator(t, "POST", "/api/v2/alerts", http.StatusOK, models.LabelSet{}) |
| 110 | defer ts.Close() |
| 111 | |
| 112 | // Raise two alarms |
Mohamed Abukar | af0c570 | 2020-03-11 10:29:40 +0200 | [diff] [blame] | 113 | 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] | 114 | assert.Nil(t, alarmer.Clear(a), "clear failed") |
| 115 | |
Mohamed Abukar | af0c570 | 2020-03-11 10:29:40 +0200 | [diff] [blame] | 116 | 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] | 117 | assert.Nil(t, alarmer.Clear(b), "clear failed") |
| 118 | |
| 119 | time.Sleep(time.Duration(2) * time.Second) |
Abukar Mohamed | 121e8b6 | 2020-09-18 11:41:33 +0000 | [diff] [blame^] | 120 | assert.Equal(t, len(alarmManager.activeAlarms), 0) |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | func TestAlarmsSuppresedSucess(t *testing.T) { |
| 124 | ts := CreatePromAlertSimulator(t, "POST", "/api/v2/alerts", http.StatusOK, models.LabelSet{}) |
| 125 | defer ts.Close() |
| 126 | |
| 127 | // Raise two similar/matching alarms ... the second one suppresed |
Mohamed Abukar | af0c570 | 2020-03-11 10:29:40 +0200 | [diff] [blame] | 128 | 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] | 129 | assert.Nil(t, alarmer.Raise(a), "raise failed") |
| 130 | assert.Nil(t, alarmer.Raise(a), "raise failed") |
| 131 | |
Mohamed Abukar | ebe58c2 | 2020-03-13 14:40:47 +0200 | [diff] [blame] | 132 | VerifyAlarm(t, a, 1) |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | func TestInvalidAlarms(t *testing.T) { |
| 136 | a := alarmer.NewAlarm(1111, alarm.SeverityMajor, "Some App data", "eth 0 1") |
| 137 | assert.Nil(t, alarmer.Raise(a), "raise failed") |
| 138 | time.Sleep(time.Duration(2) * time.Second) |
| 139 | } |
| 140 | |
| 141 | func TestAlarmHandlingErrorCases(t *testing.T) { |
Abukar Mohamed | 121e8b6 | 2020-09-18 11:41:33 +0000 | [diff] [blame^] | 142 | ok, err := alarmManager.HandleAlarms(&xapp.RMRParams{}) |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 143 | assert.Equal(t, err.Error(), "unexpected end of JSON input") |
| 144 | assert.Nil(t, ok, "raise failed") |
| 145 | } |
| 146 | |
| 147 | func TestConsumeUnknownMessage(t *testing.T) { |
Abukar Mohamed | 121e8b6 | 2020-09-18 11:41:33 +0000 | [diff] [blame^] | 148 | err := alarmManager.Consume(&xapp.RMRParams{}) |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 149 | assert.Nil(t, err, "raise failed") |
| 150 | } |
| 151 | |
| 152 | func TestStatusCallback(t *testing.T) { |
Abukar Mohamed | 121e8b6 | 2020-09-18 11:41:33 +0000 | [diff] [blame^] | 153 | assert.Equal(t, true, alarmManager.StatusCB()) |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 154 | } |
| 155 | |
Mohamed Abukar | ebe58c2 | 2020-03-13 14:40:47 +0200 | [diff] [blame] | 156 | func VerifyAlarm(t *testing.T, a alarm.Alarm, expectedCount int) string { |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 157 | receivedAlert := waitForEvent() |
| 158 | |
Abukar Mohamed | 121e8b6 | 2020-09-18 11:41:33 +0000 | [diff] [blame^] | 159 | assert.Equal(t, len(alarmManager.activeAlarms), expectedCount) |
| 160 | _, ok := alarmManager.IsMatchFound(a) |
Mohamed Abukar | ebe58c2 | 2020-03-13 14:40:47 +0200 | [diff] [blame] | 161 | assert.True(t, ok) |
Mohamed Abukar | 4e7e712 | 2020-03-04 10:01:45 +0200 | [diff] [blame] | 162 | |
| 163 | return receivedAlert |
| 164 | } |
| 165 | |
| 166 | func VerifyAlert(t *testing.T, receivedAlert, expectedAlert string) { |
| 167 | receivedAlert = strings.Replace(fmt.Sprintf("%v", receivedAlert), "\r\n", " ", -1) |
| 168 | //assert.Equal(t, receivedAlert, e) |
| 169 | } |
| 170 | |
| 171 | func CreatePromAlertSimulator(t *testing.T, method, url string, status int, respData interface{}) *httptest.Server { |
| 172 | l, err := net.Listen("tcp", "localhost:9093") |
| 173 | if err != nil { |
| 174 | t.Error("Failed to create listener: " + err.Error()) |
| 175 | } |
| 176 | ts := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 177 | assert.Equal(t, r.Method, method) |
| 178 | assert.Equal(t, r.URL.String(), url) |
| 179 | |
| 180 | fireEvent(t, r.Body) |
| 181 | |
| 182 | w.Header().Add("Content-Type", "application/json") |
| 183 | w.WriteHeader(status) |
| 184 | b, _ := json.Marshal(respData) |
| 185 | w.Write(b) |
| 186 | })) |
| 187 | ts.Listener.Close() |
| 188 | ts.Listener = l |
| 189 | |
| 190 | ts.Start() |
| 191 | |
| 192 | return ts |
| 193 | } |
| 194 | |
| 195 | func waitForEvent() string { |
| 196 | receivedAlert := <-eventChan |
| 197 | return receivedAlert |
| 198 | } |
| 199 | |
| 200 | func fireEvent(t *testing.T, body io.ReadCloser) { |
| 201 | reqBody, err := ioutil.ReadAll(body) |
| 202 | assert.Nil(t, err, "ioutil.ReadAll failed") |
| 203 | assert.NotNil(t, reqBody, "ioutil.ReadAll failed") |
| 204 | |
| 205 | eventChan <- fmt.Sprintf("%s", reqBody) |
| 206 | } |