From 4efbe7f9bd091cb96202529fe5957936dfba0260 Mon Sep 17 00:00:00 2001 From: Andre Marcelo-Tanner Date: Sat, 9 Mar 2024 22:23:32 -0500 Subject: [PATCH] WIP: adding tests Signed-off-by: Andre Marcelo-Tanner --- pkg/services/datadog.go | 73 ++++++++--------------------------- pkg/services/datadog_test.go | 74 ++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 57 deletions(-) create mode 100644 pkg/services/datadog_test.go diff --git a/pkg/services/datadog.go b/pkg/services/datadog.go index b60d3a7e..ca61449f 100644 --- a/pkg/services/datadog.go +++ b/pkg/services/datadog.go @@ -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 { @@ -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 } diff --git a/pkg/services/datadog_test.go b/pkg/services/datadog_test.go new file mode 100644 index 00000000..91a4ff7f --- /dev/null +++ b/pkg/services/datadog_test.go @@ -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(¬ification, 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 `/`") +} +*/