From 1e9650a0f91450b1e938d734c596f559fc6660c5 Mon Sep 17 00:00:00 2001 From: Elvin Efendi Date: Thu, 10 Dec 2020 22:41:41 -0500 Subject: [PATCH] fix flaky lua tests --- .../etc/nginx/lua/test/configuration_test.lua | 8 ++-- rootfs/etc/nginx/lua/test/monitor_test.lua | 38 ++++++++++++++++++- 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/rootfs/etc/nginx/lua/test/configuration_test.lua b/rootfs/etc/nginx/lua/test/configuration_test.lua index ce00980b3..64a048929 100644 --- a/rootfs/etc/nginx/lua/test/configuration_test.lua +++ b/rootfs/etc/nginx/lua/test/configuration_test.lua @@ -6,7 +6,7 @@ local certificate_data = ngx.shared.certificate_data local certificate_servers = ngx.shared.certificate_servers local ocsp_response_cache = ngx.shared.ocsp_response_cache -function get_backends() +local function get_backends() return { { name = "my-dummy-backend-1", ["load-balance"] = "sticky", @@ -30,7 +30,7 @@ function get_backends() } end -function get_mocked_ngx_env() +local function get_mocked_ngx_env() local _ngx = { status = ngx.HTTP_OK, var = {}, @@ -281,9 +281,7 @@ describe("Configuration", function() local s = spy.on(ngx, "log") assert.has_no.errors(configuration.handle_servers) - assert.spy(s).was_called_with(ngx.ERR, - string.format("error setting certificate for %s: error\nerror setting certificate for %s: error\n", UUID, uuid2)) - assert.same(ngx.status, ngx.HTTP_INTERNAL_SERVER_ERROR) + assert.same(ngx.HTTP_INTERNAL_SERVER_ERROR, ngx.status) end) it("logs a warning when entry is forcibly stored", function() diff --git a/rootfs/etc/nginx/lua/test/monitor_test.lua b/rootfs/etc/nginx/lua/test/monitor_test.lua index ca6dbd663..ea1bff4d3 100644 --- a/rootfs/etc/nginx/lua/test/monitor_test.lua +++ b/rootfs/etc/nginx/lua/test/monitor_test.lua @@ -1,3 +1,4 @@ +local cjson = require("cjson.safe") local original_ngx = ngx local function reset_ngx() @@ -107,7 +108,42 @@ describe("Monitor", function() monitor.flush() - local expected_payload = '[{"requestLength":256,"ingress":"example","status":"200","service":"http-svc","requestTime":0.04,"namespace":"default","host":"example.com","method":"GET","upstreamResponseTime":0.02,"upstreamResponseLength":456,"upstreamLatency":0.01,"path":"\\/","responseLength":512},{"requestLength":256,"ingress":"example","status":"201","service":"http-svc","requestTime":0.04,"namespace":"default","host":"example.com","method":"POST","upstreamResponseTime":0.02,"upstreamResponseLength":456,"upstreamLatency":0.01,"path":"\\/","responseLength":512}]' + local expected_payload = cjson.encode({ + { + host = "example.com", + namespace = "default", + ingress = "example", + service = "http-svc", + path = "/", + + method = "GET", + status = "200", + requestLength = 256, + requestTime = 0.04, + responseLength = 512, + + upstreamLatency = 0.01, + upstreamResponseTime = 0.02, + upstreamResponseLength = 456, + }, + { + host = "example.com", + namespace = "default", + ingress = "example", + service = "http-svc", + path = "/", + + method = "POST", + status = "201", + requestLength = 256, + requestTime = 0.04, + responseLength = 512, + + upstreamLatency = 0.01, + upstreamResponseTime = 0.02, + upstreamResponseLength = 456, + }, + }) assert.stub(tcp_mock.connect).was_called_with(tcp_mock, "unix:/tmp/prometheus-nginx.socket") assert.stub(tcp_mock.send).was_called_with(tcp_mock, expected_payload)