file_reader_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package acquisition
  2. import (
  3. "fmt"
  4. "testing"
  5. "github.com/crowdsecurity/crowdsec/pkg/csconfig"
  6. "github.com/crowdsecurity/crowdsec/pkg/types"
  7. "github.com/stretchr/testify/assert"
  8. "gopkg.in/tomb.v2"
  9. )
  10. func TestLoadAcquisitionConfig(t *testing.T) {
  11. testFilePath := "./tests/test.log"
  12. tests := []struct {
  13. csConfig *csconfig.CrowdSec
  14. result *FileAcquisCtx
  15. err string
  16. }{
  17. {
  18. csConfig: &csconfig.CrowdSec{
  19. SingleFile: testFilePath,
  20. SingleFileLabel: "my_test_log",
  21. Profiling: false,
  22. },
  23. result: &FileAcquisCtx{
  24. Files: []FileCtx{
  25. {
  26. Type: "file",
  27. Mode: "cat",
  28. Filename: testFilePath,
  29. Filenames: []string{},
  30. Labels: map[string]string{
  31. "type": "my_test_log",
  32. },
  33. Profiling: false,
  34. },
  35. },
  36. Profiling: false,
  37. },
  38. err: "",
  39. },
  40. {
  41. csConfig: &csconfig.CrowdSec{
  42. SingleFile: testFilePath,
  43. SingleFileLabel: "my_test_log",
  44. Profiling: true,
  45. },
  46. result: &FileAcquisCtx{
  47. Files: []FileCtx{
  48. {
  49. Type: "file",
  50. Mode: "cat",
  51. Filename: testFilePath,
  52. Filenames: []string{},
  53. Labels: map[string]string{
  54. "type": "my_test_log",
  55. },
  56. Profiling: false,
  57. },
  58. },
  59. Profiling: true,
  60. },
  61. err: "",
  62. },
  63. {
  64. csConfig: &csconfig.CrowdSec{
  65. SingleFile: "./tests/non_exist.log",
  66. SingleFileLabel: "my_non_exist_test_log",
  67. Profiling: true,
  68. },
  69. result: nil,
  70. err: "unable to start file acquisition, bailout unable to glob path './tests/non_exist.log'",
  71. },
  72. }
  73. for _, test := range tests {
  74. fmt.Printf("testing : %+v\n", test.csConfig)
  75. result, err := LoadAcquisitionConfig(test.csConfig)
  76. assert.Equal(t, test.result, result)
  77. if test.err == "" && err == nil {
  78. continue
  79. }
  80. assert.EqualError(t, err, test.err)
  81. }
  82. }
  83. func TestAcquisStartReading(t *testing.T) {
  84. // Test in TAIL mode
  85. acquisFilePath := "./tests/acquis_test_log.yaml"
  86. csConfig := &csconfig.CrowdSec{
  87. AcquisitionFile: acquisFilePath,
  88. Profiling: false,
  89. }
  90. fCTX, err := LoadAcquisitionConfig(csConfig)
  91. if err != nil {
  92. t.Fatalf(err.Error())
  93. }
  94. outputChan := make(chan types.Event)
  95. acquisTomb := tomb.Tomb{}
  96. AcquisStartReading(fCTX, outputChan, &acquisTomb)
  97. if !acquisTomb.Alive() {
  98. t.Fatal("acquisition tomb is not alive")
  99. }
  100. // Test in CAT mode
  101. testFilePath := "./tests/test.log"
  102. csConfig = &csconfig.CrowdSec{
  103. SingleFile: testFilePath,
  104. SingleFileLabel: "my_test_log",
  105. Profiling: false,
  106. }
  107. fCTX, err = LoadAcquisitionConfig(csConfig)
  108. if err != nil {
  109. t.Fatalf(err.Error())
  110. }
  111. outputChan = make(chan types.Event)
  112. acquisTomb = tomb.Tomb{}
  113. AcquisStartReading(fCTX, outputChan, &acquisTomb)
  114. if !acquisTomb.Alive() {
  115. t.Fatal("acquisition tomb is not alive")
  116. }
  117. // Test with a .gz file
  118. testFilePath = "./tests/test.log.gz"
  119. csConfig = &csconfig.CrowdSec{
  120. SingleFile: testFilePath,
  121. SingleFileLabel: "my_test_log",
  122. Profiling: false,
  123. }
  124. fCTX, err = LoadAcquisitionConfig(csConfig)
  125. if err != nil {
  126. t.Fatalf(err.Error())
  127. }
  128. outputChan = make(chan types.Event)
  129. acquisTomb = tomb.Tomb{}
  130. AcquisStartReading(fCTX, outputChan, &acquisTomb)
  131. if !acquisTomb.Alive() {
  132. t.Fatal("acquisition tomb is not alive")
  133. }
  134. }