autosched_test.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // autosched_test.go - tests del auto-update programado (#759): vencimiento
  2. // daily/weekly/monthly, validación, roundtrip kv y disparo con fakes.
  3. package updater
  4. import (
  5. "context"
  6. "sync/atomic"
  7. "testing"
  8. "time"
  9. )
  10. func autoIntPtr(v int) *int { return &v }
  11. func TestAutoUpdateDueDaily(t *testing.T) {
  12. now := time.Date(2026, 9, 13, 10, 0, 0, 0, time.Local) // domingo
  13. slot := time.Date(2026, 9, 13, 3, 30, 0, 0, time.Local)
  14. st := AutoUpdateSettings{Enabled: true, Kind: AutoDaily, Time: "03:30"}
  15. if !AutoUpdateDue(st, 0, now) {
  16. t.Fatalf("slot pasado sin lastRun debe disparar")
  17. }
  18. if AutoUpdateDue(st, slot.UnixMilli(), now) {
  19. t.Fatalf("ya disparado hoy no repite")
  20. }
  21. if AutoUpdateDue(st, 0, slot.Add(-time.Minute)) {
  22. t.Fatalf("antes del slot no dispara")
  23. }
  24. if AutoUpdateDue(AutoUpdateSettings{Kind: AutoDaily, Time: "03:30"}, 0, now) {
  25. t.Fatalf("disabled no dispara")
  26. }
  27. }
  28. func TestAutoUpdateDueWeeklyMonthly(t *testing.T) {
  29. now := time.Date(2026, 9, 13, 10, 0, 0, 0, time.Local) // domingo 10:00
  30. weekly := AutoUpdateSettings{Enabled: true, Kind: AutoWeekly, DayOfWeek: autoIntPtr(0), Time: "04:00"}
  31. if !AutoUpdateDue(weekly, 0, now) {
  32. t.Fatalf("domingo despues del slot debe disparar")
  33. }
  34. weeklyTue := AutoUpdateSettings{Enabled: true, Kind: AutoWeekly, DayOfWeek: autoIntPtr(2), Time: "04:00"}
  35. if AutoUpdateDue(weeklyTue, 0, now) {
  36. t.Fatalf("otro dia no dispara")
  37. }
  38. monthly := AutoUpdateSettings{Enabled: true, Kind: AutoMonthly, DayOfMonth: autoIntPtr(13), Time: "04:00"}
  39. if !AutoUpdateDue(monthly, 0, now) {
  40. t.Fatalf("dia 13 despues del slot debe disparar")
  41. }
  42. clampFeb := AutoUpdateSettings{Enabled: true, Kind: AutoMonthly, DayOfMonth: autoIntPtr(31), Time: "04:00"}
  43. if !AutoUpdateDue(clampFeb, 0, time.Date(2026, 2, 28, 5, 0, 0, 0, time.Local)) {
  44. t.Fatalf("clamp 31->28 en febrero dispara")
  45. }
  46. }
  47. func TestValidateAutoUpdate(t *testing.T) {
  48. if err := ValidateAutoUpdate(AutoUpdateSettings{Kind: AutoDaily}); err == nil {
  49. t.Fatalf("daily sin time falla")
  50. }
  51. if err := ValidateAutoUpdate(AutoUpdateSettings{Kind: AutoWeekly, DayOfWeek: autoIntPtr(1)}); err == nil {
  52. t.Fatalf("weekly sin time falla")
  53. }
  54. if err := ValidateAutoUpdate(AutoUpdateSettings{Kind: "hourly"}); err == nil {
  55. t.Fatalf("kind desconocido falla")
  56. }
  57. if err := ValidateAutoUpdate(AutoUpdateSettings{Kind: AutoMonthly, DayOfMonth: autoIntPtr(1), Time: "04:00"}); err != nil {
  58. t.Fatalf("monthly valido: %v", err)
  59. }
  60. }
  61. func TestAutoUpdateSettingsRoundtrip(t *testing.T) {
  62. db := openDB(t)
  63. st := AutoUpdateSettings{Enabled: true, Kind: AutoWeekly, DayOfWeek: autoIntPtr(4), Time: "02:15"}
  64. if err := SaveAutoUpdateSettings(db, st); err != nil {
  65. t.Fatalf("save: %v", err)
  66. }
  67. got := LoadAutoUpdateSettings(db)
  68. if !got.Enabled || got.Kind != AutoWeekly || got.DayOfWeek == nil || *got.DayOfWeek != 4 || got.Time != "02:15" {
  69. t.Fatalf("roundtrip: %+v", got)
  70. }
  71. if err := SaveAutoUpdateSettings(db, AutoUpdateSettings{Enabled: true, Kind: "nope"}); err == nil {
  72. t.Fatalf("guardar basura debe fallar")
  73. }
  74. if _, r := AutoRunState(db); r != "" {
  75. t.Fatalf("sin disparos lastResult vacío, got %q", r)
  76. }
  77. }
  78. // fakeUA: CheckerApplier scripted.
  79. type fakeUA struct {
  80. status Status
  81. applied atomic.Int32
  82. applyRet bool
  83. applyHook func()
  84. }
  85. func (f *fakeUA) Check(ctx context.Context) Status { return f.status }
  86. func (f *fakeUA) CanApply() bool { return f.status.CanApply }
  87. func (f *fakeUA) ApplyBy(by string) bool {
  88. f.applied.Add(1)
  89. if f.applyHook != nil {
  90. f.applyHook()
  91. }
  92. return f.applyRet
  93. }
  94. func TestSchedulerShotAppliesWhenDue(t *testing.T) {
  95. db := openDB(t)
  96. latest := "v9.9.9"
  97. ua := &fakeUA{
  98. status: Status{UpdateAvailable: true, CanApply: true, Latest: &latest},
  99. applyRet: true,
  100. }
  101. s := NewScheduler(db, ua)
  102. s.now = func() time.Time { return time.Date(2026, 9, 13, 10, 0, 0, 0, time.Local) }
  103. _ = AutoUpdateSettings{Enabled: true, Kind: AutoDaily, Time: "03:30"}
  104. s.shot(AutoUpdateSettings{}, autoRunState{})
  105. if ua.applied.Load() != 1 {
  106. t.Fatalf("debe aplicar: %d", ua.applied.Load())
  107. }
  108. if _, res := AutoRunState(db); res != "applied" {
  109. t.Fatalf("lastResult: %q", res)
  110. }
  111. lr, _ := AutoRunState(db)
  112. if lr == 0 {
  113. t.Fatalf("lastRun debe quedar marcado")
  114. }
  115. }
  116. func TestSchedulerShotCheckFailed(t *testing.T) {
  117. // #743: estado rancio tras fetch fallido: NUNCA aplicar sobre él.
  118. db := openDB(t)
  119. ua := &fakeUA{status: Status{CheckFailed: true, UpdateAvailable: true, CanApply: true}}
  120. s := NewScheduler(db, ua)
  121. s.shot(AutoUpdateSettings{}, autoRunState{})
  122. if ua.applied.Load() != 0 {
  123. t.Fatalf("check fallido no debe aplicar")
  124. }
  125. if _, res := AutoRunState(db); res != "check-failed" {
  126. t.Fatalf("lastResult: %q", res)
  127. }
  128. }
  129. func TestSchedulerShotUpToDate(t *testing.T) {
  130. db := openDB(t)
  131. ua := &fakeUA{status: Status{UpdateAvailable: false, CanApply: true}}
  132. s := NewScheduler(db, ua)
  133. s.shot(AutoUpdateSettings{}, autoRunState{})
  134. if ua.applied.Load() != 0 {
  135. t.Fatalf("sin novedad no aplica")
  136. }
  137. if _, res := AutoRunState(db); res != "up-to-date" {
  138. t.Fatalf("lastResult: %q", res)
  139. }
  140. }
  141. func TestSchedulerTickMarksBeforeApply(t *testing.T) {
  142. // El last_run se persiste ANTES del apply: si el proceso muere en el
  143. // restart, el arranque posterior no re-dispara el mismo slot.
  144. db := openDB(t)
  145. latest := "v9.9.9"
  146. ua := &fakeUA{
  147. status: Status{UpdateAvailable: true, CanApply: true, Latest: &latest},
  148. applyRet: true,
  149. }
  150. if err := SaveAutoUpdateSettings(db, AutoUpdateSettings{Enabled: true, Kind: AutoDaily, Time: "00:00"}); err != nil {
  151. t.Fatal(err)
  152. }
  153. now := time.Date(2026, 9, 13, 10, 0, 0, 0, time.Local)
  154. s := NewScheduler(db, ua)
  155. s.now = func() time.Time { return now }
  156. // El apply "mata" el proceso: last_run ya debe estar persistido.
  157. ua.applyHook = func() {
  158. lr, _ := AutoRunState(db)
  159. if lr == 0 {
  160. t.Errorf("last_run debe persistir ANTES del apply")
  161. }
  162. }
  163. s.tick()
  164. deadline := time.Now().Add(2 * time.Second)
  165. for time.Now().Before(deadline) && ua.applied.Load() == 0 {
  166. time.Sleep(10 * time.Millisecond)
  167. }
  168. if ua.applied.Load() != 1 {
  169. t.Fatalf("tick con settings vencidos debe aplicar: %d", ua.applied.Load())
  170. }
  171. }