Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(clusterconfig): add unit test for active_alerts.go and rename it to gather_active_alerts.go #730

Merged
merged 2 commits into from
Mar 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions pkg/gatherers/clusterconfig/gather_active_alerts_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
}
}