Skip to content

Commit

Permalink
tests(integration): add missing /status api tests (#10629)
Browse files Browse the repository at this point in the history
* 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 <vinicius.mignot@gmail.com>
Co-authored-by: Guilherme Salazar <gsz@acm.org>
  • Loading branch information
3 people committed Apr 17, 2023
1 parent 8ca656d commit b41d5bb
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 119 deletions.
4 changes: 3 additions & 1 deletion .ci/run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
194 changes: 108 additions & 86 deletions spec/02-integration/08-status_api/01-core_routes_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand All @@ -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)

Loading

1 comment on commit b41d5bb

@khcp-gha-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bazel Build

Docker image available kong/kong:b41d5bb05b35c5d52f9e87d8d96660719e85896a
Artifacts available https://github.com/Kong/kong/actions/runs/4722385662

Please sign in to comment.