From 74efadcdd65ca245ff69ca568eeba0d6c8b2f386 Mon Sep 17 00:00:00 2001 From: chronolaw Date: Fri, 10 Jan 2025 16:48:27 +0800 Subject: [PATCH] tests(clustering/rpc): add cases for notifications --- .../18-hybrid_rpc/07-notification_spec.lua | 78 +++++++++++++++++++ .../plugins/rpc-notification-test/handler.lua | 37 +++++++++ .../plugins/rpc-notification-test/schema.lua | 12 +++ 3 files changed, 127 insertions(+) create mode 100644 spec/02-integration/18-hybrid_rpc/07-notification_spec.lua create mode 100644 spec/fixtures/custom_plugins/kong/plugins/rpc-notification-test/handler.lua create mode 100644 spec/fixtures/custom_plugins/kong/plugins/rpc-notification-test/schema.lua diff --git a/spec/02-integration/18-hybrid_rpc/07-notification_spec.lua b/spec/02-integration/18-hybrid_rpc/07-notification_spec.lua new file mode 100644 index 000000000000..55e948abd66f --- /dev/null +++ b/spec/02-integration/18-hybrid_rpc/07-notification_spec.lua @@ -0,0 +1,78 @@ +local helpers = require "spec.helpers" + + +-- register a test rpc service in custom plugin rpc-notification-test +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,rpc-notification-test", + nginx_worker_processes = 4, -- multiple workers + cluster_rpc = "on", -- enable rpc + cluster_incremental_sync = "off", -- disable incremetanl sync + })) + + 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-notification-test", + nginx_worker_processes = 4, -- multiple workers + cluster_rpc = "on", -- enable rpc + cluster_incremental_sync = "off", -- disable incremetanl sync + })) + end) + + lazy_teardown(function() + helpers.stop_kong("servroot2") + helpers.stop_kong() + end) + + describe("notification works", function() + it("in custom plugin", function() + -- cp logs + helpers.pwait_until(function() + assert.logfile().has.line( + "notification is hello", true) + assert.logfile().has.line( + "[rpc] notifying kong.test.notification(node_id:", true) + assert.logfile().has.line( + "[rpc] notification has no response", true) + assert.logfile().has.no.line( + "assert failed", true) + return true + end, 15) + + -- dp logs + helpers.pwait_until(function() + assert.logfile("servroot2/logs/error.log").has.line( + "[rpc] notifying kong.test.notification(node_id: control_plane) via local", true) + assert.logfile("servroot2/logs/error.log").has.line( + "notification is world", true) + assert.logfile("servroot2/logs/error.log").has.line( + "[rpc] notification has no response", true) + assert.logfile("servroot2/logs/error.log").has.no.line( + "assert failed", true) + return true + end, 15) + + end) + end) + end) +end -- for _, strategy diff --git a/spec/fixtures/custom_plugins/kong/plugins/rpc-notification-test/handler.lua b/spec/fixtures/custom_plugins/kong/plugins/rpc-notification-test/handler.lua new file mode 100644 index 000000000000..c8877b5f6fea --- /dev/null +++ b/spec/fixtures/custom_plugins/kong/plugins/rpc-notification-test/handler.lua @@ -0,0 +1,37 @@ +local RpcNotificationTestHandler = { + VERSION = "1.0", + PRIORITY = 1000, +} + + +function RpcNotificationTestHandler:init_worker() + kong.rpc.callbacks:register("kong.test.notification", function(node_id, msg) + ngx.log(ngx.DEBUG, "notification is ", msg) + + local role = kong.configuration.role + + -- cp notify dp back + if role == "control_plane" then + local res, err = kong.rpc:notify(node_id, "kong.test.notification", "world") + assert(res == true) + assert(err == nil) + end + + -- perr should not get this by notification + return role + end) + + local worker_events = assert(kong.worker_events) + + -- if rpc is ready we will start to notify + worker_events.register(function(capabilities_list) + -- dp notify cp + local res, err = kong.rpc:notify("control_plane", "kong.test.notification", "hello") + + assert(res == true) + assert(err == nil) + end, "clustering:jsonrpc", "connected") +end + + +return RpcNotificationTestHandler diff --git a/spec/fixtures/custom_plugins/kong/plugins/rpc-notification-test/schema.lua b/spec/fixtures/custom_plugins/kong/plugins/rpc-notification-test/schema.lua new file mode 100644 index 000000000000..886dc7eb1234 --- /dev/null +++ b/spec/fixtures/custom_plugins/kong/plugins/rpc-notification-test/schema.lua @@ -0,0 +1,12 @@ +return { + name = "rpc-notification-test", + fields = { + { + config = { + type = "record", + fields = { + }, + }, + }, + }, +}