-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
5 changed files
with
139 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,7 +22,3 @@ func Execute() { | |
os.Exit(1) | ||
} | ||
} | ||
|
||
func init() { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
} | ||
|
||
}) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
} | ||
|
||
}) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters