log_retention_test.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. package executor
  2. import (
  3. "testing"
  4. "time"
  5. auth "github.com/OliveTin/OliveTin/internal/auth"
  6. config "github.com/OliveTin/OliveTin/internal/config"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/stretchr/testify/require"
  9. )
  10. func newRetentionTestExecutor(t *testing.T, action *config.Action) (*Executor, *config.Config, *ActionBinding) {
  11. t.Helper()
  12. cfg := config.DefaultConfig()
  13. cfg.LogHistoryLimit = config.MinimumLogHistoryLimit
  14. cfg.Actions = append(cfg.Actions, action)
  15. cfg.Sanitize()
  16. e := DefaultExecutor(cfg)
  17. e.RebuildActionMap()
  18. binding := e.FindBindingWithNoEntity(action)
  19. require.NotNil(t, binding)
  20. return e, cfg, binding
  21. }
  22. func addRetentionTestLog(e *Executor, entry *InternalLogEntry) {
  23. e.logmutex.Lock()
  24. e.addLogToBindingMap(entry)
  25. e.logmutex.Unlock()
  26. e.SetLog(entry.ExecutionTrackingID, entry)
  27. }
  28. func assertLogStoresConsistent(t *testing.T, e *Executor, bindingID string) {
  29. t.Helper()
  30. e.logmutex.RLock()
  31. defer e.logmutex.RUnlock()
  32. assert.Len(t, e.logs, len(e.logsTrackingIdsByDate), "map and date index must hold the same entries")
  33. for _, entry := range e.LogsByBindingId[bindingID] {
  34. _, found := e.logs[entry.ExecutionTrackingID]
  35. assert.True(t, found, "per-binding logs must not keep evicted entries")
  36. }
  37. }
  38. func TestLogHistoryEvictsOldestFinishedEntries(t *testing.T) {
  39. e, cfg, binding := newRetentionTestExecutor(t, &config.Action{Title: "retention", Shell: "true"})
  40. entries := make([]*InternalLogEntry, 0, 250)
  41. for range 250 {
  42. entry := newQueueTestLogEntry(binding, true)
  43. entries = append(entries, entry)
  44. addRetentionTestLog(e, entry)
  45. }
  46. e.logmutex.RLock()
  47. assert.Len(t, e.logsTrackingIdsByDate, cfg.LogHistoryLimit)
  48. assert.Equal(t, entries[150].ExecutionTrackingID, e.logsTrackingIdsByDate[0], "oldest entries are evicted first")
  49. assert.Len(t, e.LogsByBindingId[binding.ID], cfg.LogHistoryLimit)
  50. e.logmutex.RUnlock()
  51. _, found := e.GetLog(entries[0].ExecutionTrackingID)
  52. assert.False(t, found)
  53. _, found = e.GetLog(entries[249].ExecutionTrackingID)
  54. assert.True(t, found)
  55. assert.Equal(t, int64(249), entries[249].Index, "indexes keep increasing after eviction")
  56. assertLogStoresConsistent(t, e, binding.ID)
  57. }
  58. func TestLogHistoryKeepsRunningEntries(t *testing.T) {
  59. e, cfg, binding := newRetentionTestExecutor(t, &config.Action{Title: "retention", Shell: "true"})
  60. running := newQueueTestLogEntry(binding, false)
  61. addRetentionTestLog(e, running)
  62. for range 250 {
  63. addRetentionTestLog(e, newQueueTestLogEntry(binding, true))
  64. }
  65. _, found := e.GetLog(running.ExecutionTrackingID)
  66. assert.True(t, found, "running executions must never be evicted")
  67. e.logmutex.RLock()
  68. assert.Len(t, e.logsTrackingIdsByDate, cfg.LogHistoryLimit)
  69. e.logmutex.RUnlock()
  70. assertLogStoresConsistent(t, e, binding.ID)
  71. }
  72. func TestLogHistoryKeepsEntriesCountingTowardsRateLimit(t *testing.T) {
  73. limited := &config.Action{
  74. Title: "rate limited",
  75. Shell: "true",
  76. MaxRate: []config.RateSpec{{Limit: 3, Duration: "1h"}},
  77. }
  78. e, _, limitedBinding := newRetentionTestExecutor(t, limited)
  79. other := &config.Action{Title: "other", Shell: "true"}
  80. e.Cfg.Actions = append(e.Cfg.Actions, other)
  81. e.RebuildActionMap()
  82. otherBinding := e.FindBindingWithNoEntity(other)
  83. require.NotNil(t, otherBinding)
  84. counted := make([]*InternalLogEntry, 0, 3)
  85. for range 3 {
  86. entry := newQueueTestLogEntry(limitedBinding, true)
  87. counted = append(counted, entry)
  88. addRetentionTestLog(e, entry)
  89. }
  90. blocked := newQueueTestLogEntry(limitedBinding, true)
  91. blocked.Blocked = true
  92. addRetentionTestLog(e, blocked)
  93. for range 250 {
  94. addRetentionTestLog(e, newQueueTestLogEntry(otherBinding, true))
  95. }
  96. for _, entry := range counted {
  97. _, found := e.GetLog(entry.ExecutionTrackingID)
  98. assert.True(t, found, "entries inside the maxRate window must not be evicted")
  99. }
  100. _, found := e.GetLog(blocked.ExecutionTrackingID)
  101. assert.False(t, found, "blocked entries do not count towards the rate limit and can be evicted")
  102. req := &ExecutionRequest{Binding: limitedBinding, executor: e, logEntry: &InternalLogEntry{}}
  103. assert.Equal(t, 3, getExecutionsCount(limited.MaxRate[0], req), "flooding other actions must not reset the rate limit")
  104. assertLogStoresConsistent(t, e, limitedBinding.ID)
  105. }
  106. func TestLogHistoryBoundsDeniedRequests(t *testing.T) {
  107. action := &config.Action{Title: "admins only", Shell: "true"}
  108. e, cfg, binding := newRetentionTestExecutor(t, action)
  109. cfg.DefaultPermissions.Exec = false
  110. user := auth.UserGuest(cfg)
  111. for range 250 {
  112. wg, _ := e.ExecRequest(&ExecutionRequest{AuthenticatedUser: user, Cfg: cfg, Binding: binding})
  113. wg.Wait()
  114. }
  115. e.logmutex.RLock()
  116. defer e.logmutex.RUnlock()
  117. assert.LessOrEqual(t, len(e.logsTrackingIdsByDate), cfg.LogHistoryLimit, "denied requests must not grow the log store without limit")
  118. assert.LessOrEqual(t, len(e.LogsByBindingId[binding.ID]), cfg.LogHistoryLimit)
  119. }
  120. func TestLogHistoryLimitIsSanitized(t *testing.T) {
  121. cfg := config.DefaultConfig()
  122. assert.Equal(t, config.DefaultLogHistoryLimit, cfg.LogHistoryLimit)
  123. cfg.LogHistoryLimit = 5
  124. cfg.Sanitize()
  125. assert.Equal(t, config.MinimumLogHistoryLimit, cfg.LogHistoryLimit)
  126. }
  127. func TestLogHistoryLoadedFromDiskIsBounded(t *testing.T) {
  128. e, cfg, binding := newRetentionTestExecutor(t, &config.Action{Title: "retention", Shell: "true"})
  129. loaded := make([]*InternalLogEntry, 0, 250)
  130. for i := range 250 {
  131. entry := newQueueTestLogEntry(binding, true)
  132. entry.DatetimeStarted = time.Now().Add(time.Duration(i-250) * time.Minute)
  133. loaded = append(loaded, entry)
  134. }
  135. e.restoreLogsToExecutor(loaded, 0)
  136. e.logmutex.RLock()
  137. assert.Len(t, e.logsTrackingIdsByDate, cfg.LogHistoryLimit)
  138. assert.Equal(t, loaded[150].ExecutionTrackingID, e.logsTrackingIdsByDate[0], "the newest loaded logs are kept")
  139. e.logmutex.RUnlock()
  140. assertLogStoresConsistent(t, e, binding.ID)
  141. }