Skip to content

Commit

Permalink
WIP: adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kzap committed Mar 10, 2024
1 parent d4cb4dd commit 9494631
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 57 deletions.
73 changes: 16 additions & 57 deletions pkg/services/datadog.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,71 +2,41 @@ package services

import (
"bytes"
"context"
"fmt"
texttemplate "text/template"

slackutil "github.com/argoproj/notifications-engine/pkg/util/slack"
)

type DatadogNotification struct {
Attachments string `json:"attachments,omitempty"`
Blocks string `json:"blocks,omitempty"`
GroupingKey string `json:"groupingKey"`
NotifyBroadcast bool `json:"notifyBroadcast"`
DeliveryPolicy slackutil.DeliveryPolicy `json:"deliveryPolicy"`
AlertType string `json:"alertType,omitempty"`
Tags string `json:"tags,omitempty"`
}

func (n *DatadogNotification) GetTemplater(name string, f texttemplate.FuncMap) (Templater, error) {
slackAttachments, err := texttemplate.New(name).Funcs(f).Parse(n.Attachments)
if err != nil {
return nil, err
}
slackBlocks, err := texttemplate.New(name).Funcs(f).Parse(n.Blocks)
if err != nil {
return nil, err
}
groupingKey, err := texttemplate.New(name).Funcs(f).Parse(n.GroupingKey)
tags, err := texttemplate.New(name).Funcs(f).Parse(n.Tags)
if err != nil {
return nil, err
}

return func(notification *Notification, vars map[string]interface{}) error {
if notification.Slack == nil {
notification.Slack = &SlackNotification{}
if notification.Datadog == nil {
notification.Datadog = &DatadogNotification{}
}
var slackAttachmentsData bytes.Buffer
if err := slackAttachments.Execute(&slackAttachmentsData, vars); err != nil {
return err
}
notification.Slack.Attachments = slackAttachmentsData.String()

var slackBlocksData bytes.Buffer
if err := slackBlocks.Execute(&slackBlocksData, vars); err != nil {
return err
}
notification.Slack.Blocks = slackBlocksData.String()
notification.Datadog.AlertType = n.AlertType

var groupingKeyData bytes.Buffer
if err := groupingKey.Execute(&groupingKeyData, vars); err != nil {
var tagsData bytes.Buffer
if err := tags.Execute(&tagsData, vars); err != nil {
return err
}
notification.Slack.GroupingKey = groupingKeyData.String()
notification.Datadog.Tags = tagsData.String()

notification.Slack.NotifyBroadcast = n.NotifyBroadcast
notification.Slack.DeliveryPolicy = n.DeliveryPolicy
return nil
}, nil
}

type DatadogOptions struct {
Username string `json:"username"`
Icon string `json:"icon"`
Token string `json:"token"`
SigningSecret string `json:"signingSecret"`
Channels []string `json:"channels"`
InsecureSkipVerify bool `json:"insecureSkipVerify"`
ApiURL string `json:"apiURL"`
DisableUnfurl bool `json:"disableUnfurl"`
DDApiKey string `json:"ddApiKey"`
DDAppKey string `json:"ddAppKey"`
}

type datadogService struct {
Expand All @@ -78,19 +48,8 @@ func NewDatadogService(opts DatadogOptions) NotificationService {
}

func (s *datadogService) Send(notification Notification, dest Destination) error {
slackNotification, msgOptions, err := buildMessageOptions(notification, dest, s.opts)
if err != nil {
return err
}
return slackutil.NewThreadedClient(
newSlackClient(s.opts),
slackState,
).SendMessage(
context.TODO(),
dest.Recipient,
slackNotification.GroupingKey,
slackNotification.NotifyBroadcast,
slackNotification.DeliveryPolicy,
msgOptions,
)
// print notificaiton.Datadog
fmt.Printf("%+v", notification.Datadog)

return nil
}
74 changes: 74 additions & 0 deletions pkg/services/datadog_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package services

import (
"testing"
"text/template"

"github.com/stretchr/testify/assert"
)

func TestGetTemplater_Datadog(t *testing.T) {
n := Notification{
Datadog: &DatadogNotification{
AlertType: "info",
Tags: "{{.context.argocdUrl}}",
},
}
templater, err := n.GetTemplater("", template.FuncMap{})

if !assert.NoError(t, err) {
return
}

var notification Notification
err = templater(&notification, map[string]interface{}{
"context": map[string]interface{}{
"argocdUrl": "https://example.com",
"state": "success",
},
"app": map[string]interface{}{
"metadata": map[string]interface{}{
"name": "argocd-notifications",
},
"spec": map[string]interface{}{
"source": map[string]interface{}{
"repoURL": "https://github.com/argoproj-labs/argocd-notifications.git",
},
},
"status": map[string]interface{}{
"operationState": map[string]interface{}{
"syncResult": map[string]interface{}{
"revision": "0123456789",
},
},
},
},
})

if !assert.NoError(t, err) {
return
}

assert.Equal(t, "https://github.com/argoproj-labs/argocd-notifications.git", notification.GitHub.repoURL)
assert.Equal(t, "0123456789", notification.GitHub.revision)
assert.Equal(t, "success", notification.GitHub.Status.State)
assert.Equal(t, "continuous-delivery/argocd-notifications", notification.GitHub.Status.Label)
assert.Equal(t, "https://example.com/applications/argocd-notifications", notification.GitHub.Status.TargetURL)
}

/*
func TestSend_DataDogService_BadURL(t *testing.T) {
e := datadogService{}.Send(
Notification{
GitHub: &GitHubNotification{
repoURL: "hello",
},
},
Destination{
Service: "",
Recipient: "",
},
)
assert.ErrorContains(t, e, "does not have a `/`")
}
*/

0 comments on commit 9494631

Please sign in to comment.