Skip to content

Commit 9c4931a

Browse files
committed
feat: add Dockerfile and test suite for memory leak detection
1 parent 4f11c6a commit 9c4931a

File tree

6 files changed

+166
-0
lines changed

6 files changed

+166
-0
lines changed

.memory_leak/Dockerfile

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
FROM alpine:3.21.2 as libjwt-builder
2+
3+
WORKDIR /home/app
4+
RUN apk add --no-cache git cmake make gcc g++ jansson-dev openssl-dev
5+
RUN git clone --depth 1 --branch v3.2.0 https://github.com/benmcollins/libjwt.git && \
6+
mkdir libjwt/build && \
7+
cd libjwt/build && cmake .. && make && make install
8+
9+
FROM openresty/openresty:1.25.3.2-3-bullseye-valgrind
10+
11+
RUN apt update
12+
RUN apt install -y cpanminus valgrind libjansson-dev # Substituí apk por apt
13+
RUN cpanm -v --notest Test::Nginx
14+
15+
COPY ./lib/resty/libjwt /usr/local/openresty/lualib/resty/libjwt
16+
COPY --from=libjwt-builder /usr/local/lib/libjwt.so /usr/local/lib/libjwt.so
17+
RUN ldconfig # Atualiza cache do linker para garantir que libjwt seja encontrada
18+
19+
COPY ./.memory_leak/test /t
20+
COPY ./.memory_leak/valgrind.suppress /valgrind.suppress
21+
COPY ./.memory_leak/valgrind.awk /valgrind.awk
22+
COPY ./.memory_leak/test_memory_leak.sh /test_memory_leak.sh
23+
CMD ["/bin/bash", "-c", "/test_memory_leak.sh"]

.memory_leak/test/teste.t

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# vim:set ft= ts=4 sw=4 et fdm=marker:
2+
3+
use Test::Nginx::Socket::Lua;
4+
5+
repeat_each(10);
6+
7+
plan tests => blocks() * repeat_each() * 2;
8+
9+
run_tests();
10+
11+
__DATA__
12+
13+
=== TEST 1: sanity (integer)
14+
--- config
15+
location /lua {
16+
echo 2;
17+
}
18+
--- request
19+
GET /lua
20+
--- response_body
21+
2
22+
23+
=== TEST 2: sanity (string)
24+
--- config
25+
location /public {
26+
default_type application/json;
27+
return 200 '{"message": "Hello, World!"}\n';
28+
}
29+
--- request
30+
GET /public
31+
--- response_body
32+
{"message": "Hello, World!"}
33+
34+
=== TEST 3: sanity (string)
35+
--- config
36+
location = /t {
37+
content_by_lua_block {
38+
local ffi = require("ffi");
39+
40+
-- Define the C functions we need
41+
ffi.cdef[[
42+
void* malloc(size_t size);
43+
void free(void* ptr);
44+
]]
45+
46+
local leak_size = 1024 * 1024
47+
-- local ptr = ffi.C.malloc(leak_size);
48+
ngx.say("testing the tsuru")
49+
}
50+
}
51+
--- request
52+
GET /t
53+
--- response_body
54+
testing the tsuru

.memory_leak/test_memory_leak.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/bash
2+
3+
echo "Running tests without valgrind"
4+
TEST_NGINX_USE_VALGRIND=1 prove -r t 2>&1 | tee /tmp/result.TEST_NGINX_USE_VALGRIND
5+
awk -f /valgrind.awk /tmp/result.TEST_NGINX_USE_VALGRIND > /tmp/has-valgrind.log
6+
if [[ -s /tmp/has-valgrind.log ]]; then
7+
cat /tmp/has-valgrind.log >&2
8+
exit 1
9+
fi
10+
echo "No valgrind errors found"
11+
exit 0

.memory_leak/valgrind.awk

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/awk -f
2+
3+
BEGIN {
4+
sep
5+
while (getline) {
6+
if ($0 ~ /^==[[:digit:]]+==\s*$/) {
7+
sep = "\n\n\n"
8+
} else if ($0 ~ /^==[[:digit:]]+==/) {
9+
printf "%s%s\n", sep, $0
10+
sep = ""
11+
}
12+
}
13+
}

.memory_leak/valgrind.suppress

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
{
2+
<insert_a_suppression_name_here>
3+
Memcheck:Param
4+
epoll_ctl(event)
5+
fun:epoll_ctl
6+
obj:/usr/local/openresty-valgrind/nginx/sbin/nginx
7+
obj:/usr/local/openresty-valgrind/nginx/sbin/nginx
8+
obj:/usr/local/openresty-valgrind/nginx/sbin/nginx
9+
fun:ngx_single_process_cycle
10+
fun:main
11+
}
12+
{
13+
<insert_a_suppression_name_here>
14+
Memcheck:Leak
15+
match-leak-kinds: definite
16+
fun:malloc
17+
fun:ngx_alloc
18+
fun:ngx_set_environment
19+
fun:ngx_single_process_cycle
20+
fun:main
21+
}
22+
{
23+
<insert_a_suppression_name_here>
24+
Memcheck:Param
25+
epoll_ctl(event)
26+
fun:epoll_ctl
27+
obj:/usr/local/openresty-valgrind/nginx/sbin/nginx
28+
fun:ngx_handle_read_event
29+
fun:ngx_http_init_connection
30+
fun:ngx_event_accept
31+
obj:/usr/local/openresty-valgrind/nginx/sbin/nginx
32+
fun:ngx_process_events_and_timers
33+
fun:ngx_single_process_cycle
34+
fun:main
35+
}
36+
{
37+
<insert_a_suppression_name_here>
38+
Memcheck:Param
39+
epoll_ctl(event)
40+
fun:epoll_ctl
41+
obj:/usr/local/openresty-valgrind/nginx/sbin/nginx
42+
fun:ngx_handle_read_event
43+
fun:ngx_http_lua_pipe_add_signal_handler
44+
fun:ngx_http_lua_init_worker
45+
fun:ngx_single_process_cycle
46+
fun:main
47+
}
48+
{
49+
<insert_a_suppression_name_here>
50+
Memcheck:Param
51+
epoll_ctl(event)
52+
fun:epoll_ctl
53+
obj:/usr/local/openresty-valgrind/nginx/sbin/nginx
54+
obj:/usr/local/openresty-valgrind/nginx/sbin/nginx
55+
fun:ngx_single_process_cycle
56+
fun:main
57+
}

docker-compose.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
services:
22
nginx:
3+
profiles:
4+
- dev
35
build:
46
context: .
57
dockerfile: Dockerfile.nginx
68
ports:
79
- "8888:8888"
810
volumes:
911
- ./tokens:/usr/share/tokens
12+
memory_leak:
13+
profiles:
14+
- memory_leak
15+
build:
16+
context: .
17+
dockerfile: ./.memory_leak/Dockerfile

0 commit comments

Comments
 (0)