76 lines
3.2 KiB
Go
76 lines
3.2 KiB
Go
package main
|
||
|
||
import (
|
||
"testing"
|
||
"time"
|
||
)
|
||
|
||
func TestMaintenanceFiltersSelectedTargets(t *testing.T) {
|
||
window := MaintenanceWindow{Active: true, ServiceIDs: []int64{7}, VMIDs: []int{105}}
|
||
alerts := []Alert{
|
||
{ID: "service-7-local"},
|
||
{ID: "service-tls-8-public"},
|
||
{ID: "backup-old-105"},
|
||
{ID: "task-failed-x", Message: "VMID 105, status error"},
|
||
{ID: "cpu-usage"},
|
||
}
|
||
active, planned := splitMaintenanceAlerts(alerts, window)
|
||
if len(active) != 2 || len(planned) != 3 {
|
||
t.Fatalf("неверное разделение: active=%+v planned=%+v", active, planned)
|
||
}
|
||
}
|
||
|
||
func TestMaintenanceExpiresAutomatically(t *testing.T) {
|
||
store, err := openStore(":memory:")
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
defer store.Close()
|
||
window, err := store.SaveMaintenanceWindow(MaintenanceWindow{EndsAt: time.Now().Add(time.Hour).Unix(), Reason: "Обновление", AllAlerts: true})
|
||
if err != nil || !window.Active {
|
||
t.Fatalf("режим не включён: %+v %v", window, err)
|
||
}
|
||
if _, err = store.db.Exec(`UPDATE settings SET value=? WHERE key='maintenance_window'`, `{"enabled":true,"startedAt":1,"endsAt":2,"reason":"test","allAlerts":true}`); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if expired := store.MaintenanceWindow(); expired.Active || expired.Enabled {
|
||
t.Fatalf("просроченный режим остался активным: %+v", expired)
|
||
}
|
||
}
|
||
|
||
func TestRunningBackupCreatesScopedAutomaticMaintenance(t *testing.T) {
|
||
now := time.Now()
|
||
window := automaticBackupMaintenance(ProxmoxActivity{Tasks: []ProxmoxTask{{Type: "vzdump", ID: "105"}, {Type: "vzdump", ID: "106", EndTime: now.Unix()}}}, []ServiceLink{{ServiceID: 7, VMID: 105}, {ServiceID: 8, VMID: 999}}, now)
|
||
if !window.Active || !window.Automatic || window.Source != "backup" || len(window.VMIDs) != 1 || window.VMIDs[0] != 105 || len(window.ServiceIDs) != 1 || window.ServiceIDs[0] != 7 {
|
||
t.Fatalf("неверное автоматическое окно: %+v", window)
|
||
}
|
||
}
|
||
|
||
func TestRunningBackupWithoutVMIDSuppressesAllAlerts(t *testing.T) {
|
||
window := automaticBackupMaintenance(ProxmoxActivity{Tasks: []ProxmoxTask{{Type: "vzdump", UPID: "unknown"}}}, nil, time.Now())
|
||
if !window.Active || !window.AllAlerts {
|
||
t.Fatalf("backup с неизвестной целью должен безопасно подавить алерты: %+v", window)
|
||
}
|
||
}
|
||
|
||
func TestPlannedAlertOnlyAppearsInHistory(t *testing.T) {
|
||
store, err := openStore(":memory:")
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
defer store.Close()
|
||
alert := Alert{ID: "service-7-local", Severity: "critical", Source: "Сервисы", Title: "Недоступен"}
|
||
visible, err := store.SyncAlertsWithMaintenance(nil, []Alert{alert}, "Обновление")
|
||
if err != nil || len(visible) != 0 {
|
||
t.Fatalf("плановый алерт стал активным: %+v %v", visible, err)
|
||
}
|
||
history, err := store.AlertHistory(10)
|
||
if err != nil || len(history) != 1 || !history[0].Planned || history[0].MaintenanceReason != "Обновление" {
|
||
t.Fatalf("плановое событие не сохранено: %+v %v", history, err)
|
||
}
|
||
pending, err := store.PendingEmailAlerts()
|
||
if err != nil || len(pending) != 0 {
|
||
t.Fatalf("плановое событие попало в почту: %+v %v", pending, err)
|
||
}
|
||
}
|