Skip to content

Commit

Permalink
[#115][request] Openresty test examples (#119)
Browse files Browse the repository at this point in the history
* Develop to master (#117)

* new incremental version (#111)

* fixing mocka failing some nginx tests

* version 1.3.0

* update code

* making a centos docker

* add loop script

* working nginx/openresty

* add boiler plate

* small refactoring

* update some more

* fixing issues and adding docker-compose for running

* correct mapping

* adding example

* adding examples

* adding examples

* finished example

* support and example + coverage

* update
  • Loading branch information
atrifan committed Nov 12, 2020
1 parent 1d86cfa commit e9921ee
Show file tree
Hide file tree
Showing 22 changed files with 528 additions and 48 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
.idea/
*.iml
logs/
test-results-openresty.xml
luacov.report.out
luacov.stats.out
coverage_report.xml
coverage_modules.lua
devops
test-results.xml
*.src.rock
Expand Down
7 changes: 7 additions & 0 deletions .luacov.openresty
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
exclude = {
".*test.*$",
".*mocka.*$",
".*5%.1.*$",
".*ngx.*$",
".*lualib.*resty.*$"
}
30 changes: 20 additions & 10 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM alpine:3.4
FROM centos:7

WORKDIR /mocka_space
ARG luarocks=2.4.1
Expand All @@ -17,29 +17,39 @@ ENV COVERALLS_REPO_TOKEN=
ENV DOCS_FOLDER=
ENV LUACHECK_PARAMS=

ADD ./docker /scripts
RUN chmod -R +x /scripts
RUN yum update -y \
&& yum install sudo wget curl make gcc g++ pcre-devel zlib-devel readline-devel lua-devel git geoip-devel \
jq sudo ncurses-libs libc-dev build-base git bash unzip libev libev-devel glibc-devel -y

ADD ./ /tmp/mocka/
#RUN ln -s /usr/bin/lua5.1 /usr/bin/lua

RUN apk update \
&& apk add sudo curl make gcc g++ readline-dev lua5.1 lua5.1-dev git ncurses-libs libc-dev build-base git bash unzip libev libev-dev
ADD ./docker /scripts
ADD ./ /tmp/mocka/
RUN chmod -R +x /scripts

RUN ln -s /usr/bin/lua5.1 /usr/bin/lua
RUN /scripts/lua_rocks.sh

RUN /scripts/lua_rocks.sh \
&& /scripts/luacheck.sh \
RUN ln -s /usr/local/bin/luarocks /usr/bin/luarocks
RUN /scripts/luacheck.sh \
&& /scripts/ldoc.sh

RUN /scripts/openresty.sh

RUN git config --global url."https://".insteadOf git://

RUN sudo luarocks install lua-cjson 2.1.0-1\
&& sudo luarocks install lua-ev \
&& sudo luarocks install luabitop \
&& sudo luarocks install lua-resty-iputils
&& sudo luarocks install lua-resty-iputils \
&& sudo luarocks install lua-resty-http 0.13

RUN /scripts/mocka.sh

RUN rm -rf /tmp/mocka

ENV PATH="/usr/local/openresty/bin:/usr/local/openresty/nginx/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

ADD ./nginx /usr/local/openresty/nginx/conf
RUN mkdir /var/log/nginx

CMD "/scripts/run_tests.sh"
29 changes: 29 additions & 0 deletions config_cobertura.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by trifan.
--- DateTime: 13/08/2018 17:06
---
local coverage_info = require "coverage_modules"
local configuration = {
-- standard luacov configuration keys and values here
cobertura = {
-- this function will be called for each filename in the stats file
-- the function may be used to manipulate the path before the file is
-- processed by the report generator
mangleFile = function(filename)
-- do stuff with the filename here
local words = filename:gmatch("([^/%.)]+)")
local splitPath = {}
for word in words do
table.insert(splitPath, word)
end

if coverage_info.modules[splitPath[#splitPath - 1]] then
return coverage_info.modules[splitPath[#splitPath - 1]]
end

return filename
end
}
}
return configuration
1 change: 1 addition & 0 deletions coverage_modules.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

21 changes: 21 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
version: '2.1'
services:
mocka_openresty:
mem_limit: 2048m
image: mocka:latest
entrypoint: /scripts/start_nginx.sh
privileged: true
cap_add:
- SYS_PTRACE
volumes:
- ./nginx:/usr/local/openresty/nginx/conf
- ./example/openresty:/usr/local/openresty/nginx/conf/tests
- ./logs:/var/log/nginx
- ./test-results-openresty.xml:/mocka_space/test-results.xml
- ./coverage_report.xml:/mocka_space/coverage_report.xml
- ./.luacov.openresty:/mocka_space/.luacov
- ./luacov.stats.out:/mocka_space/luacov.stats.out
- ./luacov.report.out:/mocka_space/luacov.report.out
- ./config_cobertura.lua:/mocka_space/config_cobertura.lua
- ./coverage_modules.lua:/mocka_space/coverage_modules.lua
- ./cores:/tmp/cores
18 changes: 18 additions & 0 deletions docker/loop.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash

echo "This is a idle script (infinite loop) to keep container running."
echo "Please replace this script."

cleanup ()
{
kill -s SIGTERM $!
exit 0
}

trap cleanup SIGINT SIGTERM

while [ 1 ]
do
sleep 60 &
wait $!
done
8 changes: 8 additions & 0 deletions docker/openresty.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash
wget https://openresty.org/package/centos/openresty.repo
sudo mv openresty.repo /etc/yum.repos.d/

# update the yum index:
sudo yum check-update

sudo yum install openresty -y
3 changes: 3 additions & 0 deletions docker/start_nginx.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

nginx
13 changes: 13 additions & 0 deletions example/openresty/conf.d/test.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
server {
listen 9191;
server_name test.example.adobe;
access_log /var/log/nginx/test.example.adobe_access.log;
error_log /var/log/nginx/test.example.adobe_error.log debug;

location /test {
content_by_lua_block {
local module = require "adobe.module.say":new()
module:say("hello")
}
}
}
40 changes: 40 additions & 0 deletions example/openresty/lua/adobe/example/test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
local http_util = require "mocka.http_util"

return function()
-- order matters - first beforeEach then afterEach
beforeEach(function()
-- do something here
end)
afterEach(function()
-- do something here
end)
test('test spy say negation', function()
spy('adobe.module.say', "say", function(self, what)
ngx.status = 200
ngx.print("not " .. what)
end)

local res, err = http_util:request("127.0.0.1", 9191)
:path("/test")
:header("Host", "test.example.adobe")
:get()

assertNil(err)
assertEquals(res.status, 200)

--test that the spy works
assertEquals(res.body, "not hello")
end)
test('test that a spy is back to normal', function()
local res, err = http_util:request("127.0.0.1", 9191)
:path("/test")
:header("Host", "test.example.adobe")
:get()

assertNil(err)
assertEquals(res.status, 200)

calls(spy("adobe.module.say").say, 1)
assertEquals(res.body, "hello")
end)
end
21 changes: 21 additions & 0 deletions example/openresty/lua/integration_tests.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--
-- Created by IntelliJ IDEA.
-- User: trifan
-- Date: 07/01/17
-- Time: 03:15
-- To change this template use File | Settings | File Templates.
--
local defaultTests = {
"adobe.example.test"
}

return {
["run"] = function(self, tests)
if tests == nil or next(tests) == nil then
tests = defaultTests
end
return require("mocka.ngx_suite")(tests)
end
}


39 changes: 39 additions & 0 deletions nginx/include/entrypoint.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@


lua_package_path '/usr/local/openresty/nginx/conf/lua/?.lua;scripts/?.lua;/usr/local/lib/lua/?.lua;/opt/zbstudio/lualibs/?/?.lua;/opt/zbstudio/lualibs/?.lua;/usr/local/openresty/nginx/conf/tests/lua/?.lua;;';
lua_package_cpath '/opt/zbstudio/bin/linux/x64/clibs/?.so;/usr/local/openresty/nginx/conf/lua/?.so;/usr/local/openresty/nginx/conf/tests/lua/?.so;;';

init_worker_by_lua_file /usr/local/openresty/nginx/conf/lua/worker_init.lua;


server_names_hash_bucket_size 1024;

# Sendfile copies data between one FD and other from within the kernel.
# More efficient than read() + write(), since the requires transferring data to and from the user space.
sendfile on;

# Tcp_nopush causes nginx to attempt to send its HTTP response head in one packet,
# instead of using partial frames. This is useful for prepending headers before calling sendfile,
# or for throughput optimization.
tcp_nopush on;

# Caches information about open FDs, freqently accessed files.
open_file_cache max=200000 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 2;
open_file_cache_errors on;


#define the limit request zone for default locations
limit_req_zone $http_x_forwarded_for zone=free_ip_def:10m rate=10r/s;
client_body_temp_path /var/tmp/client_body_temp 1 2;
proxy_temp_path /var/tmp/proxy_temp;
keepalive_timeout 65;
# allow the server to close the connection after a client stops responding. Frees up socket-associated memory.
reset_timedout_connection on;

default_type application/octet-stream;

variables_hash_max_size 2048;
proxy_headers_hash_max_size 2048;
#gzip on;
29 changes: 29 additions & 0 deletions nginx/include/luaMock.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
server {
listen 9191;
server_name mocka.tests.io;

access_log /var/log/nginx/mocka.log;
error_log /var/log/nginx/mocka_error.log debug ;

# url /run-tests?tests=com.adobe.io.sometest
location /run-tests {
content_by_lua_block {
local tests = ngx.var.arg_tests
if tests == nil or tests == "" then
tests = {}
else
local tests_string = ngx.var.arg_tests
tests = {}
for test_name in tests_string:gmatch("([^,]+)") do
table.insert(tests, test_name)
end
end
local runner = require("luacov.runner")
runner.init()
local integration_tests = require "integration_tests"
local response = integration_tests:run(tests)
ngx.status = 200
ngx.say(response)
}
}
}
15 changes: 15 additions & 0 deletions nginx/lua/adobe/module/say.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
local M = {}

function M:new(o)
o = o or {}
setmetatable(o, self)
self.__index = self
return o
end

function M:say(what)
ngx.status = 200
ngx.print(what)
end

return M
Loading

0 comments on commit e9921ee

Please sign in to comment.