- planification crontab par stack (autoupdate.json), incompatible avec l'exclusion
- scheduler a la minute ; pull puis down + up -d seulement si une image a change (mode autoapply)
- API PUT .../targets/{t}/auto et GET /api/cron (apercu des prochaines executions)
- interface : option dans le detail, badge sarcelle, vue de suivi "Mises a jour auto"
- TZ dans docker-compose / .env.example (tzdata embarque)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LNfwm6UCYFyvNQNSF5i9Gy
101 lines
2.8 KiB
Go
101 lines
2.8 KiB
Go
package fleet
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestAutoUpdate(t *testing.T) {
|
|
dir := t.TempDir()
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
m, err := Open(ctx, dir, exclTestRunner{}, 1)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
srv, err := m.Add(Server{Name: "s1", Host: "h1", Enabled: true, Targets: []Target{{User: "root"}}})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
tid := srv.Targets[0].ID
|
|
m.scans[Key(srv.ID, tid)] = &Scan{Stacks: []Stack{{Dir: "/home/a", Name: "a", Status: StatusUpdates}}}
|
|
key := ExclusionKey(srv.ID, tid, "/home/a")
|
|
|
|
if err := m.SetAuto(srv.ID, tid, "/home/a", "61 * * * *", "test"); !errors.Is(err, ErrInvalid) {
|
|
t.Fatalf("planification invalide : %v", err)
|
|
}
|
|
if err := m.SetAuto(srv.ID, tid, "/home/x", "@daily", "test"); !errors.Is(err, ErrNotFound) {
|
|
t.Fatalf("stack inconnue : %v", err)
|
|
}
|
|
if err := m.SetAuto(srv.ID, tid, "/home/a", " 0 4 * * * ", "test"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
st := m.Snapshot()[0].Scans[tid].Stacks[0]
|
|
if st.Auto == nil || st.Auto.Schedule != "0 4 * * *" || st.Auto.Next.IsZero() {
|
|
t.Fatalf("programmation non affichee : %+v", st.Auto)
|
|
}
|
|
|
|
// Persistance.
|
|
m2, err := Open(ctx, dir, exclTestRunner{}, 1)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if e := m2.auto[key]; e == nil || e.Schedule != "0 4 * * *" {
|
|
t.Fatal("programmation non relue")
|
|
}
|
|
|
|
// Echeance : la mise a jour part et son resultat est note.
|
|
m.autoTick(time.Date(2026, 1, 1, 4, 0, 0, 0, time.Local))
|
|
deadline := time.Now().Add(5 * time.Second)
|
|
for {
|
|
m.mu.Lock()
|
|
e := *m.auto[key]
|
|
m.mu.Unlock()
|
|
if !e.LastRun.IsZero() {
|
|
if e.LastOK || e.LastMessage == "" {
|
|
t.Fatalf("resultat inattendu : %+v", e) // exclTestRunner ne repond rien
|
|
}
|
|
break
|
|
}
|
|
if time.Now().After(deadline) {
|
|
t.Fatal("resultat non note")
|
|
}
|
|
time.Sleep(10 * time.Millisecond)
|
|
}
|
|
|
|
// Exclusion : la programmation est supprimee, puis refusee.
|
|
if err := m.SetExcluded(srv.ID, tid, "/home/a", true); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if m.auto[key] != nil {
|
|
t.Fatal("programmation conservee sur une stack exclue")
|
|
}
|
|
if err := m.SetAuto(srv.ID, tid, "/home/a", "@daily", "test"); !errors.Is(err, ErrExcluded) {
|
|
t.Fatalf("programmation d'une stack exclue : %v", err)
|
|
}
|
|
if err := m.SetExcluded(srv.ID, tid, "/home/a", false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// Desactivation.
|
|
if err := m.SetAuto(srv.ID, tid, "/home/a", "@daily", "test"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := m.SetAuto(srv.ID, tid, "/home/a", "", "test"); err != nil || m.auto[key] != nil {
|
|
t.Fatalf("desactivation : %v", err)
|
|
}
|
|
|
|
// Suppression du serveur : programmations oubliees.
|
|
if err := m.SetAuto(srv.ID, tid, "/home/a", "@daily", "test"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := m.Delete(srv.ID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(m.auto) != 0 {
|
|
t.Fatalf("programmations restantes : %v", m.auto)
|
|
}
|
|
}
|