rt_sink_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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.*" ],
  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. scopematch
  41. list
  42. string: 'data.write'
  43. statematch
  44. map
  45. kvp
  46. string: 'val'
  47. null
  48. priority
  49. number: 10
  50. suppresses
  51. list
  52. string: 'rule2'
  53. statements
  54. identifier: log
  55. funccall
  56. string: 'rule1 < '
  57. identifier: event
  58. `[1:])
  59. if err != nil {
  60. t.Error(err)
  61. return
  62. }
  63. // Nothing defined in the global scope
  64. if vs.String() != `
  65. GlobalScope {
  66. }`[1:] {
  67. t.Error("Unexpected result: ", vs)
  68. return
  69. }
  70. if res := fmt.Sprint(testprocessor.Rules()["rule1"]); res !=
  71. `Rule:rule1 [My cool rule] (Priority:10 Kind:[core.*] Scope:[data.write] StateMatch:{"val":null} Suppress:[rule2])` {
  72. t.Error("Unexpected result:", res)
  73. return
  74. }
  75. // Test case 1 - Multiple rules, scope match, priorities and waiting for finish (no errors)
  76. _, err = UnitTestEval(
  77. `
  78. sink rule1
  79. kindmatch [ "web.page.index" ],
  80. scopematch [ "request.read" ],
  81. {
  82. log("rule1 - Handling request: ", event.kind)
  83. addEvent("Rule1Event1", "not_existing", event.state)
  84. addEvent("Rule1Event2", "web.log", event.state)
  85. }
  86. sink rule2
  87. kindmatch [ "web.page.*" ],
  88. priority 1, # Ensure this rule is always executed after rule1
  89. {
  90. log("rule2 - Tracking user:", event.state.user)
  91. if event.state.user == "bar" {
  92. sinkError("User bar was here", [123])
  93. }
  94. }
  95. sink rule3
  96. kindmatch [ "web.log" ],
  97. {
  98. log("rule3 - Logging user:", event.state.user)
  99. }
  100. res := addEventAndWait("request", "web.page.index", {
  101. "user" : "foo"
  102. }, {
  103. "request.read" : true
  104. })
  105. log("ErrorResult:", res, " ", len(res) == 0)
  106. res := addEventAndWait("request", "web.page.index", {
  107. "user" : "bar"
  108. }, {
  109. "request.read" : false
  110. })
  111. log("ErrorResult:", res, " ", res == null)
  112. `, vs)
  113. if err != nil {
  114. t.Error(err)
  115. return
  116. }
  117. if testlogger.String() != `
  118. rule1 - Handling request: web.page.index
  119. rule2 - Tracking user:foo
  120. rule3 - Logging user:foo
  121. ErrorResult:null true
  122. rule2 - Tracking user:bar
  123. ErrorResult:[
  124. {
  125. "errors": {
  126. "rule2": {
  127. "detail": [
  128. 123
  129. ],
  130. "message": "ECAL error in ECALTestRuntime: Error in sink (User bar was here) (Line:17 Pos:13)"
  131. }
  132. },
  133. "event": {
  134. "kind": "web.page.index",
  135. "name": "request",
  136. "state": {
  137. "user": "bar"
  138. }
  139. }
  140. }
  141. ] false`[1:] {
  142. t.Error("Unexpected result:", testlogger.String())
  143. return
  144. }
  145. // Test case 2 - unexpected error
  146. _, err = UnitTestEval(
  147. `
  148. sink rule1
  149. kindmatch [ "test" ],
  150. {
  151. log("rule1 - ", event.kind)
  152. noexitingfunctioncall()
  153. }
  154. err := addEventAndWait("someevent", "test", {})
  155. if err != null {
  156. error(err[0].errors)
  157. }
  158. `, vs)
  159. if err != nil {
  160. t.Error(err)
  161. return
  162. }
  163. if testlogger.String() != `
  164. rule1 - test
  165. error: {
  166. "rule1": {
  167. "detail": null,
  168. "message": "ECAL error in ECALTestRuntime: Unknown construct (Unknown function: noexitingfunctioncall) (Line:6 Pos:9)"
  169. }
  170. }`[1:] {
  171. t.Error("Unexpected result:", testlogger.String())
  172. return
  173. }
  174. // Test case 3 - rule suppression
  175. _, err = UnitTestEval(
  176. `
  177. sink rule1
  178. kindmatch [ "test.event" ],
  179. suppresses [ "rule3" ],
  180. {
  181. log("rule1 - Handling request: ", event.kind)
  182. }
  183. sink rule2
  184. kindmatch [ "test.*" ],
  185. priority 1, # Ensure this rule is always executed after rule1
  186. {
  187. log("rule2 - Handling request: ", event.kind)
  188. }
  189. sink rule3
  190. kindmatch [ "test.*" ],
  191. priority 1, # Ensure this rule is always executed after rule1
  192. {
  193. log("rule3 - Handling request: ", event.kind)
  194. }
  195. err := addEventAndWait("myevent", "test.event", {})
  196. if len(err) > 0 {
  197. error(err[0].errors)
  198. }
  199. `, vs)
  200. if err != nil {
  201. t.Error(err)
  202. return
  203. }
  204. if testlogger.String() != `
  205. rule1 - Handling request: test.event
  206. rule2 - Handling request: test.event`[1:] {
  207. t.Error("Unexpected result:", testlogger.String())
  208. return
  209. }
  210. // Test case 4 - state match
  211. _, err = UnitTestEval(
  212. `
  213. sink rule1
  214. kindmatch [ "test.event" ],
  215. statematch { "a" : null },
  216. {
  217. log("rule1 - Handling request: ", event.kind)
  218. }
  219. sink rule2
  220. kindmatch [ "test.*" ],
  221. priority 1,
  222. statematch { "b" : 1 },
  223. {
  224. log("rule2 - Handling request: ", event.kind)
  225. }
  226. sink rule3
  227. kindmatch [ "test.*" ],
  228. priority 2,
  229. statematch { "c" : 2 },
  230. {
  231. log("rule3 - Handling request: ", event.kind)
  232. }
  233. err := addEventAndWait("myevent", "test.event", {
  234. "a" : "foo",
  235. "b" : 1,
  236. })
  237. if len(err) > 0 {
  238. error(err[0].errors)
  239. }
  240. `, vs)
  241. if err != nil {
  242. t.Error(err)
  243. return
  244. }
  245. if testlogger.String() != `
  246. rule1 - Handling request: test.event
  247. rule2 - Handling request: test.event`[1:] {
  248. t.Error("Unexpected result:", testlogger.String())
  249. return
  250. }
  251. }