From 9bb381b008bc59fd27609721c43b090bb04711ee Mon Sep 17 00:00:00 2001 From: xiaojingchen Date: Tue, 12 Mar 2019 17:37:27 +0800 Subject: [PATCH 1/2] add slack --- tests/slack/slack.go | 137 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 137 insertions(+) create mode 100644 tests/slack/slack.go diff --git a/tests/slack/slack.go b/tests/slack/slack.go new file mode 100644 index 00000000000..554585b6fac --- /dev/null +++ b/tests/slack/slack.go @@ -0,0 +1,137 @@ +package slack + +import ( + "bytes" + "encoding/json" + "fmt" + "net/http" +) + +var ( + Channel string + WebhookUrl string +) + +type Field struct { + Title string `json:"title"` + Value string `json:"value"` + Short bool `json:"short"` +} + +type Attachment struct { + Fallback string `json:"fallback"` + Color string `json:"color"` + PreText string `json:"pretext"` + AuthorName string `json:"author_name"` + AuthorLink string `json:"author_link"` + AuthorIcon string `json:"author_icon"` + Title string `json:"title"` + TitleLink string `json:"title_link"` + Text string `json:"text"` + ImageUrl string `json:"image_url"` + Fields []*Field `json:"fields"` + Footer string `json:"footer"` + FooterIcon string `json:"footer_icon"` + Timestamp int64 `json:"ts"` + MarkdownIn []string `json:"mrkdwn_in"` +} + +type Payload struct { + Parse string `json:"parse,omitempty"` + Username string `json:"username,omitempty"` + IconUrl string `json:"icon_url,omitempty"` + IconEmoji string `json:"icon_emoji,omitempty"` + Channel string `json:"channel,omitempty"` + Text string `json:"text,omitempty"` + LinkNames string `json:"link_names,omitempty"` + Attachments []Attachment `json:"attachments,omitempty"` + UnfurlLinks bool `json:"unfurl_links,omitempty"` + UnfurlMedia bool `json:"unfurl_media,omitempty"` +} + +func (attachment *Attachment) AddField(field Field) *Attachment { + attachment.Fields = append(attachment.Fields, &field) + return attachment +} + +func Send(webhookUrl string, proxy string, payload Payload) error { + body, err := json.Marshal(payload) + if err != nil { + return err + } + req, err := http.NewRequest("POST", webhookUrl, bytes.NewBuffer(body)) + req.Header.Set("Content-Type", "application/json") + + client := &http.Client{} + resp, err := client.Do(req) + if err != nil { + return err + } + defer resp.Body.Close() + + if err != nil { + return err + } + if resp.StatusCode >= http.StatusBadRequest { + return fmt.Errorf("Error sending msg %+v. Status: %v", payload, resp.Status) + } + return nil +} + +func SendErrMsg(msg string) error { + attachment := Attachment{ + Title: "operator stability test failed", + Color: "fatal", + } + payload := Payload{ + Username: "operator-test", + Channel: Channel, + Text: msg, + IconEmoji: ":ghost:", + Attachments: []Attachment{attachment}, + } + err := Send(WebhookUrl, "", payload) + if err != nil { + return err + } + return nil +} + +func SendGoodMsg(msg string) error { + attachment := Attachment{ + Title: "operator stability test success", + Color: "good", + } + payload := Payload{ + Username: "operator-test", + Channel: Channel, + Text: msg, + IconEmoji: ":sun_with_face:", + Attachments: []Attachment{attachment}, + } + err := Send(WebhookUrl, "", payload) + if err != nil { + return err + } + + return nil +} + +func SendWarnMsg(msg string) error { + attachment := Attachment{ + Title: "operator stability test happen warning", + Color: "warning", + } + payload := Payload{ + Username: "operator-test", + Channel: Channel, + Text: msg, + IconEmoji: ":imp:", + Attachments: []Attachment{attachment}, + } + err := Send(WebhookUrl, "", payload) + if err != nil { + return err + } + return nil +} From 4b4a7c82ad3d8756955c3f7ccd2b5baa8b9469c4 Mon Sep 17 00:00:00 2001 From: weekface Date: Fri, 15 Mar 2019 15:54:34 +0800 Subject: [PATCH 2/2] Update tests/slack/slack.go Co-Authored-By: xiaojingchen --- tests/slack/slack.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/slack/slack.go b/tests/slack/slack.go index 554585b6fac..b024a127788 100644 --- a/tests/slack/slack.go +++ b/tests/slack/slack.go @@ -99,7 +99,7 @@ func SendErrMsg(msg string) error { func SendGoodMsg(msg string) error { attachment := Attachment{ - Title: "operator stability test success", + Title: "operator stability test succeeded", Color: "good", } payload := Payload{