rt_sink_test.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. }
  102. res := addEventAndWait("request", "web.page.index", {
  103. "user" : "foo"
  104. }, {
  105. "request.read" : true
  106. })
  107. log("ErrorResult:", res, " ", len(res) == 0)
  108. res := addEventAndWait("request", "web.page.index", {
  109. "user" : "bar"
  110. }, {
  111. "request.read" : false
  112. })
  113. log("ErrorResult:", res, " ", res == null)
  114. `, vs)
  115. if err != nil {
  116. t.Error(err)
  117. return
  118. }
  119. if testlogger.String() != `
  120. rule1 - Handling request: web.page.index
  121. rule2 - Tracking user:foo
  122. rule3 - Logging user:foo
  123. ErrorResult:null true
  124. rule2 - Tracking user:bar
  125. ErrorResult:[
  126. {
  127. "errors": {
  128. "rule2": {
  129. "data": [
  130. 123
  131. ],
  132. "detail": "User bar was seen",
  133. "error": "ECAL error in ECALTestRuntime: UserBarWasHere (User bar was seen) (Line:18 Pos:13)",
  134. "type": "UserBarWasHere"
  135. }
  136. },
  137. "event": {
  138. "kind": "web.page.index",
  139. "name": "request",
  140. "state": {
  141. "user": "bar"
  142. }
  143. }
  144. }
  145. ] false`[1:] {
  146. t.Error("Unexpected result:", testlogger.String())
  147. return
  148. }
  149. // Test case 2 - unexpected error
  150. _, err = UnitTestEval(
  151. `
  152. sink rule1
  153. kindmatch [ "test" ],
  154. {
  155. log("rule1 - ", event.kind)
  156. noexitingfunctioncall()
  157. }
  158. err := addEventAndWait("someevent", "test", {})
  159. if err != null {
  160. error(err[0].errors)
  161. }
  162. `, vs)
  163. if err != nil {
  164. t.Error(err)
  165. return
  166. }
  167. if testlogger.String() != `
  168. rule1 - test
  169. error: {
  170. "rule1": {
  171. "data": null,
  172. "detail": "Unknown function: noexitingfunctioncall",
  173. "error": "ECAL error in ECALTestRuntime: Unknown construct (Unknown function: noexitingfunctioncall) (Line:6 Pos:9)",
  174. "type": "Unknown construct"
  175. }
  176. }`[1:] {
  177. t.Error("Unexpected result:", testlogger.String())
  178. return
  179. }
  180. // Test case 3 - rule suppression
  181. _, err = UnitTestEval(
  182. `
  183. sink rule1
  184. kindmatch [ "test.event" ],
  185. suppresses [ "rule3" ],
  186. {
  187. log("rule1 - Handling request: ", event.kind)
  188. }
  189. sink rule2
  190. kindmatch [ "test.*" ],
  191. priority 1, # Ensure this rule is always executed after rule1
  192. {
  193. log("rule2 - Handling request: ", event.kind)
  194. }
  195. sink rule3
  196. kindmatch [ "test.*" ],
  197. priority 1, # Ensure this rule is always executed after rule1
  198. {
  199. log("rule3 - Handling request: ", event.kind)
  200. }
  201. err := addEventAndWait("myevent", "test.event", {})
  202. if len(err) > 0 {
  203. error(err[0].errors)
  204. }
  205. `, vs)
  206. if err != nil {
  207. t.Error(err)
  208. return
  209. }
  210. if testlogger.String() != `
  211. rule1 - Handling request: test.event
  212. rule2 - Handling request: test.event`[1:] {
  213. t.Error("Unexpected result:", testlogger.String())
  214. return
  215. }
  216. // Test case 4 - state match
  217. _, err = UnitTestEval(
  218. `
  219. sink rule1
  220. kindmatch [ "test.event", "foo.*" ],
  221. statematch { "a" : null },
  222. {
  223. log("rule1 - Handling request: ", event.kind)
  224. }
  225. sink rule2
  226. kindmatch [ "test.*" ],
  227. priority 1,
  228. statematch { "b" : 1 },
  229. {
  230. log("rule2 - Handling request: ", event.kind)
  231. }
  232. sink rule3
  233. kindmatch [ "test.*" ],
  234. priority 2,
  235. statematch { "c" : 2 },
  236. {
  237. log("rule3 - Handling request: ", event.kind)
  238. }
  239. err := addEventAndWait("myevent", "test.event", {
  240. "a" : "foo",
  241. "b" : 1,
  242. })
  243. if len(err) > 0 {
  244. error(err[0].errors)
  245. }
  246. `, vs)
  247. if err != nil {
  248. t.Error(err)
  249. return
  250. }
  251. if testlogger.String() != `
  252. rule1 - Handling request: test.event
  253. rule2 - Handling request: test.event`[1:] {
  254. t.Error("Unexpected result:", testlogger.String())
  255. return
  256. }
  257. }
  258. func TestSinkErrorConditions(t *testing.T) {
  259. vs := scope.NewScope(scope.GlobalScope)
  260. _, err := UnitTestEval(
  261. `
  262. sink test
  263. apa
  264. kindmatch [ "test.event", "foo.*" ],
  265. statematch { "a" : null },
  266. {
  267. log("rule1 - Handling request: ", event.kind)
  268. }
  269. `, vs)
  270. if err == nil || err.Error() != "ECAL error in ECALTestRuntime: Invalid construct (Unknown expression in sink declaration apa) (Line:3 Pos:5)" {
  271. t.Error("Unexpected result:", err)
  272. return
  273. }
  274. _, err = UnitTestEval(
  275. `
  276. sink test
  277. kindmatch 1,
  278. statematch { "a" : null },
  279. {
  280. log("rule1 - Handling request: ", event.kind)
  281. }
  282. `, vs)
  283. if err == nil || err.Error() != "ECAL error in ECALTestRuntime: Invalid construct (Expected a list as value) (Line:3 Pos:5)" {
  284. t.Error("Unexpected result:", err)
  285. return
  286. }
  287. _, err = UnitTestEval(
  288. `
  289. sink test
  290. statematch { "a" : null },
  291. {
  292. log("rule1 - Handling request: ", event.kind)
  293. }
  294. `, vs)
  295. if err == nil || err.Error() != "ECAL error in ECALTestRuntime: Invalid state (Cannot add rule without a kind match: test) (Line:2 Pos:1)" {
  296. t.Error("Unexpected result:", err)
  297. return
  298. }
  299. _, err = UnitTestEval(
  300. `
  301. sink test
  302. priority "Hans",
  303. {
  304. log("rule1 - Handling request: ", event.kind)
  305. }
  306. `, vs)
  307. if err == nil || err.Error() != "ECAL error in ECALTestRuntime: Invalid construct (Expected a number as value) (Line:3 Pos:5)" {
  308. t.Error("Unexpected result:", err)
  309. return
  310. }
  311. _, err = UnitTestEval(
  312. `
  313. sink test
  314. statematch "Hans",
  315. {
  316. log("rule1 - Handling request: ", event.kind)
  317. }
  318. `, vs)
  319. if err == nil || err.Error() != "ECAL error in ECALTestRuntime: Invalid construct (Expected a map as value) (Line:3 Pos:5)" {
  320. t.Error("Unexpected result:", err)
  321. return
  322. }
  323. }