2 * Copyright (c) 2020 AT&T Intellectual Property.
3 * Copyright (c) 2020 Nokia.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * This source code is part of the near-RT RIC (RAN Intelligent Controller)
18 * platform project (RICP).
26 "github.com/stretchr/testify/assert"
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"
42 var alarmAdapter *AlarmAdapter
43 var alarmer *alarm.RICAlarm
44 var eventChan chan string
47 func TestMain(M *testing.M) {
48 alarmAdapter = NewAlarmAdapter("localhost:9093", 500)
49 go alarmAdapter.Run(false)
50 time.Sleep(time.Duration(2) * time.Second)
52 // Wait until RMR is up-and-running
53 for !xapp.Rmr.IsReady() {
54 time.Sleep(time.Duration(1) * time.Second)
57 alarmer, _ = alarm.InitAlarm("my-pod", "my-app")
58 eventChan = make(chan string)
63 func TestNewAlarmStoredAndPostedSucess(t *testing.T) {
64 ts := CreatePromAlertSimulator(t, "POST", "/api/v2/alerts", http.StatusOK, models.LabelSet{})
67 a := alarmer.NewAlarm(RIC_RT_DISTRIBUTION_FAILED, alarm.SeverityMajor, "Some App data", "eth 0 1")
68 assert.Nil(t, alarmer.Raise(a), "raise failed")
70 VerifyAlarm(t, a, 1, 0)
73 func TestAlarmClearedSucess(t *testing.T) {
74 ts := CreatePromAlertSimulator(t, "POST", "/api/v2/alerts", http.StatusOK, models.LabelSet{})
78 a := alarmer.NewAlarm(RIC_RT_DISTRIBUTION_FAILED, alarm.SeverityMajor, "Some App data", "eth 0 1")
79 assert.Nil(t, alarmer.Raise(a), "raise failed")
81 VerifyAlarm(t, a, 1, 0)
83 // Now Clear the alarm and check alarm is removed
84 a = alarmer.NewAlarm(RIC_RT_DISTRIBUTION_FAILED, alarm.SeverityCleared, "Some App data", "eth 0 1")
85 assert.Nil(t, alarmer.Clear(a), "clear failed")
87 time.Sleep(time.Duration(2) * time.Second)
88 assert.Equal(t, len(alarmAdapter.activeAlarms), 0)
91 func TestMultipleAlarmsRaisedSucess(t *testing.T) {
92 ts := CreatePromAlertSimulator(t, "POST", "/api/v2/alerts", http.StatusOK, models.LabelSet{})
96 a := alarmer.NewAlarm(RIC_RT_DISTRIBUTION_FAILED, alarm.SeverityMajor, "Some App data", "eth 0 1")
97 assert.Nil(t, alarmer.Raise(a), "raise failed")
99 b := alarmer.NewAlarm(CONNECTIVITY_LOST_TO_DBAAS, alarm.SeverityMinor, "Hello", "abcd 11")
100 assert.Nil(t, alarmer.Raise(b), "raise failed")
102 VerifyAlarm(t, a, 2, 0)
103 VerifyAlarm(t, b, 2, 1)
106 func TestMultipleAlarmsClearedSucess(t *testing.T) {
107 ts := CreatePromAlertSimulator(t, "POST", "/api/v2/alerts", http.StatusOK, models.LabelSet{})
111 a := alarmer.NewAlarm(RIC_RT_DISTRIBUTION_FAILED, alarm.SeverityMajor, "Some App data", "eth 0 1")
112 assert.Nil(t, alarmer.Clear(a), "clear failed")
114 b := alarmer.NewAlarm(CONNECTIVITY_LOST_TO_DBAAS, alarm.SeverityMinor, "Hello", "abcd 11")
115 assert.Nil(t, alarmer.Clear(b), "clear failed")
117 time.Sleep(time.Duration(2) * time.Second)
118 assert.Equal(t, len(alarmAdapter.activeAlarms), 0)
121 func TestAlarmsSuppresedSucess(t *testing.T) {
122 ts := CreatePromAlertSimulator(t, "POST", "/api/v2/alerts", http.StatusOK, models.LabelSet{})
125 // Raise two similar/matching alarms ... the second one suppresed
126 a := alarmer.NewAlarm(RIC_RT_DISTRIBUTION_FAILED, alarm.SeverityMajor, "Some App data", "eth 0 1")
127 assert.Nil(t, alarmer.Raise(a), "raise failed")
128 assert.Nil(t, alarmer.Raise(a), "raise failed")
130 VerifyAlarm(t, a, 1, 0)
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)
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")
145 func TestConsumeUnknownMessage(t *testing.T) {
146 err := alarmAdapter.Consume(&xapp.RMRParams{})
147 assert.Nil(t, err, "raise failed")
150 func TestStatusCallback(t *testing.T) {
151 assert.Equal(t, true, alarmAdapter.StatusCB())
154 func VerifyAlarm(t *testing.T, a alarm.Alarm, count, idx int) string {
155 receivedAlert := waitForEvent()
157 assert.Equal(t, len(alarmAdapter.activeAlarms), count)
159 b := alarmAdapter.activeAlarms[idx]
160 assert.Equal(t, b.ManagedObjectId, a.ManagedObjectId)
161 assert.Equal(t, b.ApplicationId, a.ApplicationId)
162 assert.Equal(t, b.SpecificProblem, a.SpecificProblem)
163 assert.Equal(t, b.PerceivedSeverity, a.PerceivedSeverity)
164 assert.Equal(t, b.AdditionalInfo, a.AdditionalInfo)
165 assert.Equal(t, b.IdentifyingInfo, a.IdentifyingInfo)
170 func VerifyAlert(t *testing.T, receivedAlert, expectedAlert string) {
171 receivedAlert = strings.Replace(fmt.Sprintf("%v", receivedAlert), "\r\n", " ", -1)
172 //assert.Equal(t, receivedAlert, e)
175 func CreatePromAlertSimulator(t *testing.T, method, url string, status int, respData interface{}) *httptest.Server {
176 l, err := net.Listen("tcp", "localhost:9093")
178 t.Error("Failed to create listener: " + err.Error())
180 ts := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
181 assert.Equal(t, r.Method, method)
182 assert.Equal(t, r.URL.String(), url)
186 w.Header().Add("Content-Type", "application/json")
187 w.WriteHeader(status)
188 b, _ := json.Marshal(respData)
199 func waitForEvent() string {
200 receivedAlert := <-eventChan
204 func fireEvent(t *testing.T, body io.ReadCloser) {
205 reqBody, err := ioutil.ReadAll(body)
206 assert.Nil(t, err, "ioutil.ReadAll failed")
207 assert.NotNil(t, reqBody, "ioutil.ReadAll failed")
209 eventChan <- fmt.Sprintf("%s", reqBody)