diff --git a/action/noopAction_test.go b/action/noopAction_test.go new file mode 100644 index 0000000..4c31b9e --- /dev/null +++ b/action/noopAction_test.go @@ -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) + } + }) + +} diff --git a/cmd/root.go b/cmd/root.go index 385a02b..b2e0211 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -22,7 +22,3 @@ func Execute() { os.Exit(1) } } - -func init() { - -} diff --git a/enrichment/noopEnrichment_test.go b/enrichment/noopEnrichment_test.go new file mode 100644 index 0000000..e87d304 --- /dev/null +++ b/enrichment/noopEnrichment_test.go @@ -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) + + } + + }) + +} diff --git a/enrichment/upperCaseEncrichment_test.go b/enrichment/upperCaseEncrichment_test.go new file mode 100644 index 0000000..13fdec8 --- /dev/null +++ b/enrichment/upperCaseEncrichment_test.go @@ -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) + + } + + }) + +} diff --git a/types/alertManagerConfig.go b/types/alertManagerConfig.go index 42a14f0..28c46c8 100644 --- a/types/alertManagerConfig.go +++ b/types/alertManagerConfig.go @@ -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 } }