Codebase list lua-nginx-redis-connector / upstream/0.10
Import upstream version 0.10 Debian Janitor 2 years ago
13 changed file(s) with 336 addition(s) and 148 deletion(s). Raw diff Collapse all Expand all
0 std = "ngx_lua"
1 redefined = false
0 sudo: required
1 dist: trusty
2
3 os: linux
4
5 language: c
6
7 compiler: gcc
8
9 addons:
10 apt:
11 packages:
12 - luarocks
13 - redis
14 - lsof
15
16 cache:
17 directories:
18 - download-cache
19
20 env:
21 global:
22 - JOBS=3
23 - NGX_BUILD_JOBS=$JOBS
24 - LUAJIT_PREFIX=/opt/luajit21
25 - LUAJIT_LIB=$LUAJIT_PREFIX/lib
26 - LUAJIT_INC=$LUAJIT_PREFIX/include/luajit-2.1
27 - LUA_INCLUDE_DIR=$LUAJIT_INC
28 - OPENSSL_PREFIX=/opt/ssl
29 - OPENSSL_LIB=$OPENSSL_PREFIX/lib
30 - OPENSSL_INC=$OPENSSL_PREFIX/include
31 - OPENSSL_VER=1.1.1f
32 - LD_LIBRARY_PATH=$LUAJIT_LIB:$LD_LIBRARY_PATH
33 - TEST_NGINX_SLEEP=0.006
34 - LUACHECK_VER=0.21.1
35 jobs:
36 - NGINX_VERSION=1.17.8
37
38 before_install:
39 - sudo luarocks install luacov
40 - sudo luarocks install lua-resty-redis
41 - sudo luarocks install luacheck $LUACHECK_VER
42 - luacheck -q .
43
44 install:
45 - if [ ! -d download-cache ]; then mkdir download-cache; fi
46 - if [ ! -f download-cache/openssl-$OPENSSL_VER.tar.gz ]; then wget -O download-cache/openssl-$OPENSSL_VER.tar.gz https://www.openssl.org/source/openssl-$OPENSSL_VER.tar.gz; fi
47 - sudo apt-get install -qq -y cpanminus axel
48 - sudo cpanm --notest Test::Nginx > build.log 2>&1 || (cat build.log && exit 1)
49 - git clone https://github.com/openresty/openresty.git ../openresty
50 - git clone https://github.com/openresty/nginx-devel-utils.git
51 - git clone https://github.com/openresty/lua-cjson.git
52 - git clone https://github.com/openresty/lua-nginx-module.git ../lua-nginx-module
53 - git clone https://github.com/openresty/stream-lua-nginx-module.git ../stream-lua-nginx-module
54 - git clone https://github.com/openresty/lua-resty-core.git ../lua-resty-core
55 - git clone https://github.com/openresty/lua-resty-lrucache.git ../lua-resty-lrucache
56 - git clone https://github.com/openresty/echo-nginx-module.git ../echo-nginx-module
57 - git clone https://github.com/openresty/no-pool-nginx.git ../no-pool-nginx
58 - git clone -b v2.1-agentzh https://github.com/openresty/luajit2.git
59
60 script:
61 - sudo iptables -A OUTPUT -p tcp --dst 127.0.0.2 --dport 12345 -j DROP
62 - cd luajit2/
63 - make -j$JOBS CCDEBUG=-g Q= PREFIX=$LUAJIT_PREFIX CC=$CC XCFLAGS='-DLUA_USE_APICHECK -DLUA_USE_ASSERT' > build.log 2>&1 || (cat build.log && exit 1)
64 - sudo make install PREFIX=$LUAJIT_PREFIX > build.log 2>&1 || (cat build.log && exit 1)
65 - cd ../lua-cjson && make && sudo PATH=$PATH make install && cd ..
66 - tar zxf download-cache/openssl-$OPENSSL_VER.tar.gz
67 - cd openssl-$OPENSSL_VER/
68 - ./config shared --prefix=$OPENSSL_PREFIX -DPURIFY > build.log 2>&1 || (cat build.log && exit 1)
69 - make -j$JOBS > build.log 2>&1 || (cat build.log && exit 1)
70 - sudo make PATH=$PATH install_sw > build.log 2>&1 || (cat build.log && exit 1)
71 - cd ..
72 - export PATH=$PWD/work/nginx/sbin:$PWD/nginx-devel-utils:$PATH
73 - export NGX_BUILD_CC=$CC
74 - ngx-build $NGINX_VERSION --with-ipv6 --with-http_realip_module --with-http_ssl_module --with-cc-opt="-I$OPENSSL_INC" --with-ld-opt="-L$OPENSSL_LIB -Wl,-rpath,$OPENSSL_LIB" --add-module=../echo-nginx-module --add-module=../lua-nginx-module --add-module=../stream-lua-nginx-module --with-stream --with-stream_ssl_module --with-debug > build.log 2>&1 || (cat build.log && exit 1)
75 - nginx -V
76 - ldd `which nginx`|grep -E 'luajit|ssl|pcre'
77 - mkdir -p tmp
78 - TMP_DIR=$PWD/tmp make test_all
22 OPENRESTY_PREFIX = /usr/local/openresty
33
44 TEST_FILE ?= t
5 TMP_DIR ?= /tmp
56 SENTINEL_TEST_FILE ?= $(TEST_FILE)/sentinel
67
78 REDIS_CMD = redis-server
1011 REDIS_SOCK = /redis.sock
1112 REDIS_PID = /redis.pid
1213 REDIS_LOG = /redis.log
13 REDIS_PREFIX = /tmp/redis-
14 REDIS_PREFIX = $(TMP_DIR)/redis-
1415
1516 # Overrideable redis test variables
16 TEST_REDIS_PORTS ?= 6379 6380 6378
17 TEST_REDIS_DATABASE ?= 1
17 TEST_REDIS_PORT ?= 6380
18 TEST_REDIS_PORT_SL1 ?= 6381
19 TEST_REDIS_PORT_SL2 ?= 6382
20 TEST_REDIS_PORTS ?= $(TEST_REDIS_PORT) $(TEST_REDIS_PORT_SL1) $(TEST_REDIS_PORT_SL2)
21 TEST_REDIS_DATABASE ?= 1
22 TEST_REDIS_SOCKET ?= $(REDIS_PREFIX)$(TEST_REDIS_PORT)$(REDIS_SOCK)
1823
19 REDIS_FIRST_PORT := $(firstword $(TEST_REDIS_PORTS))
20 REDIS_SLAVE_ARG := --slaveof 127.0.0.1 $(REDIS_FIRST_PORT)
21 REDIS_CLI := redis-cli -p $(REDIS_FIRST_PORT) -n $(TEST_REDIS_DATABASE)
22
23 # Override socket for running make test on its own
24 # (make test TEST_REDIS_SOCKET=/path/to/sock.sock)
25 TEST_REDIS_SOCKET ?= $(REDIS_PREFIX)$(REDIS_FIRST_PORT)$(REDIS_SOCK)
24 REDIS_SLAVE_ARG := --slaveof 127.0.0.1 $(TEST_REDIS_PORT)
25 REDIS_CLI := redis-cli -p $(TEST_REDIS_PORT) -n $(TEST_REDIS_DATABASE)
2626
2727 # Overrideable redis + sentinel test variables
28 TEST_SENTINEL_PORTS ?= 6381 6382 6383
28 TEST_SENTINEL_PORT1 ?= 6390
29 TEST_SENTINEL_PORT2 ?= 6391
30 TEST_SENTINEL_PORT3 ?= 6392
31 TEST_SENTINEL_PORTS ?= $(TEST_SENTINEL_PORT1) $(TEST_SENTINEL_PORT2) $(TEST_SENTINEL_PORT3)
2932 TEST_SENTINEL_MASTER_NAME ?= mymaster
3033 TEST_SENTINEL_PROMOTION_TIME ?= 20
3134
3235 # Command line arguments for redis tests
3336 TEST_REDIS_VARS = PATH=$(OPENRESTY_PREFIX)/nginx/sbin:$(PATH) \
34 TEST_REDIS_SOCKET=unix://$(TEST_REDIS_SOCKET) \
35 TEST_REDIS_DATABASE=$(TEST_REDIS_DATABASE) \
37 TEST_NGINX_REDIS_PORT=$(TEST_REDIS_PORT) \
38 TEST_NGINX_REDIS_PORT_SL1=$(TEST_REDIS_PORT_SL1) \
39 TEST_NGINX_REDIS_PORT_SL2=$(TEST_REDIS_PORT_SL2) \
40 TEST_NGINX_REDIS_SOCKET=unix:$(TEST_REDIS_SOCKET) \
41 TEST_NGINX_REDIS_DATABASE=$(TEST_REDIS_DATABASE) \
3642 TEST_NGINX_NO_SHUFFLE=1
3743
3844 # Command line arguments for sentinel tests
3945 TEST_SENTINEL_VARS = PATH=$(OPENRESTY_PREFIX)/nginx/sbin:$(PATH) \
40 TEST_SENTINEL_PORT=$(firstword $(TEST_SENTINEL_PORTS)) \
41 TEST_SENTINEL_MASTER_NAME=$(TEST_SENTINEL_MASTER_NAME) \
42 TEST_REDIS_DATABASE=$(TEST_REDIS_DATABASE) \
46 TEST_NGINX_REDIS_PORT=$(TEST_NGINX_REDIS_PORT) \
47 TEST_NGINX_REDIS_PORT_SL1=$(TEST_NGINX_REDIS_PORT_SL1) \
48 TEST_NGINX_REDIS_PORT_SL2=$(TEST_NGINX_REDIS_PORT_SL2) \
49 TEST_NGINX_SENTINEL_PORT1=$(TEST_NGINX_SENTINEL_PORT1) \
50 TEST_NGINX_SENTINEL_PORT2=$(TEST_NGINX_SENTINEL_PORT2) \
51 TEST_NGINX_SENTINEL_PORT3=$(TEST_NGINX_SENTINEL_PORT3) \
52 TEST_NGINX_SENTINEL_MASTER_NAME=$(TEST_NGINX_SENTINEL_MASTER_NAME) \
53 TEST_NGINX_REDIS_DATABASE=$(TEST_NGINX_REDIS_DATABASE) \
4354 TEST_NGINX_NO_SHUFFLE=1
4455
4556 # Sentinel configuration can only be set by a config file
4657 define TEST_SENTINEL_CONFIG
47 sentinel monitor $(TEST_SENTINEL_MASTER_NAME) 127.0.0.1 $(REDIS_FIRST_PORT) 2
58 sentinel monitor $(TEST_SENTINEL_MASTER_NAME) 127.0.0.1 $(TEST_REDIS_PORT) 2
4859 sentinel down-after-milliseconds $(TEST_SENTINEL_MASTER_NAME) 2000
4960 sentinel failover-timeout $(TEST_SENTINEL_MASTER_NAME) 10000
5061 sentinel parallel-syncs $(TEST_SENTINEL_MASTER_NAME) 5
7586 test: test_redis
7687 test_all: start_redis_instances sleep test_redis stop_redis_instances
7788
89 check:
90 luacheck lib
91
7892 sleep:
7993 sleep 3
8094
8195 start_redis_instances: check_ports create_sentinel_config
96 $(REDIS_CMD) --version
97
8298 @$(foreach port,$(TEST_REDIS_PORTS), \
83 [[ "$(port)" != "$(REDIS_FIRST_PORT)" ]] && \
99 [[ "$(port)" != "$(TEST_REDIS_PORT)" ]] && \
84100 SLAVE="$(REDIS_SLAVE_ARG)" || \
85101 SLAVE="" && \
86102 $(MAKE) start_redis_instance args="$$SLAVE" port=$(port) \
93109 prefix=$(REDIS_PREFIX)$(port) && \
94110 ) true
95111
96
97112 stop_redis_instances: delete_sentinel_config
98113 -@$(foreach port,$(TEST_REDIS_PORTS) $(TEST_SENTINEL_PORTS), \
99114 $(MAKE) stop_redis_instance cleanup_redis_instance port=$(port) \
104119 start_redis_instance:
105120 -@echo "Starting redis on port $(port) with args: \"$(args)\""
106121 -@mkdir -p $(prefix)
107 @$(REDIS_CMD) $(args) \
122 $(REDIS_CMD) $(args) \
108123 --pidfile $(prefix)$(REDIS_PID) \
109124 --bind 127.0.0.1 --port $(port) \
110125 --unixsocket $(prefix)$(REDIS_SOCK) \
00 # lua-resty-redis-connector
1
2 [![Build Status](https://travis-ci.org/ledgetech/lua-resty-redis-connector.svg?branch=master)](https://travis-ci.org/ledgetech/lua-resty-redis-connector)
13
24 Connection utilities for [lua-resty-redis](https://github.com/openresty/lua-resty-redis), making it easy and reliable to connect to Redis hosts, either directly or via [Redis Sentinel](http://redis.io/topics/sentinel).
35
9092
9193 Again, `PASSWORD` and `DB` are optional. `ROLE` must be either `m` or `s` for master / slave respectively.
9294
95 On versions of Redis newer than 5.0.1, Sentinels can optionally require their own password. If enabled, provide this password in the `sentinel_password` parameter.
96
9397 A table of `sentinels` must also be supplied. e.g.
9498
9599 ```lua
96100 local redis, err = rc:connect{
97101 url = "sentinel://mymaster:a/2",
98102 sentinels = {
99 { host = "127.0.0.1", port = 26379" },
100 }
103 { host = "127.0.0.1", port = 26379 },
104 },
105 sentinel_password = "password"
101106 }
102107 ```
103108
132137 port = "6379",
133138 path = "", -- unix socket path, e.g. /tmp/redis.sock
134139 password = "",
140 sentinel_password = "",
135141 db = 0,
136142
137143 master_name = "mymaster",
44 license=2bsd
55 lib_dir=lib
66 doc_dir=lib
7 repo_link=https://github.com/pintsized/lua-resty-redis-connector
7 repo_link=https://github.com/ledgetech/lua-resty-redis-connector
88 main_module=lib/resty/redis/connector.lua
44 local ngx_ERR = ngx.ERR
55 local ngx_re_match = ngx.re.match
66
7 local str_find = string.find
78 local tbl_remove = table.remove
89 local tbl_sort = table.sort
910 local ok, tbl_new = pcall(require, "table.new")
1011 if not ok then
11 tbl_new = function (narr, nrec) return {} end
12 tbl_new = function (narr, nrec) return {} end -- luacheck: ignore 212
1213 end
1314
1415 local redis = require("resty.redis")
2122 -- A metatable which prevents undefined fields from being created / accessed
2223 local fixed_field_metatable = {
2324 __index =
24 function(t, k)
25 function(t, k) -- luacheck: ignore 212
2526 error("field " .. tostring(k) .. " does not exist", 3)
2627 end,
2728 __newindex =
28 function(t, k, v)
29 function(t, k, v) -- luacheck: ignore 212
2930 error("attempt to create new field " .. tostring(k), 3)
3031 end,
3132 }
6566 if t1 == nil then t1 = {} end
6667 if defaults == nil then defaults = {} end
6768 if type(t1) == "table" and type(defaults) == "table" then
68 local mt = getmetatable(defaults)
6969 local copy = {}
7070 for t1_key, t1_value in next, t1, nil do
7171 copy[tbl_copy(t1_key)] = tbl_copy_merge_defaults(
9595 port = 6379,
9696 path = "", -- /tmp/redis.sock
9797 password = "",
98 sentinel_password = "",
9899 db = 0,
99100 url = "", -- DSN url
100101
123124
124125
125126 local _M = {
126 _VERSION = '0.06',
127 _VERSION = '0.09',
127128 }
128129
129130 local mt = { __index = _M }
155156 local parsed_params = {}
156157
157158 for i,v in ipairs(fields) do
158 parsed_params[v] = m[i + 1]
159 if v == "db" or v == "port" then
160 parsed_params[v] = tonumber(m[i + 1])
161 else
162 parsed_params[v] = m[i + 1]
163 end
164
159165 if v == "role" then
160166 parsed_params[v] = roles[parsed_params[v]]
161167 end
163169
164170 return tbl_copy_merge_defaults(params, parsed_params)
165171 end
172
173 return params
166174 end
167175 _M.parse_dsn = parse_dsn
168176
172180 if config and config.url then
173181 local err
174182 config, err = parse_dsn(config)
175 if not ok then ngx_log(ngx_ERR, err) end
183 if err then ngx_log(ngx_ERR, err) end
176184 end
177185
178186 local ok, config = pcall(tbl_copy_merge_defaults, config, DEFAULTS)
197205 if params and params.url then
198206 local err
199207 params, err = parse_dsn(params)
200 if not ok then ngx_log(ngx_ERR, err) end
208 if err then ngx_log(ngx_ERR, err) end
201209 end
202210
203211 params = tbl_copy_merge_defaults(params, self.config)
225233 local role = params.role
226234 local db = params.db
227235 local password = params.password
236 local sentinel_password = params.sentinel_password
237 if sentinel_password then
238 for _,host in ipairs(sentinels) do
239 host.password = sentinel_password
240 end
241 end
228242
229243 local sentnl, err, previous_errors = self:try_hosts(sentinels)
230244 if not sentnl then
233247
234248 if role == "master" then
235249 local master, err = get_master(sentnl, master_name)
236 if master then
237 master.db = db
238 master.password = password
239
240 local redis, err = self:connect_to_host(master)
241 if redis then
242 sentnl:set_keepalive()
243 return redis, err
244 else
245 if role == "master" then
246 return nil, err
247 end
248 end
249 end
250
250 if not master then
251 return nil, err
252 end
253
254 sentnl:set_keepalive()
255
256 master.db = db
257 master.password = password
258
259 local redis, err = self:connect_to_host(master)
260 if not redis then
261 return nil, err
262 end
263
264 return redis
251265 else
252266 -- We want a slave
253267 local slaves, err = get_slaves(sentnl, master_name)
254 sentnl:set_keepalive()
255
256268 if not slaves then
257269 return nil, err
258270 end
259271
272 sentnl:set_keepalive()
273
260274 -- Put any slaves on 127.0.0.1 at the front
261275 tbl_sort(slaves, sort_by_localhost)
262276
263277 if db or password then
264 for i,slave in ipairs(slaves) do
278 for _, slave in ipairs(slaves) do
265279 slave.db = db
266280 slave.password = password
267281 end
270284 local slave, err, previous_errors = self:try_hosts(slaves)
271285 if not slave then
272286 return nil, err, previous_errors
273 else
274 return slave
275 end
287 end
288
289 return slave
276290 end
277291 end
278292
297311
298312 function _M.connect_to_host(self, host)
299313 local r = redis.new()
300 local config = self.config
314
315 -- config options in 'host' should override the global defaults
316 -- host contains keys that aren't in config
317 -- this can break tbl_copy_merge_defaults, hence the mannual loop here
318 local config = tbl_copy(self.config)
319 for k, _ in pairs(config) do
320 if host[k] then
321 config[k] = host[k]
322 end
323 end
324
301325 r:set_timeout(config.connect_timeout)
302326
303327 -- Stub out methods for disabled commands
304328 if next(config.disabled_commands) then
305329 for _, cmd in ipairs(config.disabled_commands) do
306 r[cmd] = function(...)
330 r[cmd] = function()
307331 return nil, ("Command "..cmd.." is disabled")
308332 end
309333 end
329353 if not ok then
330354 return nil, err
331355 else
332 r:set_timeout(self, config.read_timeout)
356 r:set_timeout(config.read_timeout)
333357
334358 local password = host.password
335359 if password and password ~= "" then
336360 local res, err = r:auth(password)
337361 if err then
338 ngx_log(ngx_ERR, err)
339362 return res, err
340363 end
341364 end
342365
343366 -- No support for DBs in proxied Redis.
344367 if config.connection_is_proxied ~= true and host.db ~= nil then
345 r:select(host.db)
368 local res, err = r:select(host.db)
369
370 -- SELECT will fail if we are connected to sentinel:
371 -- detect it and ignore error message it that's the case
372 if err and str_find(err, "ERR unknown command") then
373 local role = r:role()
374 if role and role[1] == "sentinel" then
375 err = nil
376 end
377 end
378 if err then
379 return res, err
380 end
346381 end
347382 return r, nil
348383 end
349384 end
350385
351386
352 local function set_keepalive(self, redis)
387 function _M.set_keepalive(self, redis)
353388 -- Restore connection to "NORMAL" before putting into keepalive pool,
354389 -- ignoring any errors.
355390 -- Proxied Redis does not support transactions.
362397 config.keepalive_timeout, config.keepalive_poolsize
363398 )
364399 end
365 _M.set_keepalive = set_keepalive
366
367400
368401
369402 -- Deprecated: use config table in new() or connect() instead.
44 local tbl_insert = table.insert
55 local ok, tbl_new = pcall(require, "table.new")
66 if not ok then
7 tbl_new = function (narr, nrec) return {} end
7 tbl_new = function (narr, nrec) return {} end -- luacheck: ignore 212
88 end
99
1010
1111 local _M = {
12 _VERSION = '0.06'
12 _VERSION = '0.09'
1313 }
1414
1515
3838 host[slave[i]] = slave[i + 1]
3939 end
4040
41 if host["master-link-status"] == "ok" then
41 local master_link_status_ok = host["master-link-status"] == "ok"
42 local is_down = host["flags"] and (string.find(host["flags"],"s_down")
43 or string.find(host["flags"],"disconnected"))
44 if master_link_status_ok and not is_down then
4245 host.host = host.ip -- for parity with other functions
4346 tbl_insert(hosts, host)
4447 end
+0
-27
lua-resty-redis-connector-0.06-0.rockspec less more
0 package = "lua-resty-redis-connector"
1 version = "0.06-0"
2 source = {
3 url = "git://github.com/pintsized/lua-resty-redis-connector",
4 tag = "v0.06"
5 }
6 description = {
7 summary = "Connection utilities for lua-resty-redis.",
8 detailed = [[
9 Connection utilities for lua-resty-redis, making it easy and
10 reliable to connect to Redis hosts, either directly or via Redis
11 Sentinel.
12 ]],
13 homepage = "https://github.com/pintsized/lua-resty-redis-connector",
14 license = "2-clause BSD",
15 maintainer = "James Hurst <james@pintsized.co.uk>"
16 }
17 dependencies = {
18 "lua >= 5.1",
19 }
20 build = {
21 type = "builtin",
22 modules = {
23 ["resty.redis.connector"] = "lib/resty/redis/connector.lua",
24 ["resty.redis.sentinel"] = "lib/resty/redis/sentinel.lua"
25 }
26 }
0 package = "lua-resty-redis-connector"
1 version = "0.09-0"
2 source = {
3 url = "git://github.com/ledgetech/lua-resty-redis-connector",
4 tag = "v0.09"
5 }
6 description = {
7 summary = "Connection utilities for lua-resty-redis.",
8 detailed = [[
9 Connection utilities for lua-resty-redis, making it easy and
10 reliable to connect to Redis hosts, either directly or via Redis
11 Sentinel.
12 ]],
13 homepage = "https://github.com/ledgetech/lua-resty-redis-connector",
14 license = "2-clause BSD",
15 maintainer = "James Hurst <james@pintsized.co.uk>"
16 }
17 dependencies = {
18 "lua >= 5.1",
19 }
20 build = {
21 type = "builtin",
22 modules = {
23 ["resty.redis.connector"] = "lib/resty/redis/connector.lua",
24 ["resty.redis.sentinel"] = "lib/resty/redis/sentinel.lua"
25 }
26 }
2626 --- config
2727 location /t {
2828 content_by_lua_block {
29 local rc = require("resty.redis.connector").new()
30
31 local redis = assert(rc:connect(), "rc:connect should return postively")
32 assert(redis:set("foo", "bar"), "redis:set should return positvely")
33 assert(redis:get("foo") == "bar", "get(foo) should return bar")
34 redis:close()
29 local rc = assert(require("resty.redis.connector").new())
3530 }
3631 }
3732 --- request
4742 content_by_lua_block {
4843 local config = {
4944 connect_timeout = 500,
50 port = 6380,
45 port = $TEST_NGINX_REDIS_PORT,
5146 db = 6,
5247 }
5348 local rc = require("resty.redis.connector").new(config)
7166 content_by_lua_block {
7267 local rc = require("resty.redis.connector").new({
7368 connect_timeout = 500,
74 port = 6380,
69 port = $TEST_NGINX_REDIS_PORT,
7570 db = 6,
7671 keepalive_poolsize = 10,
7772 })
8378 assert(rc.config.keepalive_poolsize == 10,
8479 "keepalive_poolsize should be 10")
8580
86 local redis = assert(rc:connect({ port = 6379 }),
81 local redis = assert(rc:connect({ port = $TEST_NGINX_REDIS_PORT, disabled_commands = {"set"} }),
8782 "rc:connect should return positively")
83
84 local ok, err = redis:set("foo", "bar")
85 assert( ok == nil and (string.find(err, "disabled") ~= nil) , "Disabled commands not passed through" )
8886 }
8987 }
9088 --- request
10098 content_by_lua_block {
10199 local rc, err = require("resty.redis.connector").new({
102100 connect_timeout = 500,
103 port = 6380,
101 port = $TEST_NGINX_REDIS_PORT,
104102 db = 6,
105103 foo = "bar",
106104 })
119117 keepalive_poolsize = 30,
120118
121119 host = "127.0.0.1",
122 port = 6379,
120 port = $TEST_NGINX_REDIS_PORT,
123121 path = "",
124122 password = "",
125123 db = 0,
143141 keepalive_poolsize = 30,
144142
145143 host = "127.0.0.1",
146 port = 6379,
144 port = $TEST_NGINX_REDIS_PORT,
147145 path = "",
148146 password = "",
149147 db = 0,
1212
1313 $ENV{TEST_NGINX_RESOLVER} = '8.8.8.8';
1414 $ENV{TEST_NGINX_REDIS_PORT} ||= 6379;
15 $ENV{TEST_NGINX_REDIS_SOCKET} ||= 'unix://tmp/redis/redis.sock';
1516
1617 no_long_string();
1718 run_tests();
6263 assert(redis and not err,
6364 "try_hosts should return a connection and no error")
6465
65 assert(previous_errors[1] == "connection refused",
66 "previous_errors[1] should be 'connection refused'")
67 assert(previous_errors[2] == "connection refused",
68 "previous_errors[2] should be 'connection refused'")
66 assert(string.len(previous_errors[1]) > 0,
67 "previous_errors[1] should contain an error")
68 assert(string.len(previous_errors[2]) > 0,
69 "previous_errors[2] should contain an error")
6970
7071 assert(redis:set("dog", "an animal"),
7172 "redis connection should be working")
8182 assert(not redis and err == "no hosts available",
8283 "no available hosts should return an error")
8384
84 assert(previous_errors[1] == "connection refused",
85 "previous_errors[1] should be 'connection refused'")
86 assert(previous_errors[2] == "connection refused",
87 "previous_errors[2] should be 'connection refused'")
85 assert(string.len(previous_errors[1]) > 0,
86 "previous_errors[1] should contain an error")
87 assert(string.len(previous_errors[2]) > 0,
88 "previous_errors[2] should contain an error")
8889 }
8990 }
9091 --- request
201202 })
202203
203204 local redis, err = rc:connect()
204 assert(not redis and string.find(err, "ERR Client sent AUTH, but no password is set"),
205 assert(not redis and string.find(err, "ERR") and string.find(err, "AUTH"),
205206 "connect should fail with password error")
206207
207208 }
208209 }
209210 --- request
210211 GET /t
211 --- error_log
212 ERR Client sent AUTH, but no password is set
213
214
215 === TEST 7: unix domain socket
212 --- no_error_log
213 [error]
214
215
216 === TEST 7: Bad unix domain socket path should fail
216217 --- http_config eval: $::HttpConfig
217218 --- config
218219 location /t {
219220 lua_socket_log_errors Off;
220221 content_by_lua_block {
221222 local redis, err = require("resty.redis.connector").new({
222 path = "unix://tmp/redis.sock",
223 path = "unix://GARBAGE_PATH_AKFDKAJSFKJSAFLKJSADFLKJSANCKAJSNCKJSANCLKAJS",
223224 }):connect()
224225
225226 assert(not redis and err == "no such file or directory",
226227 "bad domain socket should fail")
228 }
229 }
230 --- request
231 GET /t
232 --- no_error_log
233 [error]
234
235
236 === TEST 7.1: Good unix domain socket path should succeed
237 --- http_config eval: $::HttpConfig
238 --- config
239 location /t {
240 lua_socket_log_errors Off;
241 content_by_lua_block {
242 local redis, err = require("resty.redis.connector").new({
243 path = "$TEST_NGINX_REDIS_SOCKET",
244 }):connect()
245
246 assert (redis and not err,
247 "connection should be valid")
248
249 redis:close()
227250 }
228251 }
229252 --- request
292315 local rc = require("resty.redis.connector")
293316
294317 local user_params = {
295 url = "redis://foo@127.0.0.1:6381/4",
318 url = "redis://foo@127.0.0.1:$TEST_NGINX_REDIS_PORT/4",
296319 db = 2,
297320 password = "bar",
298321 host = "example.com",
306329 assert(params.password == "bar", "password should be bar")
307330 assert(params.host == "example.com", "host should be example.com")
308331
309 assert(tonumber(params.port) == 6381, "ort should still be 6381")
332 assert(tonumber(params.port) == $TEST_NGINX_REDIS_PORT, "port should still be $TEST_NGINX_REDIS_PORT")
310333
311334 }
312335 }
359382 GET /t
360383 --- no_error_log
361384 [error]
385
386
387 === TEST 10: DSN without DB
388 --- http_config eval: $::HttpConfig
389 --- config
390 location /t {
391 lua_socket_log_errors Off;
392 content_by_lua_block {
393 local user_params = {
394 url = "redis://foo.example:$TEST_NGINX_REDIS_PORT",
395 host = "127.0.0.1",
396 }
397
398 local rc, err = require("resty.redis.connector").new(user_params)
399 assert(rc and not err, "new should return positively")
400
401 local redis, err = rc:connect()
402 assert(redis and not err, "connect should return positively")
403 assert(redis:set("cat", "dog") and redis:get("cat") == "dog")
404 }
405 }
406 --- request
407 GET /t
408 --- no_error_log
409 [error]
1111 };
1212
1313 $ENV{TEST_NGINX_RESOLVER} = '8.8.8.8';
14 $ENV{TEST_NGINX_REDIS_PORT} ||= 6379;
14 $ENV{TEST_NGINX_REDIS_PORT} ||= 6380;
15 $ENV{TEST_NGINX_REDIS_PORT_SL1} ||= 6381;
16 $ENV{TEST_NGINX_REDIS_PORT_SL2} ||= 6382;
17 $ENV{TEST_NGINX_SENTINEL_PORT1} ||= 6390;
18 $ENV{TEST_NGINX_SENTINEL_PORT2} ||= 6391;
19 $ENV{TEST_NGINX_SENTINEL_PORT3} ||= 6392;
1520
1621 no_long_string();
1722 run_tests();
2530 content_by_lua_block {
2631 local rc = require("resty.redis.connector").new()
2732
28 local sentinel, err = rc:connect{ url = "redis://127.0.0.1:6381" }
29 assert(sentinel and not err, "sentinel should connect without errors")
33 local sentinel, err = rc:connect{ url = "redis://127.0.0.1:$TEST_NGINX_SENTINEL_PORT1" }
34 assert(sentinel and not err, "sentinel should connect without errors but got " .. tostring(err))
3035
3136 local master, err = require("resty.redis.sentinel").get_master(
3237 sentinel,
3540
3641 assert(master and not err, "get_master should return the master")
3742
38 assert(master.host == "127.0.0.1" and tonumber(master.port) == 6379,
39 "host should be 127.0.0.1 and port should be 6379")
43 assert(master.host == "127.0.0.1" and tonumber(master.port) == $TEST_NGINX_REDIS_PORT,
44 "host should be 127.0.0.1 and port should be $TEST_NGINX_REDIS_PORT")
4045
4146 sentinel:close()
4247 }
5762 local master, err = rc:connect({
5863 url = "sentinel://mymaster:m/3",
5964 sentinels = {
60 { host = "127.0.0.1", port = 6381 }
65 { host = "127.0.0.1", port = $TEST_NGINX_SENTINEL_PORT1 }
6166 }
6267 })
6368
8186 content_by_lua_block {
8287 local rc = require("resty.redis.connector").new()
8388
84 local sentinel, err = rc:connect{ url = "redis://127.0.0.1:6381" }
89 local sentinel, err = rc:connect{ url = "redis://127.0.0.1:$TEST_NGINX_SENTINEL_PORT1" }
8590 assert(sentinel and not err, "sentinel should connect without error")
8691
8792 local slaves, err = require("resty.redis.sentinel").get_slaves(
9196
9297 assert(slaves and not err, "slaves should be returned without error")
9398
94 local slaveports = { ["6378"] = false, ["6380"] = false }
99 local slaveports = { ["$TEST_NGINX_REDIS_PORT_SL1"] = false, ["$TEST_NGINX_REDIS_PORT_SL2"] = false }
95100
96101 for _,slave in ipairs(slaves) do
97102 slaveports[tostring(slave.port)] = true
98103 end
99104
100 assert(slaveports["6378"] == true and slaveports["6380"] == true,
105 assert(slaveports["$TEST_NGINX_REDIS_PORT_SL1"] == true and slaveports["$TEST_NGINX_REDIS_PORT_SL2"] == true,
101106 "slaves should both be found")
102107
103108 sentinel:close()
116121 content_by_lua_block {
117122 local rc = require("resty.redis.connector").new()
118123
119 local sentinel, err = rc:connect({ url = "redis://127.0.0.1:6381" })
124 local sentinel, err = rc:connect({ url = "redis://127.0.0.1:$TEST_NGINX_SENTINEL_PORT1" })
120125 assert(sentinel and not err, "sentinel should connect without error")
121126
122127 local slaves, err = require("resty.redis.sentinel").get_slaves(
126131
127132 assert(slaves and not err, "slaves should be returned without error")
128133
129 local slaveports = { ["6378"] = false, ["6380"] = false }
134 local slaveports = { ["$TEST_NGINX_REDIS_PORT_SL1"] = false, ["$TEST_NGINX_REDIS_PORT_SL2"] = false }
130135
131136 for _,slave in ipairs(slaves) do
132137 slaveports[tostring(slave.port)] = true
133138 end
134139
135 assert(slaveports["6378"] == true and slaveports["6380"] == true,
140 assert(slaveports["$TEST_NGINX_REDIS_PORT_SL1"] == true and slaveports["$TEST_NGINX_REDIS_PORT_SL2"] == true,
136141 "slaves should both be found")
137142
138143 -- connect to one and remove it
139144 local r = require("resty.redis.connector").new():connect({
140 port = 6378,
145 port = $TEST_NGINX_REDIS_PORT_SL1,
141146 })
142147 r:slaveof("127.0.0.1", 7000)
143148
150155
151156 assert(slaves and not err, "slaves should be returned without error")
152157
153 local slaveports = { ["6378"] = false, ["6380"] = false }
158 local slaveports = { ["$TEST_NGINX_REDIS_PORT_SL1"] = false, ["$TEST_NGINX_REDIS_PORT_SL2"] = false }
154159
155160 for _,slave in ipairs(slaves) do
156161 slaveports[tostring(slave.port)] = true
157162 end
158163
159 assert(slaveports["6378"] == false and slaveports["6380"] == true,
160 "only 6380 should be found")
161
162 r:slaveof("127.0.0.1", 6379)
164 assert(slaveports["$TEST_NGINX_REDIS_PORT_SL1"] == false and slaveports["$TEST_NGINX_REDIS_PORT_SL2"] == true,
165 "only $TEST_NGINX_REDIS_PORT_SL2 should be found")
166
167 r:slaveof("127.0.0.1", $TEST_NGINX_REDIS_PORT)
163168
164169 sentinel:close()
165170 }
180185
181186 local params = {
182187 sentinels = {
183 { host = "127.0.0.1", port = 6381 },
184 { host = "127.0.0.1", port = 6382 },
185 { host = "127.0.0.1", port = 6383 },
188 { host = "127.0.0.1", port = $TEST_NGINX_SENTINEL_PORT1 },
189 { host = "127.0.0.1", port = $TEST_NGINX_SENTINEL_PORT2 },
190 { host = "127.0.0.1", port = $TEST_NGINX_SENTINEL_PORT3 },
186191 },
187192 master_name = "mymaster",
188193 role = "master",
213218
214219 local params = {
215220 sentinels = {
216 { host = "127.0.0.1", port = 6381 },
217 { host = "127.0.0.1", port = 6382 },
218 { host = "127.0.0.1", port = 6383 },
221 { host = "127.0.0.1", port = $TEST_NGINX_SENTINEL_PORT1 },
222 { host = "127.0.0.1", port = $TEST_NGINX_SENTINEL_PORT2 },
223 { host = "127.0.0.1", port = $TEST_NGINX_SENTINEL_PORT3 },
219224 },
220225 master_name = "mymaster",
221226 role = "slave",
225230 local sentinel = require("resty.redis.sentinel")
226231 sentinel.get_slaves = function()
227232 return {
228 { host = "127.0.0.1", port = 6380 },
229 { host = "127.0.0.1", port = 6378 },
230 { host = "127.0.0.1", port = 6377 },
231 { host = "134.123.51.2", port = 6380 },
233 { host = "127.0.0.1", port = $TEST_NGINX_REDIS_PORT_SL1 },
234 { host = "127.0.0.1", port = $TEST_NGINX_REDIS_PORT_SL2 },
235 { host = "134.123.51.2", port = $TEST_NGINX_REDIS_PORT_SL1 },
232236 }
233237 end
234238
3939 close $in;
4040
4141 #print "Checking use of Lua global variables in file $file ...\n";
42 system("luac5.1 -p -l $file | grep ETGLOBAL | grep -vE '(require|type|tostring|error|ngx|ndk|jit|setmetatable|getmetatable|string|table|io|os|print|tonumber|math|pcall|xpcall|unpack|pairs|ipairs|assert|module|package|coroutine|[gs]etfenv|next|select|rawset|rawget|debug)\$'");
42 system("luac -p -l $file | grep ETGLOBAL | grep -vE '(require|type|tostring|error|ngx|ndk|jit|setmetatable|getmetatable|string|table|io|os|print|tonumber|math|pcall|xpcall|unpack|pairs|ipairs|assert|module|package|coroutine|[gs]etfenv|next|select|rawset|rawget|debug)\$'");
4343 #file_contains($file, "attempt to write to undeclared variable");
4444 system("grep -H -n -E --color '.{120}' $file");
4545 }