test.tcl 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. set ::num_tests 0
  2. set ::num_passed 0
  3. set ::num_failed 0
  4. set ::num_skipped 0
  5. set ::num_aborted 0
  6. set ::tests_failed {}
  7. set ::cur_test ""
  8. proc fail {msg} {
  9. error "assertion:$msg"
  10. }
  11. proc assert {condition} {
  12. if {![uplevel 1 [list expr $condition]]} {
  13. set context "(context: [info frame -1])"
  14. error "assertion:Expected [uplevel 1 [list subst -nocommands $condition]] $context"
  15. }
  16. }
  17. proc assert_no_match {pattern value} {
  18. if {[string match $pattern $value]} {
  19. set context "(context: [info frame -1])"
  20. error "assertion:Expected '$value' to not match '$pattern' $context"
  21. }
  22. }
  23. proc assert_match {pattern value} {
  24. if {![string match $pattern $value]} {
  25. set context "(context: [info frame -1])"
  26. error "assertion:Expected '$value' to match '$pattern' $context"
  27. }
  28. }
  29. proc assert_failed {expected_err detail} {
  30. if {$detail ne ""} {
  31. set detail "(detail: $detail)"
  32. } else {
  33. set detail "(context: [info frame -2])"
  34. }
  35. error "assertion:$expected_err $detail"
  36. }
  37. proc assert_equal {value expected {detail ""}} {
  38. if {$expected ne $value} {
  39. assert_failed "Expected '$value' to be equal to '$expected'" $detail
  40. }
  41. }
  42. proc assert_lessthan {value expected {detail ""}} {
  43. if {!($value < $expected)} {
  44. assert_failed "Expected '$value' to be less than '$expected'" $detail
  45. }
  46. }
  47. proc assert_lessthan_equal {value expected {detail ""}} {
  48. if {!($value <= $expected)} {
  49. assert_failed "Expected '$value' to be less than or equal to '$expected'" $detail
  50. }
  51. }
  52. proc assert_morethan {value expected {detail ""}} {
  53. if {!($value > $expected)} {
  54. assert_failed "Expected '$value' to be more than '$expected'" $detail
  55. }
  56. }
  57. proc assert_morethan_equal {value expected {detail ""}} {
  58. if {!($value >= $expected)} {
  59. assert_failed "Expected '$value' to be more than or equal to '$expected'" $detail
  60. }
  61. }
  62. proc assert_range {value min max {detail ""}} {
  63. if {!($value <= $max && $value >= $min)} {
  64. assert_failed "Expected '$value' to be between to '$min' and '$max'" $detail
  65. }
  66. }
  67. proc assert_error {pattern code} {
  68. if {[catch {uplevel 1 $code} error]} {
  69. assert_match $pattern $error
  70. } else {
  71. error "assertion:Expected an error but nothing was caught"
  72. }
  73. }
  74. proc assert_encoding {enc key} {
  75. if {$::ignoreencoding} {
  76. return
  77. }
  78. set dbg [r debug object $key]
  79. assert_match "* encoding:$enc *" $dbg
  80. }
  81. proc assert_type {type key} {
  82. assert_equal $type [r type $key]
  83. }
  84. # Wait for the specified condition to be true, with the specified number of
  85. # max retries and delay between retries. Otherwise the 'elsescript' is
  86. # executed.
  87. proc wait_for_condition {maxtries delay e _else_ elsescript} {
  88. while {[incr maxtries -1] >= 0} {
  89. set errcode [catch {uplevel 1 [list expr $e]} result]
  90. if {$errcode == 0} {
  91. if {$result} break
  92. } else {
  93. return -code $errcode $result
  94. }
  95. after $delay
  96. }
  97. if {$maxtries == -1} {
  98. set errcode [catch [uplevel 1 $elsescript] result]
  99. return -code $errcode $result
  100. }
  101. }
  102. proc search_pattern_list {value pattern_list} {
  103. set n 0
  104. foreach el $pattern_list {
  105. if {[string length $el] > 0 && [regexp -- $el $value]} {
  106. return $n
  107. }
  108. incr n
  109. }
  110. return -1
  111. }
  112. proc test {name code {okpattern undefined} {tags {}}} {
  113. # abort if test name in skiptests
  114. if {[search_pattern_list $name $::skiptests] >= 0} {
  115. incr ::num_skipped
  116. send_data_packet $::test_server_fd skip $name
  117. return
  118. }
  119. # abort if only_tests was set but test name is not included
  120. if {[llength $::only_tests] > 0 && [search_pattern_list $name $::only_tests] < 0} {
  121. incr ::num_skipped
  122. send_data_packet $::test_server_fd skip $name
  123. return
  124. }
  125. set tags [concat $::tags $tags]
  126. if {![tags_acceptable $tags err]} {
  127. incr ::num_aborted
  128. send_data_packet $::test_server_fd ignore "$name: $err"
  129. return
  130. }
  131. incr ::num_tests
  132. set details {}
  133. lappend details "$name in $::curfile"
  134. # set a cur_test global to be logged into new servers that are spown
  135. # and log the test name in all existing servers
  136. set prev_test $::cur_test
  137. set ::cur_test "$name in $::curfile"
  138. if {!$::external} {
  139. foreach srv $::servers {
  140. set stdout [dict get $srv stdout]
  141. set fd [open $stdout "a+"]
  142. puts $fd "### Starting test $::cur_test"
  143. close $fd
  144. }
  145. }
  146. send_data_packet $::test_server_fd testing $name
  147. if {[catch {set retval [uplevel 1 $code]} error]} {
  148. set assertion [string match "assertion:*" $error]
  149. if {$assertion || $::durable} {
  150. # durable prevents the whole tcl test from exiting on an exception.
  151. # an assertion is handled gracefully anyway.
  152. set msg [string range $error 10 end]
  153. lappend details $msg
  154. if {!$assertion} {
  155. lappend details $::errorInfo
  156. }
  157. lappend ::tests_failed $details
  158. incr ::num_failed
  159. send_data_packet $::test_server_fd err [join $details "\n"]
  160. if {$::stop_on_failure} {
  161. puts "Test error (last server port:[srv port], log:[srv stdout]), press enter to teardown the test."
  162. flush stdout
  163. gets stdin
  164. }
  165. } else {
  166. # Re-raise, let handler up the stack take care of this.
  167. error $error $::errorInfo
  168. }
  169. } else {
  170. if {$okpattern eq "undefined" || $okpattern eq $retval || [string match $okpattern $retval]} {
  171. incr ::num_passed
  172. send_data_packet $::test_server_fd ok $name
  173. } else {
  174. set msg "Expected '$okpattern' to equal or match '$retval'"
  175. lappend details $msg
  176. lappend ::tests_failed $details
  177. incr ::num_failed
  178. send_data_packet $::test_server_fd err [join $details "\n"]
  179. }
  180. }
  181. if {$::traceleaks} {
  182. set output [exec leaks redis-server]
  183. if {![string match {*0 leaks*} $output]} {
  184. send_data_packet $::test_server_fd err "Detected a memory leak in test '$name': $output"
  185. }
  186. }
  187. set ::cur_test $prev_test
  188. }