file_test.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. package fileacquisition
  2. import (
  3. "fmt"
  4. "os"
  5. "testing"
  6. "time"
  7. "github.com/crowdsecurity/crowdsec/pkg/cstest"
  8. "github.com/crowdsecurity/crowdsec/pkg/types"
  9. log "github.com/sirupsen/logrus"
  10. "github.com/sirupsen/logrus/hooks/test"
  11. "github.com/stretchr/testify/assert"
  12. "gopkg.in/tomb.v2"
  13. )
  14. func TestBadConfiguration(t *testing.T) {
  15. tests := []struct {
  16. config string
  17. expectedErr string
  18. }{
  19. {
  20. config: `foobar: asd.log`,
  21. expectedErr: "line 1: field foobar not found in type fileacquisition.FileConfiguration",
  22. },
  23. {
  24. config: `mode: tail`,
  25. expectedErr: "no filename or filenames configuration provided",
  26. },
  27. {
  28. config: `filename: "[asd-.log"`,
  29. expectedErr: "Glob failure: syntax error in pattern",
  30. },
  31. }
  32. subLogger := log.WithFields(log.Fields{
  33. "type": "file",
  34. })
  35. for _, test := range tests {
  36. f := FileSource{}
  37. err := f.Configure([]byte(test.config), subLogger)
  38. assert.Contains(t, err.Error(), test.expectedErr)
  39. }
  40. }
  41. func TestConfigureDSN(t *testing.T) {
  42. tests := []struct {
  43. dsn string
  44. expectedErr string
  45. }{
  46. {
  47. dsn: "asd://",
  48. expectedErr: "invalid DSN asd:// for file source, must start with file://",
  49. },
  50. {
  51. dsn: "file://",
  52. expectedErr: "empty file:// DSN",
  53. },
  54. {
  55. dsn: "file:///etc/passwd?log_level=warn",
  56. expectedErr: "",
  57. },
  58. {
  59. dsn: "file:///etc/passwd?log_level=foobar",
  60. expectedErr: "unknown level foobar: not a valid logrus Level:",
  61. },
  62. }
  63. subLogger := log.WithFields(log.Fields{
  64. "type": "file",
  65. })
  66. for _, test := range tests {
  67. f := FileSource{}
  68. err := f.ConfigureByDSN(test.dsn, map[string]string{"type": "testtype"}, subLogger)
  69. cstest.AssertErrorContains(t, err, test.expectedErr)
  70. }
  71. }
  72. func TestOneShot(t *testing.T) {
  73. tests := []struct {
  74. config string
  75. expectedConfigErr string
  76. expectedErr string
  77. expectedOutput string
  78. expectedLines int
  79. logLevel log.Level
  80. setup func()
  81. afterConfigure func()
  82. teardown func()
  83. }{
  84. {
  85. config: `
  86. mode: cat
  87. filename: /etc/shadow`,
  88. expectedConfigErr: "",
  89. expectedErr: "failed opening /etc/shadow: open /etc/shadow: permission denied",
  90. expectedOutput: "",
  91. logLevel: log.WarnLevel,
  92. expectedLines: 0,
  93. },
  94. {
  95. config: `
  96. mode: cat
  97. filename: /`,
  98. expectedConfigErr: "",
  99. expectedErr: "",
  100. expectedOutput: "/ is a directory, ignoring it",
  101. logLevel: log.WarnLevel,
  102. expectedLines: 0,
  103. },
  104. {
  105. config: `
  106. mode: cat
  107. filename: "[*-.log"`,
  108. expectedConfigErr: "Glob failure: syntax error in pattern",
  109. expectedErr: "",
  110. expectedOutput: "",
  111. logLevel: log.WarnLevel,
  112. expectedLines: 0,
  113. },
  114. {
  115. config: `
  116. mode: cat
  117. filename: /do/not/exist`,
  118. expectedConfigErr: "",
  119. expectedErr: "",
  120. expectedOutput: "No matching files for pattern /do/not/exist",
  121. logLevel: log.WarnLevel,
  122. expectedLines: 0,
  123. },
  124. {
  125. config: `
  126. mode: cat
  127. filename: test_files/test.log`,
  128. expectedConfigErr: "",
  129. expectedErr: "",
  130. expectedOutput: "",
  131. expectedLines: 5,
  132. logLevel: log.WarnLevel,
  133. },
  134. {
  135. config: `
  136. mode: cat
  137. filename: test_files/test.log.gz`,
  138. expectedConfigErr: "",
  139. expectedErr: "",
  140. expectedOutput: "",
  141. expectedLines: 5,
  142. logLevel: log.WarnLevel,
  143. },
  144. {
  145. config: `
  146. mode: cat
  147. filename: test_files/bad.gz`,
  148. expectedConfigErr: "",
  149. expectedErr: "failed to read gz test_files/bad.gz: unexpected EOF",
  150. expectedOutput: "",
  151. expectedLines: 0,
  152. logLevel: log.WarnLevel,
  153. },
  154. {
  155. config: `
  156. mode: cat
  157. filename: test_files/test_delete.log`,
  158. setup: func() {
  159. os.Create("test_files/test_delete.log")
  160. },
  161. afterConfigure: func() {
  162. os.Remove("test_files/test_delete.log")
  163. },
  164. expectedErr: "could not stat file test_files/test_delete.log : stat test_files/test_delete.log: no such file or directory",
  165. },
  166. }
  167. for _, ts := range tests {
  168. logger, hook := test.NewNullLogger()
  169. logger.SetLevel(ts.logLevel)
  170. subLogger := logger.WithFields(log.Fields{
  171. "type": "file",
  172. })
  173. tomb := tomb.Tomb{}
  174. out := make(chan types.Event)
  175. f := FileSource{}
  176. if ts.setup != nil {
  177. ts.setup()
  178. }
  179. err := f.Configure([]byte(ts.config), subLogger)
  180. cstest.AssertErrorContains(t, err, ts.expectedConfigErr)
  181. if err != nil {
  182. continue
  183. }
  184. if ts.afterConfigure != nil {
  185. ts.afterConfigure()
  186. }
  187. actualLines := 0
  188. if ts.expectedLines != 0 {
  189. go func() {
  190. READLOOP:
  191. for {
  192. select {
  193. case <-out:
  194. actualLines++
  195. case <-time.After(1 * time.Second):
  196. break READLOOP
  197. }
  198. }
  199. }()
  200. }
  201. err = f.OneShotAcquisition(out, &tomb)
  202. cstest.AssertErrorContains(t, err, ts.expectedErr)
  203. if ts.expectedLines != 0 {
  204. assert.Equal(t, actualLines, ts.expectedLines)
  205. }
  206. if ts.expectedOutput != "" {
  207. assert.Contains(t, hook.LastEntry().Message, ts.expectedOutput)
  208. hook.Reset()
  209. }
  210. if ts.teardown != nil {
  211. ts.teardown()
  212. }
  213. }
  214. }
  215. func TestLiveAcquisition(t *testing.T) {
  216. tests := []struct {
  217. config string
  218. expectedErr string
  219. expectedOutput string
  220. expectedLines int
  221. logLevel log.Level
  222. setup func()
  223. afterConfigure func()
  224. teardown func()
  225. }{
  226. {
  227. config: `
  228. mode: tail
  229. filename: /etc/shadow`,
  230. expectedErr: "",
  231. expectedOutput: "unable to read /etc/shadow : open /etc/shadow: permission denied",
  232. logLevel: log.InfoLevel,
  233. expectedLines: 0,
  234. },
  235. {
  236. config: `
  237. mode: tail
  238. filename: /`,
  239. expectedErr: "",
  240. expectedOutput: "/ is a directory, ignoring it",
  241. logLevel: log.WarnLevel,
  242. expectedLines: 0,
  243. },
  244. {
  245. config: `
  246. mode: tail
  247. filename: /do/not/exist`,
  248. expectedErr: "",
  249. expectedOutput: "No matching files for pattern /do/not/exist",
  250. logLevel: log.WarnLevel,
  251. expectedLines: 0,
  252. },
  253. {
  254. config: `
  255. mode: tail
  256. filenames:
  257. - test_files/*.log
  258. force_inotify: true`,
  259. expectedErr: "",
  260. expectedOutput: "",
  261. expectedLines: 5,
  262. logLevel: log.DebugLevel,
  263. },
  264. {
  265. config: `
  266. mode: tail
  267. filenames:
  268. - test_files/*.log
  269. force_inotify: true`,
  270. expectedErr: "",
  271. expectedOutput: "",
  272. expectedLines: 0,
  273. logLevel: log.DebugLevel,
  274. afterConfigure: func() {
  275. os.Create("test_files/a.log")
  276. os.Remove("test_files/a.log")
  277. },
  278. },
  279. {
  280. config: `
  281. mode: tail
  282. filenames:
  283. - test_files/*.log
  284. force_inotify: true`,
  285. expectedErr: "",
  286. expectedOutput: "",
  287. expectedLines: 5,
  288. logLevel: log.DebugLevel,
  289. afterConfigure: func() {
  290. os.Create("test_files/a.log")
  291. time.Sleep(1 * time.Second)
  292. os.Chmod("test_files/a.log", 0000)
  293. },
  294. teardown: func() {
  295. os.Chmod("test_files/a.log", 0644)
  296. os.Remove("test_files/a.log")
  297. },
  298. },
  299. {
  300. config: `
  301. mode: tail
  302. filenames:
  303. - test_files/*.log
  304. force_inotify: true`,
  305. expectedErr: "",
  306. expectedOutput: "",
  307. expectedLines: 5,
  308. logLevel: log.DebugLevel,
  309. afterConfigure: func() {
  310. os.Mkdir("test_files/pouet/", 0700)
  311. },
  312. teardown: func() {
  313. os.Remove("test_files/pouet/")
  314. },
  315. },
  316. }
  317. for _, ts := range tests {
  318. logger, hook := test.NewNullLogger()
  319. logger.SetLevel(ts.logLevel)
  320. subLogger := logger.WithFields(log.Fields{
  321. "type": "file",
  322. })
  323. tomb := tomb.Tomb{}
  324. out := make(chan types.Event)
  325. f := FileSource{}
  326. if ts.setup != nil {
  327. ts.setup()
  328. }
  329. err := f.Configure([]byte(ts.config), subLogger)
  330. if err != nil {
  331. t.Fatalf("Unexpected error : %s", err)
  332. }
  333. if ts.afterConfigure != nil {
  334. ts.afterConfigure()
  335. }
  336. actualLines := 0
  337. if ts.expectedLines != 0 {
  338. go func() {
  339. READLOOP:
  340. for {
  341. select {
  342. case <-out:
  343. actualLines++
  344. case <-time.After(2 * time.Second):
  345. break READLOOP
  346. }
  347. }
  348. }()
  349. }
  350. err = f.StreamingAcquisition(out, &tomb)
  351. cstest.AssertErrorContains(t, err, ts.expectedErr)
  352. if ts.expectedLines != 0 {
  353. fd, err := os.Create("test_files/stream.log")
  354. if err != nil {
  355. t.Fatalf("could not create test file : %s", err)
  356. }
  357. for i := 0; i < 5; i++ {
  358. _, err = fd.WriteString(fmt.Sprintf("%d\n", i))
  359. if err != nil {
  360. t.Fatalf("could not write test file : %s", err)
  361. os.Remove("test_files/stream.log")
  362. }
  363. }
  364. fd.Close()
  365. //we sleep to make sure we detect the new file
  366. time.Sleep(1 * time.Second)
  367. os.Remove("test_files/stream.log")
  368. assert.Equal(t, actualLines, ts.expectedLines)
  369. }
  370. if ts.expectedOutput != "" {
  371. if hook.LastEntry() == nil {
  372. t.Fatalf("expected output %s, but got nothing", ts.expectedOutput)
  373. }
  374. assert.Contains(t, hook.LastEntry().Message, ts.expectedOutput)
  375. hook.Reset()
  376. }
  377. if ts.teardown != nil {
  378. ts.teardown()
  379. }
  380. tomb.Kill(nil)
  381. }
  382. }