Skip to content

Commit

Permalink
fixed tests and resty-events usage
Browse files Browse the repository at this point in the history
  • Loading branch information
locao committed Jun 10, 2022
1 parent 084029d commit 960a031
Show file tree
Hide file tree
Showing 48 changed files with 4,877 additions and 9 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/build_and_test_with_resty_events.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
runs-on: ubuntu-20.04
strategy:
matrix:
openresty-version: [1.15.8.3]
openresty-version: [1.15.8.3, 1.17.8.2, 1.19.9.1, 1.21.4.1]

steps:
- name: Update and install OS dependencies
Expand Down Expand Up @@ -121,7 +121,7 @@ jobs:
if: steps.cache-deps.outputs.cache-hit != 'true'
run: |
cd lua-resty-events
OPENRESTY_PREFIX=$INSTALL_ROOT/openresty PREFIX=$INSTALL_ROOT make install
OPENRESTY_PREFIX=$INSTALL_ROOT PREFIX=$INSTALL_ROOT LUA_LIB_DIR=$INSTALL_ROOT/lualib make install
- name: Install lua-resty-healthcheck
run: luarocks make
Expand All @@ -133,4 +133,4 @@ jobs:
run: |
eval `luarocks path`
eval $(perl -I $HOME/perl5/lib/perl5/ -Mlocal::lib)
TEST_NGINX_RANDOMIZE=1 prove -I. -r t
TEST_NGINX_RANDOMIZE=1 prove -I. -r t/with_resty-events
64 changes: 64 additions & 0 deletions .github/workflows/build_and_test_with_worker_events.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
name: Build and test

on: [push, pull_request]

jobs:
build:
name: CI using lua-resty-worker-events
runs-on: ubuntu-20.04
strategy:
matrix:
openresty-version: [1.15.8.3, 1.17.8.2, 1.19.9.1, 1.21.4.1]

steps:
- name: Update and install OS dependencies
run: |
sudo apt-get update && sudo apt-get install -y libssl-dev ssl-cert
sudo systemctl disable nginx
sudo systemctl stop nginx
- name: Set environment variables
env:
OPENRESTY_VER: ${{ matrix.openresty-version }}
run: |
echo "/usr/local/openresty/nginx/sbin" >> $GITHUB_PATH
- name: Checkout lua-resty-healthcheck
uses: actions/checkout@v3

- name: Install OpenResty ${{ matrix.openresty-version }}
env:
OPENRESTY_VER: ${{ matrix.openresty-version }}
run: |
sudo apt-get -y install --no-install-recommends wget gnupg ca-certificates
wget -O - https://openresty.org/package/pubkey.gpg | sudo apt-key add -
echo "deb http://openresty.org/package/ubuntu $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/openresty.list
sudo apt-get update
sudo apt-get -y install openresty=$OPENRESTY_VER-1~focal1
- name: Install LuaRocks
run: sudo apt-get install -y luarocks

- name: Install manual dependencies
run: |
sudo luarocks install luacheck
sudo luarocks install lua-resty-worker-events 1.0.0
- name: Install Test::NGINX
run: |
sudo apt-get install cpanminus
cpanm --notest --local-lib=$HOME/perl5 local::lib && eval $(perl -I $HOME/perl5/lib/perl5/ -Mlocal::lib)
cpanm --notest Test::Nginx
- name: Checkout lua-resty-healthcheck
uses: actions/checkout@v3

- name: Install lua-resty-healthcheck
run: sudo luarocks make

- name: Run tests
run: |
eval `luarocks path`
eval $(perl -I $HOME/perl5/lib/perl5/ -Mlocal::lib)
TEST_NGINX_RANDOMIZE=1 prove -I. -r t/with_worker-events
6 changes: 4 additions & 2 deletions lib/resty/healthcheck.lua
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,6 @@ local function load_events_module(module_name)
assert(worker_events, "could not load lua-resty-worker-events")
assert(worker_events._VERSION == RESTY_WORKER_EVENTS_VER,
"unsupported lua-resty-worker-events version")
assert(worker_events.configured(), "please configure the " ..
"'lua-resty-worker-events' module before using 'lua-resty-healthcheck'")

elseif module_name == "resty.events" then
worker_events = require("resty.events.compat")
Expand All @@ -110,6 +108,10 @@ local function load_events_module(module_name)
error("unknown events module")
end

module_name = module_name or "events"
assert(worker_events.configured(), "please configure the '" ..
module_name .. "' module before using 'lua-resty-healthcheck'")

end


Expand Down
229 changes: 229 additions & 0 deletions t/with_resty-events/00-new.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
use Test::Nginx::Socket::Lua;
use Cwd qw(cwd);

workers(1);

plan tests => repeat_each() * (blocks() * 3) - 2;

my $pwd = cwd();
$ENV{TEST_NGINX_SERVROOT} = server_root();

our $HttpConfig = qq{
lua_package_path "$pwd/lib/?.lua;;";
lua_shared_dict test_shm 8m;

server {
server_name kong_worker_events;
listen unix:$ENV{TEST_NGINX_SERVROOT}/worker_events.sock;
access_log off;
location / {
content_by_lua_block {
require("resty.events.compat").run()
}
}
}
};

run_tests();

__DATA__

=== TEST 1: new() requires worker_events to be configured
--- http_config eval: $::HttpConfig
--- config
location = /t {
content_by_lua_block {
local healthcheck = require("resty.healthcheck")
local ok, err = pcall(healthcheck.new, {
events_module = "resty.events",
})
ngx.log(ngx.ERR, err)
}
}
--- request
GET /t
--- response_body

--- error_log
please configure

=== TEST 2: new() requires 'name'
--- http_config eval: $::HttpConfig
--- config
location = /t {
content_by_lua_block {
local we = require "resty.events.compat"
assert(we.configure({ unique_timeout = 5, broker_id = 0, listening = "unix:" .. ngx.config.prefix() .. "worker_events.sock" }))
local healthcheck = require("resty.healthcheck")
local ok, err = pcall(healthcheck.new, {
events_module = "resty.events",
shm_name = "test_shm",
})
ngx.log(ngx.ERR, err)
}
}
--- request
GET /t
--- response_body

--- error_log
required option 'name' is missing

=== TEST 3: new() fails with invalid shm
--- http_config eval: $::HttpConfig
--- config
location = /t {
content_by_lua_block {
local we = require "resty.events.compat"
assert(we.configure({ unique_timeout = 5, broker_id = 0, listening = "unix:" .. ngx.config.prefix() .. "worker_events.sock" }))
local healthcheck = require("resty.healthcheck")
local ok, err = pcall(healthcheck.new, {
name = "testing",
shm_name = "invalid_shm",
events_module = "resty.events",
})
ngx.log(ngx.ERR, err)
}
}
--- request
GET /t
--- response_body

--- error_log
no shm found by name

=== TEST 4: new() initializes with default config
--- http_config eval: $::HttpConfig
--- config
location = /t {
content_by_lua_block {
local we = require "resty.events.compat"
assert(we.configure({ unique_timeout = 5, broker_id = 0, listening = "unix:" .. ngx.config.prefix() .. "worker_events.sock" }))
local healthcheck = require("resty.healthcheck")
local ok, err = pcall(healthcheck.new, {
name = "testing",
shm_name = "test_shm",
events_module = "resty.events",
})
}
}
--- request
GET /t
--- response_body

--- error_log
Healthchecker started!

=== TEST 5: new() only accepts http or tcp types
--- http_config eval: $::HttpConfig
--- config
location = /t {
content_by_lua_block {
local we = require "resty.events.compat"
assert(we.configure({ unique_timeout = 5, broker_id = 0, listening = "unix:" .. ngx.config.prefix() .. "worker_events.sock" }))
local healthcheck = require("resty.healthcheck")
local ok, err = pcall(healthcheck.new, {
name = "testing",
shm_name = "test_shm",
events_module = "resty.events",
type = "http",
})
ngx.say(ok)
local ok, err = pcall(healthcheck.new, {
name = "testing",
shm_name = "test_shm",
events_module = "resty.events",
type = "tcp",
})
ngx.say(ok)
local ok, err = pcall(healthcheck.new, {
name = "testing",
shm_name = "test_shm",
events_module = "resty.events",
type = "get lost",
})
ngx.say(ok)
}
}
--- request
GET /t
--- response_body
true
true
false

=== TEST 6: new() deals with bad inputs
--- http_config eval: $::HttpConfig
--- config
location = /t {
content_by_lua_block {
local we = require "resty.events.compat"
assert(we.configure({ unique_timeout = 5, broker_id = 0, listening = "unix:" .. ngx.config.prefix() .. "worker_events.sock" }))
local healthcheck = require("resty.healthcheck")

-- tests for failure
local tests = {
{ active = { timeout = -1 }},
{ active = { timeout = 1e+42 }},
{ active = { concurrency = -1 }},
{ active = { concurrency = 1e42 }},
{ active = { healthy = { interval = -1 }}},
{ active = { healthy = { interval = 1e42 }}},
{ active = { healthy = { successes = -1 }}},
{ active = { healthy = { successes = 1e42 }}},
{ active = { unhealthy = { interval = -1 }}},
{ active = { unhealthy = { interval = 1e42 }}},
{ active = { unhealthy = { tcp_failures = -1 }}},
{ active = { unhealthy = { tcp_failures = 1e42 }}},
{ active = { unhealthy = { timeouts = -1 }}},
{ active = { unhealthy = { timeouts = 1e42 }}},
{ active = { unhealthy = { http_failures = -1 }}},
{ active = { unhealthy = { http_failures = 1e42 }}},
{ passive = { healthy = { successes = -1 }}},
{ passive = { healthy = { successes = 1e42 }}},
{ passive = { unhealthy = { tcp_failures = -1 }}},
{ passive = { unhealthy = { tcp_failures = 1e42 }}},
{ passive = { unhealthy = { timeouts = -1 }}},
{ passive = { unhealthy = { timeouts = 1e42 }}},
{ passive = { unhealthy = { http_failures = -1 }}},
{ passive = { unhealthy = { http_failures = 1e42 }}},
}
for _, test in ipairs(tests) do
local ok, err = pcall(healthcheck.new, {
name = "testing",
shm_name = "test_shm",
events_module = "resty.events",
type = "http",
checks = test,
})
ngx.say(ok)
end
}
}
--- request
GET /t
--- response_body
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
Loading

0 comments on commit 960a031

Please sign in to comment.