From b41d5bb05b35c5d52f9e87d8d96660719e85896a Mon Sep 17 00:00:00 2001 From: Zijing Zhang <50045289+pluveto@users.noreply.github.com> Date: Mon, 17 Apr 2023 22:29:27 +0800 Subject: [PATCH] tests(integration): add missing /status api tests (#10629) * fix: add tests * tests(status_api): fixed tests * tests(integration) wait until changes are applied to avoid flakiness * fix(integration): make cassandra tests happy * fix format * Update 03-config_persistence_spec.lua --------- Co-authored-by: Vinicius Mignot Co-authored-by: Guilherme Salazar --- .ci/run_tests.sh | 4 +- .../08-status_api/01-core_routes_spec.lua | 194 ++++++++++-------- .../11-dbless/03-config_persistence_spec.lua | 64 +++--- 3 files changed, 143 insertions(+), 119 deletions(-) diff --git a/.ci/run_tests.sh b/.ci/run_tests.sh index 865e5bfd6371..cb8595b32fbf 100755 --- a/.ci/run_tests.sh +++ b/.ci/run_tests.sh @@ -52,7 +52,9 @@ if [ "$TEST_SUITE" == "dbless" ]; then eval "$TEST_CMD" spec/02-integration/02-cmd \ spec/02-integration/05-proxy \ spec/02-integration/04-admin_api/02-kong_routes_spec.lua \ - spec/02-integration/04-admin_api/15-off_spec.lua + spec/02-integration/04-admin_api/15-off_spec.lua \ + spec/02-integration/08-status_api/01-core_routes_spec.lua \ + spec/02-integration/11-dbless fi if [ "$TEST_SUITE" == "plugins" ]; then set +ex diff --git a/spec/02-integration/08-status_api/01-core_routes_spec.lua b/spec/02-integration/08-status_api/01-core_routes_spec.lua index 72623cabcdd6..992e4bd778c5 100644 --- a/spec/02-integration/08-status_api/01-core_routes_spec.lua +++ b/spec/02-integration/08-status_api/01-core_routes_spec.lua @@ -3,25 +3,28 @@ local cjson = require "cjson" for _, strategy in helpers.all_strategies() do -describe("Status API - with strategy #" .. strategy, function() - local client +describe("Status API #" .. strategy, function() lazy_setup(function() - helpers.get_db_utils(nil, {}) -- runs migrations + helpers.get_db_utils(strategy, { + "plugins", + "routes", + "services", + }) assert(helpers.start_kong { status_listen = "127.0.0.1:9500", plugins = "admin-api-method", + database = strategy, }) - client = helpers.http_client("127.0.0.1", 9500, 20000) end) lazy_teardown(function() - if client then client:close() end helpers.stop_kong() end) describe("core", function() it("/status returns status info with blank configuration_hash (declarative config) or without it (db mode)", function() + local client = helpers.http_client("127.0.0.1", 9500, 20000) local res = assert(client:send { method = "GET", path = "/status" @@ -30,9 +33,7 @@ describe("Status API - with strategy #" .. strategy, function() local json = cjson.decode(body) assert.is_table(json.database) assert.is_table(json.server) - assert.is_boolean(json.database.reachable) - assert.is_number(json.server.connections_accepted) assert.is_number(json.server.connections_active) assert.is_number(json.server.connections_handled) @@ -45,102 +46,123 @@ describe("Status API - with strategy #" .. strategy, function() else assert.is_nil(json.configuration_hash) -- not present in DB mode end + client:close() end) - - it("/status starts providing a config_hash once an initial configuration has been pushed in dbless mode #off", function() - -- push an initial configuration so that a configuration_hash will be present - local postres = assert(client:send { - method = "POST", - path = "/config", - body = { - config = [[ - _format_version: "1.1" - services: - - host = "konghq.com" - ]], - }, - headers = { - ["Content-Type"] = "application/json" - } - }) - assert.res_status(201, postres) - - local res = assert(client:send { - method = "GET", - path = "/status" - }) - local body = assert.res_status(200, res) - local json = cjson.decode(body) - assert.is_table(json.database) - assert.is_table(json.server) - assert.is_boolean(json.database.reachable) - assert.is_number(json.server.connections_accepted) - assert.is_number(json.server.connections_active) - assert.is_number(json.server.connections_handled) - assert.is_number(json.server.connections_reading) - assert.is_number(json.server.connections_writing) - assert.is_number(json.server.connections_waiting) - assert.is_number(json.server.total_requests) - assert.is_string(json.configuration_hash) - assert.equal(32, #json.configuration_hash) - end) - end) + describe("plugins", function() it("can add endpoints", function() - local res = assert(client:send { + local client = helpers.http_client("127.0.0.1", 9500, 20000) + local res = assert(client:send({ method = "GET", path = "/hello" - }) + })) local body = assert.res_status(200, res) local json = cjson.decode(body) assert.same(json, { hello = "from status api" }) + client:close() end) end) end) -end -for _, strategy in helpers.all_strategies() do - describe("Status API - with strategy #" .. strategy, function() - local h2_client - - lazy_setup(function() - helpers.get_db_utils(nil, {}) -- runs migrations - assert(helpers.start_kong { - status_listen = "127.0.0.1:9500 ssl http2", - plugins = "admin-api-method", - }) - h2_client = helpers.http2_client("127.0.0.1", 9500, true) - print("h2_client = ", require("inspect")(h2_client)) - end) +describe("Status API #" .. strategy, function() + local h2_client - lazy_teardown(function() - helpers.stop_kong() - end) + lazy_setup(function() + helpers.get_db_utils(strategy, {}) + assert(helpers.start_kong({ + status_listen = "127.0.0.1:9500 ssl http2", + })) + h2_client = helpers.http2_client("127.0.0.1", 9500, true) + end) - it("supports HTTP/2 #test", function() - local res, headers = assert(h2_client { - headers = { - [":method"] = "GET", - [":path"] = "/status", - [":authority"] = "127.0.0.1:9500", - }, - }) - local json = cjson.decode(res) + lazy_teardown(function() + helpers.stop_kong() + end) - assert.equal('200', headers:get ":status") + it("supports HTTP/2", function() + local res, headers = assert(h2_client({ + headers = { + [":method"] = "GET", + [":path"] = "/status", + [":authority"] = "127.0.0.1:9500", + }, + })) + local json = cjson.decode(res) + + assert.equal('200', headers:get(":status")) + + assert.is_table(json.database) + assert.is_boolean(json.database.reachable) + + assert.is_number(json.server.connections_accepted) + assert.is_number(json.server.connections_active) + assert.is_number(json.server.connections_handled) + assert.is_number(json.server.connections_reading) + assert.is_number(json.server.connections_writing) + assert.is_number(json.server.connections_waiting) + assert.is_number(json.server.total_requests) + end) +end) +end - assert.is_table(json.database) - assert.is_boolean(json.database.reachable) +describe("/status provides config_hash", function() + lazy_setup(function() + helpers.get_db_utils("off", { + "plugins", + "services", + }) + assert(helpers.start_kong { + status_listen = "127.0.0.1:9500", + database = "off", + }) + end) - assert.is_number(json.server.connections_accepted) - assert.is_number(json.server.connections_active) - assert.is_number(json.server.connections_handled) - assert.is_number(json.server.connections_reading) - assert.is_number(json.server.connections_writing) - assert.is_number(json.server.connections_waiting) - assert.is_number(json.server.total_requests) - end) + lazy_teardown(function() + helpers.stop_kong() end) -end + + it("once an initial configuration has been pushed in dbless mode #off", function() + local admin_client = helpers.http_client("127.0.0.1", 9001) + -- push an initial configuration so that a configuration_hash will be present + local postres = assert(admin_client:send { + method = "POST", + path = "/config", + body = { + config = [[ +_format_version: "3.0" +services: + - name: example-service + url: http://example.test +]], + }, + headers = { + ["Content-Type"] = "application/json" + } + }) + assert.res_status(201, postres) + admin_client:close() + local client = helpers.http_client("127.0.0.1", 9500) + local res = assert(client:send { + method = "GET", + path = "/status" + }) + local body = assert.res_status(200, res) + local json = cjson.decode(body) + assert.is_table(json.database) + assert.is_table(json.server) + assert.is_boolean(json.database.reachable) + assert.is_number(json.server.connections_accepted) + assert.is_number(json.server.connections_active) + assert.is_number(json.server.connections_handled) + assert.is_number(json.server.connections_reading) + assert.is_number(json.server.connections_writing) + assert.is_number(json.server.connections_waiting) + assert.is_number(json.server.total_requests) + assert.is_string(json.configuration_hash) + assert.equal(32, #json.configuration_hash) + client:close() + end) +end) + diff --git a/spec/02-integration/11-dbless/03-config_persistence_spec.lua b/spec/02-integration/11-dbless/03-config_persistence_spec.lua index 334b7f3ef994..a9c63bdaf672 100644 --- a/spec/02-integration/11-dbless/03-config_persistence_spec.lua +++ b/spec/02-integration/11-dbless/03-config_persistence_spec.lua @@ -14,20 +14,13 @@ local SERVICE_YML = [[ ]] describe("dbless persistence #off", function() - local admin_client, proxy_client - lazy_setup(function() assert(helpers.start_kong({ - database = "off", + database = "off", })) - - admin_client = assert(helpers.admin_client()) - proxy_client = assert(helpers.proxy_client()) end) lazy_teardown(function() - admin_client:close() - proxy_client:close() helpers.stop_kong(nil, true) end) @@ -38,32 +31,34 @@ describe("dbless persistence #off", function() end local config = table.concat(buffer, "\n") + local admin_client = assert(helpers.admin_client()) local res = admin_client:post("/config",{ body = { config = config }, headers = { - ["Content-Type"] = "application/json" + ["Content-Type"] = "application/json", } }) assert.res_status(201, res) + admin_client:close() assert(helpers.restart_kong({ - database = "off", + database = "off", })) - proxy_client:close() - proxy_client = assert(helpers.proxy_client()) + local proxy_client = assert(helpers.proxy_client()) res = assert(proxy_client:get("/1", { headers = { host = "example1.dev" } })) assert.res_status(401, res) res = assert(proxy_client:get("/1000", { headers = { host = "example1.dev" } })) assert.res_status(401, res) + proxy_client:close() assert.logfile().has.line("found persisted lmdb config") end) end) describe("dbless persistence with a declarative config #off", function() - local admin_client, proxy_client, yaml_file + local yaml_file lazy_setup(function() yaml_file = helpers.make_yaml_file([[ @@ -82,39 +77,42 @@ describe("dbless persistence with a declarative config #off", function() before_each(function() assert(helpers.start_kong({ - database = "off", + database = "off", declarative_config = yaml_file, })) - admin_client = assert(helpers.admin_client()) - proxy_client = assert(helpers.proxy_client()) + local admin_client = assert(helpers.admin_client()) + local proxy_client = assert(helpers.proxy_client()) local res = assert(proxy_client:get("/test", { headers = { host = "example1.dev" } })) assert.res_status(401, res) + proxy_client:close() local buffer = {"_format_version: '1.1'", "services:"} local i = 500 buffer[#buffer + 1] = fmt(SERVICE_YML, i, i, i, i) local config = table.concat(buffer, "\n") - local res = admin_client:post("/config",{ + local res = admin_client:post("/config", { body = { config = config }, headers = { - ["Content-Type"] = "application/json" + ["Content-Type"] = "application/json", } }) assert.res_status(201, res) - res = assert(proxy_client:get("/500", { headers = { host = "example1.dev" } })) - assert.res_status(401, res) + admin_client:close() - proxy_client:close() + assert + .with_timeout(5) + .eventually(function() + proxy_client = assert(helpers.proxy_client()) + res = proxy_client:get("/500", { headers = { host = "example1.dev" } }) + res:read_body() + proxy_client:close() + return res and res.status == 401 + end) + .is_truthy() end) after_each(function() - if admin_client then - admin_client:close() - end - if proxy_client then - proxy_client:close() - end helpers.stop_kong(nil, true) end) lazy_teardown(function() @@ -123,32 +121,34 @@ describe("dbless persistence with a declarative config #off", function() it("doesn't load the persisted lmdb config if a declarative config is set on restart", function() assert(helpers.restart_kong({ - database = "off", + database = "off", declarative_config = yaml_file, })) - proxy_client = assert(helpers.proxy_client()) + local proxy_client = assert(helpers.proxy_client()) local res = assert(proxy_client:get("/test", { headers = { host = "example1.dev" } })) assert.res_status(401, res) res = assert(proxy_client:get("/500", { headers = { host = "example1.dev" } })) assert.res_status(404, res) -- 404, only the declarative config is loaded + proxy_client:close() end) it("doesn't load the persisted lmdb config if a declarative config is set on reload", function() assert(helpers.reload_kong("off", "reload --prefix " .. helpers.test_conf.prefix, { - database = "off", + database = "off", declarative_config = yaml_file, })) local res helpers.wait_until(function() - proxy_client = assert(helpers.proxy_client()) + local proxy_client = assert(helpers.proxy_client()) res = assert(proxy_client:get("/test", { headers = { host = "example1.dev" } })) proxy_client:close() return res.status == 401 end) - proxy_client = assert(helpers.proxy_client()) + local proxy_client = assert(helpers.proxy_client()) res = assert(proxy_client:get("/500", { headers = { host = "example1.dev" } })) assert.res_status(404, res) -- 404, only the declarative config is loaded + proxy_client:close() end) end)