diff options
Diffstat (limited to 'examples/redis-unstable/tests/unit/moduleapi/configaccess.tcl')
| -rw-r--r-- | examples/redis-unstable/tests/unit/moduleapi/configaccess.tcl | 227 |
1 files changed, 0 insertions, 227 deletions
diff --git a/examples/redis-unstable/tests/unit/moduleapi/configaccess.tcl b/examples/redis-unstable/tests/unit/moduleapi/configaccess.tcl deleted file mode 100644 index 11aa962..0000000 --- a/examples/redis-unstable/tests/unit/moduleapi/configaccess.tcl +++ /dev/null | |||
| @@ -1,227 +0,0 @@ | |||
| 1 | set testmodule [file normalize tests/modules/configaccess.so] | ||
| 2 | set othermodule [file normalize tests/modules/moduleconfigs.so] | ||
| 3 | |||
| 4 | start_server {tags {"modules external:skip"}} { | ||
| 5 | r module load $testmodule | ||
| 6 | r module loadex $othermodule CONFIG moduleconfigs.mutable_bool yes | ||
| 7 | |||
| 8 | test {Test module config get with standard Redis configs} { | ||
| 9 | # Test getting standard Redis configs of different types | ||
| 10 | set maxmemory [r config get maxmemory] | ||
| 11 | assert_equal [lindex $maxmemory 1] [r configaccess.getnumeric maxmemory] | ||
| 12 | |||
| 13 | set port [r config get port] | ||
| 14 | assert_equal [lindex $port 1] [r configaccess.getnumeric port] | ||
| 15 | |||
| 16 | set appendonly [r config get appendonly] | ||
| 17 | assert_equal [string is true [lindex $appendonly 1]] [r configaccess.getbool appendonly] | ||
| 18 | |||
| 19 | # Test string config | ||
| 20 | set logfile [r config get logfile] | ||
| 21 | assert_equal [lindex $logfile 1] [r configaccess.get logfile] | ||
| 22 | |||
| 23 | # Test SDS config | ||
| 24 | set requirepass [r config get requirepass] | ||
| 25 | assert_equal [lindex $requirepass 1] [r configaccess.get requirepass] | ||
| 26 | |||
| 27 | # Test special config | ||
| 28 | set oom_score_adj_values [r config get oom-score-adj-values] | ||
| 29 | assert_equal [lindex $oom_score_adj_values 1] [r configaccess.get oom-score-adj-values] | ||
| 30 | |||
| 31 | set maxmemory_policy_name [r configaccess.getenum maxmemory-policy] | ||
| 32 | assert_equal [lindex [r config get maxmemory-policy] 1] $maxmemory_policy_name | ||
| 33 | |||
| 34 | # Test percent config | ||
| 35 | r config set maxmemory 100000 | ||
| 36 | r configaccess.setnumeric maxmemory-clients -50 | ||
| 37 | assert_equal [lindex [r config get maxmemory-clients] 1] 50% | ||
| 38 | |||
| 39 | # Test multi-argument enum config | ||
| 40 | r config set moduleconfigs.flags "one two four" | ||
| 41 | assert_equal "five two" [r configaccess.getenum moduleconfigs.flags] | ||
| 42 | |||
| 43 | # Test getting multi-argument enum config via generic get | ||
| 44 | r config set moduleconfigs.flags "two four" | ||
| 45 | assert_equal "two four" [r configaccess.get moduleconfigs.flags] | ||
| 46 | } | ||
| 47 | |||
| 48 | test {Test module config get with non-existent configs} { | ||
| 49 | # Test getting non-existent configs | ||
| 50 | catch {r configaccess.getnumeric nonexistent_config} err | ||
| 51 | assert_match "ERR*" $err | ||
| 52 | |||
| 53 | catch {r configaccess.getbool nonexistent_config} err | ||
| 54 | assert_match "ERR*" $err | ||
| 55 | |||
| 56 | catch {r configaccess.get nonexistent_config} err | ||
| 57 | assert_match "ERR*" $err | ||
| 58 | |||
| 59 | catch {r configaccess.getenum nonexistent_config} err | ||
| 60 | assert_match "ERR*" $err | ||
| 61 | } | ||
| 62 | |||
| 63 | test {Test module config set with standard Redis configs} { | ||
| 64 | # Test setting numeric config | ||
| 65 | set old_maxmemory_samples [r config get maxmemory-samples] | ||
| 66 | r configaccess.setnumeric maxmemory-samples 10 | ||
| 67 | assert_equal "maxmemory-samples 10" [r config get maxmemory-samples] | ||
| 68 | r config set maxmemory-samples [lindex $old_maxmemory_samples 1] | ||
| 69 | |||
| 70 | # Test setting bool config | ||
| 71 | set old_protected_mode [r config get protected-mode] | ||
| 72 | r configaccess.setbool protected-mode no | ||
| 73 | assert_equal "protected-mode no" [r config get protected-mode] | ||
| 74 | r config set protected-mode [lindex $old_protected_mode 1] | ||
| 75 | |||
| 76 | # Test setting string config | ||
| 77 | set old_masteruser [r config get masteruser] | ||
| 78 | r configaccess.set masteruser "__newmasteruser__" | ||
| 79 | assert_equal "__newmasteruser__" [lindex [r config get masteruser] 1] | ||
| 80 | r config set masteruser [lindex $old_masteruser 1] | ||
| 81 | |||
| 82 | # Test setting enum config | ||
| 83 | set old_loglevel [r config get loglevel] | ||
| 84 | r config set loglevel "notice" ; # Set to some value we are sure is different than the one tested | ||
| 85 | r configaccess.setenum loglevel warning | ||
| 86 | assert_equal "loglevel warning" [r config get loglevel] | ||
| 87 | r config set loglevel [lindex $old_loglevel 1] | ||
| 88 | |||
| 89 | # Test setting multi-argument enum config | ||
| 90 | r config set moduleconfigs.flags "one two four" | ||
| 91 | assert_equal "moduleconfigs.flags {five two}" [r config get moduleconfigs.flags] | ||
| 92 | r configaccess.setenum moduleconfigs.flags "two four" | ||
| 93 | assert_equal "moduleconfigs.flags {two four}" [r config get moduleconfigs.flags] | ||
| 94 | |||
| 95 | # Test setting multi-argument enum config via generic set | ||
| 96 | r config set moduleconfigs.flags "one two four" | ||
| 97 | assert_equal "moduleconfigs.flags {five two}" [r config get moduleconfigs.flags] | ||
| 98 | r configaccess.set moduleconfigs.flags "two four" | ||
| 99 | assert_equal "moduleconfigs.flags {two four}" [r config get moduleconfigs.flags] | ||
| 100 | } | ||
| 101 | |||
| 102 | test {Test module config set with module configs} { | ||
| 103 | # Test setting module bool config | ||
| 104 | assert_equal "OK" [r configaccess.setbool configaccess.bool no] | ||
| 105 | assert_equal "configaccess.bool no" [r config get configaccess.bool] | ||
| 106 | |||
| 107 | # Test setting module bool config from another module | ||
| 108 | assert_equal "OK" [r configaccess.setbool moduleconfigs.mutable_bool no] | ||
| 109 | assert_equal "moduleconfigs.mutable_bool no" [r config get moduleconfigs.mutable_bool] | ||
| 110 | |||
| 111 | # Test setting module numeric config | ||
| 112 | assert_equal "OK" [r configaccess.setnumeric moduleconfigs.numeric 100] | ||
| 113 | assert_equal "moduleconfigs.numeric 100" [r config get moduleconfigs.numeric] | ||
| 114 | |||
| 115 | # Test setting module enum config | ||
| 116 | assert_equal "OK" [r configaccess.setenum moduleconfigs.enum "five"] | ||
| 117 | assert_equal "moduleconfigs.enum five" [r config get moduleconfigs.enum] | ||
| 118 | } | ||
| 119 | |||
| 120 | test {Test module config set with error cases} { | ||
| 121 | # Test setting a non-existent config | ||
| 122 | catch {r configaccess.setbool nonexistent_config yes} err | ||
| 123 | assert_match "*ERR*" $err | ||
| 124 | |||
| 125 | # Test setting a read-only config | ||
| 126 | catch {r configaccess.setbool moduleconfigs.immutable_bool yes} err | ||
| 127 | assert_match "*ERR*" $err | ||
| 128 | |||
| 129 | # Test setting an enum config with invalid value | ||
| 130 | catch {r configaccess.setenumname moduleconfigs.enum "invalid_value"} err | ||
| 131 | assert_match "*ERR*" $err | ||
| 132 | |||
| 133 | # Test setting a numeric config with out-of-range value | ||
| 134 | catch {r configaccess.setnumeric moduleconfigs.numeric 5000} err | ||
| 135 | assert_match "*ERR*" $err | ||
| 136 | catch {r configaccess.setnumeric maxclients -1} err | ||
| 137 | assert_match "*Failed to set numeric config maxclients: argument must be between*" $err | ||
| 138 | catch {r configaccess.setnumeric maxclients -9223372036854775808} err | ||
| 139 | assert_match "*Failed to set numeric config maxclients: argument must be between*" $err | ||
| 140 | |||
| 141 | # Sanity check | ||
| 142 | assert_equal [r configaccess.setnumeric maxmemory 18446744073709551615] "OK" | ||
| 143 | assert_equal [r configaccess.setnumeric maxmemory -1] "OK" | ||
| 144 | } | ||
| 145 | |||
| 146 | test {Test module get all configs} { | ||
| 147 | # Get all configs using the module command | ||
| 148 | set all_configs [r configaccess.getconfigs] | ||
| 149 | |||
| 150 | # Verify the number of configs matches the number of configs returned | ||
| 151 | # by Redis's native CONFIG GET command. | ||
| 152 | set all_configs_std_pairs [llength [r config get *]] | ||
| 153 | |||
| 154 | # When comparing with the standard CONFIG GET command, we need to divide | ||
| 155 | # by 2 because the standard command returns a flattened array of | ||
| 156 | # key-value pairs whereas our testing command returns an array of pairs. | ||
| 157 | assert_equal [llength $all_configs] [expr $all_configs_std_pairs / 2] | ||
| 158 | |||
| 159 | # Verify all the configs are present in both replies. | ||
| 160 | foreach config_pair $all_configs { | ||
| 161 | assert_equal 2 [llength $config_pair] | ||
| 162 | set config_name [lindex $config_pair 0] | ||
| 163 | set config_value [lindex $config_pair 1] | ||
| 164 | |||
| 165 | # Verify that we can get this config using standard config get | ||
| 166 | set redis_config [r config get $config_name] | ||
| 167 | assert {[llength $redis_config] != 0} | ||
| 168 | |||
| 169 | assert_equal $config_value [lindex $redis_config 1] | ||
| 170 | } | ||
| 171 | |||
| 172 | # Test that module configs are also included | ||
| 173 | set found_module_config 0 | ||
| 174 | foreach config_pair $all_configs { | ||
| 175 | set config_name [lindex $config_pair 0] | ||
| 176 | if {$config_name eq "configaccess.bool"} { | ||
| 177 | set found_module_config 1 | ||
| 178 | break | ||
| 179 | } | ||
| 180 | } | ||
| 181 | |||
| 182 | assert {$found_module_config == 1} | ||
| 183 | |||
| 184 | # Test pattern matching | ||
| 185 | set moduleconfigs_count [r configaccess.getconfigs "moduleconfigs.*"] | ||
| 186 | assert_equal 7 [llength $moduleconfigs_count] | ||
| 187 | |||
| 188 | set memoryconfigs_count [r configaccess.getconfigs "*memory"] | ||
| 189 | assert_equal 3 [llength $memoryconfigs_count] | ||
| 190 | } | ||
| 191 | |||
| 192 | test {Test module config type detection} { | ||
| 193 | # Test getting config types for different config types | ||
| 194 | assert_equal "bool" [r configaccess.getconfigtype appendonly] | ||
| 195 | assert_equal "numeric" [r configaccess.getconfigtype port] | ||
| 196 | assert_equal "string" [r configaccess.getconfigtype logfile] | ||
| 197 | assert_equal "enum" [r configaccess.getconfigtype maxmemory-policy] | ||
| 198 | |||
| 199 | # Test with module config | ||
| 200 | assert_equal "bool" [r configaccess.getconfigtype configaccess.bool] | ||
| 201 | |||
| 202 | # Test with non-existent config | ||
| 203 | catch {r configaccess.getconfigtype nonexistent_config} err | ||
| 204 | assert_match "ERR Config does not exist" $err | ||
| 205 | } | ||
| 206 | |||
| 207 | test {Test config rollback on apply} { | ||
| 208 | set og_port [lindex [r config get port] 1] | ||
| 209 | |||
| 210 | set used_port [find_available_port $::baseport $::portcount] | ||
| 211 | |||
| 212 | # Run a dummy server on used_port so we know we can't configure redis to | ||
| 213 | # use it. It's ok for this to fail because that means used_port is invalid | ||
| 214 | # anyway | ||
| 215 | catch {set sockfd [socket -server dummy_accept -myaddr 127.0.0.1 $used_port]} e | ||
| 216 | if {$::verbose} { puts "dummy_accept: $e" } | ||
| 217 | |||
| 218 | # Try to listen on the used port, pass some more configs to make sure the | ||
| 219 | # returned failure message is for the first bad config and everything is rolled back. | ||
| 220 | assert_error "ERR Failed to set numeric config port: Unable to listen on this port*" { | ||
| 221 | eval "r configaccess.setnumeric port $used_port" | ||
| 222 | } | ||
| 223 | |||
| 224 | assert_equal [lindex [r config get port] 1] $og_port | ||
| 225 | close $sockfd | ||
| 226 | } | ||
| 227 | } | ||
