Skip to content

Commit

Permalink
add unit test for teams, fix type on errors, add comment why ignoring…
Browse files Browse the repository at this point in the history
… return values
  • Loading branch information
tmu-sprd authored and dhollinger committed Jun 5, 2024
1 parent 05fb5e2 commit 1ab9ea3
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/chatops/teams.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (c *ChatOps) teams(code int, target string, output interface{}) (*adaptivec

// Change texts and add details button, depending on type of output (error, QueueItem, any)
switch fmt.Sprintf("%T", output) {
case "error":
case "*errors.errorString":
messageTextBlock = NewTextBlock(fmt.Sprintf("Error: %s", output), color)
details = false
case "*queue.QueueItem":
Expand Down Expand Up @@ -132,6 +132,7 @@ func ScanforWarn(output interface{}) bool {
func getCaller() string {
var callerFunction string

// ignoring return values 'file name' and 'line number in that file', because those values are not needed
pc, _, _, ok := runtime.Caller(3)
runtimedetails := runtime.FuncForPC(pc)
if ok && runtimedetails != nil {
Expand Down
116 changes: 116 additions & 0 deletions lib/chatops/teams_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package chatops

import (
"encoding/json"
"fmt"
"testing"

"github.com/atc0005/go-teams-notify/v2/adaptivecard"
"github.com/stretchr/testify/assert"
)

func TestTeams(t *testing.T) {
t.Run("Teams Message Post", func(t *testing.T) {
serverURI := "https://example.webhook.office.com/webhook/xxx"
c := ChatOps{
Service: "teams",
TestMode: true,
ServerURI: &serverURI,
}

t.Run("Good module", func(t *testing.T) {
// test a successful message
msg, err := c.teams(202, "good_module", "things went well")

// Prepare message payload
msg.Prepare()

var data adaptivecard.Message
json.Unmarshal([]byte(msg.PrettyPrint()), &data)

assert.NoError(t, err, "should not error")

// assert header text
// two spaces in the text "Deploy good_module", because calling function can't be determined
assert.Equal(t, data.Attachments[0].Content.Body[0].Text, "Deploy good_module", `header should be "Deploy good_module"`)
assert.Equal(t, data.Attachments[0].Content.Body[0].Color, "good", `header color should be "good"`)

// assert body text
assert.Equal(t, data.Attachments[0].Content.Body[1].Text, "Deployment of good_module successful", `body text should be "Deployment of good_module successful"`)
assert.Equal(t, data.Attachments[0].Content.Body[1].Color, "good", `body color should be "good"`)

// assert details text
assert.Equal(t, data.Attachments[0].Content.Body[2].Text, "r10k output:\n\nthings went well", `details text should be "r10k output:\n\nthings went well"`)
})
t.Run("Failed module", func(t *testing.T) {
// test a fail message
msg, err := c.teams(500, "fail_module", "something failed")

// Prepare message payload
msg.Prepare()

var data adaptivecard.Message
json.Unmarshal([]byte(msg.PrettyPrint()), &data)
// log.Print(msg.PrettyPrint())
// log.Print(data.Attachments[0].Content.Body[0].Text)

assert.NoError(t, err, "should not error")

// assert header text
// two spaces in the text "Deploy fail_module", because calling function can't be determined
assert.Equal(t, data.Attachments[0].Content.Body[0].Text, "Deploy fail_module", `header should be "Deploy fail_module"`)
assert.Equal(t, data.Attachments[0].Content.Body[0].Color, "attention", `header color should be "attention"`)

// assert body text
assert.Equal(t, data.Attachments[0].Content.Body[1].Text, "Deployment of fail_module failed", `body text should be "Deployment of fail_module failed"`)
assert.Equal(t, data.Attachments[0].Content.Body[1].Color, "attention", `body color should be "attention"`)

// assert details text
assert.Equal(t, data.Attachments[0].Content.Body[2].Text, "r10k output:\n\nsomething failed", `details text should be "r10k output:\n\nsomething failed"`)
})
t.Run("Module with warnings", func(t *testing.T) {
// test a message with warning
msg, err := c.teams(202, "warn_module", "WARN -> there are warnings")

// Prepare message payload
msg.Prepare()

var data adaptivecard.Message
json.Unmarshal([]byte(msg.PrettyPrint()), &data)
// log.Print(msg.PrettyPrint())
// log.Print(data.Attachments[0].Content.Body[0].Text)

assert.NoError(t, err, "should not error")

// assert header text
// two spaces in the text "Deploy warn_module", because calling function can't be determined
assert.Equal(t, data.Attachments[0].Content.Body[0].Text, "Deploy warn_module", `header should be "Deploy warn_module"`)
assert.Equal(t, data.Attachments[0].Content.Body[0].Color, "warning", `header color should be "attention"`)

// assert body text
assert.Equal(t, data.Attachments[0].Content.Body[1].Text, "Deployment of warn_module successful with Warnings", `body text should be "Deployment of warn_module successful with Warnings"`)
assert.Equal(t, data.Attachments[0].Content.Body[1].Color, "warning", `body color should be "attention"`)

// assert details text
assert.Equal(t, data.Attachments[0].Content.Body[2].Text, "r10k output:\n\nWARN -> there are warnings", `details text should be "r10k output:\n\nWARN -> there are warnings"`)
})
t.Run("Error", func(t *testing.T) {
// test an error state
msg, err := c.teams(500, "error_module", fmt.Errorf("an error occurred"))

// Prepare message payload
msg.Prepare()

var data adaptivecard.Message
json.Unmarshal([]byte(msg.PrettyPrint()), &data)

assert.NoError(t, err, "should not error")

// assert header text
// two spaces in the text "Deploy warn_module", because calling function can't be determined
assert.Equal(t, data.Attachments[0].Content.Body[0].Text, "Deploy error_module", `header should be "Deploy error_module"`)
assert.Equal(t, data.Attachments[0].Content.Body[0].Color, "attention", `header color should be "attention"`)
assert.Equal(t, data.Attachments[0].Content.Body[1].Text, "Error: an error occurred", `body should be "Error: an error occurred"`)
})
})
}

0 comments on commit 1ab9ea3

Please sign in to comment.