"Fossies" - the Fresh Open Source Software Archive

Member "redis-7.0.5/tests/sentinel/tests/03-runtime-reconf.tcl" (21 Sep 2022, 8476 Bytes) of package /linux/misc/redis-7.0.5.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Tcl/Tk source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "03-runtime-reconf.tcl": 7.0.2_vs_7.0.3.

    1 # Test runtime reconfiguration command SENTINEL SET.
    2 source "../tests/includes/init-tests.tcl"
    3 set num_sentinels [llength $::sentinel_instances]
    4 
    5 set ::user "testuser"
    6 set ::password "secret"
    7 
    8 proc server_set_password {} {
    9     foreach_redis_id id {
   10         assert_equal {OK} [R $id CONFIG SET requirepass $::password]
   11         assert_equal {OK} [R $id AUTH $::password]
   12         assert_equal {OK} [R $id CONFIG SET masterauth $::password]
   13     }
   14 }
   15 
   16 proc server_reset_password {} {
   17     foreach_redis_id id {
   18         assert_equal {OK} [R $id CONFIG SET requirepass ""]
   19         assert_equal {OK} [R $id CONFIG SET masterauth ""]
   20     }
   21 }
   22 
   23 proc server_set_acl {id} {
   24     assert_equal {OK} [R $id ACL SETUSER $::user on >$::password allchannels +@all]
   25     assert_equal {OK} [R $id ACL SETUSER default off]
   26 
   27     R $id CLIENT KILL USER default SKIPME no
   28     assert_equal {OK} [R $id AUTH $::user $::password]
   29     assert_equal {OK} [R $id CONFIG SET masteruser $::user]
   30     assert_equal {OK} [R $id CONFIG SET masterauth $::password]
   31 }
   32 
   33 proc server_reset_acl {id} {
   34     assert_equal {OK} [R $id ACL SETUSER default on]
   35     assert_equal {1} [R $id ACL DELUSER $::user]
   36 
   37     assert_equal {OK} [R $id CONFIG SET masteruser ""]
   38     assert_equal {OK} [R $id CONFIG SET masterauth ""]
   39 }
   40 
   41 proc verify_sentinel_connect_replicas {id} {
   42     foreach replica [S $id SENTINEL REPLICAS mymaster] {
   43         if {[string match "*disconnected*" [dict get $replica flags]]} {
   44             return 0
   45         }
   46     }
   47     return 1
   48 }
   49 
   50 proc wait_for_sentinels_connect_servers { {is_connect 1} } {
   51     foreach_sentinel_id id {
   52         wait_for_condition 1000 50 {
   53             [string match "*disconnected*" [dict get [S $id SENTINEL MASTER mymaster] flags]] != $is_connect
   54         } else {
   55             fail "At least some sentinel can't connect to master"
   56         }
   57 
   58         wait_for_condition 1000 50 {
   59             [verify_sentinel_connect_replicas $id] == $is_connect
   60         } else {
   61             fail "At least some sentinel can't connect to replica"
   62         }
   63     }
   64 }
   65 
   66 test "Sentinels (re)connection following SENTINEL SET mymaster auth-pass" {
   67     # 3 types of sentinels to test:
   68     # (re)started while master changed pwd. Manage to connect only after setting pwd
   69     set sent2re 0
   70     # (up)dated in advance with master new password
   71     set sent2up 1
   72     # (un)touched. Yet manage to maintain (old) connection
   73     set sent2un 2
   74 
   75     wait_for_sentinels_connect_servers
   76     kill_instance sentinel $sent2re
   77     server_set_password
   78     assert_equal {OK} [S $sent2up SENTINEL SET mymaster auth-pass $::password]
   79     restart_instance sentinel $sent2re
   80 
   81     # Verify sentinel that restarted failed to connect master
   82     wait_for_condition 100 50 {
   83         [string match "*disconnected*" [dict get [S $sent2re SENTINEL MASTER mymaster] flags]] != 0
   84     } else {
   85         fail "Expected to be disconnected from master due to wrong password"
   86     }
   87 
   88     # Update restarted sentinel with master password
   89     assert_equal {OK} [S $sent2re SENTINEL SET mymaster auth-pass $::password]
   90 
   91     # All sentinels expected to connect successfully
   92     wait_for_sentinels_connect_servers
   93 
   94     # remove requirepass and verify sentinels manage to connect servers
   95     server_reset_password
   96     wait_for_sentinels_connect_servers
   97     # Sanity check
   98     verify_sentinel_auto_discovery
   99 }
  100 
  101 test "Sentinels (re)connection following master ACL change" {
  102     # Three types of sentinels to test during ACL change:
  103     # 1. (re)started Sentinel. Manage to connect only after setting new pwd
  104     # 2. (up)dated Sentinel, get just before ACL change the new password
  105     # 3. (un)touched Sentinel that kept old connection with master and didn't
  106     #    set new ACL password won't persist ACL pwd change (unlike legacy auth-pass)
  107     set sent2re 0
  108     set sent2up 1
  109     set sent2un 2
  110 
  111     wait_for_sentinels_connect_servers
  112     # kill sentinel 'sent2re' and restart it after ACL change
  113     kill_instance sentinel $sent2re
  114 
  115     # Update sentinel 'sent2up' with new user and pwd
  116     assert_equal {OK} [S $sent2up SENTINEL SET mymaster auth-user $::user]
  117     assert_equal {OK} [S $sent2up SENTINEL SET mymaster auth-pass $::password]
  118 
  119     foreach_redis_id id {
  120         server_set_acl $id
  121     }
  122 
  123     restart_instance sentinel $sent2re
  124 
  125     # Verify sentinel that restarted failed to reconnect master
  126     wait_for_condition 100 50 {
  127         [string match "*disconnected*" [dict get [S $sent2re SENTINEL MASTER mymaster] flags]] != 0
  128     } else {
  129         fail "Expected: Restarted sentinel to be disconnected from master due to obsolete password"
  130     }
  131 
  132     # Verify sentinel with updated password managed to connect (wait for sentinelTimer to reconnect)
  133     wait_for_condition 100 50 {
  134         [string match "*disconnected*" [dict get [S $sent2up SENTINEL MASTER mymaster] flags]] == 0
  135     } else {
  136         fail "Expected: Sentinel to be connected to master"
  137     }
  138 
  139     # Verify sentinel untouched gets failed to connect master
  140     wait_for_condition 100 50 {
  141         [string match "*disconnected*" [dict get [S $sent2un SENTINEL MASTER mymaster] flags]] != 0
  142     } else {
  143         fail "Expected: Sentinel to be disconnected from master due to obsolete password"
  144     }
  145 
  146     # Now update all sentinels with new password
  147     foreach_sentinel_id id {
  148         assert_equal {OK} [S $id SENTINEL SET mymaster auth-user $::user]
  149         assert_equal {OK} [S $id SENTINEL SET mymaster auth-pass $::password]
  150     }
  151 
  152     # All sentinels expected to connect successfully
  153     wait_for_sentinels_connect_servers
  154 
  155     # remove requirepass and verify sentinels manage to connect servers
  156     foreach_redis_id id {
  157         server_reset_acl $id
  158     }
  159 
  160     wait_for_sentinels_connect_servers
  161     # Sanity check
  162     verify_sentinel_auto_discovery
  163 }
  164 
  165 test "Set parameters in normal case" {
  166 
  167     set info [S 0 SENTINEL master mymaster]
  168     set origin_quorum [dict get $info quorum]
  169     set origin_down_after_milliseconds [dict get $info down-after-milliseconds]
  170     set update_quorum [expr $origin_quorum+1]
  171     set update_down_after_milliseconds [expr $origin_down_after_milliseconds+1000]
  172 
  173     assert_equal [S 0 SENTINEL SET mymaster quorum $update_quorum] "OK"
  174     assert_equal [S 0 SENTINEL SET mymaster down-after-milliseconds $update_down_after_milliseconds] "OK"
  175 
  176     set update_info [S 0 SENTINEL master mymaster]
  177     assert {[dict get $update_info quorum] != $origin_quorum}
  178     assert {[dict get $update_info down-after-milliseconds] != $origin_down_after_milliseconds}
  179 
  180     #restore to origin config parameters
  181     assert_equal [S 0 SENTINEL SET mymaster quorum $origin_quorum] "OK"
  182     assert_equal [S 0 SENTINEL SET mymaster down-after-milliseconds $origin_down_after_milliseconds] "OK"
  183 }
  184 
  185 test "Set parameters in normal case with bad format" {
  186 
  187     set info [S 0 SENTINEL master mymaster]
  188     set origin_down_after_milliseconds [dict get $info down-after-milliseconds]
  189 
  190     assert_error "ERR Invalid argument '-20' for SENTINEL SET 'down-after-milliseconds'*" {S 0 SENTINEL SET mymaster down-after-milliseconds -20}
  191     assert_error "ERR Invalid argument 'abc' for SENTINEL SET 'down-after-milliseconds'*" {S 0 SENTINEL SET mymaster down-after-milliseconds "abc"}
  192 
  193     set current_info [S 0 SENTINEL master mymaster]
  194     assert {[dict get $current_info down-after-milliseconds] == $origin_down_after_milliseconds}
  195 }
  196 
  197 test "Sentinel Set with other error situations" {
  198 
  199    # non-existing script
  200    assert_error "ERR Notification script seems non existing*" {S 0 SENTINEL SET mymaster notification-script test.txt}
  201 
  202    # wrong parameter number
  203    assert_error "ERR wrong number of arguments for 'sentinel|set' command" {S 0 SENTINEL SET mymaster fakeoption}
  204 
  205    # unknown parameter option
  206    assert_error "ERR Unknown option or number of arguments for SENTINEL SET 'fakeoption'" {S 0 SENTINEL SET mymaster fakeoption fakevalue}
  207 
  208    # save new config to disk failed
  209    set info [S 0 SENTINEL master mymaster]
  210    set origin_quorum [dict get $info quorum]
  211    set update_quorum [expr $origin_quorum+1]
  212    set sentinel_id 0
  213    set configfilename [file join "sentinel_$sentinel_id" "sentinel.conf"]
  214    set configfilename_bak [file join "sentinel_$sentinel_id" "sentinel.conf.bak"]
  215 
  216    file rename $configfilename $configfilename_bak
  217    file mkdir $configfilename
  218 
  219    catch {[S 0 SENTINEL SET mymaster quorum $update_quorum]} err
  220 
  221    file delete $configfilename
  222    file rename $configfilename_bak $configfilename
  223 
  224    assert_match "ERR Failed to save config file*" $err
  225 }