2
0
mirror of https://github.com/xcat2/xcat-core.git synced 2026-11-22 02:12:16 +00:00
Files
xcat-core/xCAT-test/unit/xcatd_service_direct_exec.t
T
Daniel Hilst 3d58aefcba fix(xcat-core): a relocated xcatd loads the modules of another installation
xcatd.service sets XCATROOT and PATH to the literal /opt/xcat. The
previous ExecStart sourced xcat.sh, which %post generates from
RPM_INSTALL_PREFIX0, so a relocated package got its own prefix. With the
package installed under /srv/xcat the daemon symlink reaches
/srv/xcat/sbin/xcatd and the Perl search path still names /opt/xcat, so
xcatd cannot load its own modules unless the administrator overrides the
environment or a default installation is left behind.

%post writes /etc/xcat/xcatroot.env with XCATROOT and PATH built from
RPM_INSTALL_PREFIX0, and the unit reads it with EnvironmentFile. The
order is the default in the unit, then that file, then
/etc/sysconfig/xcat, so the recorded prefix replaces the default and the
site file keeps the last word. ExecStart stays a direct call, so the
kernel still enters xcat_t on the xcatd script. %preun removes the file
on erase. The file is not in %files, so rpm --verify is unaffected.

xcatd_relocated_startup.t fails 3 checks of its /srv/xcat subtest
without this change.

Signed-off-by: Daniel Hilst <392820+dhilst@users.noreply.github.com>
2026-10-21 16:50:33 -03:00

67 lines
2.3 KiB
Perl

#!/usr/bin/env perl
use strict;
use warnings;
use FindBin;
use File::Slurper qw(read_lines);
use Test::More;
# The unit file is the artifact: systemd reads it, nothing in the tree runs it.
my $unit_file = "$FindBin::Bin/../../xCAT-server/etc/init.d/xcatd.service";
# Returns the [Service] settings as name => [values], in file order.
sub service_settings {
my ($path) = @_;
my (%settings, $section);
foreach my $line (read_lines($path)) {
next if $line =~ /^\s*(?:#|;|$)/;
if ($line =~ /^\s*\[(\w+)\]\s*$/) {
$section = $1;
next;
}
next unless defined $section && $section eq 'Service';
my ($name, $value) = $line =~ /^\s*(\w+)\s*=\s*(.*?)\s*$/ or next;
push @{ $settings{$name} }, $value;
}
return \%settings;
}
my $service = service_settings($unit_file);
# systemd starts a shell in initrc_t, which is not an entrypoint of xcat_t.
my @exec = @{ $service->{ExecStart} || [] };
is(scalar(@exec), 1, 'the unit has one ExecStart');
my ($program) = split /\s+/, ($exec[0] // '');
is($program, '/usr/sbin/xcatd', 'ExecStart runs the xcatd binary, not a shell');
like($exec[0] // '', qr{\s-p\s+/run/xcatd\.pid(?:\s|$)},
'... and passes the pid file that PIDFile names');
is_deeply($service->{PIDFile}, ['/run/xcatd.pid'], 'PIDFile is /run/xcatd.pid');
# Environment= replaces what /etc/profile.d/xcat.sh gave xcatd.
my %env;
foreach my $setting (@{ $service->{Environment} || [] }) {
foreach my $pair ($setting =~ /("[^"]*"|\S+)/g) {
$pair =~ s/^"|"$//g;
my ($name, $value) = split /=/, $pair, 2;
$env{$name} = $value;
}
}
is($env{XCATROOT}, '/opt/xcat', 'the unit sets XCATROOT=/opt/xcat');
is($env{PERL_BADLANG}, '0', 'the unit sets PERL_BADLANG=0');
my @path = split /:/, ($env{PATH} // '');
foreach my $dir (qw(/opt/xcat/bin /opt/xcat/sbin /opt/xcat/share/xcat/tools
/usr/sbin /usr/bin /sbin /bin)) {
ok((grep { $_ eq $dir } @path), "the unit PATH has $dir");
}
# A later setting replaces an earlier one, so the order decides who wins: the
# prefix %post recorded beats the default in the unit, and the site file beats
# both.
is_deeply($service->{EnvironmentFile},
[ '-/etc/xcat/xcatroot.env', '-/etc/sysconfig/xcat' ],
'the install prefix is read before the site file, and both are optional');
done_testing();