2
0
mirror of https://github.com/xcat2/xcat-core.git synced 2026-12-04 08:07:04 +00:00
Files
xcat-core/xCAT-test/unit/builddebs_lock.t
T
Daniel Hilst 9d8d03d06e test(xcat-core): the build lock refusal is asserted by a phrase every failure prints
Three tests match qr/already holds/ against the refusal that
acquire_lock_dir prints. That sentence is printed for every acquire
failure, so the checks pass whether the lock is held by a live build or the
mkdir failed with ENOENT or EACCES. They cannot tell contention from
anything else, which is the one thing the operator reads the message for.

Each of the three now asserts the contention report and the pid that holds
the lock. build_lock_stale_owner.t adds a lock under a directory that does
not exist, and asserts that the refusal carries the real error and claims
no concurrent build.

release_build_locks has no check at all. It is what a signal handler calls,
because DESTROY does not run when a signal ends the process, and it reaches
a lock only while that lock is in the registry. One check now takes a lock
and frees it through release_build_locks.

Signed-off-by: Daniel Hilst <392820+dhilst@users.noreply.github.com>
2026-10-22 10:17:15 -03:00

59 lines
2.6 KiB
Perl

#!/usr/bin/env perl
# The deb build lock (VersatusHPC/xcat-core#52).
#
# builddebs.pl builds in-place in its own checkout -- it rewrites debian/changelog and
# debian/control and runs dpkg-buildpackage inside the package directories -- so two
# builds of the SAME checkout would corrupt each other and must fail fast, while two
# builds of DIFFERENT checkouts share nothing and must run concurrently. The historic
# host-global lock got that backwards and made the devel and stable CD lanes collide.
#
# This drives the real lock. The lock is a function, so it is called directly.
use strict;
use warnings;
use File::Temp qw(tempdir);
use FindBin;
use lib "$FindBin::Bin/../lib";
use lib "$FindBin::Bin/../../build-utils/lib";
use Test::More;
use XCAT::BuildUtils qw(lock_id_for take_build_lock);
my $lockdir = tempdir(CLEANUP => 1);
is( lock_id_for('/opt/builds/devel/xcat-core'),
lock_id_for('/opt/builds/devel/xcat-core'),
'one checkout always maps to one lock id' );
isnt( lock_id_for('/opt/builds/devel/xcat-core'),
lock_id_for('/opt/builds/stable/xcat-core'),
'two checkouts map to different lock ids' );
like( lock_id_for('/any/path'), qr/\A[0-9a-f]{12}\z/,
'the id is filesystem-safe, so it can name a file' );
is( length lock_id_for(''), 12, 'an empty path still yields an id rather than dying' );
# Same checkout: the second build must be refused.
my $devel = '/opt/builds/devel/xcat-core';
my $first = take_build_lock($devel, $lockdir);
ok( $first, 'the first build of a checkout takes the lock' );
my $second = eval { take_build_lock($devel, $lockdir) };
ok( !$second, 'a second build of the SAME checkout is refused' );
my $refusal = $@ // '';
like( $refusal, qr/another build still holds/, 'and reports contention' );
like( $refusal, qr/still held after [0-9]+ retr\w+ by pid $$\b/,
'and names the pid holding it, so contention is told from any other failure' );
like( $refusal, qr/\Q$devel\E/, 'and names the checkout that is already building' );
# Different checkout: must not be blocked by the first.
my $stable = eval { take_build_lock('/opt/builds/stable/xcat-core', $lockdir) };
ok( $stable, 'a build of a DIFFERENT checkout runs concurrently' );
# Releasing lets the next build in. The lock is a directory now, not an flock on a filehandle --
# an NFS re-export refuses locks outright (errno 524) -- so it is freed when the returned object
# goes out of scope, not when a handle is closed.
undef $first;
my $again = eval { take_build_lock($devel, $lockdir) };
ok( $again, 'the lock is released when the returned value goes out of scope' );
done_testing();