commandfilter.tcl 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. set testmodule [file normalize tests/modules/commandfilter.so]
  2. start_server {tags {"modules"}} {
  3. r module load $testmodule log-key 0
  4. test {Retain a command filter argument} {
  5. # Retain an argument now. Later we'll try to re-read it and make sure
  6. # it is not corrupt and that valgrind does not complain.
  7. r rpush some-list @retain my-retained-string
  8. r commandfilter.retained
  9. } {my-retained-string}
  10. test {Command Filter handles redirected commands} {
  11. r set mykey @log
  12. r lrange log-key 0 -1
  13. } "{set mykey @log}"
  14. test {Command Filter can call RedisModule_CommandFilterArgDelete} {
  15. r rpush mylist elem1 @delme elem2
  16. r lrange mylist 0 -1
  17. } {elem1 elem2}
  18. test {Command Filter can call RedisModule_CommandFilterArgInsert} {
  19. r del mylist
  20. r rpush mylist elem1 @insertbefore elem2 @insertafter elem3
  21. r lrange mylist 0 -1
  22. } {elem1 --inserted-before-- @insertbefore elem2 @insertafter --inserted-after-- elem3}
  23. test {Command Filter can call RedisModule_CommandFilterArgReplace} {
  24. r del mylist
  25. r rpush mylist elem1 @replaceme elem2
  26. r lrange mylist 0 -1
  27. } {elem1 --replaced-- elem2}
  28. test {Command Filter applies on RM_Call() commands} {
  29. r del log-key
  30. r commandfilter.ping
  31. r lrange log-key 0 -1
  32. } "{ping @log}"
  33. test {Command Filter applies on Lua redis.call()} {
  34. r del log-key
  35. r eval "redis.call('ping', '@log')" 0
  36. r lrange log-key 0 -1
  37. } "{ping @log}"
  38. test {Command Filter applies on Lua redis.call() that calls a module} {
  39. r del log-key
  40. r eval "redis.call('commandfilter.ping')" 0
  41. r lrange log-key 0 -1
  42. } "{ping @log}"
  43. test {Command Filter strings can be retained} {
  44. r commandfilter.retained
  45. } {my-retained-string}
  46. test {Command Filter is unregistered implicitly on module unload} {
  47. r del log-key
  48. r module unload commandfilter
  49. r set mykey @log
  50. r lrange log-key 0 -1
  51. } {}
  52. r module load $testmodule log-key 0
  53. test {Command Filter unregister works as expected} {
  54. # Validate reloading succeeded
  55. r del log-key
  56. r set mykey @log
  57. assert_equal "{set mykey @log}" [r lrange log-key 0 -1]
  58. # Unregister
  59. r commandfilter.unregister
  60. r del log-key
  61. r set mykey @log
  62. r lrange log-key 0 -1
  63. } {}
  64. r module unload commandfilter
  65. r module load $testmodule log-key 1
  66. test {Command Filter REDISMODULE_CMDFILTER_NOSELF works as expected} {
  67. r set mykey @log
  68. assert_equal "{set mykey @log}" [r lrange log-key 0 -1]
  69. r del log-key
  70. r commandfilter.ping
  71. assert_equal {} [r lrange log-key 0 -1]
  72. r eval "redis.call('commandfilter.ping')" 0
  73. assert_equal {} [r lrange log-key 0 -1]
  74. }
  75. }