From 7026271281c194ff79b47cd486d6bc1d3671bdf4 Mon Sep 17 00:00:00 2001 From: Xiaochen Wang Date: Tue, 26 Nov 2024 14:29:45 +0800 Subject: [PATCH] use bp.*:insert to insert routes --- .../11-dbless/04-pagination_spec.lua | 74 +++++++++---------- .../dbless-pagination-test/handler.lua | 9 ++- 2 files changed, 40 insertions(+), 43 deletions(-) diff --git a/spec/02-integration/11-dbless/04-pagination_spec.lua b/spec/02-integration/11-dbless/04-pagination_spec.lua index dca4cd129db5..bcda8b940123 100644 --- a/spec/02-integration/11-dbless/04-pagination_spec.lua +++ b/spec/02-integration/11-dbless/04-pagination_spec.lua @@ -1,70 +1,64 @@ -local fmt = string.format local helpers = require "spec.helpers" -local SERVICE_YML = [[ -- name: my-service-%d - url: https://example%d.dev - plugins: - - name: dbless-pagination-test - routes: - - name: my-route-%d - paths: - - /%d -]] +local strategy = "off" - -describe("dbless pagination #off", function() - local client +describe("dbless pagination #" .. strategy, function() + local client, admin_client lazy_setup(function() + local bp = helpers.get_db_utils(strategy, { + "routes", + "services", + "plugins", + }) + + bp.plugins:insert { + name = "dbless-pagination-test", + config = { }, + } + + for i = 1, 1001 do + local service = assert(bp.services:insert { + url = "https://example1.dev", + name = "my-serivce-" .. i, + }) + + assert(bp.routes:insert({ + paths = { "/" .. i }, + service = service, + name = "my-route-" .. i, + })) + end + assert(helpers.start_kong({ nginx_conf = "spec/fixtures/custom_nginx.template", - database = "off", + database = strategy, plugins = "bundled,dbless-pagination-test", })) print("helpers.start_kong") client = assert(helpers.proxy_client()) + admin_client = assert(helpers.admin_client()) end) lazy_teardown(function() + admin_client:close() client:close() helpers.stop_kong() end) it("Routes", function() - local buffer = {"_format_version: '3.0'", "services:"} - for i = 1, 1001 do - buffer[#buffer + 1] = fmt(SERVICE_YML, i, i, i, i) - 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", - } - }) - assert.res_status(201, res) - admin_client:close() local res = admin_client:get("/routes/my-route-1") - print(res:read_body()) - print("-------") - - -- check routes number with :page() API - --local res, err = client:get("/1", {}) + assert.res_status(200, res) local res = assert(client:send { method = "GET", path = "/1", }) - --print(require("inspect")(res)) - local resbody = res:read_body() - print"--------" - print(resbody) - --assert.response(res).has.header("X-rows-number", "test") + assert.res_status(200, res) + assert.same(res.headers["X-Rows-Number"], "512") + assert.same(res.headers["X-Max-Page-Size"], "2048") end) end) diff --git a/spec/fixtures/custom_plugins/kong/plugins/dbless-pagination-test/handler.lua b/spec/fixtures/custom_plugins/kong/plugins/dbless-pagination-test/handler.lua index ff346ce91abb..786ff0e8a5fe 100644 --- a/spec/fixtures/custom_plugins/kong/plugins/dbless-pagination-test/handler.lua +++ b/spec/fixtures/custom_plugins/kong/plugins/dbless-pagination-test/handler.lua @@ -19,10 +19,13 @@ function LmdbPaginationTestHandler:init_worker() end function LmdbPaginationTestHandler:access(conf) - ngx.log(ngx.INFO, "xxxxxxxxxxxxxx") - local rows, err, err_t, offset = kong.db.routes:page() + local rows, _, _, offset = kong.db.routes:page() + ngx.header["X-Rows-Number"] = #rows + ngx.header["X-rows-offset"] = tostring(offset) - ngx.header["X-rows-number"] = #rows + ngx.header["X-Max-Page-Size"] = kong.db.routes.pagination.max_page_size + + ngx.exit(200) end