157 lines
5.7 KiB
Go
157 lines
5.7 KiB
Go
package main
|
|
|
|
import (
|
|
"os"
|
|
"path"
|
|
"testing"
|
|
)
|
|
|
|
func TestParseSlackTestdata(t *testing.T) {
|
|
cases := map[string]struct {
|
|
inCh string
|
|
slackMessage slackMessage
|
|
message Message
|
|
}{
|
|
"human_thread_message_from_opsgenie_alert.json": {
|
|
slackMessage: slackMessage{
|
|
TS: 1712930706,
|
|
Event: slackEvent{
|
|
ID: "1712930706.598629",
|
|
Channel: "C06U1DDBBU4",
|
|
ParentID: "1712927439.728409",
|
|
Text: "I gotta do this",
|
|
Blocks: []slackBlock{{
|
|
Elements: []slackElement{{
|
|
Elements: []slackElement{{
|
|
RichText: "I gotta do this",
|
|
}},
|
|
}},
|
|
}},
|
|
Bot: slackBot{
|
|
Name: "",
|
|
},
|
|
Attachments: []slackAttachment{},
|
|
},
|
|
},
|
|
message: Message{
|
|
ID: "1712927439.728409/1712930706",
|
|
TS: 1712930706,
|
|
Source: "https://renderinc.slack.com/archives/C06U1DDBBU4/p1712927439728409",
|
|
Channel: "C06U1DDBBU4",
|
|
Thread: "1712927439.728409",
|
|
EventName: "",
|
|
Event: "",
|
|
Plaintext: "I gotta do this",
|
|
Asset: "",
|
|
},
|
|
},
|
|
"opsgenie_alert.json": {
|
|
slackMessage: slackMessage{
|
|
TS: 1712927439,
|
|
Event: slackEvent{
|
|
ID: "1712927439.728409",
|
|
Channel: "C06U1DDBBU4",
|
|
Bot: slackBot{
|
|
Name: "Opsgenie for Alert Management",
|
|
},
|
|
Attachments: []slackAttachment{{
|
|
Color: "F4511E",
|
|
Title: "#11071: [Grafana]: Firing: Alertconfig Workflow Failed",
|
|
Text: "At least one alertconfig run has failed unexpectedly.\nDashboard: <https://grafana.render.com/d/VLZU83YVk?orgId=1>\nPanel: <https://grafana.render.com/d/VLZU83YVk?orgId=1&viewPanel=17>\nSource: <https://grafana.render.com/alerting/grafana/fa7b06b8-b4d8-4979-bce7-5e1c432edd81/view?orgId=1>",
|
|
Fields: []slackField{
|
|
{Value: "P3", Title: "Priority"},
|
|
{Value: "alertname:Alertconfig Workflow Failed, grafana_folder:Datastores, rule_uid:a7639f7e-6950-41be-850a-b22119f74cbb", Title: "Tags"},
|
|
{Value: "Datastores Non-Critical", Title: "Routed Teams"},
|
|
},
|
|
Actions: []slackAction{{}, {}, {}},
|
|
}},
|
|
},
|
|
},
|
|
message: Message{
|
|
ID: "1712927439.728409/1712927439",
|
|
TS: 1712927439,
|
|
Source: "https://renderinc.slack.com/archives/C06U1DDBBU4/p1712927439728409",
|
|
Channel: "C06U1DDBBU4",
|
|
Thread: "1712927439.728409",
|
|
EventName: "Alertconfig Workflow Failed",
|
|
Event: "11071",
|
|
Plaintext: "At least one alertconfig run has failed unexpectedly.\nDashboard: <https://grafana.render.com/d/VLZU83YVk?orgId=1>\nPanel: <https://grafana.render.com/d/VLZU83YVk?orgId=1&viewPanel=17>\nSource: <https://grafana.render.com/alerting/grafana/fa7b06b8-b4d8-4979-bce7-5e1c432edd81/view?orgId=1>",
|
|
Asset: "",
|
|
},
|
|
},
|
|
"opsgenie_alert_resolved.json": {
|
|
slackMessage: slackMessage{
|
|
TS: 1712916339,
|
|
Event: slackEvent{
|
|
ID: "1712916339.000300",
|
|
Channel: "C06U1DDBBU4",
|
|
Bot: slackBot{
|
|
Name: "Opsgenie for Alert Management",
|
|
},
|
|
Attachments: []slackAttachment{{
|
|
Color: "2ecc71",
|
|
Title: "#11069: [Grafana]: Firing: Alertconfig Workflow Failed",
|
|
Text: "At least one alertconfig run has failed unexpectedly.\nDashboard: <https://grafana.render.com/d/VLZU83YVk?orgId=1>\nPanel: <https://grafana.render.com/d/VLZU83YVk?orgId=1&viewPanel=17>\nSource: <https://grafana.render.com/alerting/grafana/fa7b06b8-b4d8-4979-bce7-5e1c432edd81/view?orgId=1>",
|
|
Fields: []slackField{
|
|
{Value: "P3", Title: "Priority"},
|
|
{Value: "alertname:Alertconfig Workflow Failed, grafana_folder:Datastores, rule_uid:a7639f7e-6950-41be-850a-b22119f74cbb", Title: "Tags"},
|
|
{Value: "Datastores Non-Critical", Title: "Routed Teams"},
|
|
},
|
|
Actions: []slackAction{},
|
|
}},
|
|
},
|
|
},
|
|
message: Message{
|
|
ID: "1712916339.000300/1712916339",
|
|
TS: 1712916339,
|
|
Source: "https://renderinc.slack.com/archives/C06U1DDBBU4/p1712916339000300",
|
|
Channel: "C06U1DDBBU4",
|
|
Thread: "1712916339.000300",
|
|
EventName: "Alertconfig Workflow Failed",
|
|
Event: "11069",
|
|
Plaintext: "At least one alertconfig run has failed unexpectedly.\nDashboard: <https://grafana.render.com/d/VLZU83YVk?orgId=1>\nPanel: <https://grafana.render.com/d/VLZU83YVk?orgId=1&viewPanel=17>\nSource: <https://grafana.render.com/alerting/grafana/fa7b06b8-b4d8-4979-bce7-5e1c432edd81/view?orgId=1>",
|
|
Asset: "",
|
|
Resolved: true,
|
|
},
|
|
},
|
|
"reingested_alert.json": {
|
|
inCh: "C06U1DDBBU4",
|
|
message: Message{
|
|
ID: "1712892637.037639/1712892637",
|
|
TS: 1712892637,
|
|
Source: "https://renderinc.slack.com/archives/C06U1DDBBU4/p1712892637037639",
|
|
Channel: "C06U1DDBBU4",
|
|
Thread: "1712892637.037639",
|
|
EventName: "Alertconfig Workflow Failed",
|
|
Event: "11061",
|
|
Plaintext: "At least one alertconfig run has failed unexpectedly.\nDashboard: <https://grafana.render.com/d/VLZU83YVk?orgId=1>\nPanel: <https://grafana.render.com/d/VLZU83YVk?orgId=1&viewPanel=17>\nSource: <https://grafana.render.com/alerting/grafana/fa7b06b8-b4d8-4979-bce7-5e1c432edd81/view?orgId=1>",
|
|
Asset: "",
|
|
Resolved: true,
|
|
},
|
|
},
|
|
}
|
|
|
|
for name, d := range cases {
|
|
want := d
|
|
t.Run(name, func(t *testing.T) {
|
|
b, err := os.ReadFile(path.Join("testdata", "slack_events", name))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
t.Run("ParseSlackFromChannel "+want.inCh, func(t *testing.T) {
|
|
got, err := ParseSlackFromChannel(b, renderAssetPattern, renderDatacenterPattern, renderEventNamePattern, want.inCh)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got != want.message {
|
|
t.Errorf("wanted \n\t%+v, got\n\t%+v", want.message, got)
|
|
}
|
|
if time := got.Time(); time.Unix() != int64(got.TS) {
|
|
t.Error("not unix time", got.TS, time)
|
|
}
|
|
})
|
|
})
|
|
}
|
|
}
|