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