From a278f2bb90c28a27beb7e3fea7ade296998d42c6 Mon Sep 17 00:00:00 2001 From: chronolaw Date: Tue, 14 Jan 2025 13:07:50 +0800 Subject: [PATCH 1/4] tests(clustering/rpc): add cases for sync.v2.get_delta --- .../08-sync_v2_get_delta_spec.lua | 76 +++++++++++++++++++ .../plugins/rpc-get-delta-test/handler.lua | 60 +++++++++++++++ .../plugins/rpc-get-delta-test/schema.lua | 12 +++ 3 files changed, 148 insertions(+) create mode 100644 spec/02-integration/18-hybrid_rpc/08-sync_v2_get_delta_spec.lua create mode 100644 spec/fixtures/custom_plugins/kong/plugins/rpc-get-delta-test/handler.lua create mode 100644 spec/fixtures/custom_plugins/kong/plugins/rpc-get-delta-test/schema.lua diff --git a/spec/02-integration/18-hybrid_rpc/08-sync_v2_get_delta_spec.lua b/spec/02-integration/18-hybrid_rpc/08-sync_v2_get_delta_spec.lua new file mode 100644 index 000000000000..47b073ce52a7 --- /dev/null +++ b/spec/02-integration/18-hybrid_rpc/08-sync_v2_get_delta_spec.lua @@ -0,0 +1,76 @@ +local helpers = require "spec.helpers" + + +for _, strategy in helpers.each_strategy() do + describe("Hybrid Mode RPC #" .. strategy, function() + + lazy_setup(function() + helpers.get_db_utils(strategy, { + "clustering_data_planes", + }) -- runs migrations + + assert(helpers.start_kong({ + role = "control_plane", + cluster_cert = "spec/fixtures/kong_clustering.crt", + cluster_cert_key = "spec/fixtures/kong_clustering.key", + database = strategy, + cluster_listen = "127.0.0.1:9005", + nginx_conf = "spec/fixtures/custom_nginx.template", + plugins = "bundled", + nginx_worker_processes = 4, -- multiple workers + cluster_rpc = "on", -- enable rpc + cluster_rpc_sync = "on", -- enable rpc sync + })) + + -- register a rpc connected event in custom plugin rpc-get-delta-test + assert(helpers.start_kong({ + role = "data_plane", + database = "off", + prefix = "servroot2", + cluster_cert = "spec/fixtures/kong_clustering.crt", + cluster_cert_key = "spec/fixtures/kong_clustering.key", + cluster_control_plane = "127.0.0.1:9005", + proxy_listen = "0.0.0.0:9002", + nginx_conf = "spec/fixtures/custom_nginx.template", + plugins = "bundled,rpc-get-delta-test", + nginx_worker_processes = 4, -- multiple workers + cluster_rpc = "on", -- enable rpc + cluster_rpc_sync = "off", -- disable rpc sync + })) + end) + + lazy_teardown(function() + helpers.stop_kong("servroot2") + helpers.stop_kong() + end) + + describe("sync.v2.get_delta works", function() + it("in cp side", function() + local name = "servroot2/logs/error.log" + + -- dp logs + helpers.pwait_until(function() + assert.logfile(name).has.line( + "kong.sync.v2.get_delta ok", true) + assert.logfile(name).has.no.line( + "assert failed", true) + assert.logfile(name).has.no.line( + "[error]", true) + return true + end, 10) + + local name = nil + + -- cp logs + helpers.pwait_until(function() + assert.logfile(name).has.no.line( + "assert failed", true) + assert.logfile(name).has.no.line( + "[error]", true) + return true + end, 10) + + end) + end) + end) +end -- for _, strategy diff --git a/spec/fixtures/custom_plugins/kong/plugins/rpc-get-delta-test/handler.lua b/spec/fixtures/custom_plugins/kong/plugins/rpc-get-delta-test/handler.lua new file mode 100644 index 000000000000..cbe33f92bf35 --- /dev/null +++ b/spec/fixtures/custom_plugins/kong/plugins/rpc-get-delta-test/handler.lua @@ -0,0 +1,60 @@ +local rep = string.rep +local isempty = require("table.isempty") + + +local RpcSyncV2GetDeltaTestHandler = { + VERSION = "1.0", + PRIORITY = 1000, +} + + +function RpcSyncV2GetDeltaTestHandler:init_worker() + local worker_events = assert(kong.worker_events) + + -- if rpc is ready we will send test calls + -- cp's version now is "v02_00000" + worker_events.register(function(capabilities_list) + local node_id = "control_plane" + local method = "kong.sync.v2.get_delta" + + -- no field `default` for kong.sync.v2.get_delta + local msg = {} + local res, err = kong.rpc:call(node_id, method, msg) + + assert(not res) + assert(err == "default namespace does not exist inside params") + + -- version is invalid + local msg = { default = { version = rep("A", 32), }, } + local res, err = kong.rpc:call(node_id, method, msg) + + assert(type(res) == "table") + assert(not isempty(res.default.deltas)) + assert(res.default.wipe == true) + assert(not err) + + -- dp's version is greater than cp's version + local msg = { default = { version = "v02_" .. rep("A", 28), }, } + local res, err = kong.rpc:call(node_id, method, msg) + + assert(type(res) == "table") + assert(not isempty(res.default.deltas)) + assert(res.default.wipe == true) + assert(not err) + + -- dp's version is equal to cp's version + local msg = { default = { version = "v02_" .. rep("0", 28), }, } + local res, err = kong.rpc:call(node_id, method, msg) + + assert(type(res) == "table") + assert(isempty(res.default.deltas)) + assert(res.default.wipe == false) + assert(not err) + + ngx.log(ngx.DEBUG, "kong.sync.v2.get_delta ok") + + end, "clustering:jsonrpc", "connected") +end + + +return RpcSyncV2GetDeltaTestHandler diff --git a/spec/fixtures/custom_plugins/kong/plugins/rpc-get-delta-test/schema.lua b/spec/fixtures/custom_plugins/kong/plugins/rpc-get-delta-test/schema.lua new file mode 100644 index 000000000000..8be21169f700 --- /dev/null +++ b/spec/fixtures/custom_plugins/kong/plugins/rpc-get-delta-test/schema.lua @@ -0,0 +1,12 @@ +return { + name = "rpc-get-delta-test", + fields = { + { + config = { + type = "record", + fields = { + }, + }, + }, + }, +} From b646fd40f0e678fcf4b9493061c8cddc17aaf46a Mon Sep 17 00:00:00 2001 From: chronolaw Date: Tue, 14 Jan 2025 13:21:45 +0800 Subject: [PATCH 2/4] comments --- .../18-hybrid_rpc/08-sync_v2_get_delta_spec.lua | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/spec/02-integration/18-hybrid_rpc/08-sync_v2_get_delta_spec.lua b/spec/02-integration/18-hybrid_rpc/08-sync_v2_get_delta_spec.lua index 47b073ce52a7..447988397ebe 100644 --- a/spec/02-integration/18-hybrid_rpc/08-sync_v2_get_delta_spec.lua +++ b/spec/02-integration/18-hybrid_rpc/08-sync_v2_get_delta_spec.lua @@ -1,6 +1,9 @@ local helpers = require "spec.helpers" +-- register a rpc connected event in custom plugin rpc-get-delta-test +-- ENABLE rpc sync on cp side for testing sync.v2.get_delta +-- DISABLE rpc sync on dp side for _, strategy in helpers.each_strategy() do describe("Hybrid Mode RPC #" .. strategy, function() @@ -22,7 +25,6 @@ for _, strategy in helpers.each_strategy() do cluster_rpc_sync = "on", -- enable rpc sync })) - -- register a rpc connected event in custom plugin rpc-get-delta-test assert(helpers.start_kong({ role = "data_plane", database = "off", From a2f80ae493353f645aa7ca1c034bd882abbe7776 Mon Sep 17 00:00:00 2001 From: chronolaw Date: Wed, 15 Jan 2025 12:27:40 +0800 Subject: [PATCH 3/4] assertion failed --- .../18-hybrid_rpc/08-sync_v2_get_delta_spec.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/02-integration/18-hybrid_rpc/08-sync_v2_get_delta_spec.lua b/spec/02-integration/18-hybrid_rpc/08-sync_v2_get_delta_spec.lua index 447988397ebe..05320eff4497 100644 --- a/spec/02-integration/18-hybrid_rpc/08-sync_v2_get_delta_spec.lua +++ b/spec/02-integration/18-hybrid_rpc/08-sync_v2_get_delta_spec.lua @@ -55,7 +55,7 @@ for _, strategy in helpers.each_strategy() do assert.logfile(name).has.line( "kong.sync.v2.get_delta ok", true) assert.logfile(name).has.no.line( - "assert failed", true) + "assertion failed", true) assert.logfile(name).has.no.line( "[error]", true) return true @@ -66,7 +66,7 @@ for _, strategy in helpers.each_strategy() do -- cp logs helpers.pwait_until(function() assert.logfile(name).has.no.line( - "assert failed", true) + "assertion failed", true) assert.logfile(name).has.no.line( "[error]", true) return true From d54f13246b3c700e4ef11488089d9739bd3d4897 Mon Sep 17 00:00:00 2001 From: chronolaw Date: Wed, 15 Jan 2025 12:32:47 +0800 Subject: [PATCH 4/4] fix 07-notification_spec.lua --- spec/02-integration/18-hybrid_rpc/07-notification_spec.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/02-integration/18-hybrid_rpc/07-notification_spec.lua b/spec/02-integration/18-hybrid_rpc/07-notification_spec.lua index 18d5eb1cb4e5..2f49243181d6 100644 --- a/spec/02-integration/18-hybrid_rpc/07-notification_spec.lua +++ b/spec/02-integration/18-hybrid_rpc/07-notification_spec.lua @@ -57,7 +57,7 @@ for _, strategy in helpers.each_strategy() do assert.logfile(name).has.line( "[rpc] notification has no response", true) assert.logfile(name).has.no.line( - "assert failed", true) + "assertion failed", true) return true end, 10) @@ -72,7 +72,7 @@ for _, strategy in helpers.each_strategy() do assert.logfile(name).has.line( "[rpc] notification has no response", true) assert.logfile(name).has.no.line( - "assert failed", true) + "assertion failed", true) return true end, 10)