rdb.tcl 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. set digest [r debug digest]
  46. r debug reload
  47. set newdigest [r debug digest]
  48. assert {$digest eq $newdigest}
  49. r del stream
  50. }
  51. }
  52. # Helper function to start a server and kill it, just to check the error
  53. # logged.
  54. set defaults {}
  55. proc start_server_and_kill_it {overrides code} {
  56. upvar defaults defaults srv srv server_path server_path
  57. set config [concat $defaults $overrides]
  58. set srv [start_server [list overrides $config]]
  59. uplevel 1 $code
  60. kill_server $srv
  61. }
  62. # Make the RDB file unreadable
  63. file attributes [file join $server_path dump.rdb] -permissions 0222
  64. # Detect root account (it is able to read the file even with 002 perm)
  65. set isroot 0
  66. catch {
  67. open [file join $server_path dump.rdb]
  68. set isroot 1
  69. }
  70. # Now make sure the server aborted with an error
  71. if {!$isroot} {
  72. start_server_and_kill_it [list "dir" $server_path] {
  73. test {Server should not start if RDB file can't be open} {
  74. wait_for_condition 50 100 {
  75. [string match {*Fatal error loading*} \
  76. [exec tail -1 < [dict get $srv stdout]]]
  77. } else {
  78. fail "Server started even if RDB was unreadable!"
  79. }
  80. }
  81. }
  82. }
  83. # Fix permissions of the RDB file.
  84. file attributes [file join $server_path dump.rdb] -permissions 0666
  85. # Corrupt its CRC64 checksum.
  86. set filesize [file size [file join $server_path dump.rdb]]
  87. set fd [open [file join $server_path dump.rdb] r+]
  88. fconfigure $fd -translation binary
  89. seek $fd -8 end
  90. puts -nonewline $fd "foobar00"; # Corrupt the checksum
  91. close $fd
  92. # Now make sure the server aborted with an error
  93. start_server_and_kill_it [list "dir" $server_path] {
  94. test {Server should not start if RDB is corrupted} {
  95. wait_for_condition 50 100 {
  96. [string match {*CRC error*} \
  97. [exec tail -10 < [dict get $srv stdout]]]
  98. } else {
  99. fail "Server started even if RDB was corrupted!"
  100. }
  101. }
  102. }