From a3b9aeeb6e367f764b40b1f6b0111acca6757618 Mon Sep 17 00:00:00 2001 From: chronolaw Date: Wed, 5 Feb 2025 11:07:26 +0800 Subject: [PATCH] tests(clustering/rpc): meta: required field missing --- .../18-hybrid_rpc/10-validate_deltas_spec.lua | 75 +++++++++++++++++++ .../plugins/rpc-validation-test/handler.lua | 41 ++++++++++ .../plugins/rpc-validation-test/schema.lua | 12 +++ 3 files changed, 128 insertions(+) create mode 100644 spec/02-integration/18-hybrid_rpc/10-validate_deltas_spec.lua create mode 100644 spec/fixtures/custom_plugins/kong/plugins/rpc-validation-test/handler.lua create mode 100644 spec/fixtures/custom_plugins/kong/plugins/rpc-validation-test/schema.lua diff --git a/spec/02-integration/18-hybrid_rpc/10-validate_deltas_spec.lua b/spec/02-integration/18-hybrid_rpc/10-validate_deltas_spec.lua new file mode 100644 index 00000000000..6374e77a06b --- /dev/null +++ b/spec/02-integration/18-hybrid_rpc/10-validate_deltas_spec.lua @@ -0,0 +1,75 @@ +local rep = string.rep +local helpers = require "spec.helpers" + + +-- register a rpc connected event in custom plugin rpc-validation-test +-- DISABLE rpc sync on cp side +-- ENABLE rpc sync on dp side +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-validation-test", + nginx_worker_processes = 4, -- multiple workers + cluster_rpc = "on", -- enable rpc + cluster_rpc_sync = "off", -- disable rpc 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-validation-test", + nginx_worker_processes = 4, -- multiple workers + cluster_rpc = "on", -- enable rpc + cluster_rpc_sync = "on", -- enable rpc sync + })) + end) + + lazy_teardown(function() + helpers.stop_kong("servroot2") + helpers.stop_kong() + end) + + describe("sync.v2 validation works", function() + it("on dp side", function() + local name = "servroot2/logs/error.log" + + -- dp logs + assert.logfile(name).has.line( + "[error]", true, 10) + assert.logfile(name).has.line( + "unable to create worker mutex and sync", true, 10) + assert.logfile(name).has.line( + "'meta': required field missing", true, 10) + assert.logfile(name).has.line( + "'config': required field missing", true, 10) + + local name = nil + + -- cp logs + assert.logfile(name).has.line( + "kong.sync.v2.get_delta ok", true, 10) + assert.logfile(name).has.no.line( + "[error]", true, 0) + + end) + end) + end) +end -- for _, strategy diff --git a/spec/fixtures/custom_plugins/kong/plugins/rpc-validation-test/handler.lua b/spec/fixtures/custom_plugins/kong/plugins/rpc-validation-test/handler.lua new file mode 100644 index 00000000000..b4f33aa6424 --- /dev/null +++ b/spec/fixtures/custom_plugins/kong/plugins/rpc-validation-test/handler.lua @@ -0,0 +1,41 @@ +local fmt = string.format +local rep = string.rep + + +local RpcSyncV2ValidationHandler = { + VERSION = "1.0", + PRIORITY = 1000, +} + + +function RpcSyncV2ValidationHandler:init_worker() + -- mock function on cp side + kong.rpc.callbacks:register("kong.sync.v2.get_delta", function(node_id, current_versions) + local latest_version = fmt("v02_%028x", 10) + + local fake_uuid = "00000000-0000-0000-0000-111111111111" + + -- a basic config data, + -- it has no field "config" or "meta", + -- and will cause validation error + local deltas = { + { + entity = { + id = fake_uuid, + name = "default", + }, + type = "workspaces", + version = latest_version, + ws_id = fake_uuid, + }, + } + + ngx.log(ngx.DEBUG, "kong.sync.v2.get_delta ok: ", counter) + + return { default = { deltas = deltas, wipe = true, }, } + end) + +end + + +return RpcSyncV2ValidationHandler diff --git a/spec/fixtures/custom_plugins/kong/plugins/rpc-validation-test/schema.lua b/spec/fixtures/custom_plugins/kong/plugins/rpc-validation-test/schema.lua new file mode 100644 index 00000000000..c68c1023ef7 --- /dev/null +++ b/spec/fixtures/custom_plugins/kong/plugins/rpc-validation-test/schema.lua @@ -0,0 +1,12 @@ +return { + name = "rpc-validation-test", + fields = { + { + config = { + type = "record", + fields = { + }, + }, + }, + }, +}