From 9d9be7d2737561071be099e4753dedd6fe5fd4f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20L=C3=BCders?= Date: Tue, 7 Feb 2023 13:20:08 +0100 Subject: [PATCH 1/2] chore(clusterconfig): rename active_alerts.go to gather_active_alerts.go --- .../clusterconfig/{active_alerts.go => gather_active_alerts.go} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename pkg/gatherers/clusterconfig/{active_alerts.go => gather_active_alerts.go} (100%) diff --git a/pkg/gatherers/clusterconfig/active_alerts.go b/pkg/gatherers/clusterconfig/gather_active_alerts.go similarity index 100% rename from pkg/gatherers/clusterconfig/active_alerts.go rename to pkg/gatherers/clusterconfig/gather_active_alerts.go From 0c6e962da3f2ed9675dd17c9e0733a00bb5b01d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20L=C3=BCders?= Date: Tue, 7 Feb 2023 16:56:36 +0100 Subject: [PATCH 2/2] test(active_alerts): adding unit tests --- .../gather_active_alerts_test.go | 101 ++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 pkg/gatherers/clusterconfig/gather_active_alerts_test.go diff --git a/pkg/gatherers/clusterconfig/gather_active_alerts_test.go b/pkg/gatherers/clusterconfig/gather_active_alerts_test.go new file mode 100644 index 000000000..9c87bac7a --- /dev/null +++ b/pkg/gatherers/clusterconfig/gather_active_alerts_test.go @@ -0,0 +1,101 @@ +package clusterconfig + +import ( + "context" + "testing" + + "github.com/openshift/insights-operator/pkg/record" + "github.com/stretchr/testify/assert" +) + +func Test_gatherActiveAlerts(t *testing.T) { + responseData := ` +[ + { + "annotations": { + "description": "This is just an sample alert description", + "summary": "An alert sample summary." + }, + "endsAt": "2023-02-07T15:19:22.206Z", + "fingerprint": "6934731368443c07", + "receivers": [ + { + "name": "Watchdog" + } + ], + "startsAt": "2023-02-07T13:16:22.206Z", + "status": { + "inhibitedBy": [], + "silencedBy": [], + "state": "active" + }, + "updatedAt": "2023-02-07T15:15:22.207Z", + "generatorURL": "https://console-openshift-console.apps.cluster.test/monitoring/graph?g0.expr=vector%281%29&g0.tab=1", + "labels": { + "alertname": "Watchdog", + "namespace": "openshift-monitoring", + "openshift_io_alert_source": "platform", + "prometheus": "openshift-monitoring/k8s", + "severity": "none" + } + } +]` + + tests := []struct { + name string + mockAlertsClient *mockAlertsClient + wantRecords []record.Record + wantErrsCount int + }{ + { + name: "Get active alerts successfully", + mockAlertsClient: &mockAlertsClient{ + data: []byte(responseData), + }, + wantRecords: []record.Record{ + { + Name: "config/alerts", + Item: record.JSONMarshaller{Object: []alert{ + { + Labels: map[string]string{ + "alertname": "Watchdog", + "namespace": "openshift-monitoring", + "openshift_io_alert_source": "platform", + "prometheus": "openshift-monitoring/k8s", + "severity": "none", + }, + Annotations: map[string]string{ + "description": "This is just an sample alert description", + "summary": "An alert sample summary.", + }, + EndsAt: "2023-02-07T15:19:22.206Z", + StartsAt: "2023-02-07T13:16:22.206Z", + UpdatedAt: "2023-02-07T15:15:22.207Z", + Status: map[string]interface{}{ + "inhibitedBy": []interface{}{}, + "silencedBy": []interface{}{}, + "state": "active", + }, + }, + }}, + }, + }, + wantErrsCount: 0, + }, + { + name: "Get active alerts with error", + mockAlertsClient: &mockAlertsClient{data: nil}, + wantRecords: nil, + wantErrsCount: 1, + }, + } + + ctx := context.Background() + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + records, errs := gatherActiveAlerts(ctx, tt.mockAlertsClient.RestClient(t)) + assert.Len(t, errs, tt.wantErrsCount) + assert.Equal(t, tt.wantRecords, records) + }) + } +}