Split the single container-automation webhook URL into two independently
optional URLs — UpdateWebhookURL (fired on update/rollback/update-failed) and
HealWebhookURL (fired on auto-heal restart). The notifier routes each event to
its mechanism's URL by kind; an empty URL silences only that mechanism, so a
user can enable notifications for updates without heal (or vice-versa).
Settings gain both fields (each validated http/https, {{message}} allowed), the
NotificationPanel exposes two labeled inputs, and the golden migration output is
updated. Delivery path (goroutine/recover/timeout, {{message}} GET vs POST,
per-container stack message format) is unchanged.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
189 lines
6.6 KiB
Go
189 lines
6.6 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)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestSettingsUpdatePayloadValidateNotificationWebhookURL covers the
|
|
// per-mechanism container-automation notification webhook URLs (auto-update and
|
|
// auto-heal): each is independently optional (empty is valid), must be a valid
|
|
// http(s) URL with a host when set, and accepts the "{{message}}" placeholder.
|
|
// Each case is exercised against both the update field and the heal field.
|
|
func TestSettingsUpdatePayloadValidateNotificationWebhookURL(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
url string
|
|
wantErr bool
|
|
}{
|
|
{name: "empty is allowed (disabled)", url: "", wantErr: false},
|
|
{name: "plain https URL is allowed", url: "https://example.com/notify", wantErr: false},
|
|
{name: "http URL is allowed", url: "http://example.com/notify", wantErr: false},
|
|
{name: "URL with placeholder is allowed", url: "https://example.com/notify?msg={{message}}", wantErr: false},
|
|
{name: "missing scheme is rejected", url: "example.com/notify", wantErr: true},
|
|
{name: "non-http scheme is rejected", url: "ftp://example.com/notify", wantErr: true},
|
|
{name: "missing host is rejected", url: "https://", wantErr: true},
|
|
{name: "garbage is rejected", url: "not a url", wantErr: true},
|
|
}
|
|
|
|
fields := []struct {
|
|
name string
|
|
payload func(url string) *notificationSettingsPayload
|
|
}{
|
|
{
|
|
name: "UpdateWebhookURL",
|
|
payload: func(url string) *notificationSettingsPayload {
|
|
return ¬ificationSettingsPayload{UpdateWebhookURL: strptr(url)}
|
|
},
|
|
},
|
|
{
|
|
name: "HealWebhookURL",
|
|
payload: func(url string) *notificationSettingsPayload {
|
|
return ¬ificationSettingsPayload{HealWebhookURL: strptr(url)}
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, field := range fields {
|
|
for _, tc := range cases {
|
|
t.Run(field.name+"/"+tc.name, func(t *testing.T) {
|
|
payload := settingsUpdatePayload{
|
|
ContainerAutomation: &containerAutomationSettingsPayload{
|
|
Notification: field.payload(tc.url),
|
|
},
|
|
}
|
|
|
|
err := payload.Validate(httptest.NewRequest("PUT", "/settings", nil))
|
|
if tc.wantErr && err == nil {
|
|
t.Errorf("Validate(%q) = nil, want error", tc.url)
|
|
}
|
|
if !tc.wantErr && err != nil {
|
|
t.Errorf("Validate(%q) = %v, want nil", tc.url, err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
}
|