MCPcopy
hub / github.com/henrygd/beszel / TestDeleteOldSystemStats

Function TestDeleteOldSystemStats

internal/records/records_test.go:106–195  ·  view source on GitHub ↗

TestDeleteOldSystemStats tests the deleteOldSystemStats function

(t *testing.T)

Source from the content-addressed store, hash-verified

104
105// TestDeleteOldSystemStats tests the deleteOldSystemStats function
106func TestDeleteOldSystemStats(t *testing.T) {
107 hub, err := tests.NewTestHub(t.TempDir())
108 require.NoError(t, err)
109 defer hub.Cleanup()
110
111 // Create test system
112 user, err := tests.CreateUser(hub, "test@example.com", "testtesttest")
113 require.NoError(t, err)
114
115 system, err := tests.CreateRecord(hub, "systems", map[string]any{
116 "name": "test-system",
117 "host": "localhost",
118 "port": "45876",
119 "status": "up",
120 "users": []string{user.Id},
121 })
122 require.NoError(t, err)
123
124 now := time.Now().UTC()
125
126 // Test data for different record types and their retention periods
127 testCases := []struct {
128 recordType string
129 retention time.Duration
130 shouldBeKept bool
131 ageFromNow time.Duration
132 description string
133 }{
134 {"1m", time.Hour, true, 30 * time.Minute, "1m record within 1 hour should be kept"},
135 {"1m", time.Hour, false, 2 * time.Hour, "1m record older than 1 hour should be deleted"},
136 {"10m", 12 * time.Hour, true, 6 * time.Hour, "10m record within 12 hours should be kept"},
137 {"10m", 12 * time.Hour, false, 24 * time.Hour, "10m record older than 12 hours should be deleted"},
138 {"20m", 24 * time.Hour, true, 12 * time.Hour, "20m record within 24 hours should be kept"},
139 {"20m", 24 * time.Hour, false, 48 * time.Hour, "20m record older than 24 hours should be deleted"},
140 {"120m", 7 * 24 * time.Hour, true, 3 * 24 * time.Hour, "120m record within 7 days should be kept"},
141 {"120m", 7 * 24 * time.Hour, false, 10 * 24 * time.Hour, "120m record older than 7 days should be deleted"},
142 {"480m", 30 * 24 * time.Hour, true, 15 * 24 * time.Hour, "480m record within 30 days should be kept"},
143 {"480m", 30 * 24 * time.Hour, false, 45 * 24 * time.Hour, "480m record older than 30 days should be deleted"},
144 }
145
146 // Create test records for both system_stats and container_stats
147 collections := []string{"system_stats", "container_stats"}
148 recordIds := make(map[string][]string)
149
150 for _, collection := range collections {
151 recordIds[collection] = make([]string, 0)
152
153 for i, tc := range testCases {
154 recordTime := now.Add(-tc.ageFromNow)
155
156 var stats string
157 if collection == "system_stats" {
158 stats = fmt.Sprintf(`{"cpu": %d.0, "mem": %d}`, i*10, i*100)
159 } else {
160 stats = fmt.Sprintf(`[{"name": "container%d", "cpu": %d.0, "mem": %d}]`, i, i*5, i*50)
161 }
162
163 record, err := tests.CreateRecord(hub, collection, map[string]any{

Callers

nothing calls this directly

Calls 5

CleanupMethod · 0.95
NewTestHubFunction · 0.92
CreateUserFunction · 0.92
CreateRecordFunction · 0.92
DeleteOldSystemStatsFunction · 0.92

Tested by

no test coverage detected