Skip to content

Commit

Permalink
Merge pull request #10682 from lnussel/services_enabled
Browse files Browse the repository at this point in the history
Introduce SERVICES_ENABLED for services_enabled.pm
  • Loading branch information
foursixnine committed Jul 16, 2020
2 parents f9d5a1e + a8d041d commit 54792e0
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 9 deletions.
4 changes: 2 additions & 2 deletions lib/Utils/Systemd.pm
Expand Up @@ -63,8 +63,8 @@ Please note that return code of this function is handle by 'script_run' or
sub systemctl {
my ($command, %args) = @_;
croak "systemctl(): no command specified" if ($command =~ /^ *$/);
my $expect_false = $args{expect_false} ? '!' : '';
my @script_params = ("$expect_false systemctl --no-pager $command", timeout => $args{timeout}, fail_message => $args{fail_message});
my $expect_false = $args{expect_false} ? '! ' : '';
my @script_params = ("${expect_false}systemctl --no-pager $command", timeout => $args{timeout}, fail_message => $args{fail_message});
if ($args{ignore_failure}) {
script_run($script_params[0], $args{timeout});
} else {
Expand Down
51 changes: 44 additions & 7 deletions tests/microos/services_enabled.pm
Expand Up @@ -29,18 +29,55 @@ my %services_for = (

sub check_services {
my $services = shift;
foreach my $s (@$services) {
systemctl "is-enabled $s";
while (my ($s, $on) = each %$services) {
systemctl "is-enabled $s", expect_false => ($on ? 0 : 1);
}
}

sub map_services {
map { my $on = (s/^!//) ? 0 : 1; $_ => $on } @_;
}

sub run {
my $role = get_var('SYSTEM_ROLE');
my %services;

# the SERVICES_ENABLED var allows to overwrite the test's built
# in defaults. It's a space separated list of services to check
# for. If the first service in the list starts with a plus or
# minus, the listed services have to start with either a plus or
# minus to indicates whether they are to be added or removed
# from the built in list. Without plus or minus the built in
# list gets ignored.
# an exclamation mark in front of a service verifies the service is
# disabled.
# Example: SERVICES_ENABLED="+!sshd"
my $extra = get_var('SERVICES_ENABLED');
if ($extra && $extra !~ /^[+-]/) {
%services = map_services split(/\s+/, $extra);
} else {
my $role = get_var('SYSTEM_ROLE');

%services = map_services @{$services_for{default}};
if ($role) {
%services = (%services, map_services @{$services_for{$role}}) if $services_for{$role};
%services = (%services, map_services @{$services_for{cluster}}) if $role =~ /admin|worker/;
}

if ($extra) {
for my $s (split(/\s+/, $extra)) {
if ($s =~ s/^-//) {
delete $services{$s};
} else {
# even if there is no plus in following items we still add it
$s =~ s/^\+//;
my $on = ($s =~ s/^!//) ? 0 : 1;
$services{$s} = $on;
}
}
}
}

check_services $services_for{default};
check_services $services_for{cloud} if is_caasp('caasp');
check_services $services_for{$role} if $role;
check_services $services_for{cluster} if $role =~ /admin|worker/;
check_services \%services if %services;
}

1;

0 comments on commit 54792e0

Please sign in to comment.