Skip to content

Commit

Permalink
main fn cammot be called in Test file
Browse files Browse the repository at this point in the history
Signed-off-by: manisha kumari <[email protected]>
  • Loading branch information
ManishaKumari295 committed May 30, 2024
1 parent b7fd689 commit 62f7f31
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 46 deletions.
13 changes: 0 additions & 13 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,6 @@ func messageStatus(event *corev2.Event) string {
}
}

// Manisha found
func messageAttachment(event *corev2.Event) slack.Attachment {
description, err := templates.EvalTemplate("description", config.slackDescriptionTemplate, event)
if err != nil {
Expand Down Expand Up @@ -221,7 +220,6 @@ func messageAttachment(event *corev2.Event) slack.Attachment {
return attachment
}

// Manisha found
func sendMessage(event *corev2.Event) error {
hookmsg := &slack.WebhookMessage{
Attachments: []slack.Attachment{messageAttachment(event)},
Expand All @@ -231,17 +229,6 @@ func sendMessage(event *corev2.Event) error {
}

err := slack.PostWebhook(config.slackwebHookURL, hookmsg)

//
//hook := slack.NewWebHook(config.slackwebHookURL)
//
//
//err := hook.PostMessage(&slack.WebHookPostPayload{
// Attachments: []*slack.Attachment{messageAttachment(event)},
// Channel: config.slackChannel,
// IconUrl: config.slackIconURL,
// Username: config.slackUsername,
//})
if err != nil {
return fmt.Errorf("Failed to send Slack message: %v ", err)
}
Expand Down
46 changes: 13 additions & 33 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package main

import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"os"
"testing"

corev2 "github.com/sensu/sensu-go/api/core/v2"
Expand Down Expand Up @@ -109,7 +108,7 @@ func TestSendMessage(t *testing.T) {

var apiStub = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
body, _ := io.ReadAll(r.Body)
expectedBody := `{"channel":"#test","attachments":[{"color":"good","fallback":"RESOLVED - entity1/check1:","title":"Description","text":"","fields":[{"title":"Status","value":"Resolved","short":false},{"title":"Entity","value":"entity1","short":true},{"title":"Check","value":"check1","short":true}]}]}`
expectedBody := `{"channel":"#test","attachments":[{"color":"good","fallback":"RESOLVED - entity1/check1:","title":"Description","fields":[{"title":"Status","value":"Resolved","short":false},{"title":"Entity","value":"entity1","short":true},{"title":"Check","value":"check1","short":true}],"blocks":null}],"replace_original":false,"delete_original":false}`
assert.Equal(expectedBody, string(body))
w.WriteHeader(http.StatusOK)
_, err := w.Write([]byte(`{"ok": true}`))
Expand All @@ -123,34 +122,15 @@ func TestSendMessage(t *testing.T) {
assert.NoError(err)
}

func TestMain(t *testing.T) {
assert := assert.New(t)
file, _ := os.CreateTemp(os.TempDir(), "sensu-handler-slack-")
defer func() {
_ = os.Remove(file.Name())
}()

event := corev2.FixtureEvent("entity1", "check1")
eventJSON, _ := json.Marshal(event)
_, err := file.WriteString(string(eventJSON))
require.NoError(t, err)
require.NoError(t, file.Sync())
_, err = file.Seek(0, 0)
require.NoError(t, err)
os.Stdin = file
requestReceived := false

var apiStub = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
requestReceived = true
w.WriteHeader(http.StatusOK)
_, err := w.Write([]byte(`{"ok": true}`))
require.NoError(t, err)
}))

oldArgs := os.Args
os.Args = []string{"slack", "-w", apiStub.URL}
defer func() { os.Args = oldArgs }()

main()
assert.True(requestReceived)
func TestCheckArgs(t *testing.T) {
// Test case where webhook URL is missing
event := &corev2.Event{}
err := checkArgs(event)
assert.NotNil(t, err)
assert.Equal(t, fmt.Sprintf("--%s or SLACK_WEBHOOK_URL environment variable is required", webHookURL), err.Error())

// Test case where webhook URL is provided
config.slackwebHookURL = "http://example.com/webhook"
err = checkArgs(event)
assert.Nil(t, err)
}

0 comments on commit 62f7f31

Please sign in to comment.