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 os.Setenv("ALARM_IF_RMR", "true")
49 alarmAdapter = NewAlarmAdapter("localhost:9093", 500)
50 go alarmAdapter.Run(false)
51 time.Sleep(time.Duration(2) * time.Second)
53 // Wait until RMR is up-and-running
54 for !xapp.Rmr.IsReady() {
55 time.Sleep(time.Duration(1) * time.Second)
58 alarmer, _ = alarm.InitAlarm("my-pod", "my-app")
59 time.Sleep(time.Duration(5) * time.Second)
60 eventChan = make(chan string)
65 func TestNewAlarmStoredAndPostedSucess(t *testing.T) {
66 ts := CreatePromAlertSimulator(t, "POST", "/api/v2/alerts", http.StatusOK, models.LabelSet{})
69 a := alarmer.NewAlarm(alarm.RIC_RT_DISTRIBUTION_FAILED, alarm.SeverityCritical, "Some App data", "eth 0 1")
70 assert.Nil(t, alarmer.Raise(a), "raise failed")
75 func TestAlarmClearedSucess(t *testing.T) {
76 ts := CreatePromAlertSimulator(t, "POST", "/api/v2/alerts", http.StatusOK, models.LabelSet{})
80 a := alarmer.NewAlarm(alarm.RIC_RT_DISTRIBUTION_FAILED, alarm.SeverityCritical, "Some App data", "eth 0 1")
81 assert.Nil(t, alarmer.Raise(a), "raise failed")
85 // Now Clear the alarm and check alarm is removed
86 a = alarmer.NewAlarm(alarm.RIC_RT_DISTRIBUTION_FAILED, alarm.SeverityCritical, "Some App data", "eth 0 1")
87 assert.Nil(t, alarmer.Clear(a), "clear failed")
89 time.Sleep(time.Duration(2) * time.Second)
90 assert.Equal(t, len(alarmAdapter.activeAlarms), 0)
93 func TestMultipleAlarmsRaisedSucess(t *testing.T) {
94 ts := CreatePromAlertSimulator(t, "POST", "/api/v2/alerts", http.StatusOK, models.LabelSet{})
98 a := alarmer.NewAlarm(alarm.RIC_RT_DISTRIBUTION_FAILED, alarm.SeverityMajor, "Some App data", "eth 0 1")
99 assert.Nil(t, alarmer.Raise(a), "raise failed")
101 b := alarmer.NewAlarm(alarm.TCP_CONNECTIVITY_LOST_TO_DBAAS, alarm.SeverityMinor, "Hello", "abcd 11")
102 assert.Nil(t, alarmer.Raise(b), "raise failed")
108 func TestMultipleAlarmsClearedSucess(t *testing.T) {
109 ts := CreatePromAlertSimulator(t, "POST", "/api/v2/alerts", http.StatusOK, models.LabelSet{})
113 a := alarmer.NewAlarm(alarm.RIC_RT_DISTRIBUTION_FAILED, alarm.SeverityMajor, "Some App data", "eth 0 1")
114 assert.Nil(t, alarmer.Clear(a), "clear failed")
116 b := alarmer.NewAlarm(alarm.TCP_CONNECTIVITY_LOST_TO_DBAAS, alarm.SeverityMinor, "Hello", "abcd 11")
117 assert.Nil(t, alarmer.Clear(b), "clear failed")
119 time.Sleep(time.Duration(2) * time.Second)
120 assert.Equal(t, len(alarmAdapter.activeAlarms), 0)
123 func TestAlarmsSuppresedSucess(t *testing.T) {
124 ts := CreatePromAlertSimulator(t, "POST", "/api/v2/alerts", http.StatusOK, models.LabelSet{})
127 // Raise two similar/matching alarms ... the second one suppresed
128 a := alarmer.NewAlarm(alarm.RIC_RT_DISTRIBUTION_FAILED, alarm.SeverityMajor, "Some App data", "eth 0 1")
129 assert.Nil(t, alarmer.Raise(a), "raise failed")
130 assert.Nil(t, alarmer.Raise(a), "raise failed")
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)
141 func TestAlarmHandlingErrorCases(t *testing.T) {
142 ok, err := alarmAdapter.HandleAlarms(&xapp.RMRParams{})
143 assert.Equal(t, err.Error(), "unexpected end of JSON input")
144 assert.Nil(t, ok, "raise failed")
147 func TestConsumeUnknownMessage(t *testing.T) {
148 err := alarmAdapter.Consume(&xapp.RMRParams{})
149 assert.Nil(t, err, "raise failed")
152 func TestStatusCallback(t *testing.T) {
153 assert.Equal(t, true, alarmAdapter.StatusCB())
156 func VerifyAlarm(t *testing.T, a alarm.Alarm, expectedCount int) string {
157 receivedAlert := waitForEvent()
159 assert.Equal(t, len(alarmAdapter.activeAlarms), expectedCount)
160 _, ok := alarmAdapter.IsMatchFound(a)
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)
171 func CreatePromAlertSimulator(t *testing.T, method, url string, status int, respData interface{}) *httptest.Server {
172 l, err := net.Listen("tcp", "localhost:9093")
174 t.Error("Failed to create listener: " + err.Error())
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)
182 w.Header().Add("Content-Type", "application/json")
183 w.WriteHeader(status)
184 b, _ := json.Marshal(respData)
195 func waitForEvent() string {
196 receivedAlert := <-eventChan
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")
205 eventChan <- fmt.Sprintf("%s", reqBody)