Skip to content

Commit 7d1358c

Browse files
committed
Move creating required resources to start funcs
I don't like creating files, directories, whatever at the point of the 'class instance' initialization -- the `new` function. It's more logical and gently to create all required resources on the start.
1 parent 869ba1c commit 7d1358c

File tree

4 files changed

+51
-39
lines changed

4 files changed

+51
-39
lines changed

luatest/replica_proxy.lua

+4
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
local checks = require('checks')
66
local fiber = require('fiber')
7+
local fio = require('fio')
78
local log = require('log')
89
local socket = require('socket')
10+
local uri = require('uri')
911

1012
local utils = require('luatest.utils')
1113
local Connection = require('luatest.replica_conn')
@@ -98,6 +100,8 @@ function Proxy:start(opts)
98100
os.remove(self.client_socket_path)
99101
end
100102

103+
fio.mktree(fio.dirname(uri.parse(self.client_socket_path).service))
104+
101105
if not self.client_socket:bind('unix/', self.client_socket_path) then
102106
log.error("Failed to bind client socket: %s", self.client_socket:error())
103107
return false

luatest/replica_set.lua

+2-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ function ReplicaSet:initialize()
6262
self.alias = 'rs'
6363
self.id = ('%s-%s'):format(self.alias, utils.generate_id())
6464
self.workdir = fio.pathjoin(self._server.vardir, self.id)
65-
fio.mktree(self.workdir)
6665

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

164+
fio.mktree(self.workdir)
165+
165166
for _, server in ipairs(self.servers) do
166167
if not server.process then
167168
server:start({wait_until_ready = false})

luatest/server.lua

+36-15
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,10 @@ end
7474
-- Defaults to 'server'.
7575
-- @string[opt] object.workdir Working directory for the new server and the
7676
-- value of the `TARANTOOL_WORKDIR` env variable which is passed into the
77-
-- server process.
77+
-- server process. The directory path will be created on the server start.
7878
-- Defaults to `<vardir>/<alias>-<random id>`.
7979
-- @string[opt] object.datadir Directory path whose contents will be recursively
80-
-- copied into `object.workdir` during initialization.
80+
-- copied into `object.workdir` on the server start.
8181
-- @number[opt] object.http_port Port for HTTP connection to the new server and
8282
-- the value of the `TARANTOOL_HTTP_PORT` env variable which is passed into
8383
-- the server process.
@@ -87,7 +87,8 @@ end
8787
-- into the server process.
8888
-- @string[opt] object.net_box_uri URI for the `net.box` connection to the new
8989
-- server and the value of the `TARANTOOL_LISTEN` env variable which is passed
90-
-- into the server process.
90+
-- into the server process. If it is a Unix socket, the corresponding socket
91+
-- directory path will be created on the server start.
9192
-- @tab[opt] object.net_box_credentials Override the default credentials for the
9293
-- `net.box` connection to the new server.
9394
-- @tab[opt] object.box_cfg Extra options for `box.cfg()` and the value of the
@@ -143,15 +144,6 @@ function Server:initialize()
143144
self.workdir = fio.pathjoin(self.vardir, self.id)
144145
fio.rmtree(self.workdir)
145146
end
146-
fio.mktree(self.workdir)
147-
148-
if self.datadir ~= nil then
149-
local ok, err = fio.copytree(self.datadir, self.workdir)
150-
if not ok then
151-
error(('Failed to copy directory: %s'):format(err))
152-
end
153-
self.datadir = nil
154-
end
155147

156148
if self.http_port then
157149
self.http_client = http_client.new()
@@ -163,8 +155,7 @@ function Server:initialize()
163155
if self.net_box_uri == nil and self.net_box_port then
164156
self.net_box_uri = 'localhost:' .. self.net_box_port
165157
end
166-
local parsed_net_box_uri = uri.parse(self.net_box_uri)
167-
if parsed_net_box_uri.host == 'unix/' then
158+
if uri.parse(self.net_box_uri).host == 'unix/' then
168159
-- Linux uses max 108 bytes for Unix domain socket paths, which means a 107 characters
169160
-- string ended by a null terminator. Other systems use 104 bytes and 103 characters strings.
170161
local max_unix_socket_path = {linux = 107, other = 103}
@@ -173,7 +164,6 @@ function Server:initialize()
173164
error(('Net box URI must be <= max Unix domain socket path length (%d chars)')
174165
:format(max_unix_socket_path[system]))
175166
end
176-
fio.mktree(fio.dirname(parsed_net_box_uri.service))
177167
end
178168

179169
self.env = utils.merge(self.env or {}, self:build_env())
@@ -253,6 +243,33 @@ function Server.build_listen_uri(server_alias, extra_path)
253243
return fio.pathjoin(Server.vardir, extra_path or '', server_alias .. '.sock')
254244
end
255245

246+
--- Make the server's working directory.
247+
-- Invoked on the server's start.
248+
function Server:make_workdir()
249+
fio.mktree(self.workdir)
250+
end
251+
252+
--- Copy contents of the data directory into the server's working directory.
253+
-- Invoked on the server's start.
254+
function Server:copy_datadir()
255+
if self.datadir ~= nil then
256+
local ok, err = fio.copytree(self.datadir, self.workdir)
257+
if not ok then
258+
error(('Failed to copy %s to %s: %s'):format(self.datadir, self.workdir, err))
259+
end
260+
self.datadir = nil
261+
end
262+
end
263+
264+
--- Make directory for the server's Unix socket.
265+
-- Invoked on the server's start.
266+
function Server:make_socketdir()
267+
local parsed_net_box_uri = uri.parse(self.net_box_uri)
268+
if parsed_net_box_uri.host == 'unix/' then
269+
fio.mktree(fio.dirname(parsed_net_box_uri.service))
270+
end
271+
end
272+
256273
--- Start a server.
257274
-- Optionally waits until the server is ready.
258275
--
@@ -265,6 +282,10 @@ function Server:start(opts)
265282

266283
self:initialize()
267284

285+
self:make_workdir()
286+
self:copy_datadir()
287+
self:make_socketdir()
288+
268289
local command = self.command
269290
local args = table.copy(self.args)
270291
local env = table.copy(os.environ())

test/replica_set_test.lua

+9-23
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,26 @@ local ReplicaSet = require('luatest.replica_set')
55
local g = t.group()
66
local Server = t.Server
77

8-
g.before_all(function()
8+
g.before_each(function()
9+
g.rs = ReplicaSet:new()
910
g.box_cfg = {
1011
replication_timeout = 0.1,
1112
replication_connect_timeout = 10,
1213
replication_sync_lag = 0.01,
1314
replication_connect_quorum = 3,
1415
replication = {
15-
Server.build_listen_uri('replica1'),
16-
Server.build_listen_uri('replica2'),
17-
Server.build_listen_uri('replica3'),
16+
Server.build_listen_uri('replica1', g.rs.id),
17+
Server.build_listen_uri('replica2', g.rs.id),
18+
Server.build_listen_uri('replica3', g.rs.id),
1819
}
1920
}
2021
end)
2122

2223
g.before_test('test_save_rs_artifacts_when_test_failed', function()
23-
g.rs = ReplicaSet:new()
24-
2524
g.rs:build_and_add_server({alias = 'replica1', box_cfg = g.box_cfg})
2625
g.rs:build_and_add_server({alias = 'replica2', box_cfg = g.box_cfg})
2726
g.rs:build_and_add_server({alias = 'replica3', box_cfg = g.box_cfg})
27+
g.rs:start()
2828

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

5555
g.before_test('test_save_rs_artifacts_when_server_workdir_passed', function()
56-
g.rs = ReplicaSet:new()
57-
5856
local s1_workdir = ('%s/%s'):format(Server.vardir, os.tmpname())
5957
local s2_workdir = ('%s/%s'):format(Server.vardir, os.tmpname())
6058
local s3_workdir = ('%s/%s'):format(Server.vardir, os.tmpname())
6159

6260
g.rs:build_and_add_server({workdir = s1_workdir, alias = 'replica1', box_cfg = g.box_cfg})
6361
g.rs:build_and_add_server({workdir = s2_workdir, alias = 'replica2', box_cfg = g.box_cfg})
6462
g.rs:build_and_add_server({workdir = s3_workdir, alias = 'replica3', box_cfg = g.box_cfg})
63+
g.rs:start()
6564

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

9392
g.before_test('test_remove_rs_artifacts_when_test_success', function()
94-
g.rs = ReplicaSet:new()
95-
9693
g.rs:build_and_add_server({alias = 'replica1', box_cfg = g.box_cfg})
9794
g.rs:build_and_add_server({alias = 'replica2', box_cfg = g.box_cfg})
9895
g.rs:build_and_add_server({alias = 'replica3', box_cfg = g.box_cfg})
96+
g.rs:start()
9997

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

112-
g.before_test('test_rs_no_socket_collision_with_custom_alias', function()
113-
g.rs = ReplicaSet:new()
114-
end)
115-
116110
g.test_rs_no_socket_collision_with_custom_alias = function()
117111
local s1 = g.rs:build_server({alias = 'foo'})
118112
local s2 = g.rs:build_server({alias = 'bar'})
@@ -127,15 +121,11 @@ g.after_test('test_rs_no_socket_collision_with_custom_alias', function()
127121
g.rs:drop()
128122
end)
129123

130-
g.before_test('test_rs_custom_properties_are_not_overridden', function()
131-
g.rs = ReplicaSet:new()
132-
end)
133-
134124
g.test_rs_custom_properties_are_not_overridden = function()
135125
local socket = ('%s/custom.sock'):format(Server.vardir)
136126
local workdir = ('%s/custom'):format(Server.vardir)
137127

138-
local s = g.rs:build_server({net_box_uri = socket, workdir=workdir})
128+
local s = g.rs:build_server({net_box_uri = socket, workdir = workdir})
139129

140130
t.assert_equals(s.net_box_uri, socket)
141131
t.assert_equals(s.workdir, workdir)
@@ -145,10 +135,6 @@ g.after_test('test_rs_custom_properties_are_not_overridden', function()
145135
g.rs:drop()
146136
end)
147137

148-
g.before_test('test_rs_raise_error_when_add_custom_server', function()
149-
g.rs = ReplicaSet:new()
150-
end)
151-
152138
g.test_rs_raise_error_when_add_custom_server = function()
153139
local s = Server:new()
154140

0 commit comments

Comments
 (0)