Skip to content

Commit 18ceda0

Browse files
committed
server: fix artifact handling reset on restart
Fix a bug when server initialization didn't reset artifact handling, which caused stale artifacts to persist after server restarts. Closes #409
1 parent 2dcbed7 commit 18ceda0

File tree

3 files changed

+73
-5
lines changed

3 files changed

+73
-5
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Unreleased
44

5+
- Fixed a bug when server initialization didn't reset artifact handling,
6+
causing stale artifacts to persist after server restarts (gh-409).
57
- Group and suite hooks must now be registered using the call-style
68
API. Use:
79

luatest/server.lua

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -526,20 +526,29 @@ end
526526
-- Wait until the given condition is `true` (anything except `false` and `nil`).
527527
-- Throws an error when the server process is terminated or timeout exceeds.
528528
local function wait_for_condition(cond_desc, server, func, ...)
529+
local args = {...}
530+
local opts = {}
531+
if type(args[#args]) == 'table' and args[#args].save_artifacts ~= nil then
532+
opts = table.remove(args)
533+
end
529534
log.info('Wait for %q condition for server %q (pid: %d) within %d sec',
530535
cond_desc, server.alias, server.process.pid, WAIT_TIMEOUT)
531536
local deadline = clock.time() + WAIT_TIMEOUT
532537
while true do
533538
if not server.process:is_alive() then
534-
server:save_artifacts()
539+
if opts.save_artifacts ~= false then
540+
server:save_artifacts()
541+
end
535542
error(('Process is terminated when waiting for "%s" condition for server (alias: %s, workdir: %s, pid: %d)')
536543
:format(cond_desc, server.alias, fio.basename(server.workdir), server.process.pid))
537544
end
538-
if func(...) then
545+
if func(unpack(args)) then
539546
return
540547
end
541548
if clock.time() > deadline then
542-
server:save_artifacts()
549+
if opts.save_artifacts ~= false then
550+
server:save_artifacts()
551+
end
543552
error(('Timed out to wait for "%s" condition for server (alias: %s, workdir: %s, pid: %d) within %ds')
544553
:format(cond_desc, server.alias, fio.basename(server.workdir), server.process.pid, WAIT_TIMEOUT))
545554
end
@@ -563,7 +572,7 @@ function Server:stop()
563572
self.process:kill()
564573
local ok, err = pcall(wait_for_condition, 'process is terminated', self, function()
565574
return not self.process:is_alive()
566-
end)
575+
end, {save_artifacts = false})
567576
if not ok and not err:find('Process is terminated when waiting for') then
568577
error(err)
569578
end
@@ -593,7 +602,22 @@ end
593602
-- analysis if the test fails.
594603
function Server:drop()
595604
self:stop()
596-
self:save_artifacts()
605+
606+
local current_test = rawget(_G, 'current_test')
607+
local should_save = current_test == nil
608+
609+
if current_test then
610+
local status = current_test.status
611+
if status == 'success' or status == 'skip' or status == 'xfail' then
612+
should_save = false
613+
else
614+
should_save = true
615+
end
616+
end
617+
618+
if should_save then
619+
self:save_artifacts()
620+
end
597621

598622
self.instance_id = nil
599623
self.instance_uuid = nil

test/server_test.lua

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,3 +645,45 @@ g.test_assertion_failure = function()
645645
server:connect_net_box()
646646
helper.assert_failure(server.exec, server, function() t.assert(false) end)
647647
end
648+
649+
g.test_save_artifacts_after_restart_when_test_failed = function()
650+
local s = Server:new()
651+
652+
s:start()
653+
s:exec(function()
654+
require('log').info('before_restart_artifacts_marker')
655+
end)
656+
657+
s:restart()
658+
s:exec(function()
659+
require('log').info('after_restart_artifacts_marker')
660+
end)
661+
662+
local test = rawget(_G, 'current_test')
663+
test.status = 'fail'
664+
s:drop()
665+
test.status = 'success'
666+
667+
local log_path = fio.pathjoin(s.artifacts, s.alias .. '.log')
668+
local log_file = fio.open(log_path)
669+
local log_stat = log_file:stat()
670+
local log_content = log_file:read(log_stat.size)
671+
log_file:close()
672+
673+
t.assert_str_contains(log_content, 'before_restart_artifacts_marker')
674+
t.assert_str_contains(log_content, 'after_restart_artifacts_marker')
675+
end
676+
677+
g.test_do_not_save_server_artifacts_when_test_succeeded = function()
678+
local s = Server:new()
679+
fio.rmtree(s.artifacts)
680+
681+
s:start()
682+
683+
local test = rawget(_G, 'current_test')
684+
test.status = 'success'
685+
686+
s:drop()
687+
688+
t.assert_equals(fio.path.exists(s.artifacts), false)
689+
end

0 commit comments

Comments
 (0)