X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=manager%2Fcmd%2Fmanager_test.go;h=2589a9173ea442b31d40cf7cd7b85b50d667ca0a;hb=105030feb8fabd8b4ddff552c53e905146b2ea5f;hp=739896912b7c73001a32d331465b9c1dc639a811;hpb=478720e4a200ea2d635800813abf0b14290f8f32;p=ric-plt%2Falarm-go.git diff --git a/manager/cmd/manager_test.go b/manager/cmd/manager_test.go index 7398969..2589a91 100755 --- a/manager/cmd/manager_test.go +++ b/manager/cmd/manager_test.go @@ -21,8 +21,13 @@ package main import ( + "bytes" "encoding/json" "fmt" + "gerrit.o-ran-sc.org/r/ric-plt/alarm-go/alarm" + "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp" + "github.com/gorilla/mux" + "github.com/prometheus/alertmanager/api/v2/models" "github.com/stretchr/testify/assert" "io" "io/ioutil" @@ -30,15 +35,11 @@ import ( "net/http" "net/http/httptest" "os" + "os/exec" + "strconv" "strings" "testing" "time" - "github.com/gorilla/mux" - "strconv" - "bytes" - "gerrit.o-ran-sc.org/r/ric-plt/alarm-go/alarm" - "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp" - "github.com/prometheus/alertmanager/api/v2/models" ) var alarmManager *AlarmManager @@ -47,10 +48,10 @@ var eventChan chan string // Test cases func TestMain(M *testing.M) { - os.Setenv("ALARM_IF_RMR", "true") - alarmManager = NewAlarmManager("localhost:9093", 500) + alarmManager = NewAlarmManager("localhost:9093", 500, false) + alarmManager.alertInterval = 20000 go alarmManager.Run(false) - time.Sleep(time.Duration(2) * time.Second) + time.Sleep(time.Duration(10) * time.Second) // Wait until RMR is up-and-running for !xapp.Rmr.IsReady() { @@ -65,6 +66,22 @@ func TestMain(M *testing.M) { os.Exit(M.Run()) } +func TestGetPreDefinedAlarmDefinitions(t *testing.T) { + xapp.Logger.Info("TestGetPreDefinedAlarmDefinitions") + var alarmDefinition alarm.AlarmDefinition + req, _ := http.NewRequest("GET", "/ric/v1/alarms/define", nil) + vars := map[string]string{"alarmId": strconv.FormatUint(8004, 10)} + req = mux.SetURLVars(req, vars) + handleFunc := http.HandlerFunc(alarmManager.GetAlarmDefinition) + response := executeRequest(req, handleFunc) + checkResponseCode(t, http.StatusOK, response.Code) + json.NewDecoder(response.Body).Decode(&alarmDefinition) + xapp.Logger.Info("alarm definition = %v", alarmDefinition) + if alarmDefinition.AlarmId != alarm.RIC_RT_DISTRIBUTION_FAILED || alarmDefinition.AlarmText != "RIC ROUTING TABLE DISTRIBUTION FAILED" { + t.Errorf("Incorrect alarm definition") + } +} + func TestSetAlarmDefinitions(t *testing.T) { xapp.Logger.Info("TestSetAlarmDefinitions") var alarm8004Definition alarm.AlarmDefinition @@ -95,13 +112,13 @@ func TestSetAlarmDefinitions(t *testing.T) { alarm8008Definition.AlarmId = alarm.ACTIVE_ALARM_EXCEED_MAX_THRESHOLD alarm8008Definition.AlarmText = "ACTIVE ALARM EXCEED MAX THRESHOLD" alarm8008Definition.EventType = "storage warning" - alarm8008Definition.OperationInstructions = "clear alarms or raise threshold" + alarm8008Definition.OperationInstructions = "Clear alarms or raise threshold" var alarm8009Definition alarm.AlarmDefinition alarm8009Definition.AlarmId = alarm.ALARM_HISTORY_EXCEED_MAX_THRESHOLD alarm8009Definition.AlarmText = "ALARM HISTORY EXCEED MAX THRESHOLD" alarm8009Definition.EventType = "storage warning" - alarm8009Definition.OperationInstructions = "clear alarms or raise threshold" + alarm8009Definition.OperationInstructions = "Clear alarms or raise threshold" pbodyParams := RicAlarmDefinitions{AlarmDefinitions: []*alarm.AlarmDefinition{&alarm8004Definition, &alarm8005Definition, &alarm8006Definition, &alarm8007Definition, &alarm8008Definition, &alarm8009Definition}} pbodyEn, _ := json.Marshal(pbodyParams) @@ -145,11 +162,11 @@ func TestDeleteAlarmDefinitions(t *testing.T) { //Delete 8004 req, _ = http.NewRequest("DELETE", "/ric/v1/alarms/define", nil) - vars := map[string]string{"alarmId": strconv.FormatUint(8004, 10)} - req = mux.SetURLVars(req, vars) - handleFunc = http.HandlerFunc(alarmManager.DeleteAlarmDefinition) - response = executeRequest(req, handleFunc) - checkResponseCode(t, http.StatusOK, response.Code) + vars := map[string]string{"alarmId": strconv.FormatUint(8004, 10)} + req = mux.SetURLVars(req, vars) + handleFunc = http.HandlerFunc(alarmManager.DeleteAlarmDefinition) + response = executeRequest(req, handleFunc) + checkResponseCode(t, http.StatusOK, response.Code) //Get 8004 fail req, _ = http.NewRequest("GET", "/ric/v1/alarms/define", nil) @@ -223,6 +240,7 @@ func TestMultipleAlarmsRaisedSucess(t *testing.T) { b := alarmer.NewAlarm(alarm.TCP_CONNECTIVITY_LOST_TO_DBAAS, alarm.SeverityMinor, "Hello", "abcd 11") assert.Nil(t, alarmer.Raise(b), "raise failed") + time.Sleep(time.Duration(2) * time.Second) VerifyAlarm(t, a, 2) VerifyAlarm(t, b, 2) } @@ -257,7 +275,6 @@ func TestAlarmsSuppresedSucess(t *testing.T) { assert.Nil(t, alarmer.Clear(a), "clear failed") } - func TestInvalidAlarms(t *testing.T) { xapp.Logger.Info("TestInvalidAlarms") a := alarmer.NewAlarm(1111, alarm.SeverityMajor, "Some App data", "eth 0 1") @@ -315,10 +332,24 @@ func TestActiveAlarmMaxThresholds(t *testing.T) { ts.Close() } +func TestGetPrometheusAlerts(t *testing.T) { + time.Sleep(1 * time.Second) + xapp.Logger.Info("TestGetPrometheusAlerts") + ts := CreatePromAlertSimulator2(t, "GET", "/alerts?active=true&inhibited=true&silenced=true&unprocessed=true") + + commandReady := make(chan bool, 1) + command := "cli/alarm-cli" + args := []string{"gapam", "--active", "true", "--inhibited", "true", "--silenced", "--unprocessed", "true", "true", "--host", "localhost", "--port", "9093", "flushall"} + ExecCLICommand(commandReady, command, args...) + <-commandReady + + ts.Close() +} + func VerifyAlarm(t *testing.T, a alarm.Alarm, expectedCount int) string { receivedAlert := waitForEvent() - assert.Equal(t, len(alarmManager.activeAlarms), expectedCount) + assert.Equal(t, expectedCount, len(alarmManager.activeAlarms)) _, ok := alarmManager.IsMatchFound(a) assert.True(t, ok) @@ -354,6 +385,33 @@ func CreatePromAlertSimulator(t *testing.T, method, url string, status int, resp return ts } +func CreatePromAlertSimulator2(t *testing.T, method, url string) *httptest.Server { + l, err := net.Listen("tcp", "localhost:9093") + if err != nil { + t.Error("Failed to create listener: " + err.Error()) + } + ts := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, r.Method, method) + assert.Equal(t, r.URL.String(), url) + + w.Header().Add("Content-Type", "application/json") + w.WriteHeader(200) + // Read alerts from file + payload, err := readJSONFromFile("../testresources/prometheus-alerts.json") + if err != nil { + t.Error("Failed to send response: ", err) + } + _, err = w.Write(payload) + if err != nil { + t.Error("Failed to send response: " + err.Error()) + } + })) + ts.Listener.Close() + ts.Listener = l + ts.Start() + return ts +} + func waitForEvent() string { receivedAlert := <-eventChan return receivedAlert @@ -382,3 +440,27 @@ func checkResponseCode(t *testing.T, expected, actual int) bool { } return true } + +func ExecCLICommand(commandReady chan bool, command string, args ...string) { + go func() { + xapp.Logger.Info("Giving CLI command") + cmd := exec.Command(command, args...) + cmd.Dir = "../" + output, err := cmd.CombinedOutput() + if err != nil { + xapp.Logger.Info("CLI command failed out: %s", err) + } + xapp.Logger.Info("CLI command output: %s", output) + commandReady <- true + xapp.Logger.Info("CLI command completed") + }() +} + +func readJSONFromFile(filename string) ([]byte, error) { + file, err := ioutil.ReadFile(filename) + if err != nil { + err := fmt.Errorf("readJSONFromFile() failed: Error: %v", err) + return nil, err + } + return file, nil +}