rt_sink_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. _, err = UnitTestEval(
  76. `
  77. sink rule1
  78. kindmatch [ "web.page.index" ],
  79. scopematch [ "request.read" ],
  80. {
  81. log("rule1 > Handling request: ", event.kind)
  82. addEvent("Rule1Event1", "not_existing", event.state)
  83. addEvent("Rule1Event2", "web.log", event.state)
  84. }
  85. sink rule2
  86. kindmatch [ "web.page.*" ],
  87. priority 1, # Ensure this rule is always executed after rule1
  88. {
  89. log("rule2 > Tracking user:", event.state.user)
  90. }
  91. sink rule3
  92. kindmatch [ "web.log" ],
  93. {
  94. log("rule3 > Logging user:", event.state.user)
  95. }
  96. res := addEventAndWait("request", "web.page.index", {
  97. "user" : "foo"
  98. }, {
  99. "request.read" : true
  100. })
  101. log("ErrorResult:", res, len(res) == 0)
  102. `, vs)
  103. if err != nil {
  104. t.Error(err)
  105. return
  106. }
  107. if testlogger.String() != `
  108. rule1 > Handling request: web.page.index
  109. rule2 > Tracking user:foo
  110. rule3 > Logging user:foo
  111. ErrorResult:[] true`[1:] {
  112. t.Error("Unexpected result:", testlogger.String())
  113. return
  114. }
  115. }