MCPcopy
hub / github.com/keploy/keploy / TestDebugFileSink_RotateForScope

Function TestDebugFileSink_RotateForScope

utils/log/logger_test.go:259–332  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

257}
258
259func TestDebugFileSink_RotateForScope(t *testing.T) {
260 SetRedactor(nil)
261 console := &syncBuffer{}
262 logger := newConsoleLogger(console, zap.InfoLevel)
263
264 dir := t.TempDir()
265 originPath := filepath.Join(dir, "agent-debug.log")
266 originFile, err := os.Create(originPath)
267 if err != nil {
268 t.Fatalf("create origin: %v", err)
269 }
270 defer originFile.Close()
271
272 wrapped, sink := AddDebugFileSink(logger, originFile, 0)
273 if sink == nil {
274 t.Fatalf("AddDebugFileSink returned nil")
275 }
276 defer SetDebugFileSink(nil)
277
278 wrapped.Debug("origin-line")
279
280 if err := sink.RotateForScope("test-set-1"); err != nil {
281 t.Fatalf("RotateForScope: %v", err)
282 }
283 wrapped.Debug("scope-1-line")
284
285 if err := sink.RotateForScope("test-set-2"); err != nil {
286 t.Fatalf("RotateForScope: %v", err)
287 }
288 wrapped.Debug("scope-2-line")
289
290 // Repeat scope is a no-op.
291 if err := sink.RotateForScope("test-set-2"); err != nil {
292 t.Fatalf("repeat RotateForScope: %v", err)
293 }
294 wrapped.Debug("scope-2-line-2")
295
296 if err := sink.Flush(); err != nil {
297 t.Fatalf("flush: %v", err)
298 }
299
300 originBytes, _ := os.ReadFile(originPath)
301 if !strings.Contains(string(originBytes), "origin-line") {
302 t.Errorf("origin-line missing from %s: %s", originPath, originBytes)
303 }
304 if strings.Contains(string(originBytes), "scope-1-line") {
305 t.Errorf("scope-1-line leaked into origin file: %s", originBytes)
306 }
307
308 scope1Path := filepath.Join(dir, "test-set-1", "agent-debug.log")
309 scope1Bytes, err := os.ReadFile(scope1Path)
310 if err != nil {
311 t.Fatalf("read scope-1 file: %v", err)
312 }
313 if !strings.Contains(string(scope1Bytes), "scope-1-line") {
314 t.Errorf("scope-1-line missing from %s: %s", scope1Path, scope1Bytes)
315 }
316 if strings.Contains(string(scope1Bytes), "scope-2-line") {

Callers

nothing calls this directly

Calls 9

SetRedactorFunction · 0.85
newConsoleLoggerFunction · 0.85
AddDebugFileSinkFunction · 0.85
SetDebugFileSinkFunction · 0.85
RotateForScopeMethod · 0.80
FlushMethod · 0.80
CurrentScopeMethod · 0.80
CloseMethod · 0.65
DebugMethod · 0.65

Tested by

no test coverage detected