time_range_fix_test.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package indexer
  2. import (
  3. "os"
  4. "path/filepath"
  5. "testing"
  6. "github.com/0xJacky/Nginx-UI/model"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/stretchr/testify/require"
  9. )
  10. // TestLogRotationDocumentCountReset verifies that the document count is reset
  11. // when a log rotation is detected.
  12. func TestLogRotationDocumentCountReset(t *testing.T) {
  13. // Setup a temporary directory for our test log files
  14. tempDir, err := os.MkdirTemp("", "log_rotation_test")
  15. require.NoError(t, err)
  16. defer os.RemoveAll(tempDir)
  17. logPath := filepath.Join(tempDir, "access.log")
  18. // 1. Initial State: Simulate a fully indexed log file.
  19. // This represents the state of 'access.log' before rotation.
  20. initialContent := "line1\nline2\nline3\n"
  21. err = os.WriteFile(logPath, []byte(initialContent), 0644)
  22. require.NoError(t, err)
  23. fileInfo, err := os.Stat(logPath)
  24. require.NoError(t, err)
  25. // Create a log index record that simulates a previously indexed state.
  26. // It has a document count and the size of the file.
  27. logIndex := &model.NginxLogIndex{
  28. Path: logPath,
  29. MainLogPath: logPath,
  30. DocumentCount: 3, // 3 lines were indexed
  31. LastSize: fileInfo.Size(),
  32. Enabled: true,
  33. }
  34. // 2. Log Rotation: Simulate log rotation by creating a new, smaller file.
  35. rotatedContent := "new_line1\n"
  36. err = os.WriteFile(logPath, []byte(rotatedContent), 0644)
  37. require.NoError(t, err)
  38. newFileInfo, err := os.Stat(logPath)
  39. require.NoError(t, err)
  40. // Explicitly verify that the new file is smaller. This is crucial for the test's validity.
  41. require.Less(t, newFileInfo.Size(), logIndex.LastSize, "Test setup error: New log file must be smaller than the old one to simulate rotation.")
  42. // 3. Test the core logic directly, as it is implemented in the cron job.
  43. isRotated := newFileInfo.Size() < logIndex.LastSize
  44. assert.True(t, isRotated, "Log rotation detection logic failed.")
  45. var existingDocCount uint64
  46. if isRotated {
  47. // On rotation, the base count must be reset to zero. This is the fix we are verifying.
  48. existingDocCount = 0
  49. } else {
  50. // In a normal incremental scenario, the old count would be used.
  51. existingDocCount = logIndex.DocumentCount
  52. }
  53. // The number of documents indexed from the new, rotated file.
  54. totalDocsIndexedInNewFile := uint64(1) // We have one line in rotatedContent.
  55. // Calculate the final document count.
  56. finalDocCount := existingDocCount + totalDocsIndexedInNewFile
  57. // 5. Assert the Result
  58. // The final count must be exactly the number of lines in the new file, not an accumulation.
  59. assert.Equal(t, uint64(1), finalDocCount, "On rotation, document count should be reset to the new file's content count.")
  60. assert.NotEqual(t, uint64(4), finalDocCount, "Document count must not be the sum of old and new files (3+1).")
  61. }