Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
83bytes committed Jun 30, 2024
1 parent 0686520 commit 6dad012
Showing 5 changed files with 139 additions and 5 deletions.
55 changes: 55 additions & 0 deletions action/noopAction_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package action

import (
"alertmanager/logging"
"alertmanager/types"
"bytes"
"os"
"strings"
"testing"
)

func TestNoopAction(t *testing.T) {

t.Run("basic noop action", func(t *testing.T) {
alert := types.Alert{AlertName: "TestAlert"}
action := types.Action{ActionName: "TestAction", ActionArgs: "actionArg1,actionArg3"}
resultMap := map[string]interface{}{
"key1": "value1",
"key2": "value2",
}

log, err := logging.NewLogger("DEBUG")
if err != nil {
t.Errorf("error initializing logger")
}
var buf bytes.Buffer
log.SetOutput(&buf)
defer func() {
log.SetOutput(os.Stderr)
}()

err = NoopAction(alert, action, resultMap)
if err != nil {
t.Errorf("expected no error, got %v", err)
}

output := buf.String()
if !strings.Contains(output, "noop action called") {
t.Errorf("expected log to contain 'noop action called', got %s", output)
}
if !strings.Contains(output, "alert: TestAlert") {
t.Errorf("expected log to contain 'alert: TestAlert', got %s", output)
}
if !strings.Contains(output, "action: TestAction") {
t.Errorf("expected log to contain 'action: TestAction', got %s", output)
}
if !strings.Contains(output, "result of key1 enrichment(s): value1") {
t.Errorf("expected log to contain 'result of key1 enrichment(s): value1', got %s", output)
}
if !strings.Contains(output, "result of key2 enrichment(s): value2") {
t.Errorf("expected log to contain 'result of key2 enrichment(s): value2', got %s", output)
}
})

}
4 changes: 0 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -22,7 +22,3 @@ func Execute() {
os.Exit(1)
}
}

func init() {

}
49 changes: 49 additions & 0 deletions enrichment/noopEnrichment_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package enrichment

import (
"alertmanager/logging"
"alertmanager/types"
"os"
"strings"
"testing"
)

func TestNoopEnrichment(t *testing.T) {

t.Run("basic noop enrichment", func(t *testing.T) {
alert := types.Alert{AlertName: "TestAlert"}
enrichment := types.Enrichment{EnrichmentName: "TestEnrichment", EnrichmentArgs: "enrichmentArg1,EnrichmentArg2"}

log, err := logging.NewLogger("DEBUG")
if err != nil {
t.Errorf("error initializing logger")
}
nullFile, _ := os.OpenFile(os.DevNull, os.O_WRONLY, 0644)
log.SetOutput(nullFile)
result, _ := NoopEnrichment(alert, enrichment)

if resultStr, ok := result.(string); ok {

if !strings.Contains(resultStr, "noop enrichment called") {
t.Errorf("expected result to contain 'noop enrichment called', got %s", resultStr)
}

if !strings.Contains(resultStr, "alert: TestAlert") {
t.Errorf("expected result to contain 'alert: TestAlert', got %s", resultStr)
}

if !strings.Contains(resultStr, "enrichment: TestEnrichment") {
t.Errorf("expected result to contain 'enrichment: TestEnrichment', got %s", resultStr)
}

if !strings.Contains(resultStr, "with args: enrichmentArg1,EnrichmentArg2") {
t.Errorf("expected result to contain 'with args: enrichmentArg1,EnrichmentArg2', got %s", resultStr)
}
} else {
t.Errorf("not returing a valid string got: %s, %s", result, resultStr)

}

})

}
34 changes: 34 additions & 0 deletions enrichment/upperCaseEncrichment_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package enrichment

import (
"alertmanager/logging"
"alertmanager/types"
"strings"
"testing"
)

func TestUpperCaseEnrichment(t *testing.T) {

t.Run("upper case enrichment", func(t *testing.T) {
alert := types.Alert{AlertName: "TestAlert"}
enrichment := types.Enrichment{EnrichmentName: "UPPER_CASE", EnrichmentArgs: "enrichmentArg1,enrichmentArg2"}

_, err := logging.NewLogger("DEBUG")
if err != nil {
t.Errorf("error initializing logger")
}

result, _ := UpperCaseEnrichment(alert, enrichment)

if resultStr, ok := result.(string); ok {
if !strings.Contains(resultStr, "ENRICHMENTARG1,ENRICHMENTARG2") {
t.Errorf("expected result to contain 'ENRICHMENTARG1,ENRICHMENTARG2', got %s", resultStr)
}
} else {
t.Errorf("not returing a valid string got: %s, %s", result, resultStr)

}

})

}
2 changes: 1 addition & 1 deletion types/alertManagerConfig.go
Original file line number Diff line number Diff line change
@@ -41,7 +41,7 @@ func (am *AlertManagerConfig) GetPipelineForAlert(name string) *AlertPipelineCon
logr := logging.GetLogger()
for _, pipes := range am.AlertPipelines {
if pipes.AlertName == name {
logr.Debug("Pipeline found for alert : ", name)
logr.Debug("pipeline found for alert : ", name)
return &pipes
}
}

0 comments on commit 6dad012

Please sign in to comment.