From 0407483cd17fe931fa03389e02026a7322313bb0 Mon Sep 17 00:00:00 2001 From: Vladislav Shpilevoy Date: Fri, 6 May 2022 23:19:06 +0200 Subject: [PATCH] cfg: allow uri of any type, not only string Since 2.10.0-beta2 URI in all APIs can be passed not only as a string or number but also as a table. The table can be used to pass options like transport type (plain, SSL), encryption certificate and key, and potentially more. VShard always supported only string URIs but now it allows numbers and tables as well. In the config the replica_object.uri field is affected by the change. Part of #325 --- test/unit-luatest/config_test.lua | 44 +++++++++++++++++++++++++++++++ test/unit/config.result | 6 ++--- test/unit/config.test.lua | 2 +- vshard/cfg.lua | 8 ++++-- 4 files changed, 54 insertions(+), 6 deletions(-) create mode 100644 test/unit-luatest/config_test.lua diff --git a/test/unit-luatest/config_test.lua b/test/unit-luatest/config_test.lua new file mode 100644 index 00000000..d74b7ce7 --- /dev/null +++ b/test/unit-luatest/config_test.lua @@ -0,0 +1,44 @@ +local t = require('luatest') +local lcfg = require('vshard.cfg') +local uuid = require('uuid') + +local g = t.group('config') + +g.test_basic_uri = function() + local url = 'storage:storage@127.0.0.1:3301' + local storage_1_a = { + uri = url, + name = 'storage_1_a', + } + local config = { + sharding = { + storage_1_uuid = { + replicas = { + storage_1_a_uuid = storage_1_a, + } + }, + }, + } + t.assert(lcfg.check(config), 'normal uri') + + storage_1_a.uri = {url} + t.assert(lcfg.check(config), 'table uri') + + storage_1_a.uri = {url, params = {transport = 'plain'}} + t.assert(lcfg.check(config), 'uri with options') + + storage_1_a.uri = 3301 + t.assert(lcfg.check(config), 'number uri') + + storage_1_a.uri = 'bad uri ###' + t.assert_error(lcfg.check, config) + + storage_1_a.uri = {} + t.assert_error(lcfg.check, config) + + storage_1_a.uri = nil + t.assert_error(lcfg.check, config) + + storage_1_a.uri = uuid.new() + t.assert_error(lcfg.check, config) +end diff --git a/test/unit/config.result b/test/unit/config.result index 3ddac10f..3b749a7b 100644 --- a/test/unit/config.result +++ b/test/unit/config.result @@ -45,12 +45,12 @@ check(cfg) --- - URI must be specified ... -replica.uri = 100 +replica.uri = {} --- ... check(cfg) --- -- URI must be non-empty string +- URI must be a non-empty string, or a number, or a table ... replica.uri = 'uri:uri@uri' --- @@ -467,7 +467,7 @@ replica.uri = 'invalid uri' ... util.check_error(lcfg.check, cfg) --- -- 'Invalid URI: invalid uri' +- URI must be a non-empty string, or a number, or a table ... replica.uri = '127.0.0.1' --- diff --git a/test/unit/config.test.lua b/test/unit/config.test.lua index 11d126bc..da246028 100644 --- a/test/unit/config.test.lua +++ b/test/unit/config.test.lua @@ -24,7 +24,7 @@ cfg = {sharding = {['replicaset_uuid'] = replicaset}} -- URI is not string. check(cfg) -replica.uri = 100 +replica.uri = {} check(cfg) replica.uri = 'uri:uri@uri' diff --git a/vshard/cfg.lua b/vshard/cfg.lua index 3a96ec31..f820b541 100644 --- a/vshard/cfg.lua +++ b/vshard/cfg.lua @@ -6,7 +6,7 @@ local consts = require('vshard.consts') local function check_uri(uri) if not luri.parse(uri) then - error('Invalid URI: ' .. uri) + error('URI must be a non-empty string, or a number, or a table') end end @@ -113,7 +113,11 @@ local function validate_config(config, template, check_arg) end local replica_template = { - uri = {type = 'non-empty string', name = 'URI', check = check_uri}, + uri = { + type = {'non-empty string', 'number', 'table'}, + name = 'URI', + check = check_uri + }, name = {type = 'string', name = "Name", is_optional = true}, zone = {type = {'string', 'number'}, name = "Zone", is_optional = true}, master = {