ingress-nginx-helm/rootfs/etc/nginx/lua/test/monitor_test.lua

97 lines
3.5 KiB
Lua
Raw Normal View History

2018-06-14 02:54:09 +00:00
_G._TEST = true
2018-07-22 02:36:05 +00:00
local cjson = require("cjson")
2018-06-14 02:54:09 +00:00
describe("Monitor", function()
local monitor = require("monitor")
describe("encode_nginx_stats()", function()
it("successfuly encodes the current stats of nginx to JSON", function()
local nginx_environment = {
host = "testshop.com",
status = "200",
bytes_sent = "150",
server_protocol = "HTTP",
request_method = "GET",
location_path = "/admin",
2018-06-14 02:54:09 +00:00
request_length = "300",
2018-07-07 17:46:18 +00:00
request_time = "210",
2018-06-14 02:54:09 +00:00
proxy_upstream_name = "test-upstream",
upstream_addr = "2.2.2.2",
upstream_response_time = "200",
2018-07-07 17:46:18 +00:00
upstream_response_length = "150",
upstream_connect_time = "1",
2018-06-14 02:54:09 +00:00
upstream_status = "220",
namespace = "test-app-production",
ingress_name = "web-yml",
service_name = "test-app",
}
ngx.var = nginx_environment
local encode_nginx_stats = monitor.encode_nginx_stats
local encoded_json_stats = encode_nginx_stats()
local decoded_json_stats = cjson.decode(encoded_json_stats)
local expected_json_stats = {
host = "testshop.com",
status = "200",
2018-07-07 17:46:18 +00:00
responseLength = 150.0,
2018-06-14 02:54:09 +00:00
method = "GET",
path = "/admin",
2018-06-14 02:54:09 +00:00
requestLength = 300.0,
2018-07-07 17:46:18 +00:00
requestTime = 210.0,
endpoint = "2.2.2.2",
2018-06-14 02:54:09 +00:00
upstreamResponseTime = 200,
upstreamStatus = "220",
2018-07-07 17:46:18 +00:00
upstreamLatency = 1.0,
upstreamResponseLength = 150.0,
2018-06-14 02:54:09 +00:00
namespace = "test-app-production",
ingress = "web-yml",
service = "test-app",
}
assert.are.same(decoded_json_stats,expected_json_stats)
end)
it("replaces empty numeric keys with -1 and missing string keys with -", function()
local nginx_environment = {
remote_addr = "10.10.10.10",
realip_remote_addr = "5.5.5.5",
remote_user = "francisco",
server_protocol = "HTTP",
request_method = "GET",
location_path = "/admin",
2018-07-07 17:46:18 +00:00
request_time = "202",
2018-06-14 02:54:09 +00:00
proxy_upstream_name = "test-upstream",
upstream_addr = "2.2.2.2",
2018-07-07 17:46:18 +00:00
upstream_response_time = "201",
2018-06-14 02:54:09 +00:00
upstream_status = "220",
ingress_name = "web-yml",
}
ngx.var = nginx_environment
local encode_nginx_stats = monitor.encode_nginx_stats
local encoded_json_stats = encode_nginx_stats()
local decoded_json_stats = cjson.decode(encoded_json_stats)
local expected_json_stats = {
host = "-",
status = "-",
2018-07-07 17:46:18 +00:00
responseLength = -1,
2018-06-14 02:54:09 +00:00
method = "GET",
path = "/admin",
2018-06-14 02:54:09 +00:00
requestLength = -1,
2018-07-07 17:46:18 +00:00
requestTime = 202.0,
endpoint = "2.2.2.2",
2018-06-14 02:54:09 +00:00
upstreamStatus = "220",
namespace = "-",
ingress = "web-yml",
2018-07-07 17:46:18 +00:00
upstreamLatency = -1,
upstreamResponseTime = 201,
upstreamResponseLength = -1,
responseLength = -1,
2018-06-14 02:54:09 +00:00
service = "-",
}
assert.are.same(decoded_json_stats,expected_json_stats)
end)
end)
end)