rt_sink_test.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /*
  2. * ECAL
  3. *
  4. * Copyright 2020 Matthias Ladkau. All rights reserved.
  5. *
  6. * This Source Code Form is subject to the terms of the MIT
  7. * License, If a copy of the MIT License was not distributed with this
  8. * file, You can obtain one at https://opensource.org/licenses/MIT.
  9. */
  10. package interpreter
  11. import (
  12. "fmt"
  13. "testing"
  14. "devt.de/krotik/ecal/scope"
  15. )
  16. func TestEventProcessing(t *testing.T) {
  17. vs := scope.NewScope(scope.GlobalScope)
  18. _, err := UnitTestEvalAndAST(
  19. `
  20. /*
  21. My cool rule
  22. */
  23. sink rule1
  24. kindmatch [ "core.*", "foo.*" ],
  25. scopematch [ "data.write" ],
  26. statematch { "val" : NULL },
  27. priority 10,
  28. suppresses [ "rule2" ]
  29. {
  30. log("rule1 < ", event)
  31. }
  32. `, vs,
  33. `
  34. sink #
  35. My cool rule
  36. identifier: rule1
  37. kindmatch
  38. list
  39. string: 'core.*'
  40. string: 'foo.*'
  41. scopematch
  42. list
  43. string: 'data.write'
  44. statematch
  45. map
  46. kvp
  47. string: 'val'
  48. null
  49. priority
  50. number: 10
  51. suppresses
  52. list
  53. string: 'rule2'
  54. statements
  55. identifier: log
  56. funccall
  57. string: 'rule1 < '
  58. identifier: event
  59. `[1:])
  60. if err != nil {
  61. t.Error(err)
  62. return
  63. }
  64. // Nothing defined in the global scope
  65. if vs.String() != `
  66. GlobalScope {
  67. }`[1:] {
  68. t.Error("Unexpected result: ", vs)
  69. return
  70. }
  71. if res := fmt.Sprint(testprocessor.Rules()["rule1"]); res !=
  72. `Rule:rule1 [My cool rule] (Priority:10 Kind:[core.* foo.*] Scope:[data.write] StateMatch:{"val":null} Suppress:[rule2])` {
  73. t.Error("Unexpected result:", res)
  74. return
  75. }
  76. // Test case 1 - Multiple rules, scope match, priorities and waiting for finish (no errors)
  77. _, err = UnitTestEval(
  78. `
  79. sink rule1
  80. kindmatch [ "web.page.index" ],
  81. scopematch [ "request.read" ],
  82. {
  83. log("rule1 - Handling request: ", event.kind)
  84. addEvent("Rule1Event1", "not_existing", event.state)
  85. addEvent("Rule1Event2", "web.log", event.state)
  86. addEvent("Rule1Event3", "notexisting", event.state, {})
  87. }
  88. sink rule2
  89. kindmatch [ "web.page.*" ],
  90. priority 1, # Ensure this rule is always executed after rule1
  91. {
  92. log("rule2 - Tracking user:", event.state.user)
  93. if event.state.user == "bar" {
  94. raise("UserBarWasHere", "User bar was seen", [123])
  95. }
  96. }
  97. sink rule3
  98. kindmatch [ "web.log" ],
  99. {
  100. log("rule3 - Logging user:", event.state.user)
  101. return 123
  102. }
  103. res := addEventAndWait("request", "web.page.index", {
  104. "user" : "foo"
  105. }, {
  106. "request.read" : true
  107. })
  108. log("ErrorResult:", res, " ", len(res) == 0)
  109. res := addEventAndWait("request", "web.page.index", {
  110. "user" : "bar"
  111. }, {
  112. "request.read" : false
  113. })
  114. log("ErrorResult:", res, " ", res == null)
  115. `, vs)
  116. if err != nil {
  117. t.Error(err)
  118. return
  119. }
  120. if testlogger.String() != `
  121. rule1 - Handling request: web.page.index
  122. rule2 - Tracking user:foo
  123. rule3 - Logging user:foo
  124. ErrorResult:[
  125. {
  126. "errors": {
  127. "rule3": {
  128. "data": 123,
  129. "detail": "Return value: 123",
  130. "error": "ECAL error in ECALTestRuntime: *** return *** (Return value: 123) (Line:26 Pos:9)",
  131. "type": "*** return ***"
  132. }
  133. },
  134. "event": {
  135. "kind": "web.log",
  136. "name": "Rule1Event2",
  137. "state": {
  138. "user": "foo"
  139. }
  140. }
  141. }
  142. ] false
  143. rule2 - Tracking user:bar
  144. ErrorResult:[
  145. {
  146. "errors": {
  147. "rule2": {
  148. "data": [
  149. 123
  150. ],
  151. "detail": "User bar was seen",
  152. "error": "ECAL error in ECALTestRuntime: UserBarWasHere (User bar was seen) (Line:18 Pos:13)",
  153. "type": "UserBarWasHere"
  154. }
  155. },
  156. "event": {
  157. "kind": "web.page.index",
  158. "name": "request",
  159. "state": {
  160. "user": "bar"
  161. }
  162. }
  163. }
  164. ] false`[1:] {
  165. t.Error("Unexpected result:", testlogger.String())
  166. return
  167. }
  168. // Test case 2 - unexpected error
  169. _, err = UnitTestEval(
  170. `
  171. sink rule1
  172. kindmatch [ "test" ],
  173. {
  174. log("rule1 - ", event.kind)
  175. noexitingfunctioncall()
  176. }
  177. err := addEventAndWait("someevent", "test", {})
  178. if err != null {
  179. error(err[0].errors)
  180. }
  181. `, vs)
  182. if err != nil {
  183. t.Error(err)
  184. return
  185. }
  186. if testlogger.String() != `
  187. rule1 - test
  188. error: {
  189. "rule1": {
  190. "data": null,
  191. "detail": "Unknown function: noexitingfunctioncall",
  192. "error": "ECAL error in ECALTestRuntime: Unknown construct (Unknown function: noexitingfunctioncall) (Line:6 Pos:9)",
  193. "type": "Unknown construct"
  194. }
  195. }`[1:] {
  196. t.Error("Unexpected result:", testlogger.String())
  197. return
  198. }
  199. // Test case 3 - rule suppression
  200. _, err = UnitTestEval(
  201. `
  202. sink rule1
  203. kindmatch [ "test.event" ],
  204. suppresses [ "rule3" ],
  205. {
  206. log("rule1 - Handling request: ", event.kind)
  207. }
  208. sink rule2
  209. kindmatch [ "test.*" ],
  210. priority 1, # Ensure this rule is always executed after rule1
  211. {
  212. log("rule2 - Handling request: ", event.kind)
  213. }
  214. sink rule3
  215. kindmatch [ "test.*" ],
  216. priority 1, # Ensure this rule is always executed after rule1
  217. {
  218. log("rule3 - Handling request: ", event.kind)
  219. }
  220. err := addEventAndWait("myevent", "test.event", {})
  221. if len(err) > 0 {
  222. error(err[0].errors)
  223. }
  224. `, vs)
  225. if err != nil {
  226. t.Error(err)
  227. return
  228. }
  229. if testlogger.String() != `
  230. rule1 - Handling request: test.event
  231. rule2 - Handling request: test.event`[1:] {
  232. t.Error("Unexpected result:", testlogger.String())
  233. return
  234. }
  235. // Test case 4 - state match
  236. _, err = UnitTestEval(
  237. `
  238. sink rule1
  239. kindmatch [ "test.event", "foo.*" ],
  240. statematch { "a" : null },
  241. {
  242. log("rule1 - Handling request: ", event.kind)
  243. }
  244. sink rule2
  245. kindmatch [ "test.*" ],
  246. priority 1,
  247. statematch { "b" : 1 },
  248. {
  249. log("rule2 - Handling request: ", event.kind)
  250. }
  251. sink rule3
  252. kindmatch [ "test.*" ],
  253. priority 2,
  254. statematch { "c" : 2 },
  255. {
  256. log("rule3 - Handling request: ", event.kind)
  257. }
  258. err := addEventAndWait("myevent", "test.event", {
  259. "a" : "foo",
  260. "b" : 1,
  261. })
  262. if len(err) > 0 {
  263. error(err[0].errors)
  264. }
  265. `, vs)
  266. if err != nil {
  267. t.Error(err)
  268. return
  269. }
  270. if testlogger.String() != `
  271. rule1 - Handling request: test.event
  272. rule2 - Handling request: test.event`[1:] {
  273. t.Error("Unexpected result:", testlogger.String())
  274. return
  275. }
  276. }
  277. func TestSinkErrorConditions(t *testing.T) {
  278. vs := scope.NewScope(scope.GlobalScope)
  279. _, err := UnitTestEval(
  280. `
  281. sink test
  282. apa
  283. kindmatch [ "test.event", "foo.*" ],
  284. statematch { "a" : null },
  285. {
  286. log("rule1 - Handling request: ", event.kind)
  287. }
  288. `, vs)
  289. if err == nil || err.Error() != "ECAL error in ECALTestRuntime: Invalid construct (Unknown expression in sink declaration apa) (Line:3 Pos:5)" {
  290. t.Error("Unexpected result:", err)
  291. return
  292. }
  293. _, err = UnitTestEval(
  294. `
  295. sink test
  296. kindmatch 1,
  297. statematch { "a" : null },
  298. {
  299. log("rule1 - Handling request: ", event.kind)
  300. }
  301. `, vs)
  302. if err == nil || err.Error() != "ECAL error in ECALTestRuntime: Invalid construct (Expected a list as value) (Line:3 Pos:5)" {
  303. t.Error("Unexpected result:", err)
  304. return
  305. }
  306. _, err = UnitTestEval(
  307. `
  308. sink test
  309. statematch { "a" : null },
  310. {
  311. log("rule1 - Handling request: ", event.kind)
  312. }
  313. `, vs)
  314. if err == nil || err.Error() != "ECAL error in ECALTestRuntime: Invalid state (Cannot add rule without a kind match: test) (Line:2 Pos:1)" {
  315. t.Error("Unexpected result:", err)
  316. return
  317. }
  318. _, err = UnitTestEval(
  319. `
  320. sink test
  321. priority "Hans",
  322. {
  323. log("rule1 - Handling request: ", event.kind)
  324. }
  325. `, vs)
  326. if err == nil || err.Error() != "ECAL error in ECALTestRuntime: Invalid construct (Expected a number as value) (Line:3 Pos:5)" {
  327. t.Error("Unexpected result:", err)
  328. return
  329. }
  330. _, err = UnitTestEval(
  331. `
  332. sink test
  333. statematch "Hans",
  334. {
  335. log("rule1 - Handling request: ", event.kind)
  336. }
  337. `, vs)
  338. if err == nil || err.Error() != "ECAL error in ECALTestRuntime: Invalid construct (Expected a map as value) (Line:3 Pos:5)" {
  339. t.Error("Unexpected result:", err)
  340. return
  341. }
  342. }