mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-12-03 18:43:38 +01:00
golangci-lint had accumulated 12 findings since 5abb2d8f4. All fixed with
real dedup (no nolint suppressions) and the repo's existing errcheck idioms:
- dupl internal/monitoring: docker/host identity-conflict trackers were
structural clones; extracted a shared identityFlapTracker core with a
domain-neutral identityConflict result. Per-domain files now hold only
the window const and the model translation. Tracker-behavior tests
consolidated into identity_flap_tracker_test.go; Monitor-level
translation and Apply*Report integration tests remain per domain.
- dupl internal/api/router.go: VM/container workload chart loops shared a
16-line live-fallback block; extracted guestChartSeriesWithLiveFallback
over a guestLiveMetricsView interface both views satisfy.
- dupl internal/storagehealth/risk.go: SMART attribute copying extracted
into applySMARTAttributes shared by both assessors (same
*models.SMARTAttributes type on both inputs).
- errcheck pkg/audit/sqlite_logger.go: three defer tx.Rollback() sites
now use the repo-wide defer func() { _ = tx.Rollback() }() idiom.
- errcheck telemetry/notifications tests: send() errors now fail the
test; queue.Stop() uses the package's _ = idiom.
Full test suites pass for all six touched packages.
Contract-Neutral: lint-hygiene restoration: dupl dedup (identical logic extracted to shared helpers) and errcheck idiom fixes; no public-contract or behavioral delta
149 lines
4.1 KiB
Go
149 lines
4.1 KiB
Go
package notifications
|
|
|
|
import (
|
|
"sort"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/rcourtman/pulse-go-rewrite/internal/alerts"
|
|
)
|
|
|
|
func TestInitialNotifyTargetNormalization(t *testing.T) {
|
|
manager := NewNotificationManagerWithDataDir("", t.TempDir())
|
|
defer manager.Stop()
|
|
|
|
if actual := manager.GetInitialNotifyTarget(); actual != "all" {
|
|
t.Fatalf("default initial target = %q, want all", actual)
|
|
}
|
|
|
|
tests := map[string]string{
|
|
"EMAIL": "email",
|
|
" webhooks": "webhook",
|
|
"apprise": "apprise",
|
|
"invalid": "all",
|
|
}
|
|
for input, expected := range tests {
|
|
manager.SetInitialNotifyTarget(input)
|
|
if actual := manager.GetInitialNotifyTarget(); actual != expected {
|
|
t.Fatalf("initial target after %q = %q, want %q", input, actual, expected)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestNotificationDeliveryJobsRespectTarget(t *testing.T) {
|
|
email := EmailConfig{
|
|
Enabled: true,
|
|
SMTPHost: "smtp.example.test",
|
|
SMTPPort: 587,
|
|
From: "pulse@example.test",
|
|
To: []string{"ops@example.test"},
|
|
}
|
|
webhooks := []WebhookConfig{
|
|
{ID: "hook-1", Name: "First", URL: "https://hooks.example.test/one", Enabled: true},
|
|
{ID: "hook-2", Name: "Second", URL: "https://hooks.example.test/two", Enabled: true},
|
|
}
|
|
apprise := AppriseConfig{Enabled: true, Targets: []string{"ntfy://example.test/pulse"}}
|
|
alertList := []*alerts.Alert{{
|
|
ID: "node-1-cpu",
|
|
ResourceName: "node-1",
|
|
StartTime: time.Now(),
|
|
}}
|
|
|
|
tests := []struct {
|
|
name string
|
|
target notificationDeliveryTarget
|
|
expected []string
|
|
}{
|
|
{name: "all", target: notificationDeliveryTargetAll, expected: []string{"apprise", "email", "webhook", "webhook"}},
|
|
{name: "email", target: notificationDeliveryTargetEmail, expected: []string{"email"}},
|
|
{name: "webhook", target: notificationDeliveryTargetWebhook, expected: []string{"webhook", "webhook"}},
|
|
{name: "apprise", target: notificationDeliveryTargetApprise, expected: []string{"apprise"}},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
jobs := buildNotificationDeliveryJobsForTarget(
|
|
email,
|
|
webhooks,
|
|
apprise,
|
|
alertList,
|
|
eventAlert,
|
|
time.Time{},
|
|
test.target,
|
|
)
|
|
actual := make([]string, 0, len(jobs))
|
|
for _, job := range jobs {
|
|
actual = append(actual, job.Type)
|
|
}
|
|
sort.Strings(actual)
|
|
sort.Strings(test.expected)
|
|
if len(actual) != len(test.expected) {
|
|
t.Fatalf("job types = %#v, want %#v", actual, test.expected)
|
|
}
|
|
for index := range actual {
|
|
if actual[index] != test.expected[index] {
|
|
t.Fatalf("job types = %#v, want %#v", actual, test.expected)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestGroupedAlertsUseInitialNotifyTarget(t *testing.T) {
|
|
queue, err := NewNotificationQueue(t.TempDir())
|
|
if err != nil {
|
|
t.Fatalf("create queue: %v", err)
|
|
}
|
|
defer func() { _ = queue.Stop() }()
|
|
|
|
manager := &NotificationManager{
|
|
enabled: true,
|
|
initialTarget: notificationDeliveryTargetApprise,
|
|
emailConfig: EmailConfig{
|
|
Enabled: true,
|
|
SMTPHost: "smtp.example.test",
|
|
SMTPPort: 587,
|
|
From: "pulse@example.test",
|
|
To: []string{"ops@example.test"},
|
|
},
|
|
webhooks: []WebhookConfig{{
|
|
ID: "hook-1", Name: "Webhook", URL: "https://hooks.example.test/pulse", Enabled: true,
|
|
}},
|
|
appriseConfig: AppriseConfig{
|
|
Enabled: true,
|
|
Targets: []string{"ntfy://example.test/pulse"},
|
|
},
|
|
lastNotified: make(map[string]notificationRecord),
|
|
deliveryReceipts: make(map[string]struct{}),
|
|
queue: queue,
|
|
}
|
|
manager.pendingAlerts = []*alerts.Alert{{
|
|
ID: "node-1-cpu",
|
|
ResourceName: "node-1",
|
|
StartTime: time.Now(),
|
|
}}
|
|
|
|
manager.sendGroupedAlerts()
|
|
|
|
rows, err := queue.db.Query(`SELECT type FROM notification_queue ORDER BY created_at`)
|
|
if err != nil {
|
|
t.Fatalf("query queued types: %v", err)
|
|
}
|
|
defer rows.Close()
|
|
|
|
var queuedTypes []string
|
|
for rows.Next() {
|
|
var queuedType string
|
|
if err := rows.Scan(&queuedType); err != nil {
|
|
t.Fatalf("scan queued type: %v", err)
|
|
}
|
|
queuedTypes = append(queuedTypes, queuedType)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
t.Fatalf("iterate queued types: %v", err)
|
|
}
|
|
if len(queuedTypes) != 1 || queuedTypes[0] != "apprise" {
|
|
t.Fatalf("queued types = %#v, want only apprise", queuedTypes)
|
|
}
|
|
}
|