Files
portainer/api/http/handler/settings/settings_update_test.go
T
claude code agent 32a2b7a9ae feat(automation): health-gated rollback + per-endpoint + notify hook (#12, epic #3 M5)
P0 Health-gated rollback (standalone auto-update path): capture the previous
image id + reference + healthcheck before the recreate, then poll the new
container's health over a configurable window. On healthy proceed (and only
then clean up the old image); on unhealthy/exit/timeout re-tag the old image
back onto the original reference and Recreate (no pull) to restore it, reusing
Recreate's config preservation. The decision is a pure decideRollback() helper.

P1 Per-endpoint enable: ContainerAutomationDisabled flag on Endpoint (zero value
participates, no migration churn), checked by both daemons; settable via the
endpoint update API. UI control deferred (see report).

P2 Notifier seam: minimal Notifier interface + logNotifier, emitting structured
updated/rollback/update-failed/heal-restarted events from the daemon.

Settings: RollbackOnFailure + RollbackTimeout (default 120s) added to
ContainerAutomation.AutoUpdate, wired through defaults/migration/golden,
settings_update validation, the AutoUpdatePanel and the TS types.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-29 10:41:55 +03:00

84 lines
2.7 KiB
Go

package settings
import (
"net/http/httptest"
"testing"
)
func strptr(s string) *string { return &s }
// TestSettingsUpdatePayloadValidateAutoUpdatePollInterval covers the auto-update
// poll-interval floor (F3): durations below minAutoUpdatePollInterval (1m), as
// well as malformed or non-positive durations, must be rejected.
func TestSettingsUpdatePayloadValidateAutoUpdatePollInterval(t *testing.T) {
cases := []struct {
name string
interval string
wantErr bool
}{
{name: "one second is below the floor", interval: "1s", wantErr: true},
{name: "fifty-nine seconds is below the floor", interval: "59s", wantErr: true},
{name: "exactly one minute is allowed", interval: "1m", wantErr: false},
{name: "six hours is allowed", interval: "6h", wantErr: false},
{name: "zero is rejected", interval: "0s", wantErr: true},
{name: "negative is rejected", interval: "-5m", wantErr: true},
{name: "unparseable is rejected", interval: "soon", wantErr: true},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
payload := settingsUpdatePayload{
ContainerAutomation: &containerAutomationSettingsPayload{
AutoUpdate: &autoUpdateSettingsPayload{
PollInterval: strptr(tc.interval),
},
},
}
err := payload.Validate(httptest.NewRequest("PUT", "/settings", nil))
if tc.wantErr && err == nil {
t.Errorf("Validate(%q) = nil, want error", tc.interval)
}
if !tc.wantErr && err != nil {
t.Errorf("Validate(%q) = %v, want nil", tc.interval, err)
}
})
}
}
// TestSettingsUpdatePayloadValidateRollbackTimeout covers the M5 health-gated
// rollback timeout: it must be a positive Go duration.
func TestSettingsUpdatePayloadValidateRollbackTimeout(t *testing.T) {
cases := []struct {
name string
timeout string
wantErr bool
}{
{name: "two minutes is allowed", timeout: "120s", wantErr: false},
{name: "compound duration is allowed", timeout: "1m30s", wantErr: false},
{name: "zero is rejected", timeout: "0s", wantErr: true},
{name: "negative is rejected", timeout: "-5s", wantErr: true},
{name: "unparseable is rejected", timeout: "soon", wantErr: true},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
payload := settingsUpdatePayload{
ContainerAutomation: &containerAutomationSettingsPayload{
AutoUpdate: &autoUpdateSettingsPayload{
RollbackTimeout: strptr(tc.timeout),
},
},
}
err := payload.Validate(httptest.NewRequest("PUT", "/settings", nil))
if tc.wantErr && err == nil {
t.Errorf("Validate(%q) = nil, want error", tc.timeout)
}
if !tc.wantErr && err != nil {
t.Errorf("Validate(%q) = %v, want nil", tc.timeout, err)
}
})
}
}