Skip to content

Commit

Permalink
cfg: allow uri of any type, not only string
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Gerold103 committed May 10, 2022
1 parent 0931671 commit 0407483
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 6 deletions.
44 changes: 44 additions & 0 deletions test/unit-luatest/config_test.lua
Original file line number Diff line number Diff line change
@@ -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
6 changes: 3 additions & 3 deletions test/unit/config.result
Original file line number Diff line number Diff line change
Expand Up @@ -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'
---
Expand Down Expand Up @@ -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'
---
Expand Down
2 changes: 1 addition & 1 deletion test/unit/config.test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
8 changes: 6 additions & 2 deletions vshard/cfg.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 = {
Expand Down

0 comments on commit 0407483

Please sign in to comment.