mirror of
https://github.com/redis/redis.git
synced 2026-04-21 03:01:35 -04:00
* fix tests
* add some logging to test module
This commit is contained in:
committed by
YaacovHazan
parent
192799539f
commit
294492dbf2
@@ -1695,8 +1695,8 @@ static int RedisModule_Init(RedisModuleCtx *ctx, const char *name, int ver, int
|
|||||||
REDISMODULE_GET_API(RegisterNumericConfig);
|
REDISMODULE_GET_API(RegisterNumericConfig);
|
||||||
REDISMODULE_GET_API(RegisterStringConfig);
|
REDISMODULE_GET_API(RegisterStringConfig);
|
||||||
REDISMODULE_GET_API(RegisterEnumConfig);
|
REDISMODULE_GET_API(RegisterEnumConfig);
|
||||||
REDISMODULE_GET_API(LoadDefaultConfigs);
|
|
||||||
REDISMODULE_GET_API(LoadConfigs);
|
REDISMODULE_GET_API(LoadConfigs);
|
||||||
|
REDISMODULE_GET_API(LoadDefaultConfigs);
|
||||||
REDISMODULE_GET_API(RdbStreamCreateFromFile);
|
REDISMODULE_GET_API(RdbStreamCreateFromFile);
|
||||||
REDISMODULE_GET_API(RdbStreamFree);
|
REDISMODULE_GET_API(RdbStreamFree);
|
||||||
REDISMODULE_GET_API(RdbLoad);
|
REDISMODULE_GET_API(RdbLoad);
|
||||||
|
|||||||
@@ -169,16 +169,22 @@ int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc)
|
|||||||
REDISMODULE_NOT_USED(argv);
|
REDISMODULE_NOT_USED(argv);
|
||||||
REDISMODULE_NOT_USED(argc);
|
REDISMODULE_NOT_USED(argc);
|
||||||
|
|
||||||
if (RedisModule_Init(ctx, "moduleconfigs", 1, REDISMODULE_APIVER_1) == REDISMODULE_ERR) return REDISMODULE_ERR;
|
if (RedisModule_Init(ctx, "moduleconfigs", 1, REDISMODULE_APIVER_1) == REDISMODULE_ERR) {
|
||||||
|
RedisModule_Log(ctx, "warning", "Failed to init module");
|
||||||
|
return REDISMODULE_ERR;
|
||||||
|
}
|
||||||
|
|
||||||
if (RedisModule_RegisterBoolConfig(ctx, "mutable_bool", 1, REDISMODULE_CONFIG_DEFAULT, getBoolConfigCommand, setBoolConfigCommand, boolApplyFunc, &mutable_bool_val) == REDISMODULE_ERR) {
|
if (RedisModule_RegisterBoolConfig(ctx, "mutable_bool", 1, REDISMODULE_CONFIG_DEFAULT, getBoolConfigCommand, setBoolConfigCommand, boolApplyFunc, &mutable_bool_val) == REDISMODULE_ERR) {
|
||||||
|
RedisModule_Log(ctx, "warning", "Failed to register mutable_bool");
|
||||||
return REDISMODULE_ERR;
|
return REDISMODULE_ERR;
|
||||||
}
|
}
|
||||||
/* Immutable config here. */
|
/* Immutable config here. */
|
||||||
if (RedisModule_RegisterBoolConfig(ctx, "immutable_bool", 0, REDISMODULE_CONFIG_IMMUTABLE, getBoolConfigCommand, setBoolConfigCommand, boolApplyFunc, &immutable_bool_val) == REDISMODULE_ERR) {
|
if (RedisModule_RegisterBoolConfig(ctx, "immutable_bool", 0, REDISMODULE_CONFIG_IMMUTABLE, getBoolConfigCommand, setBoolConfigCommand, boolApplyFunc, &immutable_bool_val) == REDISMODULE_ERR) {
|
||||||
|
RedisModule_Log(ctx, "warning", "Failed to register immutable_bool");
|
||||||
return REDISMODULE_ERR;
|
return REDISMODULE_ERR;
|
||||||
}
|
}
|
||||||
if (RedisModule_RegisterStringConfig(ctx, "string", "secret password", REDISMODULE_CONFIG_DEFAULT, getStringConfigCommand, setStringConfigCommand, NULL, NULL) == REDISMODULE_ERR) {
|
if (RedisModule_RegisterStringConfig(ctx, "string", "secret password", REDISMODULE_CONFIG_DEFAULT, getStringConfigCommand, setStringConfigCommand, NULL, NULL) == REDISMODULE_ERR) {
|
||||||
|
RedisModule_Log(ctx, "warning", "Failed to register string");
|
||||||
return REDISMODULE_ERR;
|
return REDISMODULE_ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -187,59 +193,82 @@ int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc)
|
|||||||
const int int_vals[] = {0, 5, 1, 2, 4};
|
const int int_vals[] = {0, 5, 1, 2, 4};
|
||||||
|
|
||||||
if (RedisModule_RegisterEnumConfig(ctx, "enum", 1, REDISMODULE_CONFIG_DEFAULT, enum_vals, int_vals, 5, getEnumConfigCommand, setEnumConfigCommand, NULL, NULL) == REDISMODULE_ERR) {
|
if (RedisModule_RegisterEnumConfig(ctx, "enum", 1, REDISMODULE_CONFIG_DEFAULT, enum_vals, int_vals, 5, getEnumConfigCommand, setEnumConfigCommand, NULL, NULL) == REDISMODULE_ERR) {
|
||||||
|
RedisModule_Log(ctx, "warning", "Failed to register enum");
|
||||||
return REDISMODULE_ERR;
|
return REDISMODULE_ERR;
|
||||||
}
|
}
|
||||||
if (RedisModule_RegisterEnumConfig(ctx, "flags", 3, REDISMODULE_CONFIG_DEFAULT | REDISMODULE_CONFIG_BITFLAGS, enum_vals, int_vals, 5, getFlagsConfigCommand, setFlagsConfigCommand, NULL, NULL) == REDISMODULE_ERR) {
|
if (RedisModule_RegisterEnumConfig(ctx, "flags", 3, REDISMODULE_CONFIG_DEFAULT | REDISMODULE_CONFIG_BITFLAGS, enum_vals, int_vals, 5, getFlagsConfigCommand, setFlagsConfigCommand, NULL, NULL) == REDISMODULE_ERR) {
|
||||||
|
RedisModule_Log(ctx, "warning", "Failed to register flags");
|
||||||
return REDISMODULE_ERR;
|
return REDISMODULE_ERR;
|
||||||
}
|
}
|
||||||
/* Memory config here. */
|
/* Memory config here. */
|
||||||
if (RedisModule_RegisterNumericConfig(ctx, "memory_numeric", 1024, REDISMODULE_CONFIG_DEFAULT | REDISMODULE_CONFIG_MEMORY, 0, 3000000, getNumericConfigCommand, setNumericConfigCommand, longlongApplyFunc, &memval) == REDISMODULE_ERR) {
|
if (RedisModule_RegisterNumericConfig(ctx, "memory_numeric", 1024, REDISMODULE_CONFIG_DEFAULT | REDISMODULE_CONFIG_MEMORY, 0, 3000000, getNumericConfigCommand, setNumericConfigCommand, longlongApplyFunc, &memval) == REDISMODULE_ERR) {
|
||||||
|
RedisModule_Log(ctx, "warning", "Failed to register memory_numeric");
|
||||||
return REDISMODULE_ERR;
|
return REDISMODULE_ERR;
|
||||||
}
|
}
|
||||||
if (RedisModule_RegisterNumericConfig(ctx, "numeric", -1, REDISMODULE_CONFIG_DEFAULT, -5, 2000, getNumericConfigCommand, setNumericConfigCommand, longlongApplyFunc, &longval) == REDISMODULE_ERR) {
|
if (RedisModule_RegisterNumericConfig(ctx, "numeric", -1, REDISMODULE_CONFIG_DEFAULT, -5, 2000, getNumericConfigCommand, setNumericConfigCommand, longlongApplyFunc, &longval) == REDISMODULE_ERR) {
|
||||||
|
RedisModule_Log(ctx, "warning", "Failed to register numeric");
|
||||||
return REDISMODULE_ERR;
|
return REDISMODULE_ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*** unprefixed and aliased configuration ***/
|
/*** unprefixed and aliased configuration ***/
|
||||||
if (RedisModule_RegisterBoolConfig(ctx, "unprefix-bool|unprefix-bool-alias", 1, REDISMODULE_CONFIG_DEFAULT|REDISMODULE_CONFIG_UNPREFIXED,
|
if (RedisModule_RegisterBoolConfig(ctx, "unprefix-bool|unprefix-bool-alias", 1, REDISMODULE_CONFIG_DEFAULT|REDISMODULE_CONFIG_UNPREFIXED,
|
||||||
getBoolConfigCommand, setBoolConfigCommand, NULL, &no_prefix_bool) == REDISMODULE_ERR) {
|
getBoolConfigCommand, setBoolConfigCommand, NULL, &no_prefix_bool) == REDISMODULE_ERR) {
|
||||||
|
RedisModule_Log(ctx, "warning", "Failed to register unprefix-bool");
|
||||||
return REDISMODULE_ERR;
|
return REDISMODULE_ERR;
|
||||||
}
|
}
|
||||||
if (RedisModule_RegisterBoolConfig(ctx, "unprefix-noalias-bool", 1, REDISMODULE_CONFIG_DEFAULT|REDISMODULE_CONFIG_UNPREFIXED,
|
if (RedisModule_RegisterBoolConfig(ctx, "unprefix-noalias-bool", 1, REDISMODULE_CONFIG_DEFAULT|REDISMODULE_CONFIG_UNPREFIXED,
|
||||||
getBoolConfigCommand, setBoolConfigCommand, NULL, &no_prefix_bool2) == REDISMODULE_ERR) {
|
getBoolConfigCommand, setBoolConfigCommand, NULL, &no_prefix_bool2) == REDISMODULE_ERR) {
|
||||||
|
RedisModule_Log(ctx, "warning", "Failed to register unprefix-noalias-bool");
|
||||||
return REDISMODULE_ERR;
|
return REDISMODULE_ERR;
|
||||||
}
|
}
|
||||||
if (RedisModule_RegisterNumericConfig(ctx, "unprefix.numeric|unprefix.numeric-alias", -1, REDISMODULE_CONFIG_DEFAULT|REDISMODULE_CONFIG_UNPREFIXED,
|
if (RedisModule_RegisterNumericConfig(ctx, "unprefix.numeric|unprefix.numeric-alias", -1, REDISMODULE_CONFIG_DEFAULT|REDISMODULE_CONFIG_UNPREFIXED,
|
||||||
-5, 2000, getNumericConfigCommand, setNumericConfigCommand, NULL, &no_prefix_longval) == REDISMODULE_ERR) {
|
-5, 2000, getNumericConfigCommand, setNumericConfigCommand, NULL, &no_prefix_longval) == REDISMODULE_ERR) {
|
||||||
|
RedisModule_Log(ctx, "warning", "Failed to register unprefix.numeric");
|
||||||
return REDISMODULE_ERR;
|
return REDISMODULE_ERR;
|
||||||
}
|
}
|
||||||
if (RedisModule_RegisterStringConfig(ctx, "unprefix-string|unprefix.string-alias", "secret unprefix", REDISMODULE_CONFIG_DEFAULT|REDISMODULE_CONFIG_UNPREFIXED,
|
if (RedisModule_RegisterStringConfig(ctx, "unprefix-string|unprefix.string-alias", "secret unprefix", REDISMODULE_CONFIG_DEFAULT|REDISMODULE_CONFIG_UNPREFIXED,
|
||||||
getStringConfigUnprefix, setStringConfigUnprefix, NULL, NULL) == REDISMODULE_ERR) {
|
getStringConfigUnprefix, setStringConfigUnprefix, NULL, NULL) == REDISMODULE_ERR) {
|
||||||
|
RedisModule_Log(ctx, "warning", "Failed to register unprefix-string");
|
||||||
return REDISMODULE_ERR;
|
return REDISMODULE_ERR;
|
||||||
}
|
}
|
||||||
if (RedisModule_RegisterEnumConfig(ctx, "unprefix-enum|unprefix-enum-alias", 1, REDISMODULE_CONFIG_DEFAULT|REDISMODULE_CONFIG_UNPREFIXED,
|
if (RedisModule_RegisterEnumConfig(ctx, "unprefix-enum|unprefix-enum-alias", 1, REDISMODULE_CONFIG_DEFAULT|REDISMODULE_CONFIG_UNPREFIXED,
|
||||||
enum_vals, int_vals, 5, getEnumConfigUnprefix, setEnumConfigUnprefix, NULL, NULL) == REDISMODULE_ERR) {
|
enum_vals, int_vals, 5, getEnumConfigUnprefix, setEnumConfigUnprefix, NULL, NULL) == REDISMODULE_ERR) {
|
||||||
|
RedisModule_Log(ctx, "warning", "Failed to register unprefix-enum");
|
||||||
return REDISMODULE_ERR;
|
return REDISMODULE_ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
RedisModule_Log(ctx, "debug", "Registered configuration");
|
||||||
size_t len;
|
size_t len;
|
||||||
if (argc && !strcasecmp(RedisModule_StringPtrLen(argv[0], &len), "noload")) {
|
if (argc && !strcasecmp(RedisModule_StringPtrLen(argv[0], &len), "noload")) {
|
||||||
return REDISMODULE_OK;
|
return REDISMODULE_OK;
|
||||||
} else if (RedisModule_LoadDefaultConfigs(ctx) == REDISMODULE_ERR) {
|
} else if (RedisModule_LoadDefaultConfigs(ctx) == REDISMODULE_ERR) {
|
||||||
return REDISMODULE_ERR;
|
RedisModule_Log(ctx, "warning", "Failed to load default configuration");
|
||||||
} else if (RedisModule_LoadConfigs(ctx) == REDISMODULE_ERR) {
|
goto err;
|
||||||
if (strval) {
|
} else if (argc && !strcasecmp(RedisModule_StringPtrLen(argv[0], &len), "override")) {
|
||||||
RedisModule_FreeString(ctx, strval);
|
// simulate configuration values being overwritten by the command line
|
||||||
strval = NULL;
|
RedisModule_Log(ctx, "debug", "Overriding configuration values");
|
||||||
}
|
if (strval) RedisModule_FreeString(ctx, strval);
|
||||||
return REDISMODULE_ERR;
|
strval = RedisModule_CreateString(ctx, "foo", 3);
|
||||||
|
longval = memval = 123;
|
||||||
|
}
|
||||||
|
RedisModule_Log(ctx, "debug", "Loading configuration");
|
||||||
|
if (RedisModule_LoadConfigs(ctx) == REDISMODULE_ERR) {
|
||||||
|
RedisModule_Log(ctx, "warning", "Failed to load configuration");
|
||||||
|
goto err;
|
||||||
}
|
}
|
||||||
/* Creates a command which registers configs outside OnLoad() function. */
|
/* Creates a command which registers configs outside OnLoad() function. */
|
||||||
if (RedisModule_CreateCommand(ctx,"block.register.configs.outside.onload", registerBlockCheck, "write", 0, 0, 0) == REDISMODULE_ERR)
|
if (RedisModule_CreateCommand(ctx,"block.register.configs.outside.onload", registerBlockCheck, "write", 0, 0, 0) == REDISMODULE_ERR) {
|
||||||
return REDISMODULE_ERR;
|
RedisModule_Log(ctx, "warning", "Failed to register command");
|
||||||
|
goto err;
|
||||||
|
}
|
||||||
|
|
||||||
return REDISMODULE_OK;
|
return REDISMODULE_OK;
|
||||||
|
err:
|
||||||
|
if (strval) {
|
||||||
|
RedisModule_FreeString(ctx, strval);
|
||||||
|
strval = NULL;
|
||||||
|
}
|
||||||
|
return REDISMODULE_ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
int RedisModule_OnUnload(RedisModuleCtx *ctx) {
|
int RedisModule_OnUnload(RedisModuleCtx *ctx) {
|
||||||
|
|||||||
@@ -346,13 +346,12 @@ start_server {tags {"modules"}} {
|
|||||||
test {loadmodule CONFIG values take precedence over loadmoduleex ARGS values} {
|
test {loadmodule CONFIG values take precedence over loadmoduleex ARGS values} {
|
||||||
# Load module with conflicting CONFIG and ARGS values
|
# Load module with conflicting CONFIG and ARGS values
|
||||||
r module loadex $testmodule \
|
r module loadex $testmodule \
|
||||||
CONFIG moduleconfigs.mutable_bool yes \
|
CONFIG moduleconfigs.string foo \
|
||||||
CONFIG moduleconfigs.memory_numeric 2mb \
|
CONFIG moduleconfigs.memory_numeric 2mb \
|
||||||
ARGS moduleconfigs.mutable_bool no \
|
ARGS override
|
||||||
ARGS moduleconfigs.memory_numeric 4mb
|
|
||||||
|
|
||||||
# Verify CONFIG values took precedence
|
# Verify CONFIG values took precedence over the pseudo values that
|
||||||
assert_equal [r config get moduleconfigs.mutable_bool] "moduleconfigs.mutable_bool yes"
|
assert_equal [r config get moduleconfigs.string] "moduleconfigs.string foo"
|
||||||
assert_equal [r config get moduleconfigs.memory_numeric] "moduleconfigs.memory_numeric 2097152"
|
assert_equal [r config get moduleconfigs.memory_numeric] "moduleconfigs.memory_numeric 2097152"
|
||||||
|
|
||||||
r module unload moduleconfigs
|
r module unload moduleconfigs
|
||||||
@@ -361,29 +360,26 @@ start_server {tags {"modules"}} {
|
|||||||
# Test: Ensure that modified configuration values from ARGS are correctly written to the config file
|
# Test: Ensure that modified configuration values from ARGS are correctly written to the config file
|
||||||
test {Modified ARGS values are persisted after config rewrite when set through CONFIG commands} {
|
test {Modified ARGS values are persisted after config rewrite when set through CONFIG commands} {
|
||||||
# Load module with non-default ARGS values
|
# Load module with non-default ARGS values
|
||||||
r module loadex $testmodule \
|
r module loadex $testmodule ARGS override
|
||||||
ARGS moduleconfigs.memory_numeric 4mb \
|
|
||||||
ARGS moduleconfigs.string_setting "custom_value"
|
|
||||||
|
|
||||||
# Verify the initial ARGS values
|
# Verify the initial values were overwritten
|
||||||
assert_equal [r config get moduleconfigs.memory_numeric] "moduleconfigs.memory_numeric 4194304"
|
assert_equal [r config get moduleconfigs.memory_numeric] "moduleconfigs.memory_numeric 123"
|
||||||
assert_equal [r config get moduleconfigs.string_setting] "moduleconfigs.string_setting custom_value"
|
assert_equal [r config get moduleconfigs.string] "moduleconfigs.string foo"
|
||||||
|
|
||||||
# Set new values to simulate user configuration changes
|
# Set new values to simulate user configuration changes
|
||||||
r config set moduleconfigs.memory_numeric 8mb
|
r config set moduleconfigs.memory_numeric 1mb
|
||||||
r config set moduleconfigs.string_setting "modified_value"
|
r config set moduleconfigs.string "modified_value"
|
||||||
|
|
||||||
# Verify that the changes took effect
|
# Verify that the changes took effect
|
||||||
assert_equal [r config get moduleconfigs.memory_numeric] "moduleconfigs.memory_numeric 8388608"
|
assert_equal [r config get moduleconfigs.memory_numeric] "moduleconfigs.memory_numeric 1048576"
|
||||||
assert_equal [r config get moduleconfigs.string_setting] "moduleconfigs.string_setting modified_value"
|
assert_equal [r config get moduleconfigs.string] "moduleconfigs.string modified_value"
|
||||||
|
|
||||||
# Perform a config rewrite
|
# Perform a config rewrite
|
||||||
r config rewrite
|
r config rewrite
|
||||||
|
|
||||||
# Read and verify config file contents to check for persistence
|
restart_server 0 true false
|
||||||
set config_contents [file read [config_path]]
|
assert_equal [r config get moduleconfigs.memory_numeric] "moduleconfigs.memory_numeric 1048576"
|
||||||
assert_contains "moduleconfigs.memory_numeric 8388608" $config_contents
|
assert_equal [r config get moduleconfigs.string] "moduleconfigs.string modified_value"
|
||||||
assert_contains "moduleconfigs.string_setting modified_value" $config_contents
|
|
||||||
r module unload moduleconfigs
|
r module unload moduleconfigs
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user