2
0

rdb.tcl 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. set server_path [tmpdir "server.rdb-encoding-test"]
  2. # Copy RDB with different encodings in server path
  3. exec cp tests/assets/encodings.rdb $server_path
  4. start_server [list overrides [list "dir" $server_path "dbfilename" "encodings.rdb"]] {
  5. test "RDB encoding loading test" {
  6. r select 0
  7. csvdump r
  8. } {"0","compressible","string","aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  9. "0","hash","hash","a","1","aa","10","aaa","100","b","2","bb","20","bbb","200","c","3","cc","30","ccc","300","ddd","400","eee","5000000000",
  10. "0","hash_zipped","hash","a","1","b","2","c","3",
  11. "0","list","list","1","2","3","a","b","c","100000","6000000000","1","2","3","a","b","c","100000","6000000000","1","2","3","a","b","c","100000","6000000000",
  12. "0","list_zipped","list","1","2","3","a","b","c","100000","6000000000",
  13. "0","number","string","10"
  14. "0","set","set","1","100000","2","3","6000000000","a","b","c",
  15. "0","set_zipped_1","set","1","2","3","4",
  16. "0","set_zipped_2","set","100000","200000","300000","400000",
  17. "0","set_zipped_3","set","1000000000","2000000000","3000000000","4000000000","5000000000","6000000000",
  18. "0","string","string","Hello World"
  19. "0","zset","zset","a","1","b","2","c","3","aa","10","bb","20","cc","30","aaa","100","bbb","200","ccc","300","aaaa","1000","cccc","123456789","bbbb","5000000000",
  20. "0","zset_zipped","zset","a","1","b","2","c","3",
  21. }
  22. }
  23. set server_path [tmpdir "server.rdb-startup-test"]
  24. start_server [list overrides [list "dir" $server_path]] {
  25. test {Server started empty with non-existing RDB file} {
  26. r debug digest
  27. } {0000000000000000000000000000000000000000}
  28. # Save an RDB file, needed for the next test.
  29. r save
  30. }
  31. start_server [list overrides [list "dir" $server_path]] {
  32. test {Server started empty with empty RDB file} {
  33. r debug digest
  34. } {0000000000000000000000000000000000000000}
  35. }
  36. start_server [list overrides [list "dir" $server_path]] {
  37. test {Test RDB stream encoding} {
  38. for {set j 0} {$j < 1000} {incr j} {
  39. if {rand() < 0.9} {
  40. r xadd stream * foo $j
  41. } else {
  42. r xadd stream * bar $j
  43. }
  44. }
  45. r xgroup create stream mygroup 0
  46. r xreadgroup GROUP mygroup Alice COUNT 1 STREAMS stream >
  47. set digest [r debug digest]
  48. r debug reload
  49. set newdigest [r debug digest]
  50. assert {$digest eq $newdigest}
  51. r del stream
  52. }
  53. }
  54. # Helper function to start a server and kill it, just to check the error
  55. # logged.
  56. set defaults {}
  57. proc start_server_and_kill_it {overrides code} {
  58. upvar defaults defaults srv srv server_path server_path
  59. set config [concat $defaults $overrides]
  60. set srv [start_server [list overrides $config]]
  61. uplevel 1 $code
  62. kill_server $srv
  63. }
  64. # Make the RDB file unreadable
  65. file attributes [file join $server_path dump.rdb] -permissions 0222
  66. # Detect root account (it is able to read the file even with 002 perm)
  67. set isroot 0
  68. catch {
  69. open [file join $server_path dump.rdb]
  70. set isroot 1
  71. }
  72. # Now make sure the server aborted with an error
  73. if {!$isroot} {
  74. start_server_and_kill_it [list "dir" $server_path] {
  75. test {Server should not start if RDB file can't be open} {
  76. wait_for_condition 50 100 {
  77. [string match {*Fatal error loading*} \
  78. [exec tail -1 < [dict get $srv stdout]]]
  79. } else {
  80. fail "Server started even if RDB was unreadable!"
  81. }
  82. }
  83. }
  84. }
  85. # Fix permissions of the RDB file.
  86. file attributes [file join $server_path dump.rdb] -permissions 0666
  87. # Corrupt its CRC64 checksum.
  88. set filesize [file size [file join $server_path dump.rdb]]
  89. set fd [open [file join $server_path dump.rdb] r+]
  90. fconfigure $fd -translation binary
  91. seek $fd -8 end
  92. puts -nonewline $fd "foobar00"; # Corrupt the checksum
  93. close $fd
  94. # Now make sure the server aborted with an error
  95. start_server_and_kill_it [list "dir" $server_path] {
  96. test {Server should not start if RDB is corrupted} {
  97. wait_for_condition 50 100 {
  98. [string match {*CRC error*} \
  99. [exec tail -10 < [dict get $srv stdout]]]
  100. } else {
  101. fail "Server started even if RDB was corrupted!"
  102. }
  103. }
  104. }