Remove unused test files for lua-resty-redis-connector

This commit is contained in:
Théophile Diot 2025-01-16 10:26:51 +01:00
parent 714105474b
commit 1b31e33a48
No known key found for this signature in database
GPG key ID: FA995104A0BA376A
4 changed files with 0 additions and 1106 deletions

View file

@ -1,229 +0,0 @@
use Test::Nginx::Socket::Lua;
use Cwd qw(cwd);
repeat_each(2);
plan tests => repeat_each() * blocks() * 2;
my $pwd = cwd();
our $HttpConfig = qq{
lua_package_path "$pwd/lib/?.lua;;";
lua_socket_log_errors Off;
init_by_lua_block {
require("luacov.runner").init()
}
};
$ENV{TEST_NGINX_REDIS_PORT} ||= 6380;
no_long_string();
run_tests();
__DATA__
=== TEST 1: Default config
--- http_config eval: $::HttpConfig
--- config
location /t {
content_by_lua_block {
local rc = assert(require("resty.redis.connector").new())
}
}
--- request
GET /t
--- no_error_log
[error]
=== TEST 2: Defaults via new
--- http_config eval: $::HttpConfig
--- config
location /t {
content_by_lua_block {
local config = {
connect_timeout = 500,
port = $TEST_NGINX_REDIS_PORT,
db = 6,
}
local rc = require("resty.redis.connector").new(config)
assert(config ~= rc.config, "config should not equal rc.config")
assert(rc.config.connect_timeout == 500, "connect_timeout should be 500")
assert(rc.config.db == 6, "db should be 6")
assert(rc.config.role == "master", "role should be master")
}
}
--- request
GET /t
--- no_error_log
[error]
=== TEST 3: Config via connect still overrides
--- http_config eval: $::HttpConfig
--- config
location /t {
content_by_lua_block {
local rc = require("resty.redis.connector").new({
connect_timeout = 500,
port = $TEST_NGINX_REDIS_PORT,
db = 6,
keepalive_poolsize = 10,
})
assert(config ~= rc.config, "config should not equal rc.config")
assert(rc.config.connect_timeout == 500, "connect_timeout should be 500")
assert(rc.config.db == 6, "db should be 6")
assert(rc.config.role == "master", "role should be master")
assert(rc.config.keepalive_poolsize == 10,
"keepalive_poolsize should be 10")
local redis, err = rc:connect({
port = $TEST_NGINX_REDIS_PORT,
disabled_commands = { "set" }
})
if not redis or err then
ngx.log(ngx.ERR, "connect failed: ", err)
return
end
local ok, err = redis:set("foo", "bar")
assert( ok == nil and (string.find(err, "disabled") ~= nil) , "Disabled commands not passed through" )
}
}
--- request
GET /t
--- no_error_log
[error]
=== TEST 4: Unknown config errors, all config does not error
--- http_config eval: $::HttpConfig
--- config
location /t {
content_by_lua_block {
local rc, err = require("resty.redis.connector").new({
connect_timeout = 500,
port = $TEST_NGINX_REDIS_PORT,
db = 6,
foo = "bar",
})
assert(rc == nil, "rc should be nil")
assert(string.find(err, "field foo does not exist"),
"err should contain error")
-- Provide all options, without errors
assert(require("resty.redis.connector").new({
connect_timeout = 100,
send_timeout = 500,
read_timeout = 1000,
connection_options = { pool = "<host>::<port>" },
keepalive_timeout = 60000,
keepalive_poolsize = 30,
host = "127.0.0.1",
port = $TEST_NGINX_REDIS_PORT,
path = "",
username = "",
password = "",
db = 0,
url = "",
master_name = "mymaster",
role = "master",
sentinels = {},
}), "new should return positively")
-- Provide all options via connect, without errors
local rc = require("resty.redis.connector").new()
assert(rc:connect({
connect_timeout = 100,
send_timeout = 500,
read_timeout = 1000,
connection_options = { pool = "<host>::<port>" },
keepalive_timeout = 60000,
keepalive_poolsize = 30,
host = "127.0.0.1",
port = $TEST_NGINX_REDIS_PORT,
path = "",
username = "",
password = "",
db = 0,
url = "",
master_name = "mymaster",
role = "master",
sentinels = {},
}), "rc:connect should return positively")
}
}
--- request
GET /t
--- no_error_log
[error]
=== TEST 5: timeout defaults
--- http_config eval: $::HttpConfig
--- config
location /t {
content_by_lua_block {
-- global defaults
local rc = require("resty.redis.connector").new({
port = $TEST_NGINX_REDIS_PORT,
db = 6,
keepalive_poolsize = 10,
})
assert(rc.config.connect_timeout == 100, "connect_timeout should be 100")
assert(rc.config.send_timeout == 1000, "send_timeout should be 1000")
assert(rc.config.read_timeout == 1000, "read_timeout should be 1000")
local redis = assert(rc:connect(), "rc:connect should return positively")
assert(redis:set("foo", "bar"))
rc:set_keepalive(redis)
-- send_timeout defaults to read_timeout
rc = require("resty.redis.connector").new({
read_timeout = 500,
port = $TEST_NGINX_REDIS_PORT,
db = 6,
keepalive_poolsize = 10,
})
assert(rc.config.connect_timeout == 100, "connect_timeout should be 100")
assert(rc.config.send_timeout == 500, "send_timeout should be 500")
assert(rc.config.read_timeout == 500, "read_timeout should be 500")
local redis = assert(rc:connect(), "rc:connect should return positively")
assert(redis:set("foo", "bar"))
rc:set_keepalive(redis)
-- send_timeout can be set separately from read_timeout
rc = require("resty.redis.connector").new({
send_timeout = 500,
read_timeout = 200,
port = $TEST_NGINX_REDIS_PORT,
db = 6,
keepalive_poolsize = 10,
})
assert(rc.config.connect_timeout == 100, "connect_timeout should be 100")
assert(rc.config.send_timeout == 500, "send_timeout should be 500")
assert(rc.config.read_timeout == 200, "read_timeout should be 200")
}
}
--- request
GET /t
--- no_error_log
[error]

View file

@ -1,442 +0,0 @@
use Test::Nginx::Socket 'no_plan';
use Cwd qw(cwd);
my $pwd = cwd();
our $HttpConfig = qq{
lua_package_path "$pwd/lib/?.lua;;";
lua_socket_log_errors Off;
init_by_lua_block {
require("luacov.runner").init()
}
};
$ENV{TEST_NGINX_RESOLVER} = '8.8.8.8';
$ENV{TEST_NGINX_REDIS_PORT} ||= 6380;
$ENV{TEST_NGINX_REDIS_PORT_AUTH} ||= 6393;
$ENV{TEST_NGINX_REDIS_SOCKET} ||= 'unix://tmp/redis/redis.sock';
no_long_string();
run_tests();
__DATA__
=== TEST 1: basic connect
--- http_config eval: $::HttpConfig
--- config
location /t {
content_by_lua_block {
local rc = require("resty.redis.connector").new({
port = $TEST_NGINX_REDIS_PORT
})
local redis, err = assert(rc:connect(params),
"connect should return positively")
assert(redis:set("dog", "an animal"),
"redis:set should return positively")
redis:close()
}
}
--- request
GET /t
--- no_error_log
[error]
=== TEST 2: try_hosts
--- http_config eval: $::HttpConfig
--- config
location /t {
lua_socket_log_errors off;
content_by_lua_block {
local rc = require("resty.redis.connector").new({
connect_timeout = 100,
})
local hosts = {
{ host = "127.0.0.1", port = 1 },
{ host = "127.0.0.1", port = 2 },
{ host = "127.0.0.1", port = $TEST_NGINX_REDIS_PORT },
}
local redis, err, previous_errors = rc:try_hosts(hosts)
assert(redis and not err,
"try_hosts should return a connection and no error")
assert(string.len(previous_errors[1]) > 0,
"previous_errors[1] should contain an error")
assert(string.len(previous_errors[2]) > 0,
"previous_errors[2] should contain an error")
assert(redis:set("dog", "an animal"),
"redis connection should be working")
redis:close()
local hosts = {
{ host = "127.0.0.1", port = 1 },
{ host = "127.0.0.1", port = 2 },
}
local redis, err, previous_errors = rc:try_hosts(hosts)
assert(not redis and err == "no hosts available",
"no available hosts should return an error")
assert(string.len(previous_errors[1]) > 0,
"previous_errors[1] should contain an error")
assert(string.len(previous_errors[2]) > 0,
"previous_errors[2] should contain an error")
}
}
--- request
GET /t
--- no_error_log
[error]
=== TEST 3: connect_to_host
--- http_config eval: $::HttpConfig
--- config
location /t {
content_by_lua_block {
local rc = require("resty.redis.connector").new()
local host = { host = "127.0.0.1", port = $TEST_NGINX_REDIS_PORT }
local redis, err = rc:connect_to_host(host)
assert(redis and not err,
"connect_to_host should return positively")
assert(redis:set("dog", "an animal"),
"redis connection should be working")
redis:close()
}
}
--- request
GET /t
--- no_error_log
[error]
=== TEST 4: connect_to_host options ignore defaults
--- http_config eval: $::HttpConfig
--- config
location /t {
content_by_lua_block {
local rc = require("resty.redis.connector").new({
port = $TEST_NGINX_REDIS_PORT,
db = 2,
})
local redis, err = assert(rc:connect_to_host({
host = "127.0.0.1",
db = 1,
port = $TEST_NGINX_REDIS_PORT
}), "connect_to_host should return positively")
assert(redis:set("dog", "an animal") == "OK",
"set should return 'OK'")
redis:select(2)
assert(redis:get("dog") == ngx.null,
"dog should not exist in db 2")
redis:select(1)
assert(redis:get("dog") == "an animal",
"dog should be 'an animal' in db 1")
redis:close()
}
}
--- request
GET /t
--- no_error_log
[error]
=== TEST 5: Test set_keepalive method
--- http_config eval: $::HttpConfig
--- config
location /t {
lua_socket_log_errors Off;
content_by_lua_block {
local rc = require("resty.redis.connector").new({
port = $TEST_NGINX_REDIS_PORT,
})
local redis = assert(rc:connect(),
"rc:connect should return positively")
local ok, err = rc:set_keepalive(redis)
assert(not err, "set_keepalive error should be nil")
local ok, err = redis:set("foo", "bar")
assert(not ok, "ok should be nil")
assert(string.find(err, "closed"), "error should contain 'closed'")
local redis = assert(rc:connect(), "connect should return positively")
assert(redis:subscribe("channel"), "subscribe should return positively")
local ok, err = rc:set_keepalive(redis)
assert(not ok, "ok should be nil")
assert(string.find(err, "subscribed state"),
"error should contain 'subscribed state'")
}
}
--- request
GET /t
--- no_error_log
[error]
=== TEST 6: password
--- http_config eval: $::HttpConfig
--- config
location /t {
lua_socket_log_errors Off;
content_by_lua_block {
local rc = require("resty.redis.connector").new({
port = $TEST_NGINX_REDIS_PORT,
password = "foo",
})
local redis, err = rc:connect()
assert(not redis and string.find(err, "ERR") and string.find(err, "AUTH"),
"connect should fail with password error")
}
}
--- request
GET /t
--- no_error_log
[error]
=== TEST 7: username and password
--- http_config eval: $::HttpConfig
--- config
location /t {
lua_socket_log_errors Off;
content_by_lua_block {
local rc = require("resty.redis.connector").new({
port = $TEST_NGINX_REDIS_PORT,
username = "x",
password = "foo",
})
local redis, err = rc:connect()
assert(not redis and string.find(err, "WRONGPASS"),
"connect should fail with invalid username-password error")
}
}
--- request
GET /t
--- no_error_log
[error]
=== TEST 8: Bad unix domain socket path should fail
--- http_config eval: $::HttpConfig
--- config
location /t {
lua_socket_log_errors Off;
content_by_lua_block {
local redis, err = require("resty.redis.connector").new({
path = "unix://GARBAGE_PATH_AKFDKAJSFKJSAFLKJSADFLKJSANCKAJSNCKJSANCLKAJS",
}):connect()
assert(not redis and err == "no such file or directory",
"bad domain socket should fail")
}
}
--- request
GET /t
--- no_error_log
[error]
=== TEST 8.1: Good unix domain socket path should succeed
--- http_config eval: $::HttpConfig
--- config
location /t {
lua_socket_log_errors Off;
content_by_lua_block {
local redis, err = require("resty.redis.connector").new({
path = "$TEST_NGINX_REDIS_SOCKET",
}):connect()
assert (redis and not err,
"connection should be valid")
redis:close()
}
}
--- request
GET /t
--- no_error_log
[error]
=== TEST 9: parse_dsn
--- http_config eval: $::HttpConfig
--- config
location /t {
lua_socket_log_errors Off;
content_by_lua_block {
local rc = require("resty.redis.connector")
local user_params = {
url = "redis://foo@127.0.0.1:$TEST_NGINX_REDIS_PORT/4"
}
local params, err = rc.parse_dsn(user_params)
assert(params and not err,
"url should parse without error: " .. tostring(err))
assert(params.host == "127.0.0.1", "host should be localhost")
assert(tonumber(params.port) == $TEST_NGINX_REDIS_PORT,
"port should be $TEST_NGINX_REDIS_PORT")
assert(tonumber(params.db) == 4, "db should be 4")
assert(params.password == "foo", "password should be foo")
local user_params = {
url = "sentinel://foo:bar@foomaster:s/2"
}
local params, err = rc.parse_dsn(user_params)
assert(params and not err,
"url should parse without error: " .. tostring(err))
assert(params.master_name == "foomaster", "master_name should be foomaster")
assert(params.role == "slave", "role should be slave")
assert(tonumber(params.db) == 2, "db should be 2")
assert(params.username == "foo", "username should be foo")
assert(params.password == "bar", "password should be bar")
local params = {
url = "sentinels:/wrongformat",
}
local ok, err = rc.parse_dsn(params)
assert(not ok and err == "could not parse DSN: nil",
"url should fail to parse")
}
}
--- request
GET /t
--- no_error_log
[error]
=== TEST 10: params override dsn components
--- http_config eval: $::HttpConfig
--- config
location /t {
lua_socket_log_errors Off;
content_by_lua_block {
local rc = require("resty.redis.connector")
local user_params = {
url = "redis://foo@127.0.0.1:$TEST_NGINX_REDIS_PORT/4",
db = 2,
password = "bar",
host = "example.com",
}
local params, err = rc.parse_dsn(user_params)
assert(params and not err,
"url should parse without error: " .. tostring(err))
assert(tonumber(params.db) == 2, "db should be 2")
assert(params.password == "bar", "password should be bar")
assert(params.host == "example.com", "host should be example.com")
assert(tonumber(params.port) == $TEST_NGINX_REDIS_PORT, "port should still be $TEST_NGINX_REDIS_PORT")
}
}
--- request
GET /t
--- no_error_log
[error]
=== TEST 11: Integration test for parse_dsn
--- http_config eval: $::HttpConfig
--- config
location /t {
lua_socket_log_errors Off;
content_by_lua_block {
local user_params = {
url = "redis://foo.example:$TEST_NGINX_REDIS_PORT/4",
db = 2,
host = "127.0.0.1",
}
local rc, err = require("resty.redis.connector").new(user_params)
assert(rc and not err, "new should return positively")
local redis, err = rc:connect()
assert(redis and not err, "connect should return positively")
assert(redis:set("cat", "dog") and redis:get("cat") == "dog")
local redis, err = rc:connect({
url = "redis://foo.example:$TEST_NGINX_REDIS_PORT/4",
db = 2,
host = "127.0.0.1",
})
assert(redis and not err, "connect should return positively")
assert(redis:set("cat", "dog") and redis:get("cat") == "dog")
local rc2, err = require("resty.redis.connector").new()
local redis, err = rc2:connect({
url = "redis://foo.example:$TEST_NGINX_REDIS_PORT/4",
db = 2,
host = "127.0.0.1",
})
assert(redis and not err, "connect should return positively")
assert(redis:set("cat", "dog") and redis:get("cat") == "dog")
local redis, err = rc2:connect({
url = "redis://redisuser:redisuserpass@127.0.0.1:$TEST_NGINX_REDIS_PORT_AUTH/"
})
assert(redis and not err, "connect should return positively")
local username = assert(redis:acl("whoami"))
assert(username == "redisuser", "should connect as 'redisuser' but got " .. tostring(username))
}
}
--- request
GET /t
--- no_error_log
[error]
=== TEST 12: DSN without DB
--- http_config eval: $::HttpConfig
--- config
location /t {
lua_socket_log_errors Off;
content_by_lua_block {
local user_params = {
url = "redis://foo.example:$TEST_NGINX_REDIS_PORT",
host = "127.0.0.1",
}
local rc, err = require("resty.redis.connector").new(user_params)
assert(rc and not err, "new should return positively")
local redis, err = rc:connect()
assert(redis and not err, "connect should return positively")
assert(redis:set("cat", "dog") and redis:get("cat") == "dog")
}
}
--- request
GET /t
--- no_error_log
[error]

View file

@ -1,156 +0,0 @@
use Test::Nginx::Socket 'no_plan';
use Cwd qw(cwd);
my $pwd = cwd();
our $HttpConfig = qq{
lua_package_path "$pwd/lib/?.lua;;";
lua_socket_log_errors Off;
init_by_lua_block {
require("luacov.runner").init()
}
};
$ENV{TEST_NGINX_RESOLVER} = '8.8.8.8';
$ENV{TEST_NGINX_REDIS_PORT} ||= 6380;
no_long_string();
run_tests();
__DATA__
=== TEST 1: Proxy mode disables commands
--- http_config eval: $::HttpConfig
--- config
location /t {
content_by_lua_block {
local rc = require("resty.redis.connector").new({
port = $TEST_NGINX_REDIS_PORT,
connection_is_proxied = true
})
local redis, err = assert(rc:connect(params),
"connect should return positively")
assert(redis:set("dog", "an animal"),
"redis:set should return positively")
local ok, err = redis:multi()
assert(ok == nil, "redis:multi should return nil")
assert(err == "Command multi is disabled")
redis:close()
}
}
--- request
GET /t
--- no_error_log
[error]
=== TEST 2: Proxy mode disables custom commands
--- http_config eval: $::HttpConfig
--- config
location /t {
content_by_lua_block {
local rc = require("resty.redis.connector").new({
port = $TEST_NGINX_REDIS_PORT,
connection_is_proxied = true,
disabled_commands = { "foobar", "hget"}
})
local redis, err = assert(rc:connect(params),
"connect should return positively")
assert(redis:set("dog", "an animal"),
"redis:set should return positively")
assert(redis:multi(),
"redis:multi should return positively")
local ok, err = redis:hget()
assert(ok == nil, "redis:hget should return nil")
assert(err == "Command hget is disabled")
local ok, err = redis:foobar()
assert(ok == nil, "redis:foobar should return nil")
assert(err == "Command foobar is disabled")
redis:close()
}
}
--- request
GET /t
--- no_error_log
[error]
=== TEST 3: Proxy mode does not switch DB
--- http_config eval: $::HttpConfig
--- config
location /t {
content_by_lua_block {
local redis = require("resty.redis.connector").new({
port = $TEST_NGINX_REDIS_PORT,
db = 2
}):connect()
local proxy = require("resty.redis.connector").new({
port = $TEST_NGINX_REDIS_PORT,
connection_is_proxied = true,
db = 2
}):connect()
assert(redis:set("proxy", "test"),
"redis:set should return positively")
assert(proxy:get("proxy") == ngx.null,
"proxy key should not exist in proxy")
redis:seelct(2)
assert(redis:get("proxy") == "test",
"proxy key should be 'test' in db 1")
redis:close()
}
}
--- request
GET /t
--- no_error_log
[error]
=== TEST 4: Commands are disabled without proxy mode
--- http_config eval: $::HttpConfig
--- config
location /t {
content_by_lua_block {
local rc = require("resty.redis.connector").new({
port = $TEST_NGINX_REDIS_PORT,
disabled_commands = { "foobar", "hget"}
})
local redis, err = assert(rc:connect(params),
"connect should return positively")
assert(redis:set("dog", "an animal"),
"redis:set should return positively")
assert(redis:multi(),
"redis:multi should return positively")
local ok, err = redis:hget()
assert(ok == nil, "redis:hget should return nil")
assert(err == "Command hget is disabled")
local ok, err = redis:foobar()
assert(ok == nil, "redis:foobar should return nil")
assert(err == "Command foobar is disabled")
redis:close()
}
}
--- request
GET /t
--- no_error_log
[error]

View file

@ -1,279 +0,0 @@
use Test::Nginx::Socket 'no_plan';
use Cwd qw(cwd);
my $pwd = cwd();
our $HttpConfig = qq{
lua_package_path "$pwd/lib/?.lua;;";
lua_socket_log_errors Off;
init_by_lua_block {
require("luacov.runner").init()
}
};
$ENV{TEST_NGINX_RESOLVER} = '8.8.8.8';
$ENV{TEST_NGINX_REDIS_PORT} ||= 6380;
$ENV{TEST_NGINX_REDIS_PORT_SL1} ||= 6381;
$ENV{TEST_NGINX_REDIS_PORT_SL2} ||= 6382;
$ENV{TEST_NGINX_SENTINEL_PORT1} ||= 6390;
$ENV{TEST_NGINX_SENTINEL_PORT2} ||= 6391;
$ENV{TEST_NGINX_SENTINEL_PORT3} ||= 6392;
$ENV{TEST_NGINX_SENTINEL_PORT_AUTH} ||= 6393;
no_long_string();
run_tests();
__DATA__
=== TEST 1: Get the master
--- http_config eval: $::HttpConfig
--- config
location /t {
content_by_lua_block {
local rc = require("resty.redis.connector").new()
local rs = require("resty.redis.sentinel")
local sentinel, err = rc:connect{ url = "redis://127.0.0.1:$TEST_NGINX_SENTINEL_PORT1" }
assert(sentinel and not err, "sentinel should connect without errors but got " .. tostring(err))
local master, err = rs.get_master(sentinel, "mymaster")
assert(master and not err, "get_master should return the master")
assert(master.host == "127.0.0.1" and tonumber(master.port) == $TEST_NGINX_REDIS_PORT,
"host should be 127.0.0.1 and port should be $TEST_NGINX_REDIS_PORT")
master, err = rs.get_master(sentinel, "invalid-mymaster")
assert(not master and err, "invalid master name should result in error")
sentinel:close()
}
}
--- request
GET /t
--- no_error_log
[error]
=== TEST 1b: Get the master directly
--- http_config eval: $::HttpConfig
--- config
location /t {
content_by_lua_block {
local rc = require("resty.redis.connector").new()
local master, err = rc:connect({
url = "sentinel://mymaster:m/3",
sentinels = {
{ host = "127.0.0.1", port = $TEST_NGINX_SENTINEL_PORT1 }
}
})
assert(master and not err, "get_master should return the master")
assert(master:set("foo", "bar"), "set should run without error")
assert(master:get("foo") == "bar", "get(foo) should return bar")
master:close()
}
}
--- request
GET /t
--- no_error_log
[error]
=== TEST 2: Get slaves
--- http_config eval: $::HttpConfig
--- config
location /t {
content_by_lua_block {
local rc = require("resty.redis.connector").new()
local rs = require("resty.redis.sentinel")
local sentinel, err = rc:connect{ url = "redis://127.0.0.1:$TEST_NGINX_SENTINEL_PORT1" }
assert(sentinel and not err, "sentinel should connect without error")
local slaves, err = rs.get_slaves(sentinel, "mymaster")
assert(slaves and not err, "slaves should be returned without error")
local slaveports = { ["$TEST_NGINX_REDIS_PORT_SL1"] = false, ["$TEST_NGINX_REDIS_PORT_SL2"] = false }
for _,slave in ipairs(slaves) do
slaveports[tostring(slave.port)] = true
end
assert(slaveports["$TEST_NGINX_REDIS_PORT_SL1"] == true and slaveports["$TEST_NGINX_REDIS_PORT_SL2"] == true,
"slaves should both be found")
slaves, err = rs.get_slaves(sentinel, "invalid-mymaster")
assert(not slaves and err, "invalid master name should result in error")
sentinel:close()
}
}
--- request
GET /t
--- no_error_log
[error]
=== TEST 3: Get only healthy slaves
--- http_config eval: $::HttpConfig
--- config
location /t {
content_by_lua_block {
local rc = require("resty.redis.connector").new()
local sentinel, err = rc:connect({ url = "redis://127.0.0.1:$TEST_NGINX_SENTINEL_PORT1" })
assert(sentinel and not err, "sentinel should connect without error")
local slaves, err = require("resty.redis.sentinel").get_slaves(
sentinel,
"mymaster"
)
assert(slaves and not err, "slaves should be returned without error")
local slaveports = { ["$TEST_NGINX_REDIS_PORT_SL1"] = false, ["$TEST_NGINX_REDIS_PORT_SL2"] = false }
for _,slave in ipairs(slaves) do
slaveports[tostring(slave.port)] = true
end
assert(slaveports["$TEST_NGINX_REDIS_PORT_SL1"] == true and slaveports["$TEST_NGINX_REDIS_PORT_SL2"] == true,
"slaves should both be found")
-- connect to one and remove it
local r = require("resty.redis.connector").new():connect({
port = $TEST_NGINX_REDIS_PORT_SL1,
})
r:slaveof("127.0.0.1", 7000)
ngx.sleep(9)
local slaves, err = require("resty.redis.sentinel").get_slaves(
sentinel,
"mymaster"
)
assert(slaves and not err, "slaves should be returned without error")
local slaveports = { ["$TEST_NGINX_REDIS_PORT_SL1"] = false, ["$TEST_NGINX_REDIS_PORT_SL2"] = false }
for _,slave in ipairs(slaves) do
slaveports[tostring(slave.port)] = true
end
assert(slaveports["$TEST_NGINX_REDIS_PORT_SL1"] == false and slaveports["$TEST_NGINX_REDIS_PORT_SL2"] == true,
"only $TEST_NGINX_REDIS_PORT_SL2 should be found")
r:slaveof("127.0.0.1", $TEST_NGINX_REDIS_PORT)
sentinel:close()
}
}
--- request
GET /t
--- timeout: 10
--- no_error_log
[error]
=== TEST 4: connector.connect_via_sentinel
--- http_config eval: $::HttpConfig
--- config
location /t {
content_by_lua_block {
local rc = require("resty.redis.connector").new()
local params = {
sentinels = {
{ host = "127.0.0.1", port = $TEST_NGINX_SENTINEL_PORT1 },
{ host = "127.0.0.1", port = $TEST_NGINX_SENTINEL_PORT2 },
{ host = "127.0.0.1", port = $TEST_NGINX_SENTINEL_PORT3 },
},
master_name = "mymaster",
role = "master",
}
local redis, err = rc:connect_via_sentinel(params)
assert(redis and not err, "redis should connect without error")
params.role = "slave"
local redis, err = rc:connect_via_sentinel(params)
assert(redis and not err, "redis should connect without error")
}
}
--- request
GET /t
--- no_error_log
[error]
=== TEST 5: regression for slave sorting (iss12)
--- http_config eval: $::HttpConfig
--- config
location /t {
lua_socket_log_errors Off;
content_by_lua_block {
local rc = require("resty.redis.connector").new()
local params = {
sentinels = {
{ host = "127.0.0.1", port = $TEST_NGINX_SENTINEL_PORT1 },
{ host = "127.0.0.1", port = $TEST_NGINX_SENTINEL_PORT2 },
{ host = "127.0.0.1", port = $TEST_NGINX_SENTINEL_PORT3 },
},
master_name = "mymaster",
role = "slave",
}
-- hotwire get_slaves to expose sorting issue
local sentinel = require("resty.redis.sentinel")
sentinel.get_slaves = function()
return {
{ host = "127.0.0.1", port = $TEST_NGINX_REDIS_PORT_SL1 },
{ host = "127.0.0.1", port = $TEST_NGINX_REDIS_PORT_SL2 },
{ host = "134.123.51.2", port = $TEST_NGINX_REDIS_PORT_SL1 },
}
end
local redis, err = rc:connect_via_sentinel(params)
assert(redis and not err, "redis should connect without error")
}
}
--- request
GET /t
--- no_error_log
[error]
=== TEST 6: connect with acl
--- http_config eval: $::HttpConfig
--- config
location /t {
content_by_lua_block {
local rc = require("resty.redis.connector").new()
local redis, err = rc:connect({
username = "redisuser",
password = "redisuserpass",
sentinels = {
{ host = "127.0.0.1", port = $TEST_NGINX_SENTINEL_PORT_AUTH }
},
master_name = "mymaster",
sentinel_username = "sentineluser",
sentinel_username = "sentineluserpass",
})
assert(redis and not err, "redis should connect without error")
local username = assert(redis:acl("whoami"))
assert(username == "redisuser", "should connect as 'redisuser' but got " .. tostring(username))
}
}
--- request
GET /t
--- no_error_log
[error]