Files
portainer/api/http/handler/settings/settings_update_test.go
T
claude code agent be3bfd0513 fix(automation): maintainer pre-merge review — stale detection, daemon edge cases, parity (F1-F9)
F1: cap the image-status cache TTL at 5m (was 24h) — the cache is keyed by the
    LOCAL imageID, which doesn't change when upstream pushes a new image under the
    same tag, so the 24h TTL hid new images from both the badge and the auto-update
    daemon; a short TTL re-resolves the remote digest within the poll window.
F2: document that the update->rollback guard map is in-memory (restart implication).
F3: skip auto-update for an unnamed container when rollback is on (the endpoint+name
    keyed guard can't record it, so it would loop) — pure skipUnnamedForRollback + test.
F4: wrap the pre-update ContainerInspect in context.WithTimeout(endpointTimeout).
F5: document Reload() does not interrupt an in-flight tick.
F6: floor auto-heal CheckInterval at 1s (mirrors auto-update) + test.
F7: wontfix — migration is currently correct; namespace rework is out of scope.
F8: correct the misleading SSRF/AllowList comment (no filter is applied).
F9: front auto-heal interval floor + test; dedup STALE_TIME; fix invalidation comment.
Also refresh three stale '24h/long-lived cache' comments to match the 5m TTL.

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

129 lines
4.5 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)
}
})
}
}
// TestSettingsUpdatePayloadValidateAutoHealCheckInterval covers the auto-heal
// check-interval floor (F6): durations below minAutoHealCheckInterval (1s), as
// well as malformed or non-positive durations, must be rejected, mirroring the
// auto-update poll-interval validation.
func TestSettingsUpdatePayloadValidateAutoHealCheckInterval(t *testing.T) {
cases := []struct {
name string
interval string
wantErr bool
}{
{name: "one millisecond is below the floor", interval: "1ms", wantErr: true},
{name: "half a second is below the floor", interval: "500ms", wantErr: true},
{name: "exactly one second is allowed", interval: "1s", wantErr: false},
{name: "thirty seconds is allowed", interval: "30s", wantErr: false},
{name: "zero is rejected", interval: "0s", wantErr: true},
{name: "negative is rejected", interval: "-5s", 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{
AutoHeal: &autoHealSettingsPayload{
CheckInterval: 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 and its floor (F7): it must be a Go duration of at least
// minAutoUpdateRollbackTimeout (10s), rejecting near-zero, non-positive and
// malformed values.
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: "exactly the floor is allowed", timeout: "10s", wantErr: false},
{name: "one millisecond is below the floor", timeout: "1ms", wantErr: true},
{name: "nine seconds is below the floor", timeout: "9s", wantErr: true},
{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)
}
})
}
}