Skip to content

Commit

Permalink
Add test coverage for alerter service
Browse files Browse the repository at this point in the history
  • Loading branch information
jwilder committed Feb 28, 2025
1 parent 6e9e4f9 commit fc36faf
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 9 deletions.
10 changes: 1 addition & 9 deletions alerter/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ func NewService(opts *AlerterOpts) (*Alerter, error) {
queue: make(chan struct{}, opts.Concurrency),
CtrlCli: opts.CtrlCli,
ruleStore: ruleStore,
clients: make(map[string]KustoClient),
}

if opts.MSIID != "" {
Expand Down Expand Up @@ -170,15 +171,6 @@ func Lint(ctx context.Context, opts *AlerterOpts, path string) error {
Tags: opts.Tags,
})

go func() {
logger.Infof("Listening at :%d", opts.Port)
http.Handle("/metrics", promhttp.Handler())
if err := http.ListenAndServe(fmt.Sprintf(":%d", opts.Port), nil); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}()

executor.RunOnce(ctx)
if lint.HasFailedQueries() {
return fmt.Errorf("failed to lint rules")
Expand Down
103 changes: 103 additions & 0 deletions alerter/service_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package alerter_test

import (
"context"
"os"
"path/filepath"
"testing"

"github.com/Azure/adx-mon/alerter"
"github.com/stretchr/testify/require"
)

func TestNewService(t *testing.T) {
opts := &alerter.AlerterOpts{
Dev: true,
KustoEndpoints: map[string]string{"fake": "http://fake.endpoint"},
Region: "test-region",
AlertAddr: "http://localhost:8080",
Cloud: "Azure",
Port: 8080,
Concurrency: 5,
Tags: map[string]string{"env": "test"},
MaxNotifications: 10,
MSIID: "test-msi-id",
MSIResource: "https://kusto.kusto.windows.net",
KustoToken: "test-token",
CtrlCli: nil,
}

service, err := alerter.NewService(opts)
require.NoError(t, err)
require.NotNil(t, service)
}

func TestAlerter_OpenClose(t *testing.T) {
opts := &alerter.AlerterOpts{
Dev: true,
KustoEndpoints: map[string]string{"fake": "http://fake.endpoint"},
Region: "test-region",
AlertAddr: "http://localhost:8080",
Cloud: "Azure",
Port: 8080,
Concurrency: 5,
Tags: map[string]string{"env": "test"},
MaxNotifications: 10,
MSIID: "test-msi-id",
MSIResource: "https://kusto.kusto.windows.net",
KustoToken: "test-token",
CtrlCli: nil,
}

service, err := alerter.NewService(opts)
require.NoError(t, err)
require.NotNil(t, service)

ctx := context.Background()
err = service.Open(ctx)
require.NoError(t, err)

err = service.Close()
require.NoError(t, err)
}

func TestLint(t *testing.T) {

filename := filepath.Join(t.TempDir(), "alert_rule.yaml")
sampleRule := `
---
apiVersion: adx-mon.azure.com/v1
kind: AlertRule
metadata:
name: alert-name
namespace: namespace
spec:
database: DB
interval: 5m
query: |
Heartbeat
autoMitigateAfter: 1h
destination: "somewhere"
`
require.NoError(t, os.WriteFile(filename, []byte(sampleRule), 0644))

opts := &alerter.AlerterOpts{
Dev: true,
KustoEndpoints: map[string]string{"DB": "http://fake.endpoint"},
Region: "test-region",
AlertAddr: "http://localhost:8080",
Cloud: "Azure",
Port: 8080,
Concurrency: 5,
Tags: map[string]string{"env": "test"},
MaxNotifications: 10,
MSIID: "test-msi-id",
MSIResource: "https://kusto.kusto.windows.net",
KustoToken: "test-token",
CtrlCli: nil,
}

ctx := context.Background()
err := alerter.Lint(ctx, opts, filename)
require.NoError(t, err)
}

0 comments on commit fc36faf

Please sign in to comment.