2
0
mirror of https://github.com/xcat2/xcat-core.git synced 2026-07-31 10:09:40 +00:00

fix(xcat-core): keep a source tree for the unit tests to run against

The unit test stage failed on the first CI run with

    Cannot detect source of 'xCAT-test/unit/*.t'
    Files=0, Tests=0
    Result: NOTESTS

the glob reached prove unexpanded because it matched nothing. The source
tree is gone by the time the tests run: build-ubunturepo sets

    local_core_repo_path="$curdir/../../xcat-core"

and rm -rf's it before creating the apt repository there. GitHub checks
out into work/<repo>/<repo>, so for /home/runner/work/xcat-core/xcat-core
that path resolves to the checkout's own parent and the build wipes the
checkout, leaving an empty directory of the same name behind. The cd
still succeeds, which is why prove was handed a literal glob rather than
failing outright. This is also why every testcase that predates this
change proves /opt/xcat/share/xcat/tools/autotest/unit: after the build
the installed copy is the only one left.

Copy the checkout aside in preserve_source_tree() before the build and
prove that copy, so FindBin still resolves to a real source tree. Switch
to `prove -r xCAT-test/unit` as well, so a missing directory fails loudly
instead of silently degrading to a no-op the way an unmatched glob does.

Reproduced and verified by replaying the build under GitHub's directory
layout: the checkout drops to 0 test files, the preserved copy keeps all
47 and proves clean.

Signed-off-by: Daniel Hilst <392820+dhilst@users.noreply.github.com>
This commit is contained in:
Daniel Hilst
2026-07-23 10:00:19 -03:00
parent a0d2322b71
commit 63505793db
+47 -8
View File
@@ -27,10 +27,17 @@ my $check_result_str="``CI CHECK RESULT`` : ";
my $last_func_start = timelocal(localtime());
my $GITHUB_API = "https://api.github.com";
# The workflow starts us in the checked out source tree, but install_xcat()
# chdir's away from it. Remember the checkout up front: the unit tests under
# xCAT-test/unit resolve xCAT modules and fixture files relative to it.
# The workflow starts us in the checked out source tree. The unit tests under
# xCAT-test/unit resolve xCAT modules and fixture files relative to that tree
# through FindBin, so they can only be run from a source tree -- but the tree
# does not survive the build. build-ubunturepo sets
# local_core_repo_path="$curdir/../../xcat-core"
# which, under the work/<repo>/<repo> layout GitHub checks out into, resolves
# to the checkout's own parent, and it then rm -rf's that path to make room for
# the apt repository. So take a copy of the tree before building and run the
# unit tests out of the copy.
my $srcdir = getcwd();
my $unitsrc = ($ENV{'RUNNER_TEMP'} ? $ENV{'RUNNER_TEMP'} : "/tmp") . "/xcat-core-unitsrc";
#--------------------------------------------------------
# Fuction name: runcmd
@@ -275,6 +282,29 @@ sub send_back_comment{
}
}
#--------------------------------------------------------
# Fuction name: preserve_source_tree
# Description: Copy the checkout aside before the build destroys it, so the
# unit tests still have a source tree to run against afterwards.
# Must be called before build_xcat_core().
# Attributes:
# Return code: 0 Success 1 Failed
#--------------------------------------------------------
sub preserve_source_tree{
my $cmd = "rm -rf $unitsrc && cp -a $srcdir $unitsrc";
print "[preserve_source_tree] running $cmd\n";
my @output = runcmd("$cmd");
if($::RUNCMD_RC){
print RED "[preserve_source_tree] $cmd ....[Failed]\n";
print Dumper \@output;
return 1;
}
@output = runcmd("ls $unitsrc/xCAT-test/unit/*.t | wc -l");
print "[preserve_source_tree] preserved $srcdir in $unitsrc ($output[0] unit tests)\n";
return 0;
}
#--------------------------------------------------------
# Fuction name: build_xcat_core
# Description:
@@ -392,15 +422,15 @@ sub install_xcat{
#--------------------------------------------------------
# Fuction name: run_unit_tests
# Description: Run every Perl unit test under xCAT-test/unit with prove.
# The tests are source tree tests: they pull xCAT modules and
# fixture files out of the checkout through FindBin, so they
# have to be proved from the checkout root rather than from the
# copy installed under /opt/xcat/share/xcat/tools/autotest.
# Runs against the pre-build copy of the source tree taken by
# preserve_source_tree(): the tests reach for xCAT modules and
# fixture files through FindBin, so the installed copy under
# /opt/xcat/share/xcat/tools/autotest is not enough for them.
# Attributes:
# Return code: 0 all tests passed, 1 otherwise
#--------------------------------------------------------
sub run_unit_tests{
my $cmd = "cd $srcdir && prove xCAT-test/unit/*.t";
my $cmd = "cd $unitsrc && prove -r xCAT-test/unit";
print "[run_unit_tests] running $cmd\n";
my @output = runcmd("$cmd");
print Dumper \@output;
@@ -599,6 +629,15 @@ print Dumper \@ipinfo;
#Start to build xcat core
#Save the source tree before the build deletes it, the unit tests need it later
print GREEN "\n------ Preserving the source tree for the unit tests ------\n";
$rst = preserve_source_tree();
if($rst){
print RED "Preserving the source tree failed\n";
exit $rst;
}
mark_time("preserve_source_tree");
print GREEN "\n------ Building xCAT core package ------\n";
$rst = build_xcat_core();
if($rst){