Add optional automatic check cycle (clear queue -> scan FIPs -> wait -> repeat)
An admin-controlled scenario that repeats what the operator does by hand: clear the IP queue, scan and enqueue all free Floating IPs, wait until every queued address reaches a terminal state (so results are in the Registry), then wait a configurable interval and start over. - control-api: new auto_cycle singleton table (migration 0008) holding enabled/interval/max-run settings and persisted phase state, so the cycle survives restarts; engine in orchestrator/autocycle.go driven from the existing loop tick with an injectable "now" for deterministic tests. - Interval (default 1h, min 60s) and max wait (default unlimited, timeout outcome) are runtime settings, never hardcoded. - The periodic fip_scan_interval_seconds scan is skipped while the cycle is enabled. An emptied queue mid-cycle counts as finished; stopping during the pause keeps the last cycle's outcome. - API: GET/PUT /api/v1/admin/auto-cycle, POST .../start, POST .../stop. - admin-dashboard: "Автоматический цикл" panel on /settings and an "Автоцикл активен" indicator on /overview. - Tests for db, orchestrator, httpapi and dashboard; run-local-e2e.sh now exercises a full auto cycle; docs updated; bin/ rebuilt with refreshed SHA256SUMS. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
008ae1b0db
commit
cd37b10f3b
35 files changed
+2205
-16
No files matched your search
@@ -724,3 +724,186 @@ func TestFilterRegistryItems(t *testing.T) {
|
||||
t.Fatalf("expected q+status combined with AND to exclude non-matching, got %+v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAutoCyclePanelRenders(t *testing.T) {
|
||||
fake, caURL := newFakeControlAPI(t)
|
||||
finished := time.Now().Add(-time.Hour)
|
||||
fake.autoCycle = autoCycleDTO{
|
||||
IntervalSeconds: 5400, MaxRunSeconds: 1800, Phase: "waiting",
|
||||
LastRunFinishedAt: &finished, LastOutcome: "timeout", LastError: "checks did not finish", RunsTotal: 4,
|
||||
}
|
||||
ts := newTestServer(t, caURL)
|
||||
|
||||
page := get(t, ts, "/settings")
|
||||
for _, want := range []string{
|
||||
"Автоматический цикл",
|
||||
"Интервал между циклами (мин)",
|
||||
"Максимальная длительность проверки (мин, 0 = без лимита)",
|
||||
`name="interval_minutes" min="1" step="any" value="90"`,
|
||||
`name="max_run_minutes" min="0" step="any" value="30"`,
|
||||
"Автоцикл выключен",
|
||||
"пауза между циклами",
|
||||
"превышено время ожидания",
|
||||
"checks did not finish",
|
||||
`hx-put="/settings/auto-cycle"`,
|
||||
`hx-post="/settings/auto-cycle/start"`,
|
||||
"Включить",
|
||||
"Сохранить",
|
||||
} {
|
||||
if !strings.Contains(page, want) {
|
||||
t.Fatalf("expected %q on the settings page, got:\n%s", want, page)
|
||||
}
|
||||
}
|
||||
if strings.Contains(page, "Выключить") {
|
||||
t.Fatalf("stop button must be hidden while the auto-cycle is disabled, got:\n%s", page)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAutoCycleStartAndStopButtons(t *testing.T) {
|
||||
fake, caURL := newFakeControlAPI(t)
|
||||
ts := newTestServer(t, caURL)
|
||||
|
||||
body := postForm(t, ts, "POST", "/settings/auto-cycle/start", nil)
|
||||
if !fake.autoCycle.Enabled {
|
||||
t.Fatalf("expected the fake control-api auto-cycle enabled after start")
|
||||
}
|
||||
if !strings.Contains(body, "Автоцикл включён") || !strings.Contains(body, "Выключить") ||
|
||||
!strings.Contains(body, `hx-post="/settings/auto-cycle/stop"`) {
|
||||
t.Fatalf("expected the re-rendered panel to show the enabled state with a stop button, got:\n%s", body)
|
||||
}
|
||||
if strings.Contains(body, "alert-") {
|
||||
t.Fatalf("expected no error banner after a successful start, got:\n%s", body)
|
||||
}
|
||||
|
||||
body = postForm(t, ts, "POST", "/settings/auto-cycle/stop", nil)
|
||||
if fake.autoCycle.Enabled {
|
||||
t.Fatalf("expected the fake control-api auto-cycle disabled after stop")
|
||||
}
|
||||
if !strings.Contains(body, "Автоцикл выключен") || !strings.Contains(body, "Включить") ||
|
||||
!strings.Contains(body, "остановлен") {
|
||||
t.Fatalf("expected the re-rendered panel to show the stopped state, got:\n%s", body)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAutoCyclePutConvertsMinutesToSeconds(t *testing.T) {
|
||||
fake, caURL := newFakeControlAPI(t)
|
||||
ts := newTestServer(t, caURL)
|
||||
|
||||
body := postForm(t, ts, "PUT", "/settings/auto-cycle", map[string][]string{
|
||||
"interval_minutes": {"30"}, "max_run_minutes": {"10"},
|
||||
})
|
||||
if fake.autoCycle.IntervalSeconds != 1800 || fake.autoCycle.MaxRunSeconds != 600 {
|
||||
t.Fatalf("expected 1800/600 seconds at control-api, got %d/%d",
|
||||
fake.autoCycle.IntervalSeconds, fake.autoCycle.MaxRunSeconds)
|
||||
}
|
||||
if !strings.Contains(body, `name="interval_minutes" min="1" step="any" value="30"`) ||
|
||||
!strings.Contains(body, `name="max_run_minutes" min="0" step="any" value="10"`) {
|
||||
t.Fatalf("expected updated minutes in the re-rendered form, got:\n%s", body)
|
||||
}
|
||||
if fake.autoCycle.Enabled {
|
||||
t.Fatalf("saving parameters must not toggle the auto-cycle")
|
||||
}
|
||||
|
||||
// 0 = no limit.
|
||||
postForm(t, ts, "PUT", "/settings/auto-cycle", map[string][]string{
|
||||
"interval_minutes": {"30"}, "max_run_minutes": {"0"},
|
||||
})
|
||||
if fake.autoCycle.MaxRunSeconds != 0 {
|
||||
t.Fatalf("expected max_run_seconds 0, got %d", fake.autoCycle.MaxRunSeconds)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAutoCyclePutValidationErrors(t *testing.T) {
|
||||
fake, caURL := newFakeControlAPI(t)
|
||||
ts := newTestServer(t, caURL)
|
||||
|
||||
// Below the control-api minimum (60 s): rejected by control-api with
|
||||
// 400, surfaced as a warning banner; stored value is untouched.
|
||||
body := postForm(t, ts, "PUT", "/settings/auto-cycle", map[string][]string{
|
||||
"interval_minutes": {"0.5"}, "max_run_minutes": {"0"},
|
||||
})
|
||||
if !strings.Contains(body, "alert-warning") {
|
||||
t.Fatalf("expected client error banner for a too-short interval, got:\n%s", body)
|
||||
}
|
||||
if fake.autoCycle.IntervalSeconds != 3600 {
|
||||
t.Fatalf("expected interval unchanged, got %d", fake.autoCycle.IntervalSeconds)
|
||||
}
|
||||
|
||||
// Negative max run.
|
||||
body = postForm(t, ts, "PUT", "/settings/auto-cycle", map[string][]string{
|
||||
"interval_minutes": {"10"}, "max_run_minutes": {"-5"},
|
||||
})
|
||||
if !strings.Contains(body, "alert-warning") {
|
||||
t.Fatalf("expected client error banner for a negative max run, got:\n%s", body)
|
||||
}
|
||||
|
||||
// Non-numeric input is caught by the dashboard itself.
|
||||
body = postForm(t, ts, "PUT", "/settings/auto-cycle", map[string][]string{
|
||||
"interval_minutes": {"abc"}, "max_run_minutes": {"0"},
|
||||
})
|
||||
if !strings.Contains(body, "alert-warning") || !strings.Contains(body, "интервал должен быть числом минут") {
|
||||
t.Fatalf("expected a Russian banner for a non-numeric interval, got:\n%s", body)
|
||||
}
|
||||
body = postForm(t, ts, "PUT", "/settings/auto-cycle", map[string][]string{
|
||||
"interval_minutes": {"10"}, "max_run_minutes": {"abc"},
|
||||
})
|
||||
if !strings.Contains(body, "alert-warning") || !strings.Contains(body, "максимальная длительность") {
|
||||
t.Fatalf("expected a Russian banner for a non-numeric max run, got:\n%s", body)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOverviewShowsAutoCycleIndicatorWhenEnabled(t *testing.T) {
|
||||
fake, caURL := newFakeControlAPI(t)
|
||||
next := time.Now().Add(30 * time.Minute)
|
||||
fake.autoCycle = autoCycleDTO{Enabled: true, IntervalSeconds: 3600, Phase: "waiting", NextRunAt: &next}
|
||||
ts := newTestServer(t, caURL)
|
||||
|
||||
page := get(t, ts, "/overview")
|
||||
for _, want := range []string{"Автоцикл активен", "пауза между циклами", "следующий запуск"} {
|
||||
if !strings.Contains(page, want) {
|
||||
t.Fatalf("expected %q in the overview indicator, got:\n%s", want, page)
|
||||
}
|
||||
}
|
||||
// The indicator sits inside the stats block, so the DOM order
|
||||
// stats -> filter -> tables is preserved.
|
||||
indIdx := strings.Index(page, "Автоцикл активен")
|
||||
filterIdx := strings.Index(page, `id="overview-filter"`)
|
||||
tablesIdx := strings.Index(page, `id="overview-tables"`)
|
||||
if !(indIdx < filterIdx && filterIdx < tablesIdx) {
|
||||
t.Fatalf("expected indicator -> filter -> tables order, got %d/%d/%d", indIdx, filterIdx, tablesIdx)
|
||||
}
|
||||
|
||||
// The polled fragment refreshes it through the stats OOB block.
|
||||
frag := get(t, ts, "/overview/fragment")
|
||||
oobIdx := strings.Index(frag, `id="overview-stats" hx-swap-oob="true"`)
|
||||
if oobIdx < 0 || !strings.Contains(frag[oobIdx:], "Автоцикл активен") {
|
||||
t.Fatalf("expected the indicator inside the OOB stats block, got:\n%s", frag)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOverviewHidesAutoCycleIndicatorWhenDisabledOrUnavailable(t *testing.T) {
|
||||
fake, caURL := newFakeControlAPI(t)
|
||||
ts := newTestServer(t, caURL)
|
||||
|
||||
// Disabled: no indicator.
|
||||
if page := get(t, ts, "/overview"); strings.Contains(page, "Автоцикл активен") {
|
||||
t.Fatalf("indicator must be hidden while the auto-cycle is disabled, got:\n%s", page)
|
||||
}
|
||||
|
||||
// Enabled but the auto-cycle API fails: the overview still renders
|
||||
// (non-fatal), without the indicator and without an error banner.
|
||||
fake.mu.Lock()
|
||||
fake.autoCycle.Enabled = true
|
||||
fake.autoCycleDown = true
|
||||
fake.mu.Unlock()
|
||||
page := get(t, ts, "/overview")
|
||||
if strings.Contains(page, "Автоцикл активен") {
|
||||
t.Fatalf("indicator must be hidden when the auto-cycle API fails, got:\n%s", page)
|
||||
}
|
||||
if strings.Contains(page, "alert-") {
|
||||
t.Fatalf("an auto-cycle failure must not raise an error banner on the overview, got:\n%s", page)
|
||||
}
|
||||
if !strings.Contains(page, "всего IP") {
|
||||
t.Fatalf("expected the overview to still render its stats, got:\n%s", page)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user