Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse custom net box URI (Unix socket) correctly #323

Merged
merged 3 commits into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions luatest/replica_proxy.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

local checks = require('checks')
local fiber = require('fiber')
local fio = require('fio')
local log = require('log')
local socket = require('socket')
local uri = require('uri')

local utils = require('luatest.utils')
local Connection = require('luatest.replica_conn')
Expand Down Expand Up @@ -98,6 +100,8 @@ function Proxy:start(opts)
os.remove(self.client_socket_path)
end

fio.mktree(fio.dirname(uri.parse(self.client_socket_path).service))

if not self.client_socket:bind('unix/', self.client_socket_path) then
log.error("Failed to bind client socket: %s", self.client_socket:error())
return false
Expand Down
3 changes: 2 additions & 1 deletion luatest/replica_set.lua
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ function ReplicaSet:initialize()
self.alias = 'rs'
self.id = ('%s-%s'):format(self.alias, utils.generate_id())
self.workdir = fio.pathjoin(self._server.vardir, self.id)
fio.mktree(self.workdir)

if self.servers then
local configs = table.deepcopy(self.servers)
Expand Down Expand Up @@ -162,6 +161,8 @@ end
function ReplicaSet:start(opts)
checks('table', {wait_until_ready = '?boolean'})

fio.mktree(self.workdir)

for _, server in ipairs(self.servers) do
if not server.process then
server:start({wait_until_ready = false})
Expand Down
59 changes: 41 additions & 18 deletions luatest/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ end
-- Defaults to 'server'.
-- @string[opt] object.workdir Working directory for the new server and the
-- value of the `TARANTOOL_WORKDIR` env variable which is passed into the
-- server process.
-- server process. The directory path will be created on the server start.
-- Defaults to `<vardir>/<alias>-<random id>`.
-- @string[opt] object.datadir Directory path whose contents will be recursively
-- copied into `object.workdir` during initialization.
-- copied into `object.workdir` on the server start.
-- @number[opt] object.http_port Port for HTTP connection to the new server and
-- the value of the `TARANTOOL_HTTP_PORT` env variable which is passed into
-- the server process.
Expand All @@ -87,7 +87,8 @@ end
-- into the server process.
-- @string[opt] object.net_box_uri URI for the `net.box` connection to the new
-- server and the value of the `TARANTOOL_LISTEN` env variable which is passed
-- into the server process.
-- into the server process. If it is a Unix socket, the corresponding socket
-- directory path will be created on the server start.
-- @tab[opt] object.net_box_credentials Override the default credentials for the
-- `net.box` connection to the new server.
-- @tab[opt] object.box_cfg Extra options for `box.cfg()` and the value of the
Expand Down Expand Up @@ -143,25 +144,17 @@ function Server:initialize()
self.workdir = fio.pathjoin(self.vardir, self.id)
fio.rmtree(self.workdir)
Totktonada marked this conversation as resolved.
Show resolved Hide resolved
end
fio.mktree(self.workdir)

if self.datadir ~= nil then
local ok, err = fio.copytree(self.datadir, self.workdir)
if not ok then
error(('Failed to copy directory: %s'):format(err))
end
self.datadir = nil
end

if self.http_port then
self.http_client = http_client.new()
end

if self.net_box_uri == nil and self.net_box_port == nil then
self.net_box_uri = self.build_listen_uri(self.alias, self.rs_id or self.id)
end
if self.net_box_uri == nil and self.net_box_port then
self.net_box_uri = 'localhost:' .. self.net_box_port
if self.net_box_uri == nil then
if self.net_box_port == nil then
self.net_box_uri = self.build_listen_uri(self.alias, self.rs_id or self.id)
else
self.net_box_uri = 'localhost:' .. self.net_box_port
end
end
if uri.parse(self.net_box_uri).host == 'unix/' then
-- Linux uses max 108 bytes for Unix domain socket paths, which means a 107 characters
Expand All @@ -172,7 +165,6 @@ function Server:initialize()
error(('Net box URI must be <= max Unix domain socket path length (%d chars)')
:format(max_unix_socket_path[system]))
end
fio.mktree(fio.dirname(self.net_box_uri))
end

self.env = utils.merge(self.env or {}, self:build_env())
Expand Down Expand Up @@ -252,6 +244,33 @@ function Server.build_listen_uri(server_alias, extra_path)
return fio.pathjoin(Server.vardir, extra_path or '', server_alias .. '.sock')
end

--- Make the server's working directory.
-- Invoked on the server's start.
function Server:make_workdir()
fio.mktree(self.workdir)
end

--- Copy contents of the data directory into the server's working directory.
-- Invoked on the server's start.
function Server:copy_datadir()
if self.datadir ~= nil then
local ok, err = fio.copytree(self.datadir, self.workdir)
if not ok then
error(('Failed to copy %s to %s: %s'):format(self.datadir, self.workdir, err))
end
self.datadir = nil
end
end

--- Make directory for the server's Unix socket.
-- Invoked on the server's start.
function Server:make_socketdir()
local parsed_net_box_uri = uri.parse(self.net_box_uri)
if parsed_net_box_uri.host == 'unix/' then
fio.mktree(fio.dirname(parsed_net_box_uri.service))
end
end

--- Start a server.
-- Optionally waits until the server is ready.
--
Expand All @@ -264,6 +283,10 @@ function Server:start(opts)

self:initialize()

self:make_workdir()
self:copy_datadir()
self:make_socketdir()

local command = self.command
local args = table.copy(self.args)
local env = table.copy(os.environ())
Expand Down
32 changes: 9 additions & 23 deletions test/replica_set_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,26 @@ local ReplicaSet = require('luatest.replica_set')
local g = t.group()
local Server = t.Server

g.before_all(function()
g.before_each(function()
g.rs = ReplicaSet:new()
g.box_cfg = {
replication_timeout = 0.1,
replication_connect_timeout = 10,
replication_sync_lag = 0.01,
replication_connect_quorum = 3,
replication = {
Server.build_listen_uri('replica1'),
Server.build_listen_uri('replica2'),
Server.build_listen_uri('replica3'),
Server.build_listen_uri('replica1', g.rs.id),
Server.build_listen_uri('replica2', g.rs.id),
Server.build_listen_uri('replica3', g.rs.id),
}
}
end)

g.before_test('test_save_rs_artifacts_when_test_failed', function()
g.rs = ReplicaSet:new()

g.rs:build_and_add_server({alias = 'replica1', box_cfg = g.box_cfg})
g.rs:build_and_add_server({alias = 'replica2', box_cfg = g.box_cfg})
g.rs:build_and_add_server({alias = 'replica3', box_cfg = g.box_cfg})
g.rs:start()

g.rs_artifacts = ('%s/artifacts/%s'):format(Server.vardir, g.rs.id)
g.s1_artifacts = ('%s/%s'):format(g.rs_artifacts, g.rs:get_server('replica1').id)
Expand Down Expand Up @@ -53,15 +53,14 @@ g.test_save_rs_artifacts_when_test_failed = function()
end

g.before_test('test_save_rs_artifacts_when_server_workdir_passed', function()
g.rs = ReplicaSet:new()

local s1_workdir = ('%s/%s'):format(Server.vardir, os.tmpname())
local s2_workdir = ('%s/%s'):format(Server.vardir, os.tmpname())
local s3_workdir = ('%s/%s'):format(Server.vardir, os.tmpname())

g.rs:build_and_add_server({workdir = s1_workdir, alias = 'replica1', box_cfg = g.box_cfg})
g.rs:build_and_add_server({workdir = s2_workdir, alias = 'replica2', box_cfg = g.box_cfg})
g.rs:build_and_add_server({workdir = s3_workdir, alias = 'replica3', box_cfg = g.box_cfg})
g.rs:start()

g.rs_artifacts = ('%s/artifacts/%s'):format(Server.vardir, g.rs.id)
g.s1_artifacts = ('%s/%s'):format(g.rs_artifacts, g.rs:get_server('replica1').id)
Expand Down Expand Up @@ -91,11 +90,10 @@ g.test_save_rs_artifacts_when_server_workdir_passed = function()
end

g.before_test('test_remove_rs_artifacts_when_test_success', function()
g.rs = ReplicaSet:new()

g.rs:build_and_add_server({alias = 'replica1', box_cfg = g.box_cfg})
g.rs:build_and_add_server({alias = 'replica2', box_cfg = g.box_cfg})
g.rs:build_and_add_server({alias = 'replica3', box_cfg = g.box_cfg})
g.rs:start()

g.rs_artifacts = ('%s/artifacts/%s'):format(Server.vardir, g.rs.id)
g.s1_artifacts = ('%s/%s'):format(g.rs_artifacts, g.rs:get_server('replica1').id)
Expand All @@ -109,10 +107,6 @@ g.test_remove_rs_artifacts_when_test_success = function()
t.assert_equals(fio.path.exists(g.rs.workdir), false)
end

g.before_test('test_rs_no_socket_collision_with_custom_alias', function()
g.rs = ReplicaSet:new()
end)

g.test_rs_no_socket_collision_with_custom_alias = function()
local s1 = g.rs:build_server({alias = 'foo'})
local s2 = g.rs:build_server({alias = 'bar'})
Expand All @@ -127,15 +121,11 @@ g.after_test('test_rs_no_socket_collision_with_custom_alias', function()
g.rs:drop()
end)

g.before_test('test_rs_custom_properties_are_not_overridden', function()
g.rs = ReplicaSet:new()
end)

g.test_rs_custom_properties_are_not_overridden = function()
local socket = ('%s/custom.sock'):format(Server.vardir)
local workdir = ('%s/custom'):format(Server.vardir)

local s = g.rs:build_server({net_box_uri = socket, workdir=workdir})
local s = g.rs:build_server({net_box_uri = socket, workdir = workdir})

t.assert_equals(s.net_box_uri, socket)
t.assert_equals(s.workdir, workdir)
Expand All @@ -145,10 +135,6 @@ g.after_test('test_rs_custom_properties_are_not_overridden', function()
g.rs:drop()
end)

g.before_test('test_rs_raise_error_when_add_custom_server', function()
g.rs = ReplicaSet:new()
end)

g.test_rs_raise_error_when_add_custom_server = function()
local s = Server:new()

Expand Down
Loading